View Javadoc

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