23
23
import javax .servlet .http .Cookie ;
24
24
import javax .servlet .http .HttpServletRequest ;
25
25
import javax .servlet .http .HttpServletResponse ;
26
+ import java .io .ByteArrayOutputStream ;
26
27
import java .io .IOException ;
27
28
import java .nio .charset .StandardCharsets ;
28
29
import java .util .Enumeration ;
30
+ import java .util .zip .Deflater ;
29
31
30
- import static io .netty .handler .codec .http .HttpHeaderNames .CONTENT_LENGTH ;
31
- import static io .netty .handler .codec .http .HttpHeaderNames .CONTENT_MD5 ;
32
+ import static io .netty .handler .codec .http .HttpHeaderNames .*;
33
+ import static io .netty .handler .codec .http .HttpHeaderValues .CHUNKED ;
34
+ import static io .netty .handler .codec .http .HttpHeaderValues .DEFLATE ;
32
35
33
36
public class EchoHandler extends AbstractHandler {
34
37
@@ -115,18 +118,14 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
115
118
}
116
119
}
117
120
118
- String requestBodyLength = httpRequest .getHeader ("X-" + CONTENT_LENGTH );
121
+ if (httpRequest .getHeader ("X-COMPRESS" ) != null ) {
122
+ byte [] compressed = deflate (IOUtils .toByteArray (httpRequest .getInputStream ()));
123
+ httpResponse .addIntHeader (CONTENT_LENGTH .toString (), compressed .length );
124
+ httpResponse .addHeader (CONTENT_ENCODING .toString (), DEFLATE .toString ());
125
+ httpResponse .getOutputStream ().write (compressed , 0 , compressed .length );
119
126
120
- if (requestBodyLength != null ) {
121
- byte [] requestBodyBytes = IOUtils .toByteArray (httpRequest .getInputStream ());
122
- int total = requestBodyBytes .length ;
123
-
124
- httpResponse .addIntHeader ("X-" + CONTENT_LENGTH , total );
125
- String md5 = TestUtils .md5 (requestBodyBytes , 0 , total );
126
- httpResponse .addHeader (CONTENT_MD5 .toString (), md5 );
127
-
128
- httpResponse .getOutputStream ().write (requestBodyBytes , 0 , total );
129
127
} else {
128
+ httpResponse .addHeader (TRANSFER_ENCODING .toString (), CHUNKED .toString ());
130
129
int size = 16384 ;
131
130
if (httpRequest .getContentLength () > 0 ) {
132
131
size = httpRequest .getContentLength ();
@@ -148,4 +147,21 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
148
147
// FIXME don't always close, depends on the test, cf ReactiveStreamsTest
149
148
httpResponse .getOutputStream ().close ();
150
149
}
150
+
151
+ private static byte [] deflate (byte [] input ) throws IOException {
152
+ Deflater compressor = new Deflater ();
153
+ compressor .setLevel (Deflater .BEST_COMPRESSION );
154
+
155
+ compressor .setInput (input );
156
+ compressor .finish ();
157
+
158
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream (input .length )) {
159
+ byte [] buf = new byte [1024 ];
160
+ while (!compressor .finished ()) {
161
+ int count = compressor .deflate (buf );
162
+ bos .write (buf , 0 , count );
163
+ }
164
+ return bos .toByteArray ();
165
+ }
166
+ }
151
167
}
0 commit comments