This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git


The following commit(s) were added to refs/heads/2.2.X by this push:
     new 87099d9c5 o Fixed the decoding of partial HTTP messages o Improved the 
HttpStatus enum by adding a quick method to retrieve the status from its code
87099d9c5 is described below

commit 87099d9c5bd7d94c6e2428b6ebbf1ea0c020ad2f
Author: emmanuel lecharny <[email protected]>
AuthorDate: Mon Nov 16 09:01:53 2026 +0100

    o Fixed the decoding of partial HTTP messages
    o Improved the HttpStatus enum by adding a quick method to retrieve the
    status from its code
---
 .../org/apache/mina/http/HttpClientDecoder.java    | 246 +++++++++++++++------
 .../java/org/apache/mina/http/api/HttpStatus.java  |  29 +++
 .../apache/mina/http/HttpClientDecoderTest.java    | 140 ++++++++++++
 3 files changed, 353 insertions(+), 62 deletions(-)

diff --git 
a/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java 
b/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
index 4950fe422..9a8d3b486 100644
--- a/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
+++ b/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
@@ -72,6 +72,18 @@ public class HttpClientDecoder implements ProtocolDecoder {
     /** Key for indicating chunked data */
     private static final String BODY_CHUNKED = "http.ckd";
     
+    /** Key for the number of bytes remaining in the current chunk */
+    private static final String CHUNK_REMAINING_BYTES = "http.crb";
+
+    /** Key for a partially received chunk-size or trailer line */
+    private static final String PARTIAL_CHUNK_LINE = "http.pcl";
+
+    /** Key indicating that the last (zero-sized) chunk was seen and trailers 
are being consumed */
+    private static final String CHUNK_TRAILER = "http.ctr";
+
+    /** Maximum length of a chunk-size or trailer line, to bound accumulation 
*/
+    private static final int MAX_CHUNK_LINE_LENGTH = 4096;
+
     /** Regex to parse HttpRequest Request Line */
     public static final Pattern REQUEST_LINE_PATTERN = Pattern.compile(" ");
 
@@ -127,8 +139,8 @@ public class HttpClientDecoder implements ProtocolDecoder {
         DecoderState state = 
(DecoderState)session.getAttribute(DECODER_STATE_ATT);
         
         if (null == state) {
-            session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
-            state = (DecoderState)session.getAttribute(DECODER_STATE_ATT);
+            state = DecoderState.NEW;
+            session.setAttribute(DECODER_STATE_ATT, state);
         }
         
         switch (state) {
@@ -140,7 +152,7 @@ public class HttpClientDecoder implements ProtocolDecoder {
                 // grab the stored a partial HEAD request
                 ByteBuffer oldBuffer = 
(ByteBuffer)session.getAttribute(PARTIAL_HEAD_ATT);
                 // concat the old buffer and the new incoming one
-                IoBuffer.allocate(oldBuffer.remaining() + 
msg.remaining()).put(oldBuffer).put(msg).flip();
+                msg = IoBuffer.allocate(oldBuffer.remaining() + 
msg.remaining()).put(oldBuffer).put(msg).flip();
                 // now let's decode like it was a new message
     
             case NEW:
@@ -169,43 +181,52 @@ public class HttpClientDecoder implements ProtocolDecoder 
{
                     // no request decoded, we accumulate
                     session.setAttribute(PARTIAL_HEAD_ATT, partial);
                     session.setAttribute(DECODER_STATE_ATT, DecoderState.HEAD);
-                } else {
-                    out.write(rp);
-                    // is it a response with some body content ?
+                    break;
+                }
+
+                out.write(rp);
+                
+                // is it a response with some body content ?
+                if (LOGGER.isDebugEnabled()) {
+                    LOGGER.debug("response with content");
+                }
+
+                session.setAttribute(DECODER_STATE_ATT, DecoderState.BODY);
+
+                String contentLen = rp.getHeader("content-length");
+
+                if (contentLen != null) {
                     if (LOGGER.isDebugEnabled()) {
                         LOGGER.debug("response with content");
                     }
-                    
-                    session.setAttribute(DECODER_STATE_ATT, DecoderState.BODY);
-    
-                    String contentLen = rp.getHeader("content-length");
-    
-                    if (contentLen != null) {
-                        if (LOGGER.isDebugEnabled()) {
-                            LOGGER.debug("found content len : {}", contentLen);
-                        }
-                        
-                        session.setAttribute(BODY_REMAINING_BYTES, 
Integer.valueOf(contentLen));
-                    } else if 
("chunked".equalsIgnoreCase(rp.getHeader("transfer-encoding"))) {
-                        if (LOGGER.isDebugEnabled()) {
-                            LOGGER.debug("no content len but chunked");
-                        }
-                        
-                        session.setAttribute(BODY_CHUNKED, Boolean.TRUE);
-                    } else if 
("close".equalsIgnoreCase(rp.getHeader("connection"))) {
-                        session.closeNow();
-                    } else {
-                        throw new 
HttpException(HttpStatus.CLIENT_ERROR_LENGTH_REQUIRED, "no content length !");
+
+                    session.setAttribute(BODY_REMAINING_BYTES, 
Integer.valueOf(contentLen));
+                } else if 
("chunked".equalsIgnoreCase(rp.getHeader("transfer-encoding"))) {
+                    if (LOGGER.isDebugEnabled()) {
+                        LOGGER.debug("no content len but chunked");
                     }
+
+                    session.setAttribute(BODY_CHUNKED, Boolean.TRUE);
+                } else if 
("close".equalsIgnoreCase(rp.getHeader("connection"))) {
+                    session.closeNow();
+                    break;
+                } else {
+                    throw new 
HttpException(HttpStatus.CLIENT_ERROR_LENGTH_REQUIRED, "no content length !");
                 }
-    
-                break;
-    
+
+                // fallthrough, process the body bytes received along with the 
head
+
             case BODY:
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug("decoding BODY: {} bytes", msg.remaining());
                 }
-                
+
+                // a chunked body is framed by the server : decode the framing
+                if( session.getAttribute(BODY_CHUNKED) != null ) {
+                    decodeChunkedBody(session, msg, out);
+                    break;
+                }
+
                 int chunkSize = msg.remaining();
                 
                 // send the chunk of body
@@ -218,17 +239,9 @@ public class HttpClientDecoder implements ProtocolDecoder {
                 
                 msg.position(msg.limit());
                 
-                // do we have reach end of body ?
-                int remaining;
-                
-                // if chunked, remaining is the msg.remaining()
-                if( session.getAttribute(BODY_CHUNKED) != null ) {
-                    remaining = chunkSize;
-                } else {
-                    // otherwise, manage with content-length
-                    remaining = (Integer) 
session.getAttribute(BODY_REMAINING_BYTES);
-                    remaining -= chunkSize;
-                }
+                // do we have reach end of body ? manage with content-length
+                int remaining = (Integer) 
session.getAttribute(BODY_REMAINING_BYTES);
+                remaining -= chunkSize;
     
                 if (remaining <= 0 ) {
                     if (LOGGER.isDebugEnabled()) {
@@ -238,15 +251,9 @@ public class HttpClientDecoder implements ProtocolDecoder {
                     session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
                     session.removeAttribute(BODY_REMAINING_BYTES);
                     
-                    if( session.getAttribute(BODY_CHUNKED) != null ) {
-                        session.removeAttribute(BODY_CHUNKED);
-                    }
-                    
                     out.write(new HttpEndOfContent());
                 } else {
-                    if( session.getAttribute(BODY_CHUNKED) == null ) {
-                        session.setAttribute(BODY_REMAINING_BYTES, 
Integer.valueOf(remaining));
-                    }
+                    session.setAttribute(BODY_REMAINING_BYTES, 
Integer.valueOf(remaining));
                 }
     
                 break;
@@ -256,6 +263,119 @@ public class HttpClientDecoder implements ProtocolDecoder 
{
         }
     }
 
+    /**
+     * Decodes a body sent with "Transfer-Encoding: chunked". Only the chunk 
payloads
+     * are delivered to the next filter : the chunk-size lines, the CRLF 
terminating
+     * each chunk and the optional trailers are consumed by the decoder. A
+     * {@link HttpEndOfContent} is emitted once the last (zero-sized) chunk 
and its
+     * trailers have been received. The method consumes at least one byte on 
each
+     * invocation when data remains, so the codec filter can safely call it 
again
+     * while input remains.
+     */
+    private void decodeChunkedBody(IoSession session, IoBuffer msg, 
ProtocolDecoderOutput out) {
+        Integer chunkRemaining = (Integer) 
session.getAttribute(CHUNK_REMAINING_BYTES);
+
+        if ((chunkRemaining != null) && (chunkRemaining.intValue() > 0)) {
+            // deliver as much of the current chunk payload as we have received
+            int toRead = Math.min(chunkRemaining.intValue(), msg.remaining());
+
+            if (toRead == 0) {
+                // wait for more data
+                return;
+            }
+
+            IoBuffer wb = IoBuffer.allocate(toRead);
+            int oldLimit = msg.limit();
+            msg.limit(msg.position() + toRead);
+            wb.put(msg);
+            msg.limit(oldLimit);
+            wb.flip();
+            out.write(wb);
+
+            int left = chunkRemaining.intValue() - toRead;
+
+            if (left == 0) {
+                session.removeAttribute(CHUNK_REMAINING_BYTES);
+            } else {
+                session.setAttribute(CHUNK_REMAINING_BYTES, 
Integer.valueOf(left));
+            }
+
+            return;
+        }
+
+        // we are between chunks : accumulate a full line, which is either a
+        // chunk-size line, the CRLF terminating the previous chunk or a 
trailer line
+        String partial = (String) session.getAttribute(PARTIAL_CHUNK_LINE);
+        StringBuilder line = (partial == null) ? new StringBuilder() : new 
StringBuilder(partial);
+        boolean complete = false;
+
+        while (msg.hasRemaining()) {
+            char c = (char) (msg.get() & 0xFF);
+
+            if (c == '\n') {
+                complete = true;
+                break;
+            }
+
+            if (c != '\r') {
+                line.append(c);
+            }
+
+            if (line.length() > MAX_CHUNK_LINE_LENGTH) {
+                throw new HttpException(HttpStatus.CLIENT_ERROR_BAD_REQUEST, 
"Chunk size line too long");
+            }
+        }
+
+        if (!complete) {
+            // wait for more data
+            session.setAttribute(PARTIAL_CHUNK_LINE, line.toString());
+            return;
+        }
+
+        session.removeAttribute(PARTIAL_CHUNK_LINE);
+        String chunkLine = line.toString();
+
+        if (session.getAttribute(CHUNK_TRAILER) != null) {
+            // consuming the trailers following the last chunk
+            if (chunkLine.length() == 0) {
+                // end of the trailers : end of the response
+                session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
+                session.removeAttribute(BODY_CHUNKED);
+                session.removeAttribute(CHUNK_TRAILER);
+                out.write(new HttpEndOfContent());
+            }
+
+            return;
+        }
+
+        if (chunkLine.length() == 0) {
+            // the CRLF terminating the previous chunk payload
+            return;
+        }
+
+        // a chunk-size line : an hex size, optionally followed by ';' and 
extensions
+        int semicolon = chunkLine.indexOf(';');
+        String sizeString = (semicolon >= 0 ? chunkLine.substring(0, 
semicolon) : chunkLine).trim();
+        int size;
+
+        try {
+            size = Integer.parseInt(sizeString, 16);
+        } catch (NumberFormatException e) {
+            throw new HttpException(HttpStatus.CLIENT_ERROR_BAD_REQUEST, 
"Invalid chunk size : " + sizeString);
+        }
+
+        if (size < 0) {
+            throw new HttpException(HttpStatus.CLIENT_ERROR_BAD_REQUEST, 
"Invalid chunk size : " + sizeString);
+        }
+
+        if (size == 0) {
+            // last chunk : consume the trailers
+            session.setAttribute(CHUNK_TRAILER, Boolean.TRUE);
+        } else {
+            session.setAttribute(CHUNK_REMAINING_BYTES, Integer.valueOf(size));
+        }
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -271,7 +391,7 @@ public class HttpClientDecoder implements ProtocolDecoder {
     }
 
     private DefaultHttpResponse parseHttpReponseHead(ByteBuffer buffer) {
-        String raw = new String(buffer.array(), 0, buffer.limit());
+        String raw = new String(buffer.array(), buffer.position(), 
buffer.remaining());
         String[] headersAndBody = RAW_VALUE_PATTERN.split(raw, -1);
         
         if (headersAndBody.length <= 1) {
@@ -285,27 +405,29 @@ public class HttpClientDecoder implements ProtocolDecoder 
{
         String requestLine = headerFields[0];
         Map<String, String> generalHeaders = new HashMap<>();
 
-        for (String headerField:headerFields) {
-            String[] header = HEADER_VALUE_PATTERN.split(headerField);
-            generalHeaders.put(header[0].toLowerCase(), header[1]);
+        // the status line is not a header : start at 1
+        for (int i = 1; i < headerFields.length; i++) {
+            String headerField = headerFields[i];
+            int firstColon = headerField.indexOf(':');
+
+            if (firstColon > 0) {
+                generalHeaders.put(headerField.substring(0, 
firstColon).toLowerCase(),
+                        headerField.substring(firstColon + 1).trim());
+            } else {
+                generalHeaders.put(headerField.trim(), "");
+            }
         }
 
         String[] elements = RESPONSE_LINE_PATTERN.split(requestLine);
-        HttpStatus status = null;
         int statusCode = Integer.parseInt(elements[1]);
         
-        for (HttpStatus httpStatus:HttpStatus.values()) {
-            if (statusCode == httpStatus.code()) {
-                
-                break;
-            }
-        }
+        HttpStatus status = HttpStatus.getStatus(statusCode);
         
         HttpVersion version = HttpVersion.fromString(elements[0]);
 
         // we put the buffer position where we found the beginning of the HTTP 
body
-        buffer.position(headersAndBody[0].length() + 4);
-
+        buffer.position(buffer.position() + headersAndBody[0].length() + 4);
+        
         return new DefaultHttpResponse(version, status, generalHeaders);
     }
 }
diff --git a/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java 
b/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
index 9b499ce27..673430cc2 100644
--- a/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
+++ b/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
@@ -19,6 +19,9 @@
  */
 package org.apache.mina.http.api;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * An <code>Enumeration</code> of all known HTTP status codes.
  * 
@@ -236,6 +239,14 @@ public enum HttpStatus {
      * The line associated with this status, "HTTP/1.1 501 Not Implemented".
      */
     private String line;
+    
+    private static final Map<Integer, HttpStatus> BY_CODE = new HashMap<>();
+    
+    static {
+        for (HttpStatus e: values()) {
+            BY_CODE.put(e.code, e);
+        }
+    }
 
     /**
      * Create an instance of this type.
@@ -265,4 +276,22 @@ public enum HttpStatus {
     public String line() {
         return line + "\r\n";
     }
+    
+    /**
+     * Tells if an HttpStatus exists for the given code
+     * @param code The HttpStatus code to retrieve
+     * @return <code>true</code> if the HttpStatus is valid
+     */
+    public static boolean hasStatus(int code) {
+        return BY_CODE.containsKey(code);
+    }
+    
+    /**
+     * Gets an HttpStatus by its code
+     * @param code The HttpStatus code to retrieve
+     * @return The associated HttpStatus
+     */
+    public static HttpStatus getStatus(int code) {
+        return BY_CODE.get(code);
+    }
 }
diff --git 
a/mina-http/src/test/java/org/apache/mina/http/HttpClientDecoderTest.java 
b/mina-http/src/test/java/org/apache/mina/http/HttpClientDecoderTest.java
new file mode 100644
index 000000000..5ee13bdb3
--- /dev/null
+++ b/mina-http/src/test/java/org/apache/mina/http/HttpClientDecoderTest.java
@@ -0,0 +1,140 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ *
+ */
+package org.apache.mina.http;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.util.Queue;
+
+import org.apache.mina.core.buffer.IoBuffer;
+import org.apache.mina.core.session.DummySession;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.filter.codec.AbstractProtocolDecoderOutput;
+import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.http.api.HttpEndOfContent;
+import org.apache.mina.http.api.HttpResponse;
+import org.apache.mina.http.api.HttpStatus;
+import org.junit.Test;
+
+public class HttpClientDecoderTest {
+    private static final Charset ASCII = Charset.forName("US-ASCII");
+
+    private static final CharsetEncoder encoder = ASCII.newEncoder();
+
+    protected static class ProtocolDecoderQueue extends 
AbstractProtocolDecoderOutput {
+        public Queue<Object> getQueue() {
+            return this.messageQueue;
+        }
+    }
+
+    /**
+     * Feeds one network read into the decoder, calling it again while data
+     * remains, like ProtocolCodecFilter does.
+     */
+    private static void decode(ProtocolDecoder decoder, IoSession session, 
String data, ProtocolDecoderQueue out)
+            throws Exception {
+        IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
+        buffer.putString(data, encoder);
+        buffer.flip();
+
+        while (buffer.hasRemaining()) {
+            decoder.decode(session, buffer, out);
+        }
+    }
+
+    private static String asString(Object message) throws Exception {
+        return ((IoBuffer) message).getString(ASCII.newDecoder());
+    }
+
+    @Test
+    public void testResponseHeadSplitAcrossTwoReads() throws Exception {
+        ProtocolDecoder decoder = new HttpClientDecoder();
+        IoSession session = new DummySession();
+        ProtocolDecoderQueue out = new ProtocolDecoderQueue();
+
+        decode(decoder, session, "HTTP/1.1 200 OK\r\nContent-Le", out);
+        assertTrue("no message expected before the full head is received", 
out.getQueue().isEmpty());
+
+        decode(decoder, session, "ngth: 2\r\n\r\nAB", out);
+
+        assertEquals(3, out.getQueue().size());
+        HttpResponse response = (HttpResponse) out.getQueue().poll();
+        assertEquals(HttpStatus.SUCCESS_OK, response.getStatus());
+        assertEquals("2", response.getHeader("content-length"));
+        assertEquals("AB", asString(out.getQueue().poll()));
+        assertTrue(out.getQueue().poll() instanceof HttpEndOfContent);
+    }
+
+    @Test
+    public void testHeaderWithoutSpaceAfterColon() throws Exception {
+        ProtocolDecoder decoder = new HttpClientDecoder();
+        IoSession session = new DummySession();
+        ProtocolDecoderQueue out = new ProtocolDecoderQueue();
+
+        decode(decoder, session, "HTTP/1.1 200 
OK\r\nContent-Length:2\r\nX-Test:foo\r\n\r\nAB", out);
+
+        assertEquals(3, out.getQueue().size());
+        HttpResponse response = (HttpResponse) out.getQueue().poll();
+        assertEquals(HttpStatus.SUCCESS_OK, response.getStatus());
+        assertEquals("2", response.getHeader("content-length"));
+        assertEquals("foo", response.getHeader("x-test"));
+        assertEquals("AB", asString(out.getQueue().poll()));
+        assertTrue(out.getQueue().poll() instanceof HttpEndOfContent);
+    }
+
+    @Test
+    public void testChunkedBody() throws Exception {
+        ProtocolDecoder decoder = new HttpClientDecoder();
+        IoSession session = new DummySession();
+        ProtocolDecoderQueue out = new ProtocolDecoderQueue();
+
+        decode(decoder, session,
+                "HTTP/1.1 200 OK\r\nTransfer-Encoding: 
chunked\r\n\r\n3\r\nabc\r\n2\r\nde\r\n0\r\n\r\n", out);
+
+        assertEquals(4, out.getQueue().size());
+        HttpResponse response = (HttpResponse) out.getQueue().poll();
+        assertEquals(HttpStatus.SUCCESS_OK, response.getStatus());
+        assertEquals("abc", asString(out.getQueue().poll()));
+        assertEquals("de", asString(out.getQueue().poll()));
+        assertTrue("end of content must be signaled after the zero chunk",
+                out.getQueue().poll() instanceof HttpEndOfContent);
+    }
+
+    @Test
+    public void testChunkedBodySplitAcrossReads() throws Exception {
+        ProtocolDecoder decoder = new HttpClientDecoder();
+        IoSession session = new DummySession();
+        ProtocolDecoderQueue out = new ProtocolDecoderQueue();
+
+        decode(decoder, session, "HTTP/1.1 200 OK\r\nTransfer-Encoding: 
chunked\r\n\r\n3", out);
+        decode(decoder, session, "\r\nab", out);
+        decode(decoder, session, "c\r\n0\r\n\r\n", out);
+
+        assertEquals(4, out.getQueue().size());
+        HttpResponse response = (HttpResponse) out.getQueue().poll();
+        assertEquals(HttpStatus.SUCCESS_OK, response.getStatus());
+        assertEquals("ab", asString(out.getQueue().poll()));
+        assertEquals("c", asString(out.getQueue().poll()));
+        assertTrue(out.getQueue().poll() instanceof HttpEndOfContent);
+    }
+}

Reply via email to