1313 */
1414package org .asynchttpclient .test ;
1515
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 ;
2023
2124import javax .servlet .ServletException ;
2225import javax .servlet .http .Cookie ;
2326import javax .servlet .http .HttpServletRequest ;
2427import javax .servlet .http .HttpServletResponse ;
2528
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 ;
2833
2934public class EchoHandler extends AbstractHandler {
30-
35+
3136private static final Logger LOGGER =LoggerFactory .getLogger (EchoHandler .class );
3237
3338@ Override
3439public void handle (String pathInContext ,Request request ,HttpServletRequest httpRequest ,HttpServletResponse httpResponse )throws IOException ,ServletException {
3540
3641LOGGER .debug ("Echo received request {} on path {}" ,request ,pathInContext );
37-
42+
3843if (httpRequest .getHeader ("X-HEAD" ) !=null ) {
3944httpResponse .setContentLength (1 );
4045 }
@@ -49,34 +54,23 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
4954httpResponse .addHeader ("Allow" ,"GET,HEAD,POST,OPTIONS,TRACE" );
5055 }
5156
52- Enumeration <? >e =httpRequest .getHeaderNames ();
53- String param ;
57+ Enumeration <String >e =httpRequest .getHeaderNames ();
58+ String headerName ;
5459while (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 );
5963try {
6064Thread .sleep (sleepTime == -1 ?40 :sleepTime *1000 );
6165 }catch (InterruptedException ex ) {
6266 }
6367 }
6468
65- if (param .startsWith ("X-redirect" )) {
69+ if (headerName .startsWith ("X-redirect" )) {
6670httpResponse .sendRedirect (httpRequest .getHeader ("X-redirect" ));
6771return ;
6872 }
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 ));
8074 }
8175
8276String pathInfo =httpRequest .getPathInfo ();
@@ -96,27 +90,71 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
9690 }
9791 }
9892
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+ }
102102
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+ }
106107 }
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 )];
109114int read =0 ;
115+ int total =0 ;
110116while (read > -1 ) {
111- read =httpRequest .getInputStream ().read (bytes );
117+ read =httpRequest .getInputStream ().read (bytes , total , 5000 );
112118if (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+ }
114151 }
115152 }
116153 }
117154
118155request .setHandled (true );
119156httpResponse .getOutputStream ().flush ();
157+ // FIXME don't always close, depends on the test, cf ReactiveStreamsTest
120158httpResponse .getOutputStream ().close ();
121159 }
122- }
160+ }