|
31 | 31 | import java.util.concurrent.CountDownLatch;
|
32 | 32 | import java.util.concurrent.ExecutionException;
|
33 | 33 |
|
| 34 | +import javax.servlet.AsyncContext; |
| 35 | +import javax.servlet.ReadListener; |
34 | 36 | import javax.servlet.ServletException;
|
| 37 | +import javax.servlet.ServletInputStream; |
35 | 38 | import javax.servlet.http.Cookie;
|
36 | 39 | import javax.servlet.http.HttpServlet;
|
37 | 40 | import javax.servlet.http.HttpServletRequest;
|
38 | 41 | import javax.servlet.http.HttpServletResponse;
|
39 | 42 |
|
40 | 43 | import org.apache.catalina.Context;
|
| 44 | +import org.apache.catalina.Wrapper; |
41 | 45 | import org.apache.catalina.startup.Tomcat;
|
42 |
| -import org.apache.commons.io.IOUtils; |
43 | 46 | import org.asynchttpclient.AsyncHttpClient;
|
44 | 47 | import org.asynchttpclient.BoundRequestBuilder;
|
45 | 48 | import org.asynchttpclient.HttpResponseBodyPart;
|
@@ -84,7 +87,7 @@ public void setUpGlobal() throws Exception {
|
84 | 87 | tomcat.setBaseDir(path);
|
85 | 88 | Context ctx = tomcat.addContext("", path);
|
86 | 89 |
|
87 |
| - Tomcat.addServlet(ctx, "webdav", new HttpServlet() { |
| 90 | + Wrapper wrapper = Tomcat.addServlet(ctx, "webdav", new HttpServlet() { |
88 | 91 |
|
89 | 92 | @Override
|
90 | 93 | public void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException {
|
@@ -156,39 +159,45 @@ public void service(HttpServletRequest httpRequest, HttpServletResponse httpResp
|
156 | 159 | }
|
157 | 160 | }
|
158 | 161 |
|
159 |
| - String requestBodyLength = httpRequest.getHeader("X-" + CONTENT_LENGTH); |
| 162 | + final AsyncContext context = httpRequest.startAsync(); |
| 163 | + final ServletInputStream input = httpRequest.getInputStream(); |
| 164 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
160 | 165 |
|
161 |
| - if (requestBodyLength != null) { |
162 |
| - byte[] requestBodyBytes = IOUtils.toByteArray(httpRequest.getInputStream()); |
163 |
| - int total = requestBodyBytes.length; |
| 166 | + input.setReadListener(new ReadListener() { |
164 | 167 |
|
165 |
| - httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total); |
166 |
| - String md5 = TestUtils.md5(requestBodyBytes, 0, total); |
167 |
| - httpResponse.addHeader(CONTENT_MD5.toString(), md5); |
| 168 | + byte[] buffer = new byte[5 * 1024]; |
168 | 169 |
|
169 |
| - httpResponse.getOutputStream().write(requestBodyBytes, 0, total); |
170 |
| - } else { |
171 |
| - int size = 16384; |
172 |
| - if (httpRequest.getContentLength() > 0) { |
173 |
| - size = httpRequest.getContentLength(); |
| 170 | + @Override |
| 171 | + public void onError(Throwable t) { |
| 172 | + t.printStackTrace(); |
| 173 | + httpResponse.setStatus(io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR.code()); |
| 174 | + context.complete(); |
174 | 175 | }
|
175 |
| - if (size > 0) { |
176 |
| - int read = 0; |
177 |
| - while (read > -1) { |
178 |
| - byte[] bytes = new byte[size]; |
179 |
| - read = httpRequest.getInputStream().read(bytes); |
180 |
| - if (read > 0) { |
181 |
| - httpResponse.getOutputStream().write(bytes, 0, read); |
182 |
| - } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void onDataAvailable() throws IOException { |
| 179 | + int len = -1; |
| 180 | + while (input.isReady() && (len = input.read(buffer)) != -1) { |
| 181 | + baos.write(buffer, 0, len); |
183 | 182 | }
|
184 | 183 | }
|
185 |
| - } |
186 | 184 |
|
187 |
| - httpResponse.getOutputStream().flush(); |
188 |
| - // FIXME don't always close, depends on the test, cf ReactiveStreamsTest |
189 |
| -// httpResponse.getOutputStream().close(); |
| 185 | + @Override |
| 186 | + public void onAllDataRead() throws IOException { |
| 187 | + byte[] requestBodyBytes = baos.toByteArray(); |
| 188 | + int total = requestBodyBytes.length; |
| 189 | + |
| 190 | + httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total); |
| 191 | + String md5 = TestUtils.md5(requestBodyBytes, 0, total); |
| 192 | + httpResponse.addHeader(CONTENT_MD5.toString(), md5); |
| 193 | + |
| 194 | + httpResponse.getOutputStream().write(requestBodyBytes, 0, total); |
| 195 | + context.complete(); |
| 196 | + } |
| 197 | + }); |
190 | 198 | }
|
191 | 199 | });
|
| 200 | + wrapper.setAsyncSupported(true); |
192 | 201 | ctx.addServletMappingDecoded("/*", "webdav");
|
193 | 202 | tomcat.start();
|
194 | 203 | port1 = tomcat.getConnector().getLocalPort();
|
|
0 commit comments