geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r619395 - in /geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec: HttpDecoder.java HttpRequestEncoder.java HttpResponseDecoder.java
Date Thu, 07 Feb 2008 13:06:34 GMT
Author: rickmcguire
Date: Thu Feb  7 05:06:31 2008
New Revision: 619395

URL: http://svn.apache.org/viewvc?rev=619395&view=rev
Log:
GERONIMO-3824 ProtocolDecoderException is thrown if the response does not specify Content-Length
but is not chunked

Merge of 1.1.5 version into Mina 2.0 version. 


Modified:
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java?rev=619395&r1=619394&r2=619395&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
Thu Feb  7 05:06:31 2008
@@ -86,9 +86,9 @@
     private CharsetDecoder decoder = Charset.forName(HttpMessage.HTTP_ELEMENT_CHARSET).newDecoder();
 
     /**
-     * Finds a line from a ByteBuffer that ends with a CR/LF and returns the line as a String.
+     * Finds a line from a IoBuffer that ends with a CR/LF and returns the line as a String.
      * 
-     * @param in ByteBuffer containing data
+     * @param in IoBuffer containing data
      * 
      * @return a <code>String</code> representing the decoded line
      * 
@@ -224,15 +224,48 @@
     }
 
     /**
-     * Decodes content from non-chunked transcoding.
+     * Decodes content from non-chunked transcoding.  It adds exactly the amount
+     * of data specified in the content length header.
      * 
-     * @param in the <code>ByteBuffer</code> containing the content at the the
current position
-     * @param msg the <code>HttpResponseMessage</code> message to place the decoded
content
+     * @param in the <code>IoBuffer</code> containing the content at the the

+     * current position
+     * @param msg the <code>HttpResponseMessage</code> message to place the 
+     * decoded content
      * 
      * @throws Exception if any exception occurs
      */
     public void decodeContent(IoBuffer in, HttpResponseMessage msg) throws Exception {
-        byte content[] = new byte[msg.getContentLength()];
+        addContent(in, msg, msg.getContentLength());
+    }
+    
+    /**
+     * Decodes content from non-chunked transcoding.  It adds all the remaining
+     * bytes from the buffer into the response body.
+     * 
+     * @param in the <code>IoBuffer</code> containing the content at the the
current position
+     * @param msg the <code>HttpResponseMessage</code> message to place the decoded
content
+     * 
+     * @throws Exception if any exception occurs
+     */
+    public void decodeRemainingContent(IoBuffer in, HttpResponseMessage msg) throws Exception
{
+        if (in.hasRemaining()) {
+            addContent(in, msg, in.remaining());
+        }
+    }
+    
+    
+    /**
+     * Transfer a specific size of content from the buffer 
+     * to the message. 
+     * 
+     * @param in     The input buffer
+     * @param msg    The message that will receive the content.
+     * @param size   The number of bytes to transfer.
+     * 
+     * @exception Exception
+     */
+    private void addContent(IoBuffer in, HttpResponseMessage msg, int size) throws Exception
{
+        byte[] content = new byte[size];
         in.get(content);
         msg.addContent(content);
     }
@@ -240,7 +273,7 @@
     /**
      * Decodes content from chunked transcoding.
      * 
-     * @param in the <code>ByteBuffer</code> containing the content at the the
current position
+     * @param in the <code>IoBuffer</code> containing the content at the the
current position
      * @param msg the <code>HttpResponseMessage</code> message to place the decoded
content
      * 
      * @throws Exception if any exception occurs

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?rev=619395&r1=619394&r2=619395&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
Thu Feb  7 05:06:31 2008
@@ -194,7 +194,7 @@
      * Process header encoding.
      * 
      * @param msg the {@link HttpRequestMessage} message object
-     * @param buf the <code>ByteBuffer</code> in which to place the raw bytes
+     * @param buf the <code>IoBuffer</code> in which to place the raw bytes
      * @param encoder the character set encoder
      * 
      * @throws Exception if any exception occurs.

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java?rev=619395&r1=619394&r2=619395&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
Thu Feb  7 05:06:31 2008
@@ -38,7 +38,7 @@
      * Decodes the raw HTTP response from a server into a {@link HttpResponseMessage} object.
      * 
      * @param ioSession the {@link org.apache.mina.common.IoSession} representing the connection
to the server.
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * @param out {@link org.apache.mina.filter.codec.ProtocolDecoderOutput} used for output
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.  A false tells the API to fetch more data.
@@ -114,11 +114,7 @@
                 }
             }
 
-            response.setState(HttpResponseMessage.STATE_FINISHED);
-
-            out.write(response);
-
-            ioSession.removeAttribute(HttpIoHandler.CURRENT_RESPONSE);
+            completeResponse(ioSession, out, response);
 
             return true;
         } catch (NeedMoreDataException e) {
@@ -126,11 +122,33 @@
         }
     }
 
+    @Override
+    public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception
{
+        // if the response was still being decoded when the session is getting
+        // closed, we finish decoding and hand off the response
+        HttpResponseMessage response = 
+                (HttpResponseMessage)session.getAttribute(HttpIoHandler.CURRENT_RESPONSE);
+        // we're only interested in non-chunked responses with no content length
+        // specified, in which case connection close marks the end of the body
+        if (response != null &&
+                !response.isChunked() &&
+                response.getContentLength() <= 0 &&
+                response.getState() == HttpResponseMessage.STATE_HEADERS_READ) {
+            completeResponse(session, out, response);
+        }
+    }
+
+    private void completeResponse(IoSession ioSession, ProtocolDecoderOutput out, HttpResponseMessage
response) {
+            response.setState(HttpResponseMessage.STATE_FINISHED);
+            out.write(response);
+            ioSession.removeAttribute(HttpIoHandler.CURRENT_RESPONSE);
+    }
+
     /**
      * Reads the headers and processes them as header objects in the {@link HttpResponseMessage}
object.
      * 
      * @param response the {@link HttpResponseMessage} response object
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.
      * 
@@ -150,7 +168,7 @@
      * object.  This is only used in chunked transcoding.
      * 
      * @param response the {@link HttpResponseMessage} response object
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.
      * 
@@ -169,7 +187,7 @@
      * Decodes the raw headers/footers.
      * 
      * @param response the {@link HttpResponseMessage} response object
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.
      * 
@@ -199,7 +217,7 @@
      * Process and read the content.
      * 
      * @param response the {@link HttpResponseMessage} response object
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.
      * 
@@ -248,6 +266,12 @@
                 return false;
             }
             httpDecoder.decodeContent(in, response);
+        } else {
+            // it neither is chunked nor has content length; read until the
+            // session closes
+            httpDecoder.decodeRemainingContent(in, response);
+            // keep looping until the session closes
+            return false;
         }
 
         response.setState(HttpResponseMessage.STATE_CONTENT_READ);
@@ -259,7 +283,7 @@
      * Process  and read the status header.
      * 
      * @param response the {@link HttpResponseMessage} response object
-     * @param in the <code>ByteBuffer</code> that contains the raw bytes from
the server
+     * @param in the <code>IoBuffer</code> that contains the raw bytes from the
server
      * 
      * @return <code>true</code> if it can read all of the data, or <code>false</code>
if not.
      * 



Mime
View raw message