Skip to content

Commit

Permalink
Merge branch 'twwwt-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmiley committed Apr 25, 2018
2 parents d9b90d1 + 5d6e362 commit 82c6977
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
15 changes: 13 additions & 2 deletions src/main/java/org/mitre/dsmiley/httpproxy/ProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public class ProxyServlet extends HttpServlet {
/** A integer parameter name to set the socket read timeout (millis) */
public static final String P_READTIMEOUT = "http.read.timeout";

/** A boolean parameter whether to use JVM-defined system properties to configure various networking aspects. */
public static final String P_USESYSTEMPROPERTIES = "useSystemProperties";

/** The parameter name for the target (destination) URI to proxy to. */
protected static final String P_TARGET_URI = "targetUri";
protected static final String ATTR_TARGET_URI =
Expand All @@ -108,6 +111,7 @@ public class ProxyServlet extends HttpServlet {
protected boolean doPreserveHost = false;
protected boolean doPreserveCookies = false;
protected boolean doHandleRedirects = false;
protected boolean useSystemProperties = false;
protected int connectTimeout = -1;
protected int readTimeout = -1;

Expand Down Expand Up @@ -179,6 +183,11 @@ public void init() throws ServletException {
this.readTimeout = Integer.parseInt(readTimeoutString);
}

String useSystemPropertiesString = getConfigParam(P_USESYSTEMPROPERTIES);
if (useSystemPropertiesString != null) {
this.useSystemProperties = Boolean.parseBoolean(useSystemPropertiesString);
}

initTarget();//sets target*

proxyClient = createHttpClient(buildRequestConfig());
Expand Down Expand Up @@ -214,8 +223,10 @@ protected void initTarget() throws ServletException {
* In any case, it should be thread-safe.
**/
protected HttpClient createHttpClient(final RequestConfig requestConfig) {
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig).build();
HttpClientBuilder clientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
if (useSystemProperties)
clientBuilder = clientBuilder.useSystemProperties();
return clientBuilder.build();
}

/** The http client used.
Expand Down
74 changes: 52 additions & 22 deletions src/test/java/org/mitre/dsmiley/httpproxy/ProxyServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@

package org.mitre.dsmiley.httpproxy;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
Expand All @@ -43,22 +56,12 @@
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;

/**
* @author David Smiley - dsmiley@mitre.org
Expand Down Expand Up @@ -476,6 +479,33 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
assertEquals("SomeHost", proxyHost[0]);
}

@Test
public void testUseSystemProperties() throws Exception {
System.setProperty("http.proxyHost", "foo.blah.nonexisting.dns.name");
servletRunner = new ServletRunner();

Properties servletProps = new Properties();
servletProps.setProperty(ProxyServlet.P_LOG, "true");
servletProps.setProperty(ProxyServlet.P_USESYSTEMPROPERTIES, "true");
// Must use a non-local URL because localhost is in http.nonProxyHosts by default.
targetBaseUri = "http://www.google.com";
servletProps.setProperty(ProxyServlet.P_TARGET_URI, targetBaseUri);
servletRunner.registerServlet(servletPath + "/*", servletName, servletProps);

sc = servletRunner.newClient();
sc.getClientProperties().setAutoRedirect(false);//don't want httpunit itself to redirect

GetMethodWebRequest req = makeGetMethodRequest(sourceBaseUri);
try {
execAssert(req);
fail("UnknownHostException expected.");
} catch (UnknownHostException e) {
// Expected assuming that our proxy host defined above does not exist.
} finally {
System.clearProperty("http.proxyHost");
}
}

private WebResponse execAssert(GetMethodWebRequest request, String expectedUri) throws Exception {
return execAndAssert(request, expectedUri);
}
Expand Down

0 comments on commit 82c6977

Please sign in to comment.