/
EchoHandler.java
140 lines (116 loc) · 5.36 KB
/
EchoHandler.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
/*
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.test;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EchoHandler extends AbstractHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoHandler.class);
@Override
public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
LOGGER.debug("Echo received request {} on path {}", request, pathInContext);
if (httpRequest.getHeader("X-HEAD") != null) {
httpResponse.setContentLength(1);
}
if (httpRequest.getHeader("X-ISO") != null) {
httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_ISO_8859_1_CHARSET);
} else {
httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET);
}
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
httpResponse.addHeader("Allow", "GET,HEAD,POST,OPTIONS,TRACE");
}
Enumeration<String> e = httpRequest.getHeaderNames();
String headerName;
while (e.hasMoreElements()) {
headerName = e.nextElement();
if (headerName.startsWith("LockThread")) {
final int sleepTime = httpRequest.getIntHeader(headerName);
try {
Thread.sleep(sleepTime == -1 ? 40 : sleepTime * 1000);
} catch (InterruptedException ex) {
}
}
if (headerName.startsWith("X-redirect")) {
httpResponse.sendRedirect(httpRequest.getHeader("X-redirect"));
return;
}
httpResponse.addHeader("X-" + headerName, httpRequest.getHeader(headerName));
}
String pathInfo = httpRequest.getPathInfo();
if (pathInfo != null)
httpResponse.addHeader("X-pathInfo", pathInfo);
String queryString = httpRequest.getQueryString();
if (queryString != null)
httpResponse.addHeader("X-queryString", queryString);
httpResponse.addHeader("X-KEEP-ALIVE", httpRequest.getRemoteAddr() + ":" + httpRequest.getRemotePort());
Cookie[] cs = httpRequest.getCookies();
if (cs != null) {
for (Cookie c : cs) {
httpResponse.addCookie(c);
}
}
Enumeration<String> i = httpRequest.getParameterNames();
if (i.hasMoreElements()) {
StringBuilder requestBody = new StringBuilder();
while (i.hasMoreElements()) {
headerName = i.nextElement();
httpResponse.addHeader("X-" + headerName, httpRequest.getParameter(headerName));
requestBody.append(headerName);
requestBody.append("_");
}
if (requestBody.length() > 0) {
String body = requestBody.toString();
httpResponse.getOutputStream().write(body.getBytes());
}
}
String requestBodyLength = httpRequest.getHeader("X-" + CONTENT_LENGTH);
if (requestBodyLength != null) {
byte[] requestBodyBytes = IOUtils.toByteArray(httpRequest.getInputStream());
int total = requestBodyBytes.length;
httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total);
String md5 = TestUtils.md5(requestBodyBytes, 0, total);
httpResponse.addHeader(CONTENT_MD5.toString(), md5);
httpResponse.getOutputStream().write(requestBodyBytes, 0, total);
} else {
int size = 16384;
if (httpRequest.getContentLength() > 0) {
size = httpRequest.getContentLength();
}
if (size > 0) {
int read = 0;
while (read > -1) {
byte[] bytes = new byte[size];
read = httpRequest.getInputStream().read(bytes);
if (read > 0) {
httpResponse.getOutputStream().write(bytes, 0, read);
}
}
}
}
request.setHandled(true);
httpResponse.getOutputStream().flush();
// FIXME don't always close, depends on the test, cf ReactiveStreamsTest
httpResponse.getOutputStream().close();
}
}