Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't forward proto if we receive the header #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/main/java/org/mitre/dsmiley/httpproxy/ProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ 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";

/** 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 Down Expand Up @@ -173,7 +173,7 @@ public void init() throws ServletException {
if (connectTimeoutString != null) {
this.connectTimeout = Integer.parseInt(connectTimeoutString);
}

String readTimeoutString = getConfigParam(P_READTIMEOUT);
if (readTimeoutString != null) {
this.readTimeout = Integer.parseInt(readTimeoutString);
Expand Down Expand Up @@ -388,8 +388,8 @@ protected void consumeQuietly(HttpEntity entity) {
}
}

/**
* Copy request headers from the servlet client to the proxy request.
/**
* Copy request headers from the servlet client to the proxy request.
* This is easily overridden to add your own.
*/
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
Expand Down Expand Up @@ -446,6 +446,10 @@ private void setXForwardedForHeader(HttpServletRequest servletRequest,

String protoHeaderName = "X-Forwarded-Proto";
String protoHeader = servletRequest.getScheme();
String existingProtoHeader = servletRequest.getHeader(protoHeaderName);
if (existingProtoHeader != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. Please add a comment that this header is not a list.

protoHeader = existingProtoHeader;
}
proxyRequest.setHeader(protoHeaderName, protoHeader);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/mitre/dsmiley/httpproxy/ProxyServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
WebResponse rsp = execAndAssert(req, "");
}

@Test
public void testWithExistingXForwardedProto() throws Exception {
final String PROTO_HEADER = "X-Forwarded-Proto";

localTestServer.register("/targetPath*", new RequestInfoHandler() {
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
Header xForwardedProtoHeader = request.getFirstHeader(PROTO_HEADER);
assertEquals("https", xForwardedProtoHeader.getValue());
super.handle(request, response, context);
}
});

GetMethodWebRequest req = makeGetMethodRequest(sourceBaseUri);
req.setHeaderField(PROTO_HEADER, "https");
WebResponse rsp = execAndAssert(req, "");
}


@Test
public void testEnabledXForwardedFor() throws Exception {
final String FOR_HEADER = "X-Forwarded-For";
Expand Down