hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1661657 - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/nio/protocol/ httpcore/src/main/java/org/apache/http/protocol/ httpcore/src/test/java/org/apache/http/protocol/
Date Mon, 23 Feb 2015 12:31:28 GMT
Author: olegk
Date: Mon Feb 23 12:31:27 2015
New Revision: 1661657

URL: http://svn.apache.org/r1661657
Log:
HttpService to handle response messages that are not intended to enclose a content entity

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java?rev=1661657&r1=1661656&r2=1661657&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
Mon Feb 23 12:31:27 2015
@@ -499,14 +499,6 @@ public class HttpAsyncService implements
         this.exceptionLogger.log(ex);
     }
 
-    private void closeConnection(final NHttpConnection conn) {
-        try {
-            conn.close();
-        } catch (final IOException ex) {
-            log(ex);
-        }
-    }
-
     private void shutdownConnection(final NHttpConnection conn) {
         try {
             conn.shutdown();

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java?rev=1661657&r1=1661656&r2=1661657&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
Mon Feb 23 12:31:27 2015
@@ -157,11 +157,12 @@ public class HttpService {
 
         context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
 
+        HttpRequest request = null;
         HttpResponse response = null;
 
         try {
 
-            final HttpRequest request = conn.receiveRequestHeader();
+            request = conn.receiveRequestHeader();
             final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
             final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
             if (expectContinue) {
@@ -212,7 +213,9 @@ public class HttpService {
 
         this.processor.process(response, context);
         conn.sendResponseHeader(response);
-        conn.sendResponseEntity(response);
+        if (canResponseHaveBody(request, response)) {
+            conn.sendResponseEntity(response);
+        }
         conn.flush();
 
         if (!this.connStrategy.keepAlive(response, context)) {
@@ -220,6 +223,17 @@ public class HttpService {
         }
     }
 
+    private boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response)
{
+        if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod()))
{
+            return false;
+        }
+        final int status = response.getStatusLine().getStatusCode();
+        return status >= HttpStatus.SC_OK
+                && status != HttpStatus.SC_NO_CONTENT
+                && status != HttpStatus.SC_NOT_MODIFIED
+                && status != HttpStatus.SC_RESET_CONTENT;
+    }
+
     /**
      * Handles the given exception and generates an HTTP response to be sent
      * back to the client to inform about the exceptional condition encountered

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java?rev=1661657&r1=1661656&r2=1661657&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
Mon Feb 23 12:31:27 2015
@@ -154,7 +154,7 @@ public class TestHttpService {
     }
 
     @Test
-    public void testExecutionEntityEclosingRequest() throws Exception {
+    public void testExecutionEntityEnclosingRequest() throws Exception {
         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
@@ -196,7 +196,7 @@ public class TestHttpService {
     }
 
     @Test
-    public void testExecutionEntityEclosingRequestWithExpectContinue() throws Exception {
+    public void testExecutionEntityEnclosingRequestWithExpectContinue() throws Exception
{
         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
@@ -242,7 +242,7 @@ public class TestHttpService {
     }
 
     @Test
-    public void testExecutionEntityEclosingRequestCustomExpectationVerifier() throws Exception
{
+    public void testExecutionEntityEnclosingRequestCustomExpectationVerifier() throws Exception
{
         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
@@ -509,5 +509,79 @@ public class TestHttpService {
         Mockito.verify(conn).flush();
         Mockito.verify(conn, Mockito.never()).close();
     }
+
+    @Test
+    public void testNoContentResponse() throws Exception {
+        final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
+        final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+
+        final HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver);
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        final HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT,
"No Content");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(handlerResolver.lookup(request, context)).thenReturn(requestHandler);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.TRUE);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getConnection());
+        Assert.assertSame(request, context.getRequest());
+
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(requestHandler).handle(request, response, context);
+
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<HttpResponse>any());
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn, Mockito.never()).close();
+    }
+
+    @Test
+    public void testResponseToHead() throws Exception {
+        final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
+        final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+
+        final HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver);
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        final HttpRequest request = new BasicHttpRequest("HEAD", "/");
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(handlerResolver.lookup(request, context)).thenReturn(requestHandler);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.TRUE);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getConnection());
+        Assert.assertSame(request, context.getRequest());
+
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(requestHandler).handle(request, response, context);
+
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<HttpResponse>any());
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn, Mockito.never()).close();
+    }
 
 }



Mime
View raw message