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