hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r509494 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol: BufferingHttpClientHandler.java ClientConnState.java ServerConnState.java
Date Tue, 20 Feb 2007 10:21:16 GMT
Author: olegk
Date: Tue Feb 20 02:21:15 2007
New Revision: 509494

URL: http://svn.apache.org/viewvc?view=rev&rev=509494
Log:
HTTPCORE-42: Some refactoring in the BufferingHttpClientHandler laying the groundwork for
an implementation of the 'expect-continue' handshake

Modified:
    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/ClientConnState.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ServerConnState.java

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=509494&r1=509493&r2=509494
==============================================================================
--- 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
Tue Feb 20 02:21:15 2007
@@ -174,11 +174,10 @@
         HttpContext context = conn.getContext();
 
         ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
-        ContentOutputBuffer buffer = connState.getOutbuffer();
         
         try {
             
-            submitRequest(conn, buffer);                
+            submitRequest(conn, connState);                
             
         } catch (IOException ex) {
             shutdownConnection(conn);
@@ -197,6 +196,8 @@
         HttpContext context = conn.getContext();
 
         ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
+        
+        connState.setInputState(ClientConnState.RESPONSE_BODY_STREAM);
         ContentInputBuffer buffer = connState.getInbuffer();
 
         try {
@@ -204,12 +205,12 @@
             buffer.consumeContent(decoder);
             if (decoder.isCompleted()) {
 
-                processResponse(conn, buffer);
-
+                connState.setInputState(ClientConnState.RESPONSE_BODY_DONE);
+                processResponse(conn, connState);
                 // Ready for another request
-                connState.reset();
-
-                conn.requestOutput();                
+                connState.resetInput();
+                conn.requestOutput();
+                
             }
             
         } catch (IOException ex) {
@@ -229,11 +230,18 @@
         HttpContext context = conn.getContext();
 
         ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
+        
+        connState.setInputState(ClientConnState.REQUEST_BODY_STREAM);
         ContentOutputBuffer buffer = connState.getOutbuffer();
 
         try {
             
             buffer.produceContent(encoder);
+            if (encoder.isCompleted()) {
+
+                connState.setInputState(ClientConnState.REQUEST_BODY_DONE);
+                
+            }
             
         } catch (IOException ex) {
             shutdownConnection(conn);
@@ -246,11 +254,12 @@
     public void responseReceived(final NHttpClientConnection conn) {
         HttpContext context = conn.getContext();
         HttpResponse response = conn.getHttpResponse();
-        HttpRequest request = (HttpRequest) context.getAttribute(
-                HttpExecutionContext.HTTP_REQUEST);
 
         ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
-        ContentInputBuffer buffer = connState.getInbuffer();
+        
+        connState.setResponse(response);
+        connState.setInputState(ClientConnState.RESPONSE_RECEIVED);
+        HttpRequest request = connState.getRequest();
         
         if (response.getStatusLine().getStatusCode() < HttpStatus.SC_OK) {
             // Just ignore 1xx responses;
@@ -260,11 +269,9 @@
         if (!canResponseHaveBody(request, response)) {
             try {
                 
-                processResponse(conn, buffer);
-                
+                processResponse(conn, connState);
                 // Ready for another request
-                connState.reset();
-
+                connState.resetOutput();
                 conn.requestOutput();                
                 
             } catch (IOException ex) {
@@ -303,7 +310,9 @@
     
     private void submitRequest(
             final NHttpClientConnection conn, 
-            final ContentOutputBuffer outbuffer) throws IOException, HttpException {
+            final ClientConnState connState) throws IOException, HttpException {
+        
+        connState.resetOutput();
         
         HttpContext context = conn.getContext();
         HttpRequest request = this.execHandler.submitRequest(context);
@@ -314,27 +323,26 @@
         request.getParams().setDefaults(this.params);
         
         context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
-
         this.httpProcessor.process(request, context);
-        
+        connState.setRequest(request);
         conn.submitRequest(request);
+        connState.setOutputState(ClientConnState.REQUEST_SENT);
         
         if (request instanceof HttpEntityEnclosingRequest) {
             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
             if (entity != null) {
-                OutputStream outstream = new ContentOutputStream(outbuffer);
+                OutputStream outstream = new ContentOutputStream(connState.getOutbuffer());
                 entity.writeTo(outstream);
                 outstream.flush();
                 outstream.close();
             }
         }
-        
     }
     
     protected boolean canResponseHaveBody(
             final HttpRequest request, final HttpResponse response) {
 
-        if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
+        if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod()))
{
             return false;
         }
         
@@ -347,7 +355,7 @@
     
     private void processResponse(
             final NHttpClientConnection conn, 
-            final ContentInputBuffer inbuffer) throws IOException, HttpException {
+            final ClientConnState connState) throws IOException, HttpException {
 
         HttpContext context = conn.getContext();
         HttpResponse response = conn.getHttpResponse();
@@ -355,7 +363,7 @@
         if (response.getEntity() != null) {
             response.setEntity(new BufferedContent(
                     response.getEntity(), 
-                    inbuffer));
+                    connState.getInbuffer()));
         }
         
         context.setAttribute(HttpExecutionContext.HTTP_RESPONSE, response);

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ClientConnState.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ClientConnState.java?view=diff&rev=509494&r1=509493&r2=509494
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ClientConnState.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ClientConnState.java
Tue Feb 20 02:21:15 2007
@@ -31,6 +31,8 @@
 
 package org.apache.http.nio.protocol;
 
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
 import org.apache.http.nio.util.ContentInputBuffer;
 import org.apache.http.nio.util.ContentOutputBuffer;
 
@@ -41,12 +43,27 @@
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
  */
-
-public class ClientConnState {
+class ClientConnState {
+    
+    public static final int SHUTDOWN                   = -1;
+    public static final int READY                      = 0;
+    public static final int REQUEST_SENT               = 1;
+    public static final int EXPECT_CONTINUE            = 2;
+    public static final int REQUEST_BODY_STREAM        = 4;
+    public static final int REQUEST_BODY_DONE          = 8;
+    public static final int RESPONSE_RECEIVED          = 16;
+    public static final int RESPONSE_BODY_STREAM       = 32;
+    public static final int RESPONSE_BODY_DONE         = 64;
     
     private final ContentInputBuffer inbuffer; 
     private final ContentOutputBuffer outbuffer;
 
+    private volatile int inputState;
+    private volatile int outputState;
+    
+    private volatile HttpRequest request;
+    private volatile HttpResponse response;
+
     public ClientConnState(
             final ContentInputBuffer inbuffer,
             final ContentOutputBuffer outbuffer) {
@@ -69,14 +86,55 @@
         return this.outbuffer;
     }
     
+    public int getInputState() {
+        return this.inputState;
+    }
+
+    public void setInputState(int inputState) {
+        this.inputState = inputState;
+    }
+
+    public int getOutputState() {
+        return this.outputState;
+    }
+
+    public void setOutputState(int outputState) {
+        this.outputState = outputState;
+    }
+
+    public HttpRequest getRequest() {
+        return this.request;
+    }
+
+    public void setRequest(final HttpRequest request) {
+        this.request = request;
+    }
+
+    public HttpResponse getResponse() {
+        return this.response;
+    }
+
+    public void setResponse(final HttpResponse response) {
+        this.response = response;
+    }
+
     public void shutdown() {
         this.inbuffer.shutdown();
         this.outbuffer.shutdown();
+        this.inputState = SHUTDOWN;
+        this.outputState = SHUTDOWN;
     }
 
-    public void reset() {
+    public void resetInput() {
         this.inbuffer.reset();
-        this.outbuffer.reset();
+        this.response = null;
+        this.inputState = READY;
     }
     
+    public void resetOutput() {
+        this.outbuffer.reset();
+        this.request = null;
+        this.outputState = READY;
+    }
+        
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ServerConnState.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ServerConnState.java?view=diff&rev=509494&r1=509493&r2=509494
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ServerConnState.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ServerConnState.java
Tue Feb 20 02:21:15 2007
@@ -43,7 +43,7 @@
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
  */
-public class ServerConnState {
+class ServerConnState {
    
     public static final int SHUTDOWN                   = -1;
     public static final int READY                      = 0;



Mime
View raw message