13
13
*/
14
14
package org .asynchttpclient .test ;
15
15
16
- import org .eclipse .jetty .server .Request ;
17
- import org .eclipse .jetty .server .handler .AbstractHandler ;
18
- import org .slf4j .Logger ;
19
- import org .slf4j .LoggerFactory ;
16
+ import static io .netty .handler .codec .http .HttpHeaderNames .*;
17
+ import io .netty .buffer .ByteBufUtil ;
18
+ import io .netty .buffer .Unpooled ;
19
+ import io .netty .util .internal .StringUtil ;
20
+
21
+ import java .io .IOException ;
22
+ import java .util .Enumeration ;
20
23
21
24
import javax .servlet .ServletException ;
22
25
import javax .servlet .http .Cookie ;
23
26
import javax .servlet .http .HttpServletRequest ;
24
27
import javax .servlet .http .HttpServletResponse ;
25
28
26
- import java .io .IOException ;
27
- import java .util .Enumeration ;
29
+ import org .eclipse .jetty .server .Request ;
30
+ import org .eclipse .jetty .server .handler .AbstractHandler ;
31
+ import org .slf4j .Logger ;
32
+ import org .slf4j .LoggerFactory ;
28
33
29
34
public class EchoHandler extends AbstractHandler {
30
-
35
+
31
36
private static final Logger LOGGER = LoggerFactory .getLogger (EchoHandler .class );
32
37
33
38
@ Override
34
39
public void handle (String pathInContext , Request request , HttpServletRequest httpRequest , HttpServletResponse httpResponse ) throws IOException , ServletException {
35
40
36
41
LOGGER .debug ("Echo received request {} on path {}" , request , pathInContext );
37
-
42
+
38
43
if (httpRequest .getHeader ("X-HEAD" ) != null ) {
39
44
httpResponse .setContentLength (1 );
40
45
}
@@ -49,34 +54,23 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
49
54
httpResponse .addHeader ("Allow" , "GET,HEAD,POST,OPTIONS,TRACE" );
50
55
}
51
56
52
- Enumeration <? > e = httpRequest .getHeaderNames ();
53
- String param ;
57
+ Enumeration <String > e = httpRequest .getHeaderNames ();
58
+ String headerName ;
54
59
while (e .hasMoreElements ()) {
55
- param = e .nextElement ().toString ();
56
-
57
- if (param .startsWith ("LockThread" )) {
58
- final int sleepTime = httpRequest .getIntHeader (param );
60
+ headerName = e .nextElement ();
61
+ if (headerName .startsWith ("LockThread" )) {
62
+ final int sleepTime = httpRequest .getIntHeader (headerName );
59
63
try {
60
64
Thread .sleep (sleepTime == -1 ? 40 : sleepTime * 1000 );
61
65
} catch (InterruptedException ex ) {
62
66
}
63
67
}
64
68
65
- if (param .startsWith ("X-redirect" )) {
69
+ if (headerName .startsWith ("X-redirect" )) {
66
70
httpResponse .sendRedirect (httpRequest .getHeader ("X-redirect" ));
67
71
return ;
68
72
}
69
- httpResponse .addHeader ("X-" + param , httpRequest .getHeader (param ));
70
- }
71
-
72
- Enumeration <?> i = httpRequest .getParameterNames ();
73
-
74
- StringBuilder requestBody = new StringBuilder ();
75
- while (i .hasMoreElements ()) {
76
- param = i .nextElement ().toString ();
77
- httpResponse .addHeader ("X-" + param , httpRequest .getParameter (param ));
78
- requestBody .append (param );
79
- requestBody .append ("_" );
73
+ httpResponse .addHeader ("X-" + headerName , httpRequest .getHeader (headerName ));
80
74
}
81
75
82
76
String pathInfo = httpRequest .getPathInfo ();
@@ -96,27 +90,71 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
96
90
}
97
91
}
98
92
99
- if (requestBody .length () > 0 ) {
100
- httpResponse .getOutputStream ().write (requestBody .toString ().getBytes ());
101
- }
93
+ Enumeration <String > i = httpRequest .getParameterNames ();
94
+ if (i .hasMoreElements ()) {
95
+ StringBuilder requestBody = new StringBuilder ();
96
+ while (i .hasMoreElements ()) {
97
+ headerName = i .nextElement ();
98
+ httpResponse .addHeader ("X-" + headerName , httpRequest .getParameter (headerName ));
99
+ requestBody .append (headerName );
100
+ requestBody .append ("_" );
101
+ }
102
102
103
- int size = 16384 ;
104
- if (httpRequest .getContentLength () > 0 ) {
105
- size = httpRequest .getContentLength ();
103
+ if (requestBody .length () > 0 ) {
104
+ String body = requestBody .toString ();
105
+ httpResponse .getOutputStream ().write (body .getBytes ());
106
+ }
106
107
}
107
- byte [] bytes = new byte [size ];
108
- if (bytes .length > 0 ) {
108
+
109
+ String clientContentLength = httpRequest .getHeader ("X-" + CONTENT_LENGTH );
110
+ String clientMd5 = httpRequest .getHeader ("X-" + CONTENT_MD5 );
111
+
112
+ if (clientContentLength != null ) {
113
+ byte [] bytes = new byte [Integer .valueOf (clientContentLength )];
109
114
int read = 0 ;
115
+ int total = 0 ;
110
116
while (read > -1 ) {
111
- read = httpRequest .getInputStream ().read (bytes );
117
+ read = httpRequest .getInputStream ().read (bytes , total , 5000 );
112
118
if (read > 0 ) {
113
- httpResponse .getOutputStream ().write (bytes , 0 , read );
119
+ total += read ;
120
+ }
121
+ }
122
+
123
+ httpResponse .addIntHeader ("X-" + CONTENT_LENGTH , total );
124
+ String md5 = TestUtils .md5 (bytes , 0 , total );
125
+ httpResponse .addHeader (CONTENT_MD5 .toString (), md5 );
126
+
127
+ if (!md5 .equals (clientMd5 )) {
128
+ int length = total ;
129
+ int rows = length / 16 + (length % 15 == 0 ? 0 : 1 ) + 4 ;
130
+ StringBuilder buf = new StringBuilder ("JETTY" .length () + 1 + "JETTY" .length () + 2 + 10 + 1 + 2 + rows * 80 );
131
+
132
+ buf .append ("JETTY" ).append (' ' ).append ("JETTY" ).append (": " ).append (length ).append ('B' ).append (StringUtil .NEWLINE );
133
+ ByteBufUtil .appendPrettyHexDump (buf , Unpooled .wrappedBuffer (bytes ));
134
+ LOGGER .error (buf .toString ());
135
+ }
136
+
137
+ httpResponse .getOutputStream ().write (bytes , 0 , total );
138
+ } else {
139
+ int size = 16384 ;
140
+ if (httpRequest .getContentLength () > 0 ) {
141
+ size = httpRequest .getContentLength ();
142
+ }
143
+ if (size > 0 ) {
144
+ int read = 0 ;
145
+ while (read > -1 ) {
146
+ byte [] bytes = new byte [size ];
147
+ read = httpRequest .getInputStream ().read (bytes );
148
+ if (read > 0 ) {
149
+ httpResponse .getOutputStream ().write (bytes , 0 , read );
150
+ }
114
151
}
115
152
}
116
153
}
117
154
118
155
request .setHandled (true );
119
156
httpResponse .getOutputStream ().flush ();
157
+ // FIXME don't always close, depends on the test, cf ReactiveStreamsTest
120
158
httpResponse .getOutputStream ().close ();
121
159
}
122
- }
160
+ }
0 commit comments