1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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
154 scriptName = matcher.group(1);
155
156
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 }