hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r510262 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/impl/nio/ main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/protocol/
Date Wed, 21 Feb 2007 22:08:35 GMT
Author: olegk
Date: Wed Feb 21 14:08:34 2007
New Revision: 510262

URL: http://svn.apache.org/viewvc?view=rev&rev=510262
Log:
HTTPCORE-42: 
* Fixed a couple of bug related to the 'expect: continue' support. The 'expect: continue'
handshake functional at this point
* Added test coverage

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java?view=diff&rev=510262&r1=510261&r2=510262
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
Wed Feb 21 14:08:34 2007
@@ -98,6 +98,7 @@
                         this.response.setEntity(entity);
                     } else {
                         // Discard the intermediate response
+                        this.responseParser.reset();
                         this.response = null;
                     }
                 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java?view=diff&rev=510262&r1=510261&r2=510262
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
Wed Feb 21 14:08:34 2007
@@ -229,6 +229,11 @@
 
         ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
         
+        if (connState.getOutputState() == ClientConnState.EXPECT_CONTINUE) {
+            conn.suspendOutput();
+            return;
+        }
+        
         connState.setInputState(ClientConnState.REQUEST_BODY_STREAM);
         ContentOutputBuffer buffer = connState.getOutbuffer();
 
@@ -369,6 +374,7 @@
         conn.setSocketTimeout(timeout);
 
         prepareRequestBody((HttpEntityEnclosingRequest) request, connState);
+        conn.requestOutput();
         connState.setOutputState(ClientConnState.REQUEST_SENT);
     }
     

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java?view=diff&rev=510262&r1=510261&r2=510262
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
Wed Feb 21 14:08:34 2007
@@ -194,7 +194,6 @@
                     } else {
                         // The request does not meet the server expections
                         sendResponse(conn, response);
-                        return;
                     }
                 }
                 // Request content is expected. 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java?view=diff&rev=510262&r1=510261&r2=510262
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
Wed Feb 21 14:08:34 2007
@@ -674,5 +674,151 @@
         
     }
 
+    /**
+     * This test case executes a series of simple (non-pipelined) POST requests 
+     * over multiple connections using the 'expect: continue' handshake. 
+     */
+    public void testHttpPostsWithExpectContinue() throws Exception {
+        
+        final int connNo = 3;
+        final int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        final List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(20000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+        
+        List[] responseData = new List[connNo];
+        for (int i = 0; i < responseData.length; i++) {
+            responseData[i] = new ArrayList();
+        }
+        
+        // Initialize the server-side request handler
+        this.server.registerHandler("*", new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException, IOException {
+                
+                if (request instanceof HttpEntityEnclosingRequest) {
+                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] data = EntityUtils.toByteArray(incoming);
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    outgoing.setChunked(true);
+                    response.setEntity(outgoing);
+                } else {
+                    StringEntity outgoing = new StringEntity("No content"); 
+                    response.setEntity(outgoing);
+                }
+            }
+            
+        });
+
+        // Activate 'expect: continue' handshake
+        this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
true);
+        // Initialize the client side request executor
+        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+            public void initalizeContext(final HttpContext context, final Object attachment)
{
+                context.setAttribute("LIST", (List) attachment);
+                context.setAttribute("STATUS", "ready");
+            }
+
+            public HttpRequest submitRequest(final HttpContext context) {
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                String status = (String) context.getAttribute("STATUS");
+                if (!status.equals("ready")) {
+                    return null;
+                }
+                int index = 0;
+                
+                Integer intobj = (Integer) context.getAttribute("INDEX");
+                if (intobj != null) {
+                    index = intobj.intValue();
+                }
+
+                HttpPost post = null;
+                if (index < reqNo) {
+                    post = new HttpPost("/?" + index);
+                    byte[] data = (byte[]) testData.get(index);
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    outgoing.setChunked(true);
+                    post.setEntity(outgoing);
+                    
+                    context.setAttribute("INDEX", new Integer(index + 1));
+                    context.setAttribute("STATUS", "busy");
+                } else {
+                    try {
+                        conn.close();
+                    } catch (IOException ex) {
+                        ex.printStackTrace();
+                    }
+                }
+                
+                return post;
+            }
+            
+            public void handleResponse(final HttpResponse response, final HttpContext context)
{
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                
+                List list = (List) context.getAttribute("LIST");
+                
+                try {
+                    HttpEntity entity = response.getEntity();
+                    byte[] data = EntityUtils.toByteArray(entity);
+                    list.add(data);
+                } catch (IOException ex) {
+                    fail(ex.getMessage());
+                }
+
+                context.setAttribute("STATUS", "ready");
+                conn.requestInput();
+            }
+            
+        });
+        
+        this.server.start();
+        this.client.start();
+        
+        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
+        for (int i = 0; i < responseData.length; i++) {
+            this.client.openConnection(
+                    new InetSocketAddress("localhost", serverAddress.getPort()), 
+                    responseData[i]);
+        }
+     
+        this.client.await(connNo, 1000);
+        assertEquals(connNo, this.client.getConnCount());
+        
+        this.server.await(connNo, 1000);
+        assertEquals(connNo, this.server.getConnCount());
+
+        for (int c = 0; c < responseData.length; c++) {
+            List receivedPackets = responseData[c];
+            List expectedPackets = testData;
+            assertEquals(receivedPackets.size(), expectedPackets.size());
+            for (int p = 0; p < testData.size(); p++) {
+                byte[] expected = (byte[]) testData.get(p);
+                byte[] received = (byte[]) receivedPackets.get(p);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+            }
+        }
+        
+    }
+
     
 }



Mime
View raw message