-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
FastCGIProxyServletTest.java
263 lines (236 loc) · 10.3 KB
/
FastCGIProxyServletTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.fcgi.server.proxy;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.util.FutureResponseListener;
import org.eclipse.jetty.fcgi.FCGI;
import org.eclipse.jetty.fcgi.server.ServerFCGIConnectionFactory;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class FastCGIProxyServletTest
{
private final Map<String, String> fcgiParams = new HashMap<>();
private Server server;
private ServerConnector httpConnector;
private Connector fcgiConnector;
private ServletContextHandler context;
private HttpClient client;
private Path unixDomainPath;
public void prepare(boolean sendStatus200, HttpServlet servlet) throws Exception
{
QueuedThreadPool serverThreads = new QueuedThreadPool();
serverThreads.setName("server");
server = new Server(serverThreads);
httpConnector = new ServerConnector(server);
server.addConnector(httpConnector);
ServerFCGIConnectionFactory fcgi = new ServerFCGIConnectionFactory(new HttpConfiguration(), sendStatus200);
if (unixDomainPath == null)
{
fcgiConnector = new ServerConnector(server, fcgi);
}
else
{
UnixDomainServerConnector connector = new UnixDomainServerConnector(server, fcgi);
connector.setUnixDomainPath(unixDomainPath);
fcgiConnector = connector;
}
server.addConnector(fcgiConnector);
String contextPath = "/";
context = new ServletContextHandler(server, contextPath);
String servletPath = "/script";
FastCGIProxyServlet fcgiServlet = new FastCGIProxyServlet()
{
@Override
protected String rewriteTarget(HttpServletRequest request)
{
String uri = "http://localhost";
if (unixDomainPath == null)
uri += ":" + ((ServerConnector)fcgiConnector).getLocalPort();
return uri + servletPath + request.getServletPath();
}
};
ServletHolder fcgiServletHolder = new ServletHolder(fcgiServlet);
fcgiServletHolder.setName("fcgi");
fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_ROOT_INIT_PARAM, "/scriptRoot");
fcgiServletHolder.setInitParameter("proxyTo", "http://localhost");
fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_PATTERN_INIT_PARAM, "(.+?\\.php)");
fcgiParams.forEach(fcgiServletHolder::setInitParameter);
context.addServlet(fcgiServletHolder, "*.php");
context.addServlet(new ServletHolder(servlet), servletPath + "/*");
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
client = new HttpClient();
client.setExecutor(clientThreads);
server.addBean(client);
server.start();
}
@AfterEach
public void dispose() throws Exception
{
server.stop();
}
@ParameterizedTest(name = "[{index}] sendStatus200={0}")
@ValueSource(booleans = {true, false})
public void testGETWithSmallResponseContent(boolean sendStatus200) throws Exception
{
testGETWithResponseContent(sendStatus200, 1024, 0);
}
@ParameterizedTest(name = "[{index}] sendStatus200={0}")
@ValueSource(booleans = {true, false})
public void testGETWithLargeResponseContent(boolean sendStatus200) throws Exception
{
testGETWithResponseContent(sendStatus200, 16 * 1024 * 1024, 0);
}
@ParameterizedTest(name = "[{index}] sendStatus200={0}")
@ValueSource(booleans = {true, false})
public void testGETWithLargeResponseContentWithSlowClient(boolean sendStatus200) throws Exception
{
testGETWithResponseContent(sendStatus200, 16 * 1024 * 1024, 1);
}
private void testGETWithResponseContent(boolean sendStatus200, int length, long delay) throws Exception
{
byte[] data = new byte[length];
new Random().nextBytes(data);
String path = "/foo/index.php";
prepare(sendStatus200, new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
assertTrue(request.getRequestURI().endsWith(path));
response.setContentLength(data.length);
response.getOutputStream().write(data);
}
});
Request request = client.newRequest("localhost", httpConnector.getLocalPort())
.onResponseContentAsync((response, content, callback) ->
{
try
{
if (delay > 0)
TimeUnit.MILLISECONDS.sleep(delay);
callback.succeeded();
}
catch (InterruptedException x)
{
callback.failed(x);
}
})
.path(path);
FutureResponseListener listener = new FutureResponseListener(request, length);
request.send(listener);
ContentResponse response = listener.get(30, TimeUnit.SECONDS);
assertEquals(200, response.getStatus());
assertArrayEquals(data, response.getContent());
}
@ParameterizedTest(name = "[{index}] sendStatus200={0}")
@ValueSource(booleans = {true, false})
public void testURIRewrite(boolean sendStatus200) throws Exception
{
String originalPath = "/original/index.php";
String originalQuery = "foo=bar";
String remotePath = "/remote/index.php";
String pathAttribute = "_path_attribute";
String queryAttribute = "_query_attribute";
fcgiParams.put(FastCGIProxyServlet.ORIGINAL_URI_ATTRIBUTE_INIT_PARAM, pathAttribute);
fcgiParams.put(FastCGIProxyServlet.ORIGINAL_QUERY_ATTRIBUTE_INIT_PARAM, queryAttribute);
prepare(sendStatus200, new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
{
assertThat((String)request.getAttribute(FCGI.Headers.REQUEST_URI), Matchers.startsWith(originalPath));
assertEquals(originalQuery, request.getAttribute(FCGI.Headers.QUERY_STRING));
assertThat(request.getRequestURI(), Matchers.endsWith(remotePath));
}
});
context.stop();
context.insertHandler(new HandlerWrapper()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (target.startsWith("/remote/"))
{
request.setAttribute(pathAttribute, originalPath);
request.setAttribute(queryAttribute, originalQuery);
}
super.handle(target, baseRequest, request, response);
}
});
context.start();
ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort())
.path(remotePath)
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
}
@Test
@EnabledForJreRange(min = JRE.JAVA_16)
public void testUnixDomain() throws Exception
{
int maxUnixDomainPathLength = 108;
Path path = Files.createTempFile("unix", ".sock");
if (path.normalize().toAbsolutePath().toString().length() > maxUnixDomainPathLength)
path = Files.createTempFile(Path.of("/tmp"), "unix", ".sock");
assertTrue(Files.deleteIfExists(path));
unixDomainPath = path;
fcgiParams.put("unixDomainPath", path.toString());
byte[] content = new byte[512];
new Random().nextBytes(content);
prepare(true, new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.getOutputStream().write(content);
}
});
ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort())
.path("/index.php")
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
assertArrayEquals(content, response.getContent());
}
}