Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Using Jetty transparent http proxy to forward to public website

Hi everyone,

I'm trying to use Jetty transparent proxy to forward to a public web server, but currently unable to get a simple example working as I would expect.  This is my code:


--- CODE BEGIN ---


package test.myproxy;

import org.eclipse.jetty.proxy.ProxyServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class MyHttpProxy {
  public static void main(String[] args) {
    try {
      Server mServer = new Server(7070);
      ServletContextHandler mContext = new ServletContextHandler(mServer, "/");
      ServletHolder mHolder = mContext.addServlet(ProxyServlet.Transparent.class, "/myproxy/*");
      mHolder.setInitOrder(1);
      mHolder.setInitParameter("proxyTo", "http://www.htmlhelp.com");
      mHolder.setInitParameter("prefix", "/myproxy");
      mServer.start();
      mServer.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


--- CODE END ---


When I run this, I can access the website htmlhelp.com in Chrome via the URL http://127.0.0.1:7070/myproxy/ as expected.  However, the links and images on the page do not work, since they all refer to "127.0.0.1:7070/" (rather than "127.0.0.1:7070/myproxy/").  So for example, "http://127.0.0.1:7070/about/", "http://127.0.0.1:7070/icon/wdglogo.gif", etc. which of course are not recognised URL's on the Jetty server.

On further investigation, I learned that the HTTP request includes headers, one of which is "host".  It seems that a website uses this header to construct the link and image URL's.  Indeed, if I change the "host" header in the debugger to "htmlhelp.com" immediately before the request is sent, then everything loads as expected.

Now as I understand, the "host" header is an "end to end" header, meaning that the proxy should NOT modify it, so it seems that isn't an option.  Besides, doing this would cause image loading and links to bypass the proxy anyway.

Is it possible to accomplish what I'm trying to do?  Apologies for the newbie question; this HTTP and web stuff is still quite new to me.

In case it makes any difference, I'm using Jetty 9.0.7.

--
With Kind Regards,
Martin Brentnall


Back to the top