hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r368046 - in /jakarta/httpcomponents/trunk: coyote-httpconnector/src/java/org/apache/http/coyote/impl/ http-core/src/examples/org/apache/http/examples/ http-core/src/java/org/apache/http/ http-core/src/java/org/apache/http/executor/ http-co...
Date Wed, 11 Jan 2006 15:58:28 GMT
Author: olegk
Date: Wed Jan 11 07:58:05 2006
New Revision: 368046

URL: http://svn.apache.org/viewcvs?rev=368046&view=rev
Log:
HttpClientConnection and HttpServerConnection interface redesign suggested by Roland Weber
and Ortwin Glueck

Removed:
    jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/HttpRequestExecutorDemo.java
Modified:
    jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/CoyoteHttpConnection.java
    jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/HttpConnectionProcessor.java
    jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpGet.java
    jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpPost.java
    jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpServer.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpClientConnection.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpServerConnection.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/executor/HttpRequestExecutor.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/AbstractHttpConnection.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java

Modified: jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/CoyoteHttpConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/CoyoteHttpConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/CoyoteHttpConnection.java
(original)
+++ jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/CoyoteHttpConnection.java
Wed Jan 11 07:58:05 2006
@@ -32,8 +32,6 @@
 import java.io.IOException;
 import java.net.Socket;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.http.impl.DefaultHttpServerConnection;
 import org.apache.http.params.HttpParams;
 
@@ -46,9 +44,6 @@
  */
 public class CoyoteHttpConnection extends DefaultHttpServerConnection {
 
-    private static Log LOG = LogFactory.getLog(CoyoteHttpConnection.class);
-    private static Log WIRE_LOG = LogFactory.getLog("coyote.wire");
-    
     private String name = null;
     
     public CoyoteHttpConnection() {
@@ -61,46 +56,6 @@
 		super.bind(socket, params);
         this.name = socket.getRemoteSocketAddress().toString(); 
 	}
-
-	@Override
-    protected boolean isErrorEnabled() {
-        return LOG.isErrorEnabled();
-    }
-
-    @Override
-    protected void error(final String s, final Throwable t) {
-        LOG.error(s, t);
-    }
-
-    @Override
-    protected void error(final String s) {
-        LOG.error(s);
-    }
-
-    @Override
-    protected boolean isWarnEnabled() {
-        return LOG.isWarnEnabled();
-    }
-
-    @Override
-    protected void warn(final String s, final Throwable t) {
-        LOG.warn(s, t);
-    }
-
-    @Override
-    protected void warn(final String s) {
-        LOG.warn(s);
-    }
-
-    @Override
-    protected boolean isWirelogEnabled() {
-        return WIRE_LOG.isDebugEnabled();
-    }
-
-    @Override
-    protected void wirelog(String s) {
-        WIRE_LOG.debug(s);
-    }
 
 	@Override
 	public String toString() {

Modified: jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/HttpConnectionProcessor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/HttpConnectionProcessor.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/HttpConnectionProcessor.java
(original)
+++ jakarta/httpcomponents/trunk/coyote-httpconnector/src/java/org/apache/http/coyote/impl/HttpConnectionProcessor.java
Wed Jan 11 07:58:05 2006
@@ -53,6 +53,7 @@
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
+import org.apache.http.HttpMutableEntityEnclosingRequest;
 import org.apache.http.HttpMutableRequest;
 import org.apache.http.HttpMutableResponse;
 import org.apache.http.HttpRequest;
@@ -105,8 +106,8 @@
     private final Request coyotereq;
     private final Response coyoteres;
     
-    private HttpRequest httpreq = null;
-    private HttpResponse httpres = null;
+    private HttpMutableRequest httpreq = null;
+    private HttpMutableResponse httpres = null;
     
     private HttpEntity in = null;
     private CoyoteHttpEntity out = null;
@@ -204,7 +205,7 @@
         LOG.debug("Waiting for incoming HTTP request");
         this.coyotereq.getRequestProcessor().setStage(Constants.STAGE_NEW);
         try {
-            this.httpreq = this.conn.receiveRequest(this.params);
+            this.httpreq = this.conn.receiveRequestHeader(this.params);
         } catch (SocketException ex) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug(ex.getMessage());
@@ -213,11 +214,14 @@
         }
         this.coyotereq.setStartTime(System.currentTimeMillis());
         LOG.debug("HTTP request header received");
+        
+        if (this.httpreq instanceof HttpMutableEntityEnclosingRequest) {
+            this.conn.receiveRequestEntity((HttpMutableEntityEnclosingRequest)this.httpreq);
+        }
+        
         this.coyotereq.getRequestProcessor().setStage(Constants.STAGE_PREPARE);
 
-        if (this.httpreq instanceof HttpMutableRequest) {
-        	preprocessRequest((HttpMutableRequest)this.httpreq);
-        }
+        preprocessRequest(this.httpreq);
         // Add request to the HTTP context
         getContext().setAttribute(HttpProcessingContext.HTTP_REQUEST, this.httpreq);
     }
@@ -234,7 +238,7 @@
     	return response;
     }
     
-    private HttpResponse processHttpException(final HttpException ex) {
+    private HttpMutableResponse processHttpException(final HttpException ex) {
     	LOG.debug("Processing HTTP exception");
         if (ex instanceof MethodNotSupportedException) {
             return createResponse(HttpStatus.SC_NOT_IMPLEMENTED);
@@ -250,7 +254,7 @@
     		if (((HttpEntityEnclosingRequest)this.httpreq).expectContinue()) {
     	        HttpResponse ack = createResponse(HttpStatus.SC_CONTINUE);
     	        try {
-    	            this.conn.sendResponse(ack);
+    	            this.conn.sendResponseHeader(ack);
     	            LOG.debug("100-Continue sent");
     	        } catch (HttpException ex) {
     	            LOG.error("Malformed HTTP response", ex);
@@ -265,7 +269,8 @@
             if (this.httpres instanceof HttpMutableResponse) {
             	postprocessResponse((HttpMutableResponse)this.httpres);
             }
-    		this.conn.sendResponse(this.httpres);
+    		this.conn.sendResponseHeader(this.httpres);
+            this.conn.sendResponseEntity(this.httpres);
             LOG.debug("HTTP response header sent");
     	} catch (HttpException ex) {
     		LOG.error("Malformed HTTP response", ex);

Modified: jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpGet.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpGet.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpGet.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpGet.java
Wed Jan 11 07:58:05 2006
@@ -29,17 +29,26 @@
 
 package org.apache.http.examples;
 
-import org.apache.http.Header;
 import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
 import org.apache.http.Scheme;
+import org.apache.http.executor.HttpRequestExecutor;
+import org.apache.http.impl.ConnectionReuseStrategy;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.http.impl.DefaultHttpClientConnection;
 import org.apache.http.impl.DefaultHttpParams;
 import org.apache.http.impl.io.PlainSocketFactory;
 import org.apache.http.io.SocketFactory;
 import org.apache.http.message.HttpGet;
 import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
 import org.apache.http.util.EntityUtils;
 
 /**
@@ -55,8 +64,23 @@
         
         SocketFactory socketfactory = PlainSocketFactory.getSocketFactory();
         Scheme.registerScheme("http", new Scheme("http", socketfactory, 80));
+
+        HttpParams params = new DefaultHttpParams(null);
+        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
+        HttpProtocolParams.setContentCharset(params, "UTF-8");
+        HttpProtocolParams.setUserAgent(params, "Jakarta-HttpComponents/1.1");
+        HttpProtocolParams.setUseExpectContinue(params, true);
+        
+        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
+        httpexecutor.setParams(params);
+        // Required request interceptors
+        httpexecutor.addInterceptor(new RequestContent());
+        httpexecutor.addInterceptor(new RequestTargetHost());
+        // Recommended request interceptors
+        httpexecutor.addInterceptor(new RequestConnControl());
+        httpexecutor.addInterceptor(new RequestUserAgent());
+        httpexecutor.addInterceptor(new RequestExpectContinue());
         
-        HttpParams connparams = new DefaultHttpParams(null);
         HttpHost host = new HttpHost("localhost", 8080);
         HttpClientConnection conn = new DefaultHttpClientConnection(host);
         try {
@@ -66,28 +90,24 @@
                     "/servlets-examples/servlet/RequestInfoExample", 
                     "/somewhere%20in%20pampa"};
             
+            ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
+            
             for (int i = 0; i < targets.length; i++) {
                 HttpGet request = new HttpGet(targets[i]);
-                request.setHeader(new Header("Host", host.toHostString()));
-                request.setHeader(new Header("User-Agent", "Elemental HTTP client"));
-                request.setHeader(new Header("Connection", "Keep-Alive"));
-                if (!conn.isOpen()) {
-                    System.out.println("Open new connection to: " + host);
-                    conn.open(connparams);
-                } else {
-                    System.out.println("Connection kept alive. Reusing...");
-                }
                 System.out.println(">> Request URI: " + request.getRequestLine().getUri());
-                conn.sendRequest(request);
-                HttpResponse response = conn.receiveResponse(request); 
+                HttpResponse response = httpexecutor.execute(request, conn);
                 System.out.println("<< Response: " + response.getStatusLine());
                 System.out.println(EntityUtils.toString(response.getEntity()));
                 System.out.println("==============");
+                if (!connStrategy.keepAlive(response)) {
+                    conn.close();
+                } else {
+                    System.out.println("Connection kept alive...");
+                }
             }
         } finally {
             conn.close();
         }
-        
     }
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpPost.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpPost.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
    (empty)

Modified: jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpServer.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpServer.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/examples/org/apache/http/examples/ElementalHttpServer.java
Wed Jan 11 07:58:05 2006
@@ -37,8 +37,10 @@
 import java.net.URLDecoder;
 
 import org.apache.http.ConnectionClosedException;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
+import org.apache.http.HttpMutableEntityEnclosingRequest;
 import org.apache.http.HttpMutableRequest;
 import org.apache.http.HttpMutableResponse;
 import org.apache.http.HttpRequest;
@@ -94,7 +96,7 @@
     static class HttpRequestProcessor extends AbstractHttpProcessor {
         
         private final HttpServerConnection conn;
-        private final ConnectionReuseStrategy connreuse;
+        private final ConnectionReuseStrategy connStrategy;
         
         private HttpParams params = null;
 
@@ -104,7 +106,7 @@
                 throw new IllegalArgumentException("HTTP server connection may not be null");
             }
             this.conn = conn;
-            this.connreuse = new DefaultConnectionReuseStrategy();
+            this.connStrategy = new DefaultConnectionReuseStrategy();
         }
 
         public HttpParams getParams() {
@@ -134,35 +136,45 @@
             BasicHttpResponse response = new BasicHttpResponse();
             response.getParams().setDefaults(this.params);
             try {
-                HttpRequest request = this.conn.receiveRequest(this.params);
-                
-                if (request instanceof HttpMutableRequest) {
-                    preprocessRequest((HttpMutableRequest)request);
-                }
-
-                HttpVersion ver = request.getRequestLine().getHttpVersion();
-                if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
-                    ver = HttpVersion.HTTP_1_1;
-                }
-                HttpProtocolParams.setVersion(response.getParams(), ver);
-                
+                HttpMutableRequest request = this.conn.receiveRequestHeader(this.params);
                 if (request instanceof HttpEntityEnclosingRequest) {
-                    if (((HttpEntityEnclosingRequest) request).expectContinue()) {
+                    if (((HttpMutableEntityEnclosingRequest) request).expectContinue()) {
 
                         System.out.println("Expected 100 (Continue)");
                         
                         BasicHttpResponse ack = new BasicHttpResponse();
                         ack.getParams().setDefaults(this.params);
                         ack.setStatusCode(HttpStatus.SC_CONTINUE);
-                        this.conn.sendResponse(ack);
+                        this.conn.sendResponseHeader(ack);
+                        this.conn.flush();
                     }
+                    this.conn.receiveRequestEntity((HttpMutableEntityEnclosingRequest) request);
                 }
+                preprocessRequest(request);
                 System.out.println("Request received");
+
+                HttpVersion ver = request.getRequestLine().getHttpVersion();
+                if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
+                    ver = HttpVersion.HTTP_1_1;
+                }
+                HttpProtocolParams.setVersion(response.getParams(), ver);
+                
                 localContext.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
                 localContext.setAttribute(HttpExecutionContext.HTTP_RESPONSE, response);
                 handler.service(request, response);
+                
+                if (request instanceof HttpEntityEnclosingRequest) {
+                    // Make sure the request content is fully consumed
+                    HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
+                    if (entity != null && entity.getContent() != null) {
+                        entity.getContent().close();
+                    }
+                }
+                
+                postprocessResponse(response);
             } catch (ConnectionClosedException ex) {
                 System.out.println("Client closed connection");
+                closeConnection();
                 return;
             } catch (HttpException ex) {
                 handleException(ex, response);
@@ -172,10 +184,9 @@
                 return;
             }
             try {
-                if (response instanceof HttpMutableResponse) {
-                    postprocessResponse((HttpMutableResponse)response);
-                }
-                this.conn.sendResponse(response);
+                this.conn.sendResponseHeader(response);
+                this.conn.sendResponseEntity(response);
+                this.conn.flush();
                 System.out.println("Response sent");
             } catch (HttpException ex) {
                 System.err.println("Malformed response: " + ex.getMessage());
@@ -186,7 +197,7 @@
                 closeConnection();
                 return;
             }
-            if (!this.connreuse.keepAlive(response)) {
+            if (!this.connStrategy.keepAlive(response)) {
                 closeConnection();
             } else {
                 System.out.println("Connection kept alive");
@@ -249,7 +260,7 @@
                 .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
                 .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
                 .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
-                .setParameter(HttpProtocolParams.ORIGIN_SERVER, "Elemental Server/1.1")
+                .setParameter(HttpProtocolParams.ORIGIN_SERVER, "Jakarta-HttpComponents/1.1")
                 .setParameter("server.docroot", docroot);
         }
         

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpClientConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpClientConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpClientConnection.java
Wed Jan 11 07:58:05 2006
@@ -55,14 +55,21 @@
     
     void open(HttpParams params) throws IOException;
     
-    void sendRequest(HttpRequest request) throws HttpException, IOException;
+    boolean isResponseAvailable(int timeout) 
+        throws IOException; 
+    
+    void sendRequestHeader(HttpRequest request) 
+        throws HttpException, IOException;
 
-    void sendRequestHeader(HttpEntityEnclosingRequest request) throws HttpException, IOException;
+    void sendRequestEntity(HttpEntityEnclosingRequest request) 
+        throws HttpException, IOException;
 
-    void sendRequestEntity(HttpEntityEnclosingRequest request) throws HttpException, IOException;
+    HttpMutableResponse receiveResponseHeader(HttpParams params) 
+        throws HttpException, IOException;
 
-    HttpResponse receiveResponse(HttpRequest request) throws HttpException, IOException;
-
-    HttpResponse receiveResponse(HttpRequest request, int timeout) throws HttpException,
IOException;
+    void receiveResponseEntity(HttpMutableResponse response) 
+        throws HttpException, IOException;
+    
+    void flush() throws IOException;
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpServerConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpServerConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpServerConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpServerConnection.java
Wed Jan 11 07:58:05 2006
@@ -45,10 +45,22 @@
  */
 public interface HttpServerConnection extends HttpConnection {
 
-    void bind(Socket socket, HttpParams params) throws IOException;
+    void bind(Socket socket, HttpParams params) 
+        throws IOException;
     
-    HttpRequest receiveRequest(HttpParams params) throws HttpException, IOException;
+    HttpMutableRequest receiveRequestHeader(HttpParams params) 
+        throws HttpException, IOException;
 
-    void sendResponse(HttpResponse response) throws HttpException, IOException;
+    void receiveRequestEntity(HttpMutableEntityEnclosingRequest request) 
+        throws HttpException, IOException;
+
+    void sendResponseHeader(HttpResponse response) 
+        throws HttpException, IOException;
+    
+    void sendResponseEntity(HttpResponse response) 
+        throws HttpException, IOException;
+    
+    void flush()
+        throws IOException;
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/executor/HttpRequestExecutor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/executor/HttpRequestExecutor.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/executor/HttpRequestExecutor.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/executor/HttpRequestExecutor.java
Wed Jan 11 07:58:05 2006
@@ -32,7 +32,6 @@
 import java.io.IOException;
 import java.net.ProtocolException;
 
-import org.apache.http.Header;
 import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
@@ -45,7 +44,6 @@
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.AbstractHttpProcessor;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 
 /**
@@ -80,6 +78,67 @@
         this.params = params;
     }
     
+    private boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response)
{
+        if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
+            return false;
+        }
+        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; 
+    }
+
+    private HttpMutableResponse doExecute(
+            final HttpRequest request, final HttpClientConnection conn)
+                throws IOException, HttpException {
+        HttpMutableResponse response = null;
+        getContext().setAttribute(HttpExecutionContext.HTTP_REQ_SENT, 
+                new Boolean(false));
+        // Send request header
+        conn.sendRequestHeader(request);
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpVersion ver = request.getRequestLine().getHttpVersion();
+            if (ver.greaterEquals(HttpVersion.HTTP_1_1) 
+                    && ((HttpEntityEnclosingRequest)request).expectContinue()) {
+                // Flush headers
+                conn.flush();
+                if (conn.isResponseAvailable(WAIT_FOR_CONTINUE_MS)) {
+                    response = conn.receiveResponseHeader(this.params);
+                    if (canResponseHaveBody(request, response)) {
+                        conn.receiveResponseEntity(response);
+                    }
+                    int status = response.getStatusLine().getStatusCode();
+                    if (status < 200) {
+                        if (status != HttpStatus.SC_CONTINUE) {
+                            throw new ProtocolException("Unexpected response: " + 
+                                    response.getStatusLine());
+                        }
+                    } else {
+                        return response;
+                    }                    
+                }
+            }
+            conn.sendRequestEntity((HttpEntityEnclosingRequest) request);
+        }
+        conn.flush();
+        
+        getContext().setAttribute(HttpExecutionContext.HTTP_REQ_SENT, 
+                new Boolean(true)); 
+        for (;;) {
+            // Loop until non 1xx resposne is received
+            response = conn.receiveResponseHeader(this.params);
+            if (canResponseHaveBody(request, response)) {
+                conn.receiveResponseEntity(response);
+            }
+            int statuscode = response.getStatusLine().getStatusCode();
+            if (statuscode >= HttpStatus.SC_OK) {
+                break;
+            }
+        }
+        return response;
+    }
+    
     public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn)

             throws IOException, HttpException {
         
@@ -102,7 +161,7 @@
             preprocessRequest((HttpMutableRequest)request);
         }
         
-        HttpResponse response = null;
+        HttpMutableResponse response = null;
         // loop until the method is successfully processed, the retryHandler 
         // returns false or a non-recoverable exception is thrown
         for (int execCount = 0; ; execCount++) {
@@ -116,46 +175,8 @@
                     conn.open(this.params);
                     // TODO: Implement secure tunnelling
                 }
-                localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT, 
-                        new Boolean(false)); 
-                HttpVersion ver = request.getRequestLine().getHttpVersion();
-                Header expect = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
-                
-                if (expect != null 
-                        && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue())
-                        && ver.greaterEquals(HttpVersion.HTTP_1_1) 
-                        && request instanceof HttpEntityEnclosingRequest) {
-                    // Do 'expect: continue' handshake
-                    conn.sendRequestHeader((HttpEntityEnclosingRequest)request);
-                    response = conn.receiveResponse(request, WAIT_FOR_CONTINUE_MS);
-                    if (response == null) {
-                        // No response. The 'expect: continue' is likely not supported
-                        break;
-                    }
-                    int status = response.getStatusLine().getStatusCode();
-                    if (status < 200) {
-                        if (status != HttpStatus.SC_CONTINUE) {
-                            throw new ProtocolException("Unexpected response: " + 
-                                    response.getStatusLine());
-                        }
-                    } else {
-                        break;
-                    }                    
-                    conn.sendRequestEntity((HttpEntityEnclosingRequest)request);
-                } else {
-                    // Just fire and forget
-                    conn.sendRequest(request);
-                }
-                localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT, 
-                        new Boolean(true)); 
-                for (;;) {
-                    // Loop until non 1xx resposne is received
-                    response = conn.receiveResponse(request);
-                    int statuscode = response.getStatusLine().getStatusCode();
-                    if (statuscode >= HttpStatus.SC_OK) {
-                        break;
-                    }
-                }
+                response = doExecute(request, conn);
+                // exit retry loop
                 break;
             } catch (IOException ex) {
                 conn.close();
@@ -173,13 +194,7 @@
                 throw ex;
             }
         }
-        
-        // Link own parameters as defaults 
-        response.getParams().setDefaults(this.params);
-        
-        if (response instanceof HttpMutableResponse) {
-            postprocessResponse((HttpMutableResponse)response);
-        }
+        postprocessResponse(response);
         return response;
     }
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/AbstractHttpConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/AbstractHttpConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/AbstractHttpConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/AbstractHttpConnection.java
Wed Jan 11 07:58:05 2006
@@ -168,35 +168,5 @@
             return true;
         }
     }
-    
-    // Poor man's logging
-    // These methods are to be overridden at a higher level
-    
-    protected boolean isWirelogEnabled() {
-        return false;
-    }
-    
-    protected void wirelog(final String s) {
-    }
-    
-    protected boolean isErrorEnabled() {
-        return false;
-    }
-    
-    protected void error(final String s) {
-    }
-    
-    protected void error(final String s, final Throwable t) {
-    }
-
-    protected boolean isWarnEnabled() {
-        return false;
-    }
-    
-    protected void warn(final String s) {
-    }
-    
-    protected void warn(final String s, final Throwable t) {
-    }
-    
+        
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
Wed Jan 11 07:58:05 2006
@@ -41,9 +41,7 @@
 import org.apache.http.HttpHost;
 import org.apache.http.HttpMutableResponse;
 import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
 import org.apache.http.HttpResponseFactory;
-import org.apache.http.HttpStatus;
 import org.apache.http.NoHttpResponseException;
 import org.apache.http.ProtocolException;
 import org.apache.http.RequestLine;
@@ -86,7 +84,7 @@
         super();
         this.targethost = targethost;
         this.localAddress = localAddress;
-        this.buffer = new CharArrayBuffer(64);
+        this.buffer = new CharArrayBuffer(128);
         this.responsefactory = new DefaultHttpResponseFactory();
         this.entitygen = new DefaultEntityGenerator();
         this.entitywriter = new DefaultClientEntityWriter();
@@ -158,59 +156,45 @@
     	this.localAddress = localAddress;
     }
     
-    public void close() throws IOException {
-        super.close();
+    public boolean isResponseAvailable(int timeout) throws IOException {
+        assertOpen();
+        return this.datareceiver.isDataAvailable(timeout);
     }
 
-    public void sendRequest(final HttpRequest request) 
+    public void sendRequestHeader(final HttpRequest request) 
             throws HttpException, IOException {
         if (request == null) {
             throw new IllegalArgumentException("HTTP request may not be null");
         }
         assertOpen();
-        // reset the data transmitter
-        this.datatransmitter.reset(request.getParams());
-        
         sendRequestLine(request);
         sendRequestHeaders(request);
-        if (request instanceof HttpEntityEnclosingRequest) {
-            sendRequestBody((HttpEntityEnclosingRequest)request);
-        }
-        this.datatransmitter.flush();
     }
-    
-    public void sendRequestHeader(final HttpEntityEnclosingRequest request) 
+
+    public void sendRequestEntity(final HttpEntityEnclosingRequest request) 
             throws HttpException, IOException {
         if (request == null) {
             throw new IllegalArgumentException("HTTP request may not be null");
         }
         assertOpen();
-        // reset the data transmitter
-        this.datatransmitter.reset(request.getParams());
-        
-        sendRequestLine(request);
-        sendRequestHeaders(request);
-        this.datatransmitter.flush();
+        if (request.getEntity() == null) {
+            return;
+        }
+        this.entitywriter.write(
+                request.getEntity(),
+                request.getRequestLine().getHttpVersion(),
+                this.datatransmitter);
     }
 
-    public void sendRequestEntity(final HttpEntityEnclosingRequest request) 
-            throws HttpException, IOException {
-        if (request == null) {
-            throw new IllegalArgumentException("HTTP request may not be null");
-        }
-        sendRequestBody(request);
+    public void flush() throws IOException {
         this.datatransmitter.flush();
     }
-
     
     protected void sendRequestLine(final HttpRequest request) 
             throws HttpException, IOException {
         this.buffer.clear();
         RequestLine.format(this.buffer, request.getRequestLine());
         this.datatransmitter.writeLine(this.buffer);
-        if (isWirelogEnabled()) {
-            wirelog(">> " + this.buffer.toString() + "[\\r][\\n]");
-        }
     }
 
     protected void sendRequestHeaders(final HttpRequest request) 
@@ -220,62 +204,30 @@
             this.buffer.clear();
             Header.format(this.buffer, headers[i]);
             this.datatransmitter.writeLine(this.buffer);
-            if (isWirelogEnabled()) {
-                wirelog(">> " + this.buffer.toString() + "[\\r][\\n]");
-            }
         }
         this.buffer.clear();
         this.datatransmitter.writeLine(this.buffer);
-        if (isWirelogEnabled()) {
-            wirelog(">> [\\r][\\n]");
-        }
     }
 
-    protected void sendRequestBody(final HttpEntityEnclosingRequest request) 
+    public HttpMutableResponse receiveResponseHeader(final HttpParams params) 
             throws HttpException, IOException {
-        if (request.getEntity() == null) {
-            return;
-        }
-        this.entitywriter.write(
-                request.getEntity(),
-                request.getRequestLine().getHttpVersion(),
-                this.datatransmitter);
-    }
-    
-    public HttpResponse receiveResponse(final HttpRequest request) 
-            throws HttpException, IOException {
-        if (request == null) {
-            throw new IllegalArgumentException("HTTP request may not be null");
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
         }
         assertOpen();
-        // reset the data receiver
-        this.datareceiver.reset(request.getParams());
-        return readResponse(request);
+        HttpMutableResponse response = readResponseStatusLine(params);
+        readResponseHeaders(response);
+        return response;
     }
 
-    public HttpResponse receiveResponse(final HttpRequest request, int timeout) 
+    public void receiveResponseEntity(final HttpMutableResponse response)
             throws HttpException, IOException {
-        if (request == null) {
-            throw new IllegalArgumentException("HTTP request may not be null");
+        if (response == null) {
+            throw new IllegalArgumentException("HTTP response may not be null");
         }
         assertOpen();
-        // reset the data receiver
-        this.datareceiver.reset(request.getParams());
-        if (this.datareceiver.isDataAvailable(timeout)) {
-            return readResponse(request);
-        } else {
-            return null;
-        }
-    }
-
-    protected HttpResponse readResponse(final HttpRequest request)
-            throws HttpException, IOException {
-        HttpMutableResponse response = readResponseStatusLine(request.getParams());
-        readResponseHeaders(response);
-        if (canResponseHaveBody(request, response)) {
-            readResponseBody(response);
-        }
-        return response;
+        HttpEntity entity = this.entitygen.generate(this.datareceiver, response);
+        response.setEntity(entity);
     }
     
     /**
@@ -323,15 +275,9 @@
                         " failed to respond with a valid HTTP response");
             }
             count++;
-            if (isWirelogEnabled()) {
-                wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
-            }
         } while(true);
         //create the status line from the status string
         StatusLine statusline = StatusLine.parse(this.buffer, 0, this.buffer.length());
-        if (isWirelogEnabled()) {
-            wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
-        }
         HttpMutableResponse response = this.responsefactory.newHttpResponse(statusline);
         response.getParams().setDefaults(params);
         return response;
@@ -342,28 +288,7 @@
         Header[] headers = Header.parseAll(this.datareceiver);
         for (int i = 0; i < headers.length; i++) {
             response.addHeader(headers[i]);
-            if (isWirelogEnabled()) {
-                wirelog("<< " + headers[i].toString() + "[\\r][\\n]");
-            }
         }
-        wirelog("<< [\\r][\\n]");
-    }
-    
-    protected boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response)
{
-        if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
-            return false;
-        }
-        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; 
-    }
-        
-    protected void readResponseBody(
-            final HttpMutableResponse response) throws HttpException, IOException {
-        HttpEntity entity = this.entitygen.generate(this.datareceiver, response);
-        response.setEntity(entity);
     }
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java?rev=368046&r1=368045&r2=368046&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
Wed Jan 11 07:58:05 2006
@@ -38,7 +38,6 @@
 import org.apache.http.HttpException;
 import org.apache.http.HttpMutableEntityEnclosingRequest;
 import org.apache.http.HttpMutableRequest;
-import org.apache.http.HttpRequest;
 import org.apache.http.HttpRequestFactory;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpServerConnection;
@@ -105,24 +104,27 @@
         super.bind(socket, params);
     }
 
-    public HttpRequest receiveRequest(final HttpParams params) 
+    public HttpMutableRequest receiveRequestHeader(final HttpParams params) 
             throws HttpException, IOException {
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");
         }
         assertOpen();
-        // reset the data transmitter
-        this.datareceiver.reset(params);
-
         HttpMutableRequest request = receiveRequestLine(params);
         receiveRequestHeaders(request);
-
-        if (request instanceof HttpMutableEntityEnclosingRequest) {
-            receiveRequestBody((HttpMutableEntityEnclosingRequest) request);
-        }
         return request;
     }
     
+    public void receiveRequestEntity(final HttpMutableEntityEnclosingRequest request) 
+            throws HttpException, IOException {
+        if (request == null) {
+            throw new IllegalArgumentException("HTTP request may not be null");
+        }
+        assertOpen();
+        HttpEntity entity = this.entitygen.generate(this.datareceiver, request);
+        request.setEntity(entity);
+    }
+
     protected HttpMutableRequest receiveRequestLine(final HttpParams params)
             throws HttpException, IOException {
         this.buffer.clear();
@@ -131,9 +133,6 @@
             throw new ConnectionClosedException("Client closed connection"); 
         }
         RequestLine requestline = RequestLine.parse(this.buffer, 0, this.buffer.length());
-        if (isWirelogEnabled()) {
-            wirelog(">> " + this.buffer.toString() + "[\\r][\\n]");
-        }
         HttpMutableRequest request = this.requestfactory.newHttpRequest(requestline);
         request.getParams().setDefaults(params);
         return request;
@@ -144,32 +143,33 @@
         Header[] headers = Header.parseAll(this.datareceiver);
         for (int i = 0; i < headers.length; i++) {
             request.addHeader(headers[i]);
-            if (isWirelogEnabled()) {
-                wirelog(">> " + headers[i].toString() + "[\\r][\\n]");
-            }
         }
-        wirelog(">> [\\r][\\n]");
     }
 
-    protected void receiveRequestBody(final HttpMutableEntityEnclosingRequest request)
-            throws HttpException, IOException {
-        HttpEntity entity = this.entitygen.generate(this.datareceiver, request);
-        request.setEntity(entity);
+    public void flush() throws IOException {
+        assertOpen();
+        this.datatransmitter.flush();
     }
     
-	public void sendResponse(final HttpResponse response) 
+	public void sendResponseHeader(final HttpResponse response) 
             throws HttpException, IOException {
         if (response == null) {
             throw new IllegalArgumentException("HTTP response may not be null");
         }
         assertOpen();
-
-        // reset the data transmitter
-        this.datatransmitter.reset(response.getParams());
         sendResponseStatusLine(response);
         sendResponseHeaders(response);
-        sendResponseBody(response);
-        this.datatransmitter.flush();
+    }
+
+    public void sendResponseEntity(final HttpResponse response) 
+            throws HttpException, IOException {
+        if (response.getEntity() == null) {
+            return;
+        }
+        this.entitywriter.write(
+                response.getEntity(),
+                response.getStatusLine().getHttpVersion(),
+                this.datatransmitter);
     }
     
     protected void sendResponseStatusLine(final HttpResponse response) 
@@ -177,9 +177,6 @@
         this.buffer.clear();
         StatusLine.format(this.buffer, response.getStatusLine());
         this.datatransmitter.writeLine(this.buffer);
-        if (isWirelogEnabled()) {
-            wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
-        }
     }
 
     protected void sendResponseHeaders(final HttpResponse response) 
@@ -189,26 +186,9 @@
             this.buffer.clear();
             Header.format(this.buffer, headers[i]);
             this.datatransmitter.writeLine(this.buffer);
-            if (isWirelogEnabled()) {
-                wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
-            }
         }
         this.buffer.clear();
         this.datatransmitter.writeLine(this.buffer);
-        if (isWirelogEnabled()) {
-            wirelog("<< [\\r][\\n]");
-        }
-    }
-
-    protected void sendResponseBody(final HttpResponse response) 
-            throws HttpException, IOException {
-        if (response.getEntity() == null) {
-            return;
-        }
-        this.entitywriter.write(
-                response.getEntity(),
-                response.getStatusLine().getHttpVersion(),
-                this.datatransmitter);
     }
         
 }



Mime
View raw message