hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r510645 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/protocol/HttpService.java test/java/org/apache/http/mockup/TestHttpClient.java test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Date Thu, 22 Feb 2007 20:30:15 GMT
Author: olegk
Date: Thu Feb 22 12:30:13 2007
New Revision: 510645

URL: http://svn.apache.org/viewvc?view=rev&rev=510645
Log:
* Refactored HttpService class
* Ported remaining test cases from HttpCore NIO

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java?view=diff&rev=510645&r1=510644&r2=510645
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
Thu Feb 22 12:30:13 2007
@@ -126,68 +126,76 @@
         
         context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
 
-        HttpRequest request = conn.receiveRequestHeader(this.params);
-        HttpVersion ver = request.getRequestLine().getHttpVersion();
-        if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
-            // Downgrade protocol version if greater than HTTP/1.1 
-            ver = HttpVersion.HTTP_1_1;
-        }
-
-        HttpResponse response;
+        HttpResponse response = null;
         
-        if (request instanceof HttpEntityEnclosingRequest) {
-            if (((HttpEntityEnclosingRequest) request).expectContinue()) {
-                response = this.responseFactory.newHttpResponse(ver, 
-                        HttpStatus.SC_CONTINUE, context);
-                response.getParams().setDefaults(this.params);
-                
-                if (this.expectationVerifier != null) {
-                    try {
-                        this.expectationVerifier.verify(request, response, context);
-                    } catch (HttpException ex) {
-                        response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0,

-                                HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
-                        response.getParams().setDefaults(this.params);
-                        handleException(ex, response);
+        try {
+
+            HttpRequest request = conn.receiveRequestHeader(this.params);
+            HttpVersion ver = request.getRequestLine().getHttpVersion();
+            if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
+                // Downgrade protocol version if greater than HTTP/1.1 
+                ver = HttpVersion.HTTP_1_1;
+            }
+
+            boolean receiveEntity = false;
+            boolean runService = true;
+
+            if (request instanceof HttpEntityEnclosingRequest) {
+                receiveEntity = true;
+                if (((HttpEntityEnclosingRequest) request).expectContinue()) {
+                    response = this.responseFactory.newHttpResponse(ver, 
+                            HttpStatus.SC_CONTINUE, context);
+                    response.getParams().setDefaults(this.params);
+                    
+                    if (this.expectationVerifier != null) {
+                        try {
+                            this.expectationVerifier.verify(request, response, context);
+                        } catch (HttpException ex) {
+                            response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0,

+                                    HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
+                            response.getParams().setDefaults(this.params);
+                            handleException(ex, response);
+                        }
                     }
-                }
-                if (response.getStatusLine().getStatusCode() < 200) {
-                    // Send 1xx response indicating the server expections
-                    // have been met
-                    conn.sendResponseHeader(response);
-                    conn.flush();
-                } else {
-                    // The request does not meet the server expections
-                    this.processor.process(response, context);
-                    conn.sendResponseHeader(response);
-                    conn.sendResponseEntity(response);
-                    conn.flush();
-                    if (!this.connStrategy.keepAlive(response, context)) {
-                        conn.close();
+                    if (response.getStatusLine().getStatusCode() < 200) {
+                        // Send 1xx response indicating the server expections
+                        // have been met
+                        conn.sendResponseHeader(response);
+                        conn.flush();
+                        response = null;
+                    } else {
+                        // The request does not meet the server expections
+                        runService = false;
+                        receiveEntity = false;
                     }
-                    return;
                 }
             }
-            conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
-        }
 
-        response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
-        response.getParams().setDefaults(this.params);
-        
-        context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
-        context.setAttribute(HttpExecutionContext.HTTP_RESPONSE, response);
+            if (receiveEntity) {
+                conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
+            }
 
-        try {
-            this.processor.process(request, context);
-            doService(request, response, context);
+            if (response == null) {
+                response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
+                response.getParams().setDefaults(this.params);
+            }
             
-            if (request instanceof HttpEntityEnclosingRequest) {
+            if (runService) {
+                context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+                context.setAttribute(HttpExecutionContext.HTTP_RESPONSE, response);
+
+                this.processor.process(request, context);
+                doService(request, response, context);
+            }
+
+            if (receiveEntity) {
                 // Make sure the request content is fully consumed
                 HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
                 if (entity != null) {
                     entity.consumeContent();
                 }
             }
+            
         } catch (HttpException ex) {
             response = this.responseFactory.newHttpResponse
                 (HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR,
@@ -195,10 +203,12 @@
             response.getParams().setDefaults(this.params);
             handleException(ex, response);
         }
+        
         this.processor.process(response, context);
         conn.sendResponseHeader(response);
         conn.sendResponseEntity(response);
         conn.flush();
+        
         if (!this.connStrategy.keepAlive(response, context)) {
             conn.close();
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java?view=diff&rev=510645&r1=510644&r2=510645
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
Thu Feb 22 12:30:13 2007
@@ -42,6 +42,7 @@
 import org.apache.http.HttpVersion;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.BasicHttpProcessor;
@@ -66,6 +67,8 @@
         super();
         this.params = new BasicHttpParams(null);
         this.params
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
             .setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
             .setParameter(HttpProtocolParams.USER_AGENT, "TEST-CLIENT/1.1");
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java?view=diff&rev=510645&r1=510644&r2=510645
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Thu Feb 22 12:30:13 2007
@@ -36,15 +36,24 @@
 import java.util.List;
 import java.util.Random;
 
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
 import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.DefaultHttpClientConnection;
 import org.apache.http.message.HttpGet;
+import org.apache.http.message.HttpPost;
 import org.apache.http.mockup.TestHttpClient;
 import org.apache.http.mockup.TestHttpServer;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.util.EncodingUtils;
 import org.apache.http.util.EntityUtils;
 
 import junit.framework.*;
@@ -85,7 +94,7 @@
      */
     public void testSimpleHttpGets() throws Exception {
         
-        final int reqNo = 20;
+        int reqNo = 20;
         
         Random rnd = new Random();
         
@@ -139,6 +148,414 @@
                 for (int i = 0; i < expected.length; i++) {
                     assertEquals(expected[i], received[i]);
                 }
+                if (!this.client.keepAlive(response)) {
+                    conn.close();
+                }
+            }
+        } finally {
+            conn.close();
+            this.server.shutdown();
+        }
+    }
+
+    /**
+     * This test case executes a series of simple POST requests with content length 
+     * delimited content. 
+     */
+    public void testSimpleHttpPostsWithContentLength() throws Exception {
+        
+        int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(5000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+
+        // 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(false);
+                    response.setEntity(outgoing);
+                } else {
+                    StringEntity outgoing = new StringEntity("No content"); 
+                    response.setEntity(outgoing);
+                }
+            }
+            
+        });
+        
+        this.server.start();
+        
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpHost host = new HttpHost("localhost", this.server.getPort());
+        
+        try {
+            for (int r = 0; r < reqNo; r++) {
+                if (!conn.isOpen()) {
+                    Socket socket = new Socket(host.getHostName(), host.getPort());
+                    conn.bind(socket, this.client.getParams());
+                }
+                
+                HttpPost post = new HttpPost("/");
+                byte[] data = (byte[]) testData.get(r);
+                ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                post.setEntity(outgoing);
+
+                HttpResponse response = this.client.execute(post, host, conn);
+                byte[] received = EntityUtils.toByteArray(response.getEntity());
+                byte[] expected = (byte[]) testData.get(r);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+                if (!this.client.keepAlive(response)) {
+                    conn.close();
+                }
+            }
+        } finally {
+            conn.close();
+            this.server.shutdown();
+        }
+    }
+
+    /**
+     * This test case executes a series of simple POST requests with chunk 
+     * coded content content. 
+     */
+    public void testSimpleHttpPostsChunked() throws Exception {
+        
+        int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        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);
+        }
+
+        // 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);
+                }
+            }
+            
+        });
+        
+        this.server.start();
+        
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpHost host = new HttpHost("localhost", this.server.getPort());
+        
+        try {
+            for (int r = 0; r < reqNo; r++) {
+                if (!conn.isOpen()) {
+                    Socket socket = new Socket(host.getHostName(), host.getPort());
+                    conn.bind(socket, this.client.getParams());
+                }
+                
+                HttpPost post = new HttpPost("/");
+                byte[] data = (byte[]) testData.get(r);
+                ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                outgoing.setChunked(true);
+                post.setEntity(outgoing);
+
+                HttpResponse response = this.client.execute(post, host, conn);
+                byte[] received = EntityUtils.toByteArray(response.getEntity());
+                byte[] expected = (byte[]) testData.get(r);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+                if (!this.client.keepAlive(response)) {
+                    conn.close();
+                }
+            }
+        } finally {
+            conn.close();
+            this.server.shutdown();
+        }
+    }
+
+    /**
+     * This test case executes a series of simple HTTP/1.0 POST requests. 
+     */
+    public void testSimpleHttpPostsHTTP10() throws Exception {
+        
+        int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(5000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+
+        // 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(false);
+                    response.setEntity(outgoing);
+                } else {
+                    StringEntity outgoing = new StringEntity("No content"); 
+                    response.setEntity(outgoing);
+                }
+            }
+            
+        });
+        
+        this.server.start();
+        
+        // Set protocol level to HTTP/1.0
+        this.client.getParams().setParameter(
+                HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
+        
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpHost host = new HttpHost("localhost", this.server.getPort());
+        
+        try {
+            for (int r = 0; r < reqNo; r++) {
+                if (!conn.isOpen()) {
+                    Socket socket = new Socket(host.getHostName(), host.getPort());
+                    conn.bind(socket, this.client.getParams());
+                }
+                
+                HttpPost post = new HttpPost("/");
+                byte[] data = (byte[]) testData.get(r);
+                ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                post.setEntity(outgoing);
+
+                HttpResponse response = this.client.execute(post, host, conn);
+                assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getHttpVersion());
+                byte[] received = EntityUtils.toByteArray(response.getEntity());
+                byte[] expected = (byte[]) testData.get(r);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+                if (!this.client.keepAlive(response)) {
+                    conn.close();
+                }
+            }
+        } finally {
+            conn.close();
+            this.server.shutdown();
+        }
+    }
+
+    /**
+     * This test case executes a series of simple POST requests using 
+     * the 'expect: continue' handshake. 
+     */
+    public void testHttpPostsWithExpectContinue() throws Exception {
+        
+        int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(5000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+
+        // 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);
+                }
+            }
+            
+        });
+        
+        this.server.start();
+        
+        // Activate 'expect: continue' handshake
+        this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
true);
+        
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpHost host = new HttpHost("localhost", this.server.getPort());
+        
+        try {
+            for (int r = 0; r < reqNo; r++) {
+                if (!conn.isOpen()) {
+                    Socket socket = new Socket(host.getHostName(), host.getPort());
+                    conn.bind(socket, this.client.getParams());
+                }
+                
+                HttpPost post = new HttpPost("/");
+                byte[] data = (byte[]) testData.get(r);
+                ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                outgoing.setChunked(true);
+                post.setEntity(outgoing);
+
+                HttpResponse response = this.client.execute(post, host, conn);
+                byte[] received = EntityUtils.toByteArray(response.getEntity());
+                byte[] expected = (byte[]) testData.get(r);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+                if (!this.client.keepAlive(response)) {
+                    conn.close();
+                }
+            }
+        } finally {
+            conn.close();
+            this.server.shutdown();
+        }
+    }
+    
+    /**
+     * This test case executes a series of simple POST requests that do not 
+     * meet the target server expectations. 
+     */
+    public void testHttpPostsWithExpectationVerification() throws Exception {
+        
+        int reqNo = 3;
+        
+        // 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 {
+                
+                StringEntity outgoing = new StringEntity("No content"); 
+                response.setEntity(outgoing);
+            }
+            
+        });
+        
+        this.server.setExpectationVerifier(new HttpExpectationVerifier() {
+
+            public void verify(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException {
+                Header someheader = request.getFirstHeader("Secret");
+                if (someheader != null) {
+                    int secretNumber;
+                    try {
+                        secretNumber = Integer.parseInt(someheader.getValue());
+                    } catch (NumberFormatException ex) {
+                        response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
+                        return;
+                    }
+                    if (secretNumber < 2) {
+                        response.setStatusCode(HttpStatus.SC_EXPECTATION_FAILED);
+                        ByteArrayEntity outgoing = new ByteArrayEntity(
+                                EncodingUtils.getAsciiBytes("Wrong secret number")); 
+                        response.setEntity(outgoing);
+                    }
+                }
+            }
+            
+        });
+        
+        this.server.start();
+        
+        // Activate 'expect: continue' handshake
+        this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
true);
+        
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        HttpHost host = new HttpHost("localhost", this.server.getPort());
+        
+        try {
+            for (int r = 0; r < reqNo; r++) {
+                if (!conn.isOpen()) {
+                    Socket socket = new Socket(host.getHostName(), host.getPort());
+                    conn.bind(socket, this.client.getParams());
+                }
+                
+                HttpPost post = new HttpPost("/");
+                post.addHeader("Secret", Integer.toString(r));
+                ByteArrayEntity outgoing = new ByteArrayEntity(
+                        EncodingUtils.getAsciiBytes("No content")); 
+                post.setEntity(outgoing);
+
+                HttpResponse response = this.client.execute(post, host, conn);
+
+                HttpEntity entity = response.getEntity();
+                assertNotNull(entity);
+                entity.consumeContent();
+                
+                if (r < 2) {
+                    assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getStatusLine().getStatusCode());
+                } else {
+                    assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+                }
+                
                 if (!this.client.keepAlive(response)) {
                     conn.close();
                 }



Mime
View raw message