Skip to content
Draft
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
35 changes: 34 additions & 1 deletion SimpleProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
String headerName = entry.getKey();
if (headerName != null && !headerName.equalsIgnoreCase("Transfer-Encoding")) {
for (String value : entry.getValue()) {
resp.addHeader(headerName, value);
if (headerName.equalsIgnoreCase("Location") && (responseCode >= 300 && responseCode < 400)) {
resp.addHeader(headerName, rewriteUrl(value, req));
} else {
resp.addHeader(headerName, value);
}
}
}
}
Expand All @@ -154,4 +158,33 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
}
}

private String rewriteUrl(String locationHeader, HttpServletRequest request) {
if (locationHeader == null) {
return null;
}

try {
URL originalUrl = new URL(locationHeader);
String proxyHost = request.getServerName();
int proxyPort = request.getServerPort();
String proxyScheme = request.getScheme();

// Use -1 for default port
if ((proxyScheme.equals("http") && proxyPort == 80) || (proxyScheme.equals("https") && proxyPort == 443)) {
proxyPort = -1;
}


// Build the rewritten URL using the proxy's host and port
URL rewrittenUrl = new URL(proxyScheme, proxyHost, proxyPort, originalUrl.getFile());
log("Rewriting redirect Location header to: " + rewrittenUrl.toString());
return rewrittenUrl.toString();

} catch (MalformedURLException e) {
// If the location is not a valid URL (e.g., a relative path), just return it as is
log("Location header is not a valid URL, passing through: " + locationHeader);
return locationHeader;
}
}

}
6 changes: 3 additions & 3 deletions apex/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
</init-param>
<init-param>
<param-name>mapping.3.type</param-name>
<param-value>contains</param-value>
<param-value>prefix</param-value>
</init-param>
<init-param>
<param-name>mapping.3.path</param-name>
<param-value>apex/wwv_flow.ajax?</param-value>
<param-value>/apex/wwv_flow.ajax</param-value>
</init-param>
<init-param>
<param-name>mapping.3.url</param-name>
<param-value>${baseUrl}/apex/wwv_flow.ajax?</param-value>
<param-value>${baseUrl}/apex/wwv_flow.ajax</param-value>
</init-param>
<init-param>
<param-name>mapping.4.type</param-name>
Expand Down