View Javadoc

1   import java.io.IOException;
2   import java.net.MalformedURLException;
3   import java.net.Socket;
4   import java.net.SocketTimeoutException;
5   
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServlet;
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import org.eclipse.jetty.server.Handler;
12  import org.eclipse.jetty.server.Server;
13  import org.eclipse.jetty.server.handler.DefaultHandler;
14  import org.eclipse.jetty.server.handler.HandlerList;
15  import org.eclipse.jetty.webapp.WebAppContext;
16  import org.xml.sax.SAXException;
17  
18  
19  /**
20   * Repro a jetty problem.
21   * 
22   * @author hughw
23   *
24   */
25  public class Jetty400Repro extends HttpServlet{
26  
27  
28      private static final long serialVersionUID = 1L;
29      private static final int port = 8080;
30      private static final String host = "localhost";
31      private static final String uri = "/flub/servlet/";
32      
33      /**
34       * Jetty 7.0.1 returns 400 on the second POST, when you send both Connection: Keep-Alive and 
35       * Expect: 100-Continue headers in the request. 
36       * @param args
37       */
38      public static void main(String[] args) throws Exception{
39          initJetty();
40          Thread.sleep(1000);
41          
42          Socket sock = new Socket(host, port);
43          
44          sock.setSoTimeout(500);
45  
46          String body= "<flibs xmlns='http://www.flub.org/schemas/131'><flib uid='12321'><name>foo flib</name> </flib></flibs>";
47          //body= "XXX";  // => 501
48  
49          int len = body.getBytes("US-ASCII").length;
50          
51          String msg = "POST " + uri + " HTTP/1.1\r\n" + 
52          		"Content-Type: application/xml\r\n" + 
53          		"Host: 10.0.2.2:8080\r\n" + 
54          		"Content-Length: " + len + "\r\n" + 
55          		"Expect: 100-continue\r\n" + 
56          		"Connection: Keep-Alive\r\n" +
57          		"\r\n" + 
58          		body;
59          		
60           
61          
62          sock.getOutputStream().write(msg.getBytes("US-ASCII"));
63  
64          String response1 = readResponse(sock);  
65          int status1 = Integer.parseInt(response1.substring(9, 12));
66          assert 401 == status1;
67          
68          sock.getOutputStream().write(msg.getBytes("US-ASCII"));
69          
70          
71          String response2 = readResponse(sock);        
72          System.out.println(response2.substring(0, 100));
73    
74      
75          int status2 = Integer.parseInt(response2.substring(9, 12));
76          System.out.println(status2);
77          
78          assert 401 == status2;
79          
80  
81  
82      }
83  
84      private static String readResponse(Socket sock) throws IOException {
85          byte [] response = new byte [4000];
86          int n = 0;
87          for (int i=0; i< response.length && response[n] >= 0; i++){
88              try {
89                  response[n++] = (byte)sock.getInputStream().read();
90              } catch (SocketTimeoutException e) {
91                  break;
92              }
93          }
94          String sResult = new String(response);
95          return sResult;
96      }
97      
98      private static void initJetty() throws SAXException, IOException, MalformedURLException, Exception {
99  
100         Server jetty = new Server(8080);
101         
102 
103         // configure your web application
104         WebAppContext appContext = new WebAppContext();
105         appContext.setContextPath("/flub");
106         
107         appContext.addServlet(Jetty400Repro.class, "/servlet/");
108         
109         appContext.setResourceBase(".");
110         
111         
112         HandlerList handlers = new HandlerList();
113         handlers.setHandlers(new Handler[] { appContext, new DefaultHandler() });
114         jetty.setHandler(handlers);
115 
116         
117         jetty.start();
118 
119 
120     }
121 
122     @Override
123     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
124         req.getInputStream();
125         resp.sendError(401);
126     }
127 
128 }