Class ProxyServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- io.hawt.web.proxy.ProxyServlet
-
- All Implemented Interfaces:
java.io.Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
public class ProxyServlet extends javax.servlet.http.HttpServletAn HTTP reverse proxy/gateway servlet. It is designed to be extended for customization if desired. Most of the work is handled by Apache HttpClient.There are alternatives to a servlet based proxy such as Apache mod_proxy if that is available to you. However this servlet is easily customizable by Java, secure-able by your web application's security (e.g. spring-security), portable across servlet engines, and is embeddable into another web application.
Inspiration: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
Original implementation at https://github.com/mitre/HTTP-Proxy-Servlet, released under ASL 2.0.
- Author:
- David Smiley dsmiley@mitre.org
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanacceptSelfSignedCertsprotected ProxyAllowlistallowlistprotected static java.util.BitSetasciiQueryCharsstatic java.lang.StringDISABLE_PROXYprotected booleandoForwardIPprotected booleanenabledstatic java.lang.StringHAWTIO_DISABLE_PROXYstatic java.lang.StringHAWTIO_LOCAL_ADDRESS_PROBINGstatic java.lang.StringHAWTIO_PROXY_ALLOWLISTprotected static org.apache.http.message.HeaderGrouphopByHopHeadersThese are the "hop-by-hop" headers that should not be copied.static java.lang.StringLOCAL_ADDRESS_PROBINGstatic java.lang.StringP_FORWARDEDFORA boolean parameter name to enable forwarding of the client IPstatic java.lang.StringPROXY_ALLOWLISTprotected org.apache.http.impl.client.CloseableHttpClientproxyClient
-
Constructor Summary
Constructors Constructor Description ProxyServlet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcopyRequestHeaders(javax.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, java.net.URI targetUriObj)Copy request headers from the servlet client to the proxy request.protected voidcopyResponseEntity(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse)Copy response body data (the entity) from the proxy to the servlet client.protected voidcopyResponseHeaders(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse)Copy proxied response headers back to the servlet client.voiddestroy()protected booleandoResponseRedirectOrNotModifiedLogic(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, java.net.URI targetUriObj)protected static java.lang.CharSequenceencodeUriQuery(java.lang.CharSequence in)Encodes characters in the query or fragment part of the URI.java.lang.StringgetServletInfo()voidinit(javax.servlet.ServletConfig servletConfig)protected ProxyAddressparseProxyAddress(javax.servlet.http.HttpServletRequest servletRequest)protected java.lang.StringrewriteUrlFromResponse(javax.servlet.http.HttpServletRequest servletRequest, java.lang.String theUrl, java.lang.String targetUri)For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use.protected voidservice(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse)-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
-
-
-
-
Field Detail
-
P_FORWARDEDFOR
public static final java.lang.String P_FORWARDEDFOR
A boolean parameter name to enable forwarding of the client IP- See Also:
- Constant Field Values
-
PROXY_ALLOWLIST
public static final java.lang.String PROXY_ALLOWLIST
- See Also:
- Constant Field Values
-
LOCAL_ADDRESS_PROBING
public static final java.lang.String LOCAL_ADDRESS_PROBING
- See Also:
- Constant Field Values
-
DISABLE_PROXY
public static final java.lang.String DISABLE_PROXY
- See Also:
- Constant Field Values
-
HAWTIO_PROXY_ALLOWLIST
public static final java.lang.String HAWTIO_PROXY_ALLOWLIST
- See Also:
- Constant Field Values
-
HAWTIO_LOCAL_ADDRESS_PROBING
public static final java.lang.String HAWTIO_LOCAL_ADDRESS_PROBING
- See Also:
- Constant Field Values
-
HAWTIO_DISABLE_PROXY
public static final java.lang.String HAWTIO_DISABLE_PROXY
- See Also:
- Constant Field Values
-
enabled
protected boolean enabled
-
doForwardIP
protected boolean doForwardIP
-
acceptSelfSignedCerts
protected boolean acceptSelfSignedCerts
-
allowlist
protected ProxyAllowlist allowlist
-
proxyClient
protected org.apache.http.impl.client.CloseableHttpClient proxyClient
-
hopByHopHeaders
protected static final org.apache.http.message.HeaderGroup hopByHopHeaders
These are the "hop-by-hop" headers that should not be copied. http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html I use an HttpClient HeaderGroup class instead of Setbecause this approach does case-insensitive lookup faster.
-
asciiQueryChars
protected static final java.util.BitSet asciiQueryChars
-
-
Method Detail
-
getServletInfo
public java.lang.String getServletInfo()
- Specified by:
getServletInfoin interfacejavax.servlet.Servlet- Overrides:
getServletInfoin classjavax.servlet.GenericServlet
-
init
public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException- Specified by:
initin interfacejavax.servlet.Servlet- Overrides:
initin classjavax.servlet.GenericServlet- Throws:
javax.servlet.ServletException
-
destroy
public void destroy()
- Specified by:
destroyin interfacejavax.servlet.Servlet- Overrides:
destroyin classjavax.servlet.GenericServlet
-
service
protected void service(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse) throws java.io.IOException- Overrides:
servicein classjavax.servlet.http.HttpServlet- Throws:
java.io.IOException
-
parseProxyAddress
protected ProxyAddress parseProxyAddress(javax.servlet.http.HttpServletRequest servletRequest)
-
doResponseRedirectOrNotModifiedLogic
protected boolean doResponseRedirectOrNotModifiedLogic(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, java.net.URI targetUriObj) throws javax.servlet.ServletException, java.io.IOException- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
copyRequestHeaders
protected void copyRequestHeaders(javax.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, java.net.URI targetUriObj)Copy request headers from the servlet client to the proxy request.
-
copyResponseHeaders
protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse)Copy proxied response headers back to the servlet client.
-
copyResponseEntity
protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse) throws java.io.IOExceptionCopy response body data (the entity) from the proxy to the servlet client.- Throws:
java.io.IOException
-
rewriteUrlFromResponse
protected java.lang.String rewriteUrlFromResponse(javax.servlet.http.HttpServletRequest servletRequest, java.lang.String theUrl, java.lang.String targetUri)For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use.
-
encodeUriQuery
protected static java.lang.CharSequence encodeUriQuery(java.lang.CharSequence in)
Encodes characters in the query or fragment part of the URI.Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient insists that the outgoing proxied request has a valid URI because it uses Java's
URI. To be more forgiving, we must escape the problematic characters. See the URI class for the spec.- Parameters:
in- example: name=value&foo=bar#fragment
-
-