I’ve developed an Java servlet that can proxy HTTP requests from a browser.
I am having an issue proxying HTTPS requests.
The servlet doesn’t appear to receive any HTTPS requests from the browser.
Upon investigating this further I noticed that HTTP requests seem to start with a simple GET request whereas the HTTPS requests start with a CONNECT request as shown by the log extract below:
"CONNECT ajax.googleapis.com:443 HTTP/1.1" 200
My question is, is it possible to handle this request using my servlet?
public class MyProxyServlet extends HttpServlet {
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
}
@Override
protected void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
}
@Override
protected void doPost(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
}
}
If so where and how?
Since the CONNECT verb is not handled by the default
HttpServletimplementation, you will have to override theServicemethod of thejavax.servlet.http.HttpServletin your servlet and handle theCONNECTmethod yourself. The original implementation seems to ignore this with thisresp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);. Take a look at the HttpServlet source code http://www.docjar.com/html/api/javax/servlet/http/HttpServlet.java.html