View Javadoc

1   // ========================================================================
2   // $Id: ProxyServlet.java 5263 2009-06-26 09:42:21Z gregw $
3   // Copyright 2004-2004 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.servlet;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.net.HttpURLConnection;
22  import java.net.InetSocketAddress;
23  import java.net.MalformedURLException;
24  import java.net.Socket;
25  import java.net.URL;
26  import java.net.URLConnection;
27  import java.util.Enumeration;
28  import java.util.HashSet;
29  
30  import javax.servlet.Servlet;
31  import javax.servlet.ServletConfig;
32  import javax.servlet.ServletContext;
33  import javax.servlet.ServletException;
34  import javax.servlet.ServletRequest;
35  import javax.servlet.ServletResponse;
36  import javax.servlet.UnavailableException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.mortbay.util.IO;
41  
42  
43  
44  /**
45   * Proxy Servlet.
46   * <p>
47   * Forward requests to another server either as a standard web proxy (as defined by
48   * RFC2616) or as a transparent proxy.
49   *
50   */
51  public class ProxyServlet implements Servlet
52  {
53      
54      protected HashSet _DontProxyHeaders = new HashSet();
55      {
56          _DontProxyHeaders.add("proxy-connection");
57          _DontProxyHeaders.add("connection");
58          _DontProxyHeaders.add("keep-alive");
59          _DontProxyHeaders.add("transfer-encoding");
60          _DontProxyHeaders.add("te");
61          _DontProxyHeaders.add("trailer");
62          _DontProxyHeaders.add("proxy-authorization");
63          _DontProxyHeaders.add("proxy-authenticate");
64          _DontProxyHeaders.add("upgrade");
65      }
66      
67      protected ServletConfig _config;
68      protected ServletContext _context;
69      
70      /* (non-Javadoc)
71       * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
72       */
73      public void init(ServletConfig config) throws ServletException
74      {
75          this._config=config;
76          this._context=config.getServletContext();
77      }
78  
79      /* (non-Javadoc)
80       * @see javax.servlet.Servlet#getServletConfig()
81       */
82      public ServletConfig getServletConfig()
83      {
84          return _config;
85      }
86  
87      /* (non-Javadoc)
88       * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
89       */
90      public void service(ServletRequest req, ServletResponse res) throws ServletException,
91              IOException
92      {
93          HttpServletRequest request = (HttpServletRequest)req;
94          HttpServletResponse response = (HttpServletResponse)res;
95          if ("CONNECT".equalsIgnoreCase(request.getMethod()))
96          {
97              handleConnect(request,response);
98          }
99          else
100         {
101             String uri=request.getRequestURI();
102             if (request.getQueryString()!=null)
103                 uri+="?"+request.getQueryString();
104             
105             URL url=proxyHttpURL(request.getScheme(),
106                     request.getServerName(),
107                     request.getServerPort(),
108                     uri);
109             
110 
111             URLConnection connection = url.openConnection();
112             connection.setAllowUserInteraction(false);
113             
114             // Set method
115             HttpURLConnection http = null;
116             if (connection instanceof HttpURLConnection)
117             {
118                 http = (HttpURLConnection)connection;
119                 http.setRequestMethod(request.getMethod());
120                 http.setInstanceFollowRedirects(false);
121             }
122 
123             // check connection header
124             String connectionHdr = request.getHeader("Connection");
125             if (connectionHdr!=null)
126             {
127                 connectionHdr=connectionHdr.toLowerCase();
128                 if (connectionHdr.equals("keep-alive")||
129                     connectionHdr.equals("close"))
130                     connectionHdr=null;
131             }
132             
133             // copy headers
134             boolean xForwardedFor=false;
135             boolean hasContent=false;
136             Enumeration enm = request.getHeaderNames();
137             while (enm.hasMoreElements())
138             {
139                 // TODO could be better than this!
140                 String hdr=(String)enm.nextElement();
141                 String lhdr=hdr.toLowerCase();
142 
143                 if (_DontProxyHeaders.contains(lhdr))
144                     continue;
145                 if (connectionHdr!=null && connectionHdr.indexOf(lhdr)>=0)
146                     continue;
147 
148                 if ("content-type".equals(lhdr))
149                     hasContent=true;
150 
151                 Enumeration vals = request.getHeaders(hdr);
152                 while (vals.hasMoreElements())
153                 {
154                     String val = (String)vals.nextElement();
155                     if (val!=null)
156                     {
157                         connection.addRequestProperty(hdr,val);
158                         xForwardedFor|="X-Forwarded-For".equalsIgnoreCase(hdr);
159                     }
160                 }
161             }
162 
163             // Proxy headers
164             connection.setRequestProperty("Via","1.1 (jetty)");
165             if (!xForwardedFor)
166                 connection.addRequestProperty("X-Forwarded-For",
167                                               request.getRemoteAddr());
168 
169             // a little bit of cache control
170             String cache_control = request.getHeader("Cache-Control");
171             if (cache_control!=null &&
172                 (cache_control.indexOf("no-cache")>=0 ||
173                  cache_control.indexOf("no-store")>=0))
174                 connection.setUseCaches(false);
175 
176             // customize Connection
177             
178             try
179             {    
180                 connection.setDoInput(true);
181                 
182                 // do input thang!
183                 InputStream in=request.getInputStream();
184                 if (hasContent)
185                 {
186                     connection.setDoOutput(true);
187                     IO.copy(in,connection.getOutputStream());
188                 }
189                 
190                 // Connect                
191                 connection.connect();    
192             }
193             catch (Exception e)
194             {
195                 _context.log("proxy",e);
196             }
197             
198             InputStream proxy_in = null;
199 
200             // handler status codes etc.
201             int code=500;
202             if (http!=null)
203             {
204                 proxy_in = http.getErrorStream();
205                 
206                 code=http.getResponseCode();
207                 response.setStatus(code, http.getResponseMessage());
208             }
209             
210             if (proxy_in==null)
211             {
212                 try {proxy_in=connection.getInputStream();}
213                 catch (Exception e)
214                 {
215                     _context.log("stream",e);
216                     proxy_in = http.getErrorStream();
217                 }
218             }
219             
220             // clear response defaults.
221             response.setHeader("Date",null);
222             response.setHeader("Server",null);
223             
224             // set response headers
225             int h=0;
226             String hdr=connection.getHeaderFieldKey(h);
227             String val=connection.getHeaderField(h);
228             while(hdr!=null || val!=null)
229             {
230                 String lhdr = hdr!=null?hdr.toLowerCase():null;
231                 if (hdr!=null && val!=null && !_DontProxyHeaders.contains(lhdr))
232                     response.addHeader(hdr,val);
233                 
234                 h++;
235                 hdr=connection.getHeaderFieldKey(h);
236                 val=connection.getHeaderField(h);
237             }
238             response.addHeader("Via","1.1 (jetty)");
239 
240             // Handle
241             if (proxy_in!=null)
242                 IO.copy(proxy_in,response.getOutputStream());
243             
244         }
245     }
246 
247     /* ------------------------------------------------------------ */
248     /** Resolve requested URL to the Proxied URL
249      * @param scheme The scheme of the received request.
250      * @param serverName The server encoded in the received request(which 
251      * may be from an absolute URL in the request line).
252      * @param serverPort The server port of the received request (which 
253      * may be from an absolute URL in the request line).
254      * @param uri The URI of the received request.
255      * @return The URL to which the request should be proxied.
256      * @throws MalformedURLException
257      */
258     protected URL proxyHttpURL(String scheme, String serverName, int serverPort, String uri)
259         throws MalformedURLException
260     {
261         return new URL(scheme,serverName,serverPort,uri);
262     }
263     
264     /* ------------------------------------------------------------ */
265     public void handleConnect(HttpServletRequest request,
266                               HttpServletResponse response)
267         throws IOException
268     {
269         String uri = request.getRequestURI();
270         
271         String port = "";
272         String host = "";
273         
274         int c = uri.indexOf(':');
275         if (c>=0)
276         {
277             port = uri.substring(c+1);
278             host = uri.substring(0,c);
279             if (host.indexOf('/')>0)
280                 host = host.substring(host.indexOf('/')+1);
281         }
282 
283         
284        
285 
286         InetSocketAddress inetAddress = new InetSocketAddress (host, Integer.parseInt(port));
287         
288         //if (isForbidden(HttpMessage.__SSL_SCHEME,addrPort.getHost(),addrPort.getPort(),false))
289         //{
290         //    sendForbid(request,response,uri);
291         //}
292         //else
293         {
294             InputStream in=request.getInputStream();
295             OutputStream out=response.getOutputStream();
296             
297             Socket socket = new Socket(inetAddress.getAddress(),inetAddress.getPort());
298             
299             response.setStatus(200);
300             response.setHeader("Connection","close");
301             response.flushBuffer();
302             
303             IO.copyThread(socket.getInputStream(),out);
304             IO.copy(in,socket.getOutputStream());
305         }
306     }
307     
308     
309     
310     
311     /* (non-Javadoc)
312      * @see javax.servlet.Servlet#getServletInfo()
313      */
314     public String getServletInfo()
315     {
316         return "Proxy Servlet";
317     }
318 
319     /* (non-Javadoc)
320      * @see javax.servlet.Servlet#destroy()
321      */
322     public void destroy()
323     {
324 
325     }
326     /**
327      * Transparent Proxy.
328      * 
329      * This convenience extension to AsyncProxyServlet configures the servlet
330      * as a transparent proxy.   The servlet is configured with init parameter:<ul>
331      * <li> ProxyTo - a URI like http://host:80/context to which the request is proxied.
332      * <li> Prefix  - a URI prefix that is striped from the start of the forwarded URI.
333      * </ul>
334      * For example, if a request was received at /foo/bar and the ProxyTo was  http://host:80/context
335      * and the Prefix was /foo, then the request would be proxied to http://host:80/context/bar
336      *
337      */
338     public static class Transparent extends ProxyServlet
339     {
340         String _prefix;
341         String _proxyTo;
342         
343         public Transparent()
344         {    
345         }
346         
347         public Transparent(String prefix,String server, int port)
348         {
349             _prefix=prefix;
350             _proxyTo="http://"+server+":"+port;
351         }
352 
353         public void init(ServletConfig config) throws ServletException
354         {
355             if (config.getInitParameter("ProxyTo")!=null)
356                 _proxyTo=config.getInitParameter("ProxyTo");
357             if (config.getInitParameter("Prefix")!=null)
358                 _prefix=config.getInitParameter("Prefix");
359             if (_proxyTo==null)
360                 throw new UnavailableException("No ProxyTo");
361             super.init(config);
362             
363             _context.log("Transparent ProxyServlet @ "+(_prefix==null?"-":_prefix)+ " to "+_proxyTo);
364             
365         }
366         
367         protected URL proxyHttpURL(final String scheme, final String serverName, int serverPort, final String uri) throws MalformedURLException
368         {
369             if (_prefix!=null && !uri.startsWith(_prefix))
370                 return null;
371             
372             if (_prefix!=null)
373                 return new URL(_proxyTo+uri.substring(_prefix.length()));
374             return new URL(_proxyTo+uri);
375         }
376     }
377 
378 }