View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.fcgi.server.proxy;
20  
21  import java.net.URI;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  import javax.servlet.RequestDispatcher;
25  import javax.servlet.ServletConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  
29  import org.eclipse.jetty.client.HttpClient;
30  import org.eclipse.jetty.client.api.Request;
31  import org.eclipse.jetty.fcgi.FCGI;
32  import org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI;
33  import org.eclipse.jetty.http.HttpFields;
34  import org.eclipse.jetty.http.HttpScheme;
35  import org.eclipse.jetty.proxy.ProxyServlet;
36  
37  /**
38   * Specific implementation of {@link ProxyServlet.Transparent} for FastCGI.
39   * <p />
40   * This servlet accepts a HTTP request and transforms it into a FastCGI request
41   * that is sent to the FastCGI server specified in the <code>proxyTo</code>
42   * init-param.
43   * <p />
44   * This servlet accepts two additional init-params:
45   * <ul>
46   *     <li><code>scriptRoot</code>, mandatory, that must be set to the directory where
47   *     the application that must be served via FastCGI is installed and corresponds to
48   *     the FastCGI DOCUMENT_ROOT parameter</li>
49   *     <li><code>scriptPattern</code>, optional, defaults to <code>(.+?\.php)</code>,
50   *     that specifies a regular expression with at least 1 and at most 2 groups that specify
51   *     respectively:
52   *     <ul>
53   *         <li>the FastCGI SCRIPT_NAME parameter</li>
54   *         <li>the FastCGI PATH_INFO parameter</li>
55   *     </ul></li>
56   * </ul>
57   *
58   * @see TryFilesFilter
59   */
60  public class FastCGIProxyServlet extends ProxyServlet.Transparent
61  {
62      public static final String SCRIPT_ROOT_INIT_PARAM = "scriptRoot";
63      public static final String SCRIPT_PATTERN_INIT_PARAM = "scriptPattern";
64      private static final String REMOTE_ADDR_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".remoteAddr";
65      private static final String REMOTE_PORT_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".remotePort";
66      private static final String SERVER_NAME_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".serverName";
67      private static final String SERVER_ADDR_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".serverAddr";
68      private static final String SERVER_PORT_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".serverPort";
69      private static final String SCHEME_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".scheme";
70      private static final String REQUEST_URI_ATTRIBUTE = FastCGIProxyServlet.class.getName() + ".requestURI";
71  
72      private Pattern scriptPattern;
73  
74      @Override
75      public void init() throws ServletException
76      {
77          super.init();
78  
79          String value = getInitParameter(SCRIPT_PATTERN_INIT_PARAM);
80          if (value == null)
81              value = "(.+?\\.php)";
82          scriptPattern = Pattern.compile(value);
83      }
84  
85      @Override
86      protected HttpClient newHttpClient()
87      {
88          ServletConfig config = getServletConfig();
89          String scriptRoot = config.getInitParameter(SCRIPT_ROOT_INIT_PARAM);
90          if (scriptRoot == null)
91              throw new IllegalArgumentException("Mandatory parameter '" + SCRIPT_ROOT_INIT_PARAM + "' not configured");
92          return new HttpClient(new ProxyHttpClientTransportOverFCGI(scriptRoot), null);
93      }
94  
95      @Override
96      protected void customizeProxyRequest(Request proxyRequest, HttpServletRequest request)
97      {
98          proxyRequest.attribute(REMOTE_ADDR_ATTRIBUTE, request.getRemoteAddr());
99          proxyRequest.attribute(REMOTE_PORT_ATTRIBUTE, String.valueOf(request.getRemotePort()));
100         proxyRequest.attribute(SERVER_NAME_ATTRIBUTE, request.getServerName());
101         proxyRequest.attribute(SERVER_ADDR_ATTRIBUTE, request.getLocalAddr());
102         proxyRequest.attribute(SERVER_PORT_ATTRIBUTE, String.valueOf(request.getLocalPort()));
103 
104         proxyRequest.attribute(SCHEME_ATTRIBUTE, request.getScheme());
105 
106         // If we are forwarded or included, retain the original request URI.
107         String originalPath = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
108         String originalQuery = (String)request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
109         if (originalPath == null)
110         {
111             originalPath = (String)request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
112             originalQuery = (String)request.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING);
113         }
114         if (originalPath != null)
115         {
116             String originalURI = originalPath;
117             if (originalQuery != null)
118                 originalURI += "?" + originalQuery;
119             proxyRequest.attribute(REQUEST_URI_ATTRIBUTE, originalURI);
120         }
121 
122         super.customizeProxyRequest(proxyRequest, request);
123     }
124 
125     protected void customizeFastCGIHeaders(Request proxyRequest, HttpFields fastCGIHeaders)
126     {
127         fastCGIHeaders.put(FCGI.Headers.REMOTE_ADDR, (String)proxyRequest.getAttributes().get(REMOTE_ADDR_ATTRIBUTE));
128         fastCGIHeaders.put(FCGI.Headers.REMOTE_PORT, (String)proxyRequest.getAttributes().get(REMOTE_PORT_ATTRIBUTE));
129         fastCGIHeaders.put(FCGI.Headers.SERVER_NAME, (String)proxyRequest.getAttributes().get(SERVER_NAME_ATTRIBUTE));
130         fastCGIHeaders.put(FCGI.Headers.SERVER_ADDR, (String)proxyRequest.getAttributes().get(SERVER_ADDR_ATTRIBUTE));
131         fastCGIHeaders.put(FCGI.Headers.SERVER_PORT, (String)proxyRequest.getAttributes().get(SERVER_PORT_ATTRIBUTE));
132 
133         if (HttpScheme.HTTPS.is((String)proxyRequest.getAttributes().get(SCHEME_ATTRIBUTE)))
134             fastCGIHeaders.put(FCGI.Headers.HTTPS, "on");
135 
136         URI proxyRequestURI = proxyRequest.getURI();
137         String rawPath = proxyRequestURI.getRawPath();
138         String rawQuery = proxyRequestURI.getRawQuery();
139 
140         String requestURI = (String)proxyRequest.getAttributes().get(REQUEST_URI_ATTRIBUTE);
141         if (requestURI == null)
142         {
143             requestURI = rawPath;
144             if (rawQuery != null)
145                 requestURI += "?" + rawQuery;
146         }
147         fastCGIHeaders.put(FCGI.Headers.REQUEST_URI, requestURI);
148 
149         String scriptName = rawPath;
150         Matcher matcher = scriptPattern.matcher(rawPath);
151         if (matcher.matches())
152         {
153             // Expect at least one group in the regular expression.
154             scriptName = matcher.group(1);
155 
156             // If there is a second group, map it to PATH_INFO.
157             if (matcher.groupCount() > 1)
158                 fastCGIHeaders.put(FCGI.Headers.PATH_INFO, matcher.group(2));
159         }
160         fastCGIHeaders.put(FCGI.Headers.SCRIPT_NAME, scriptName);
161 
162         String root = fastCGIHeaders.get(FCGI.Headers.DOCUMENT_ROOT);
163         fastCGIHeaders.put(FCGI.Headers.SCRIPT_FILENAME, root + scriptName);
164     }
165 
166     private class ProxyHttpClientTransportOverFCGI extends HttpClientTransportOverFCGI
167     {
168         public ProxyHttpClientTransportOverFCGI(String scriptRoot)
169         {
170             super(scriptRoot);
171         }
172 
173         @Override
174         protected void customize(Request request, HttpFields fastCGIHeaders)
175         {
176             super.customize(request, fastCGIHeaders);
177             customizeFastCGIHeaders(request, fastCGIHeaders);
178         }
179     }
180 }