hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r467450 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: examples/org/apache/http/nio/examples/ main/java/org/apache/http/nio/impl/handler/
Date Tue, 24 Oct 2006 19:41:09 GMT
Author: olegk
Date: Tue Oct 24 12:41:08 2006
New Revision: 467450

URL: http://svn.apache.org/viewvc?view=rev&rev=467450
Log:
First take at the server side implementation of the new event-driven HTTP transport API. More
work is certainly needed but even at this point the implmentation _should_ be fully HTTP/1.0
and HTTP/1.1 spec compliant and appears stable. 

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
  (with props)
Removed:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncEchoServer.java

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java?view=auto&rev=467450
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
Tue Oct 24 12:41:08 2006
@@ -0,0 +1,313 @@
+package org.apache.http.nio.examples;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InterruptedIOException;
+import java.io.UnsupportedEncodingException;
+import java.net.InetSocketAddress;
+import java.net.URLDecoder;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.HttpConnection;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.FileEntity;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpParams;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOReactor;
+import org.apache.http.nio.handler.ContentDecoder;
+import org.apache.http.nio.handler.ContentEncoder;
+import org.apache.http.nio.handler.NHttpServerConnection;
+import org.apache.http.nio.handler.NHttpServiceHandler;
+import org.apache.http.nio.impl.DefaultIOReactor;
+import org.apache.http.nio.impl.handler.DefaultServerIOEventDispatch;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+import org.apache.http.util.EncodingUtils;
+
+public class NHttpServer {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 1) {
+            System.err.println("Please specify document root directory");
+            System.exit(1);
+        }
+        HttpParams params = new DefaultHttpParams(null);
+        params
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.ORIGIN_SERVER, "Jakarta-HttpComponents-NIO/1.1");
+
+        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
+                new MyNHttpServiceHandler(args[0]), params);
+        IOReactor ioReactor = new DefaultIOReactor(params);
+        try {
+            ioReactor.listen(new InetSocketAddress(8080));
+            ioReactor.execute(ioEventDispatch);
+        } catch (InterruptedIOException ex) {
+            System.err.println("Interrupted");
+        } catch (IOException e) {
+            System.err.println("I/O error: " + e.getMessage());
+        }
+        System.out.println("Shutdown");
+    }
+
+    public static class MyNHttpServiceHandler implements NHttpServiceHandler {
+
+        private final String docRoot;
+        private final HttpResponseFactory responseFactory;
+        private final ByteBuffer inbuf;
+        private final ByteBuffer outbuf;
+        private final HttpProcessor httpProcessor;
+        private final ConnectionReuseStrategy connStrategy;
+        
+        public MyNHttpServiceHandler(final String docRoot) {
+            super();
+            this.docRoot = docRoot;
+            this.responseFactory = new DefaultHttpResponseFactory();
+            this.inbuf = ByteBuffer.allocateDirect(2048);
+            this.outbuf = ByteBuffer.allocateDirect(2048);
+            BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
+            httpProcessor.addInterceptor(new ResponseDate());
+            httpProcessor.addInterceptor(new ResponseServer());                    
+            httpProcessor.addInterceptor(new ResponseContent());
+            httpProcessor.addInterceptor(new ResponseConnControl());
+            this.httpProcessor = httpProcessor;
+            this.connStrategy = new DefaultConnectionReuseStrategy();
+        }
+        
+        private void shutdownConnection(final HttpConnection conn) {
+            try {
+                conn.shutdown();
+            } catch (IOException ignore) {
+            }
+        }
+        
+        private void commitResponse(
+                final NHttpServerConnection conn,
+                final HttpResponse response) {
+            try {
+
+                ReadableByteChannel channel = null;
+                if (response.getEntity() != null) {
+                    InputStream instream = response.getEntity().getContent(); 
+                    if (instream instanceof FileInputStream) {
+                        channel = ((FileInputStream)instream).getChannel();
+                    } else {
+                        channel = Channels.newChannel(instream);
+                    }
+                    
+                }
+                
+                conn.getContext().setAttribute("out-channel", channel);
+                
+                this.httpProcessor.process(response, conn.getContext());
+                conn.submitResponse(response);
+            } catch (HttpException ex) {
+                ex.printStackTrace();
+                shutdownConnection(conn);
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                shutdownConnection(conn);
+            }
+        }
+
+        private void service(final NHttpServerConnection conn) {
+            HttpRequest request = conn.getHttpRequest();
+            HttpVersion ver = request.getRequestLine().getHttpVersion();
+            HttpResponse response =  this.responseFactory.newHttpResponse(ver, 200);
+
+            String target = request.getRequestLine().getUri();
+            try {
+
+                File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
+                if (!file.exists()) {
+                    
+                    response.setStatusCode(HttpStatus.SC_NOT_FOUND);
+                    byte[] msg = EncodingUtils.getAsciiBytes(
+                            file.getName() + ": not found");
+                    ByteArrayEntity entity = new ByteArrayEntity(msg);
+                    entity.setContentType("text/plain; charset=US-ASCII");
+                    response.setEntity(entity);
+                    
+                } else if (!file.canRead() || file.isDirectory()) {
+
+                    response.setStatusCode(HttpStatus.SC_FORBIDDEN);
+                    byte[] msg = EncodingUtils.getAsciiBytes(
+                            file.getName() + ": access denied");
+                    ByteArrayEntity entity = new ByteArrayEntity(msg);
+                    entity.setContentType("text/plain; charset=US-ASCII");
+                    response.setEntity(entity);
+
+                } else {
+
+                    FileEntity entity = new FileEntity(file, "text/html");
+                    response.setEntity(entity);
+                    
+                }
+                
+            } catch (UnsupportedEncodingException ex) {
+                throw new Error("UTF-8 not supported");
+            }
+            
+            HttpContext context = conn.getContext();
+            context.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+            context.setAttribute(HttpExecutionContext.HTTP_RESPONSE, response);
+            context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+            
+            commitResponse(conn, response);
+            
+        }
+        
+        public void requestReceived(final NHttpServerConnection conn) {
+            HttpRequest request = conn.getHttpRequest();
+            HttpVersion ver = request.getRequestLine().getHttpVersion();
+            if (request instanceof HttpEntityEnclosingRequest) {
+                
+                if (((HttpEntityEnclosingRequest) request).expectContinue()) {
+                    HttpResponse ack = this.responseFactory.newHttpResponse(ver, 100);
+                    try {
+                        conn.submitResponse(ack);
+                    } catch (HttpException ex) {
+                        ex.printStackTrace();
+                        shutdownConnection(conn);
+                        return;
+                    }
+                }
+                
+                // Request content is expected. 
+                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
+                WritableByteChannel channel = Channels.newChannel(bytestream);
+                
+                HttpContext context = conn.getContext();
+                context.setAttribute("in-buffer", bytestream);
+                context.setAttribute("in-channel", channel);
+                // Wait until the request content is fully received
+            } else {
+                // No request content is expected. 
+                //Proceed with service right away
+                service(conn);
+            }
+        }
+
+        public void closed(final NHttpServerConnection conn) {
+            System.out.println("Connection closed");
+        }
+
+        public void exception(final NHttpServerConnection conn, final HttpException ex) {
+            HttpRequest request = conn.getHttpRequest();
+            HttpVersion ver = request.getRequestLine().getHttpVersion();
+            HttpResponse response =  this.responseFactory.newHttpResponse(
+                    ver, HttpStatus.SC_BAD_REQUEST);
+            byte[] msg = EncodingUtils.getAsciiBytes(
+                    "Malformed HTTP request: " + ex.getMessage());
+            ByteArrayEntity entity = new ByteArrayEntity(msg);
+            entity.setContentType("text/plain; charset=US-ASCII");
+            response.setEntity(entity);
+            commitResponse(conn, response);
+        }
+
+        public void exception(NHttpServerConnection conn, IOException ex) {
+            System.err.println("I/O error: " + ex.getMessage());
+            shutdownConnection(conn);
+        }
+
+        public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder)
{
+
+            HttpRequest request = conn.getHttpRequest();
+            HttpContext context = conn.getContext();
+            WritableByteChannel channel = (WritableByteChannel) context
+                .getAttribute("in-channel");
+
+            try {
+                while (decoder.read(this.inbuf) > 0) {
+                    this.inbuf.flip();
+                    channel.write(this.inbuf);
+                    this.inbuf.compact();
+                }
+                if (decoder.isCompleted()) {
+                    // Request entity has been fully received
+                    
+                    ByteArrayOutputStream bytestream = (ByteArrayOutputStream) context
+                        .getAttribute("in-buffer");
+                    byte[] content = bytestream.toByteArray();
+                    
+                    ByteArrayEntity entity = new ByteArrayEntity(content);
+                    entity.setContentType(request.getFirstHeader(HTTP.CONTENT_TYPE));
+                    
+                    ((HttpEntityEnclosingRequest) request).setEntity(entity);
+                    
+                    service(conn);
+                }
+                
+            } catch (IOException ex) {
+                shutdownConnection(conn);
+                ex.printStackTrace();
+            }
+        }
+
+        public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder)
{
+
+            HttpContext context = conn.getContext();
+            HttpResponse response = conn.getHttpResponse();
+            ReadableByteChannel channel = (ReadableByteChannel) context
+                .getAttribute("out-channel");
+
+            try {
+                int bytesRead = channel.read(this.outbuf);
+                if (bytesRead == -1) {
+                    encoder.complete();
+                } else {
+                    this.outbuf.flip();
+                    encoder.write(this.outbuf);
+                    this.outbuf.compact();
+                }
+
+                if (encoder.isCompleted()) {
+                    if (!this.connStrategy.keepAlive(response, context)) {
+                        conn.close();
+                    }
+                }
+            
+            } catch (IOException ex) {
+                shutdownConnection(conn);
+                ex.printStackTrace();
+            }
+
+        }
+
+        public void timeout(final NHttpServerConnection conn) {
+            System.err.println("Timeout");
+            shutdownConnection(conn);
+        }
+        
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/NHttpServer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java?view=auto&rev=467450
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
Tue Oct 24 12:41:08 2006
@@ -0,0 +1,180 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl.handler;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestFactory;
+import org.apache.http.HttpResponse;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BasicStatusLine;
+import org.apache.http.message.BufferedHeader;
+import org.apache.http.nio.EventMask;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.handler.NHttpServerConnection;
+import org.apache.http.nio.handler.NHttpServiceHandler;
+import org.apache.http.nio.impl.codecs.HttpRequestParser;
+import org.apache.http.params.HttpParams;
+
+public class DefaultNHttpServerConnection 
+    extends NHttpConnectionBase implements NHttpServerConnection {
+
+    private HttpRequestParser requestParser;
+    
+    public DefaultNHttpServerConnection(
+            final IOSession session,
+            final HttpRequestFactory requestFactory,
+            final HttpParams params) {
+        super(session, params);
+        if (requestFactory == null) {
+            throw new IllegalArgumentException("Request factory may not be null");
+        }
+        this.requestParser = new HttpRequestParser(this.inbuf, requestFactory);
+    }
+
+    private void resetInput() {
+        this.request = null;
+        this.contentDecoder = null;
+        this.requestParser.reset();
+    }
+    
+    private void resetOutput() {
+        this.response = null;
+        this.contentEncoder = null;
+    }
+    
+    public void consumeInput(final NHttpServiceHandler handler) {
+        if (this.closed) {
+            this.session.clearEvent(EventMask.READ);
+            return;
+        }
+        try {
+            if (this.request == null) {
+                this.requestParser.fillBuffer(this.session.channel());
+                this.request = (HttpRequest) this.requestParser.parse(); 
+                if (this.request != null) {
+                    handler.requestReceived(this);
+                    if (this.request instanceof HttpEntityEnclosingRequest) {
+                        // Receive incoming entity
+                        HttpEntity entity = prepareDecoder(this.request);
+                        ((HttpEntityEnclosingRequest)this.request).setEntity(entity);
+                    } else {
+                        // No request entity is expected
+                        // Ready to receive a new request
+                        resetInput();
+                    }
+                }
+            }
+            if (this.contentDecoder != null) {
+                handler.inputReady(this, this.contentDecoder);
+                if (this.contentDecoder.isCompleted()) {
+                    // Request entity received
+                    // Ready to receive a new request
+                    resetInput();
+                }
+            }
+        } catch (IOException ex) {
+            handler.exception(this, ex);
+        } catch (HttpException ex) {
+            handler.exception(this, ex);
+        }
+    }
+
+    public void produceOutput(final NHttpServiceHandler handler) {
+        try {
+            if (this.outbuf.hasData()) {
+                this.outbuf.flush(this.session.channel());
+            }
+            if (!this.outbuf.hasData()) {
+                if (this.closed) {
+                    this.session.close();
+                } else {
+                    if (this.contentEncoder != null) {
+                        handler.outputReady(this, this.contentEncoder);
+                        if (this.contentEncoder.isCompleted()) {
+                            resetOutput();
+                        }
+                    }
+                }
+                if (this.contentEncoder == null) {
+                    this.session.clearEvent(EventMask.WRITE);
+                }
+            }
+        } catch (IOException ex) {
+            handler.exception(this, ex);
+        }
+    }
+    
+    public boolean isResponseCommitted() {
+        return this.response != null;
+    }
+
+    public void submitResponse(final HttpResponse response) throws HttpException {
+        if (response == null) {
+            throw new IllegalArgumentException("HTTP response may not be null");
+        }
+        if (this.response != null) {
+            throw new HttpException("Response already submitted");
+        }
+        this.lineBuffer.clear();
+        BasicStatusLine.format(this.lineBuffer, response.getStatusLine());
+        this.outbuf.writeLine(this.lineBuffer);
+        for (Iterator it = response.headerIterator(); it.hasNext(); ) {
+            Header header = (Header) it.next();
+            if (header instanceof BufferedHeader) {
+                // If the header is backed by a buffer, re-use the buffer
+                this.outbuf.writeLine(((BufferedHeader)header).getBuffer());
+            } else {
+                this.lineBuffer.clear();
+                BasicHeader.format(this.lineBuffer, header);
+                this.outbuf.writeLine(this.lineBuffer);
+            }
+        }
+        this.lineBuffer.clear();
+        this.outbuf.writeLine(this.lineBuffer);
+
+        if (response.getStatusLine().getStatusCode() >= 200) {
+            this.response = response;
+            prepareEncoder(response);
+        }
+        this.session.setEvent(EventMask.WRITE);
+    }
+
+    public boolean isResponseSubmitted() {
+        return this.response != null;
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultNHttpServerConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java?view=auto&rev=467450
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
Tue Oct 24 12:41:08 2006
@@ -0,0 +1,60 @@
+package org.apache.http.nio.impl.handler;
+
+import org.apache.http.impl.DefaultHttpRequestFactory;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.handler.NHttpServiceHandler;
+import org.apache.http.params.HttpParams;
+
+public class DefaultServerIOEventDispatch implements IOEventDispatch {
+
+    private static final String NHTTP_CONN = "NHTTP_CONN";
+    
+    private final NHttpServiceHandler handler;
+    private final HttpParams params;
+    
+    public DefaultServerIOEventDispatch(final NHttpServiceHandler handler, final HttpParams
params) {
+        super();
+        if (handler == null) {
+            throw new IllegalArgumentException("HTTP service handler may not be null");
+        }
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
+        }
+        this.handler = handler;
+        this.params = params;
+    }
+    
+    public void connected(final IOSession session) {
+        DefaultNHttpServerConnection conn = new DefaultNHttpServerConnection(
+                session, 
+                new DefaultHttpRequestFactory(),
+                this.params); 
+        session.setAttribute(NHTTP_CONN, conn);
+    }
+
+    public void disconnected(final IOSession session) {
+        DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session.getAttribute(
+                NHTTP_CONN);
+        this.handler.closed(conn);
+    }
+
+    public void inputReady(final IOSession session) {
+        DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session.getAttribute(
+                NHTTP_CONN);
+        conn.consumeInput(this.handler);
+    }
+
+    public void outputReady(final IOSession session) {
+        DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session.getAttribute(
+                NHTTP_CONN);
+        conn.produceOutput(this.handler);
+    }
+
+    public void timeout(final IOSession session) {
+        DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session.getAttribute(
+                NHTTP_CONN);
+        this.handler.timeout(conn);
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/DefaultServerIOEventDispatch.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java?view=auto&rev=467450
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
Tue Oct 24 12:41:08 2006
@@ -0,0 +1,210 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl.handler;
+
+import java.io.IOException;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.entity.ContentLengthStrategy;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.io.CharArrayBuffer;
+import org.apache.http.nio.EventMask;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.handler.ContentDecoder;
+import org.apache.http.nio.handler.ContentEncoder;
+import org.apache.http.nio.handler.NHttpConnection;
+import org.apache.http.nio.impl.SessionInputBuffer;
+import org.apache.http.nio.impl.SessionOutputBuffer;
+import org.apache.http.nio.impl.codecs.ChunkDecoder;
+import org.apache.http.nio.impl.codecs.ChunkEncoder;
+import org.apache.http.nio.impl.codecs.IdentityDecoder;
+import org.apache.http.nio.impl.codecs.IdentityEncoder;
+import org.apache.http.nio.impl.codecs.LengthDelimitedDecoder;
+import org.apache.http.nio.impl.codecs.LengthDelimitedEncoder;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.SyncHttpExecutionContext;
+
+public class NHttpConnectionBase implements NHttpConnection {
+
+    protected final IOSession session;
+    protected final HttpContext context;
+    
+    protected ContentLengthStrategy incomingContentStrategy;
+    protected ContentLengthStrategy outgoingContentStrategy;
+    
+    protected final SessionInputBuffer inbuf;
+    protected final SessionOutputBuffer outbuf;
+    protected final CharArrayBuffer lineBuffer;
+    
+    protected ContentDecoder contentDecoder;
+    protected ContentEncoder contentEncoder;
+    protected HttpRequest request;
+    protected HttpResponse response;
+    
+    protected volatile boolean closed;
+    
+    public NHttpConnectionBase(final IOSession session, final HttpParams params) {
+        super();
+        if (session == null) {
+            throw new IllegalArgumentException("I/O session may not be null");
+        }
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP params may not be null");
+        }
+        this.session = session;
+        this.context = new SyncHttpExecutionContext(null);
+        
+        int buffersize = HttpConnectionParams.getSocketBufferSize(params);
+        int linebuffersize = buffersize;
+        if (linebuffersize > 512) {
+            linebuffersize = 512;
+        }
+        
+        this.inbuf = new SessionInputBuffer(buffersize, linebuffersize); 
+        this.inbuf.reset(params);
+        this.outbuf = new SessionOutputBuffer(buffersize, linebuffersize); 
+        this.outbuf.reset(params);
+        this.lineBuffer = new CharArrayBuffer(64); 
+        
+        this.incomingContentStrategy = new LaxContentLengthStrategy();
+        this.outgoingContentStrategy = new StrictContentLengthStrategy();
+        
+        this.closed = false;
+        this.session.setEvent(EventMask.READ);
+    }
+
+    public HttpContext getContext() {
+        return this.context;
+    }
+
+    public HttpRequest getHttpRequest() {
+        return this.request;
+    }
+
+    public HttpResponse getHttpResponse() {
+        return this.response;
+    }
+
+    public void requestInput() {
+        if (this.contentDecoder != null) {
+            this.session.setEvent(EventMask.READ);
+        }
+    }
+
+    public void requestOutput() {
+        if (this.contentEncoder != null) {
+            this.session.setEvent(EventMask.WRITE);
+        }
+    }
+
+    public void suspendInput() {
+        if (this.contentDecoder != null) {
+            this.session.clearEvent(EventMask.READ);
+        }
+    }
+
+    public void suspendOutput() {
+        if (this.contentEncoder != null) {
+            this.session.clearEvent(EventMask.WRITE);
+        }
+    }
+
+    protected HttpEntity prepareDecoder(final HttpMessage message) throws HttpException {
+        BasicHttpEntity entity = new BasicHttpEntity();
+        long len = this.incomingContentStrategy.determineLength(message);
+        if (len == ContentLengthStrategy.CHUNKED) {
+            this.contentDecoder = new ChunkDecoder(this.session.channel(), this.inbuf);
+            entity.setChunked(true);
+            entity.setContentLength(-1);
+        } else if (len == ContentLengthStrategy.IDENTITY) {
+            this.contentDecoder = new IdentityDecoder(this.session.channel(), this.inbuf);
+            entity.setChunked(false);
+            entity.setContentLength(-1);
+        } else {
+            this.contentDecoder = new LengthDelimitedDecoder(this.session.channel(), this.inbuf,
len);
+            entity.setChunked(false);
+            entity.setContentLength(len);
+        }
+        
+        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
+        if (contentTypeHeader != null) {
+            entity.setContentType(contentTypeHeader);    
+        }
+        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
+        if (contentEncodingHeader != null) {
+            entity.setContentEncoding(contentEncodingHeader);    
+        }
+        return entity;
+    }
+
+    protected void prepareEncoder(final HttpMessage message) throws HttpException {
+        long len = this.outgoingContentStrategy.determineLength(message);
+        if (len == ContentLengthStrategy.CHUNKED) {
+            this.contentEncoder = new ChunkEncoder(this.outbuf);
+        } else if (len == ContentLengthStrategy.IDENTITY) {
+            this.contentEncoder = new IdentityEncoder(this.session.channel());
+        } else {
+            this.contentEncoder = new LengthDelimitedEncoder(this.session.channel(), len);
+        }
+    }
+
+    public void close() throws IOException {
+        this.closed = true;
+        if (this.outbuf.hasData()) {
+            this.session.setEvent(EventMask.WRITE);
+        } else {
+            this.session.close();
+        }
+    }
+
+    public boolean isOpen() {
+        return !this.closed;
+    }
+
+    public boolean isStale() {
+        return this.session.isClosed();
+    }
+
+    public void shutdown() throws IOException {
+        this.closed = true;
+        this.session.close();
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/handler/NHttpConnectionBase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message