View Javadoc

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