hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1635959 [1/2] - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ httpcore/src/main/java/org/apache/http/impl/io/ httpcore/src/main/java/org/apache/http/message/ httpcore/src/test/java/org/apach...
Date Sat, 01 Nov 2014 14:40:08 GMT
Author: olegk
Date: Sat Nov  1 14:40:07 2014
New Revision: 1635959

URL: http://svn.apache.org/r1635959
Log:
Optimization and cleanup of message parsing and formatting code

Added:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/LazyLineParser.java
      - copied, changed from r1635958, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageParser.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParser.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParserFactory.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestWriter.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParser.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParserFactory.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseWriter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParserFactory.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestWriter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParserFactory.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeader.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicStatusLine.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BufferedHeader.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderValueFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderValueParser.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/LineFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/LineParser.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestMessageParser.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicLineParser.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderElement.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageParser.java Sat Nov  1 14:40:07 2014
@@ -39,7 +39,7 @@ import org.apache.http.ParseException;
 import org.apache.http.ProtocolException;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.config.MessageConstraints;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 import org.apache.http.nio.NHttpMessageParser;
 import org.apache.http.nio.reactor.SessionInputBuffer;
@@ -75,8 +75,8 @@ public abstract class AbstractMessagePar
      * Creates an instance of AbstractMessageParser.
      *
      * @param buffer the session input buffer.
-     * @param lineParser the line parser. If {@code null} {@link BasicLineParser#INSTANCE}
-     *   will be used.
+     * @param lineParser the line parser. If {@code null}
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used.
      * @param constraints Message constraints. If {@code null}
      *   {@link MessageConstraints#DEFAULT} will be used.
      *
@@ -88,7 +88,7 @@ public abstract class AbstractMessagePar
             final MessageConstraints constraints) {
         super();
         this.sessionBuffer = Args.notNull(buffer, "Session input buffer");
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
         this.headerBufs = new ArrayList<CharArrayBuffer>();
         this.state = READ_HEAD_LINE;

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java Sat Nov  1 14:40:07 2014
@@ -30,6 +30,7 @@ package org.apache.http.impl.nio.codecs;
 import java.io.IOException;
 import java.util.Iterator;
 
+import org.apache.http.FormattedHeader;
 import org.apache.http.Header;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMessage;
@@ -87,10 +88,16 @@ public abstract class AbstractMessageWri
     public void write(final T message) throws IOException, HttpException {
         Args.notNull(message, "HTTP message");
         writeHeadLine(message);
-        for (final Iterator<?> it = message.headerIterator(); it.hasNext(); ) {
-            final Header header = (Header) it.next();
-            this.sessionBuffer.writeLine
-                (lineFormatter.formatHeader(this.lineBuf, header));
+        for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
+            final Header header = it.next();
+            if (header instanceof FormattedHeader) {
+                final CharArrayBuffer buffer = ((FormattedHeader) header).getBuffer();
+                this.sessionBuffer.writeLine(buffer);
+            } else {
+                this.lineBuf.clear();
+                lineFormatter.formatHeader(this.lineBuf, header);
+                this.sessionBuffer.writeLine(this.lineBuf);
+            }
         }
         this.lineBuf.clear();
         this.sessionBuffer.writeLine(this.lineBuf);

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParser.java Sat Nov  1 14:40:07 2014
@@ -36,7 +36,6 @@ import org.apache.http.annotation.NotThr
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.impl.DefaultHttpRequestFactory;
 import org.apache.http.message.LineParser;
-import org.apache.http.message.ParserCursor;
 import org.apache.http.nio.reactor.SessionInputBuffer;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -56,7 +55,7 @@ public class DefaultHttpRequestParser ex
      *
      * @param buffer the session input buffer.
      * @param parser the line parser. If {@code null}
-     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used.
      * @param requestFactory the request factory. If {@code null}
      *   {@link DefaultHttpRequestFactory#INSTANCE} will be used.
      * @param constraints Message constraints. If {@code null}
@@ -90,8 +89,7 @@ public class DefaultHttpRequestParser ex
     @Override
     protected HttpRequest createMessage(final CharArrayBuffer buffer)
             throws HttpException, ParseException {
-        final ParserCursor cursor = new ParserCursor(0, buffer.length());
-        final RequestLine requestLine = lineParser.parseRequestLine(buffer, cursor);
+        final RequestLine requestLine = lineParser.parseRequestLine(buffer);
         return this.requestFactory.newHttpRequest(requestLine);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParserFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParserFactory.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParserFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestParserFactory.java Sat Nov  1 14:40:07 2014
@@ -32,7 +32,7 @@ import org.apache.http.HttpRequestFactor
 import org.apache.http.annotation.Immutable;
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.impl.DefaultHttpRequestFactory;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 import org.apache.http.nio.NHttpMessageParser;
 import org.apache.http.nio.NHttpMessageParserFactory;
@@ -54,7 +54,7 @@ public class DefaultHttpRequestParserFac
     public DefaultHttpRequestParserFactory(final LineParser lineParser,
             final HttpRequestFactory requestFactory) {
         super();
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.requestFactory = requestFactory != null ? requestFactory
                 : DefaultHttpRequestFactory.INSTANCE;
     }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestWriter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestWriter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestWriter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpRequestWriter.java Sat Nov  1 14:40:07 2014
@@ -33,7 +33,6 @@ import org.apache.http.HttpRequest;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.message.LineFormatter;
 import org.apache.http.nio.reactor.SessionOutputBuffer;
-import org.apache.http.util.CharArrayBuffer;
 
 /**
  * Default {@link org.apache.http.nio.NHttpMessageWriter} implementation
@@ -68,9 +67,9 @@ public class DefaultHttpRequestWriter ex
 
     @Override
     protected void writeHeadLine(final HttpRequest message) throws IOException {
-        final CharArrayBuffer buffer = lineFormatter.formatRequestLine(
-                this.lineBuf, message.getRequestLine());
-        this.sessionBuffer.writeLine(buffer);
+        this.lineBuf.clear();
+        this.lineFormatter.formatRequestLine(this.lineBuf, message.getRequestLine());
+        this.sessionBuffer.writeLine(this.lineBuf);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParser.java Sat Nov  1 14:40:07 2014
@@ -36,7 +36,6 @@ import org.apache.http.annotation.NotThr
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.message.LineParser;
-import org.apache.http.message.ParserCursor;
 import org.apache.http.nio.reactor.SessionInputBuffer;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -56,7 +55,7 @@ public class DefaultHttpResponseParser e
      *
      * @param buffer the session input buffer.
      * @param parser the line parser. If {@code null}
-     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used.
      * @param responseFactory the response factory. If {@code null}
      *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
      * @param constraints Message constraints. If {@code null}
@@ -91,8 +90,7 @@ public class DefaultHttpResponseParser e
     @Override
     protected HttpResponse createMessage(final CharArrayBuffer buffer)
             throws HttpException, ParseException {
-        final ParserCursor cursor = new ParserCursor(0, buffer.length());
-        final StatusLine statusline = lineParser.parseStatusLine(buffer, cursor);
+        final StatusLine statusline = lineParser.parseStatusLine(buffer);
         return this.responseFactory.newHttpResponse(statusline, null);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParserFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParserFactory.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParserFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseParserFactory.java Sat Nov  1 14:40:07 2014
@@ -32,7 +32,7 @@ import org.apache.http.HttpResponseFacto
 import org.apache.http.annotation.Immutable;
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 import org.apache.http.nio.NHttpMessageParser;
 import org.apache.http.nio.NHttpMessageParserFactory;
@@ -54,7 +54,7 @@ public class DefaultHttpResponseParserFa
     public DefaultHttpResponseParserFactory(final LineParser lineParser,
             final HttpResponseFactory responseFactory) {
         super();
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.responseFactory = responseFactory != null ? responseFactory
                 : DefaultHttpResponseFactory.INSTANCE;
     }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseWriter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseWriter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseWriter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/DefaultHttpResponseWriter.java Sat Nov  1 14:40:07 2014
@@ -33,7 +33,6 @@ import org.apache.http.HttpResponse;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.message.LineFormatter;
 import org.apache.http.nio.reactor.SessionOutputBuffer;
-import org.apache.http.util.CharArrayBuffer;
 
 /**
  * Default {@link org.apache.http.nio.NHttpMessageWriter} implementation
@@ -68,9 +67,9 @@ public class DefaultHttpResponseWriter e
 
     @Override
     protected void writeHeadLine(final HttpResponse message) throws IOException {
-        final CharArrayBuffer buffer = lineFormatter.formatStatusLine(
-                this.lineBuf, message.getStatusLine());
-        this.sessionBuffer.writeLine(buffer);
+        this.lineBuf.clear();
+        this.lineFormatter.formatStatusLine(this.lineBuf, message.getStatusLine());
+        this.sessionBuffer.writeLine(this.lineBuf);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java Sat Nov  1 14:40:07 2014
@@ -41,7 +41,7 @@ import org.apache.http.annotation.NotThr
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.io.HttpMessageParser;
 import org.apache.http.io.SessionInputBuffer;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
@@ -70,8 +70,8 @@ public abstract class AbstractMessagePar
      * Creates new instance of AbstractMessageParser.
      *
      * @param buffer the session input buffer.
-     * @param lineParser the line parser. If {@code null} {@link BasicLineParser#INSTANCE}
-     *   will be used.
+     * @param lineParser the line parser. If {@code null}
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used.
      * @param constraints the message constraints. If {@code null}
      *   {@link MessageConstraints#DEFAULT} will be used.
      *
@@ -83,7 +83,7 @@ public abstract class AbstractMessagePar
             final MessageConstraints constraints) {
         super();
         this.sessionBuffer = Args.notNull(buffer, "Session input buffer");
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.messageConstraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
         this.headerLines = new ArrayList<CharArrayBuffer>();
         this.state = HEAD_LINE;
@@ -102,8 +102,8 @@ public abstract class AbstractMessagePar
      *  including the continuation lines. Setting this parameter to a negative
      *  value or zero will disable the check.
      * @return array of HTTP headers
-     * @param parser line parser to use. Can be {@code null}, in which case
-     *  the default implementation of this interface will be used.
+     * @param lineParser the line parser. If {@code null}
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used
      *
      * @throws IOException in case of an I/O error
      * @throws HttpException in case of HTTP protocol violation
@@ -112,11 +112,10 @@ public abstract class AbstractMessagePar
             final SessionInputBuffer inbuffer,
             final int maxHeaderCount,
             final int maxLineLen,
-            final LineParser parser) throws HttpException, IOException {
+            final LineParser lineParser) throws HttpException, IOException {
         final List<CharArrayBuffer> headerLines = new ArrayList<CharArrayBuffer>();
         return parseHeaders(inbuffer, maxHeaderCount, maxLineLen,
-                parser != null ? parser : BasicLineParser.INSTANCE,
-                headerLines);
+                lineParser != null ? lineParser : LazyLineParser.INSTANCE, headerLines);
     }
 
     /**

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java Sat Nov  1 14:40:07 2014
@@ -30,6 +30,7 @@ package org.apache.http.impl.io;
 import java.io.IOException;
 import java.util.Iterator;
 
+import org.apache.http.FormattedHeader;
 import org.apache.http.Header;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMessage;
@@ -87,8 +88,14 @@ public abstract class AbstractMessageWri
         writeHeadLine(message);
         for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
             final Header header = it.next();
-            this.sessionBuffer.writeLine
-                (lineFormatter.formatHeader(this.lineBuf, header));
+            if (header instanceof FormattedHeader) {
+                final CharArrayBuffer buffer = ((FormattedHeader) header).getBuffer();
+                this.sessionBuffer.writeLine(buffer);
+            } else {
+                this.lineBuf.clear();
+                lineFormatter.formatHeader(this.lineBuf, header);
+                this.sessionBuffer.writeLine(this.lineBuf);
+            }
         }
         this.lineBuf.clear();
         this.sessionBuffer.writeLine(this.lineBuf);

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParser.java Sat Nov  1 14:40:07 2014
@@ -40,7 +40,6 @@ import org.apache.http.config.MessageCon
 import org.apache.http.impl.DefaultHttpRequestFactory;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.message.LineParser;
-import org.apache.http.message.ParserCursor;
 import org.apache.http.util.CharArrayBuffer;
 
 /**
@@ -60,7 +59,7 @@ public class DefaultHttpRequestParser ex
      *
      * @param buffer the session input buffer.
      * @param lineParser the line parser. If {@code null}
-     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used.
      * @param requestFactory the response factory. If {@code null}
      *   {@link DefaultHttpRequestFactory#INSTANCE} will be used.
      * @param constraints the message constraints. If {@code null}
@@ -105,8 +104,7 @@ public class DefaultHttpRequestParser ex
         if (i == -1) {
             throw new ConnectionClosedException("Client closed connection");
         }
-        final ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
-        final RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);
+        final RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf);
         return this.requestFactory.newHttpRequest(requestline);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParserFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParserFactory.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParserFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestParserFactory.java Sat Nov  1 14:40:07 2014
@@ -35,7 +35,7 @@ import org.apache.http.impl.DefaultHttpR
 import org.apache.http.io.HttpMessageParser;
 import org.apache.http.io.HttpMessageParserFactory;
 import org.apache.http.io.SessionInputBuffer;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 
 /**
@@ -54,7 +54,7 @@ public class DefaultHttpRequestParserFac
     public DefaultHttpRequestParserFactory(final LineParser lineParser,
             final HttpRequestFactory requestFactory) {
         super();
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.requestFactory = requestFactory != null ? requestFactory
                 : DefaultHttpRequestFactory.INSTANCE;
     }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestWriter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestWriter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestWriter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpRequestWriter.java Sat Nov  1 14:40:07 2014
@@ -62,7 +62,7 @@ public class DefaultHttpRequestWriter ex
 
     @Override
     protected void writeHeadLine(final HttpRequest message) throws IOException {
-        lineFormatter.formatRequestLine(this.lineBuf, message.getRequestLine());
+        this.lineFormatter.formatRequestLine(this.lineBuf, message.getRequestLine());
         this.sessionBuffer.writeLine(this.lineBuf);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParser.java Sat Nov  1 14:40:07 2014
@@ -40,7 +40,6 @@ import org.apache.http.config.MessageCon
 import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.message.LineParser;
-import org.apache.http.message.ParserCursor;
 import org.apache.http.util.CharArrayBuffer;
 
 /**
@@ -60,7 +59,7 @@ public class DefaultHttpResponseParser e
      *
      * @param buffer the session input buffer.
      * @param lineParser the line parser. If {@code null}
-     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used
+     *   {@link org.apache.http.message.LazyLineParser#INSTANCE} will be used
      * @param responseFactory the response factory. If {@code null}
      *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
      * @param constraints the message constraints. If {@code null}
@@ -106,8 +105,7 @@ public class DefaultHttpResponseParser e
             throw new NoHttpResponseException("The target server failed to respond");
         }
         //create the status line from the status string
-        final ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
-        final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
+        final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf);
         return this.responseFactory.newHttpResponse(statusline, null);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParserFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParserFactory.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParserFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/DefaultHttpResponseParserFactory.java Sat Nov  1 14:40:07 2014
@@ -35,7 +35,7 @@ import org.apache.http.impl.DefaultHttpR
 import org.apache.http.io.HttpMessageParser;
 import org.apache.http.io.HttpMessageParserFactory;
 import org.apache.http.io.SessionInputBuffer;
-import org.apache.http.message.BasicLineParser;
+import org.apache.http.message.LazyLineParser;
 import org.apache.http.message.LineParser;
 
 /**
@@ -54,7 +54,7 @@ public class DefaultHttpResponseParserFa
     public DefaultHttpResponseParserFactory(final LineParser lineParser,
             final HttpResponseFactory responseFactory) {
         super();
-        this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
+        this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
         this.responseFactory = responseFactory != null ? responseFactory
                 : DefaultHttpResponseFactory.INSTANCE;
     }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeader.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeader.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeader.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeader.java Sat Nov  1 14:40:07 2014
@@ -34,6 +34,7 @@ import org.apache.http.HeaderElement;
 import org.apache.http.ParseException;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.util.Args;
+import org.apache.http.util.CharArrayBuffer;
 
 /**
  * Basic implementation of {@link Header}.
@@ -72,15 +73,22 @@ public class BasicHeader implements Head
 
     @Override
     public String toString() {
-        // no need for non-default formatting in toString()
-        return BasicLineFormatter.INSTANCE.formatHeader(null, this).toString();
+        final StringBuilder buf = new StringBuilder();
+        buf.append(this.name).append(": ");
+        if (this.value != null) {
+            buf.append(this.value);
+        }
+        return buf.toString();
     }
 
     @Override
     public HeaderElement[] getElements() throws ParseException {
         if (this.value != null) {
             // result intentionally not cached, it's probably not used again
-            return BasicHeaderValueParser.parseElements(this.value, null);
+            final CharArrayBuffer buffer = new CharArrayBuffer(this.value.length());
+            buffer.append(value);
+            final ParserCursor cursor = new ParserCursor(0, this.value.length());
+            return BasicHeaderValueParser.INSTANCE.parseElements(buffer, cursor);
         } else {
             return new HeaderElement[] {};
         }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java Sat Nov  1 14:40:07 2014
@@ -34,9 +34,7 @@ import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
 /**
- * Basic implementation for formatting header value elements.
- * Instances of this class are stateless and thread-safe.
- * Derived classes are expected to maintain these properties.
+ * Default {@link org.apache.http.message.HeaderValueFormatter} implementation.
  *
  * @since 4.0
  */
@@ -45,209 +43,54 @@ public class BasicHeaderValueFormatter i
 
     public final static BasicHeaderValueFormatter INSTANCE = new BasicHeaderValueFormatter();
 
-    /**
-     * Special characters that can be used as separators in HTTP parameters.
-     * These special characters MUST be in a quoted string to be used within
-     * a parameter value .
-     */
-    public final static String SEPARATORS = " ;,:@()<>\\\"/[]?={}\t";
-
-    /**
-     * Unsafe special characters that must be escaped using the backslash
-     * character
-     */
-    public final static String UNSAFE_CHARS = "\"\\";
+    private final static String SEPARATORS = " ;,:@()<>\\\"/[]?={}\t";
+    private final static String UNSAFE_CHARS = "\"\\";
 
     public BasicHeaderValueFormatter() {
         super();
     }
 
-    /**
-     * Formats an array of header elements.
-     *
-     * @param elems     the header elements to format
-     * @param quote     {@code true} to always format with quoted values,
-     *                  {@code false} to use quotes only when necessary
-     * @param formatter         the formatter to use, or {@code null}
-     *                          for the {@link #INSTANCE default}
-     *
-     * @return  the formatted header elements
-     */
-    public static
-        String formatElements(final HeaderElement[] elems,
-                              final boolean quote,
-                              final HeaderValueFormatter formatter) {
-        return (formatter != null ? formatter : BasicHeaderValueFormatter.INSTANCE)
-                .formatElements(null, elems, quote).toString();
-    }
-
-
-    // non-javadoc, see interface HeaderValueFormatter
     @Override
-    public CharArrayBuffer formatElements(final CharArrayBuffer charBuffer,
-                                          final HeaderElement[] elems,
-                                          final boolean quote) {
+    public void formatElements(
+            final CharArrayBuffer buffer, final HeaderElement[] elems, final boolean quote) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(elems, "Header element array");
-        final int len = estimateElementsLen(elems);
-        CharArrayBuffer buffer = charBuffer;
-        if (buffer == null) {
-            buffer = new CharArrayBuffer(len);
-        } else {
-            buffer.ensureCapacity(len);
-        }
 
-        for (int i=0; i<elems.length; i++) {
+        for (int i = 0; i < elems.length; i++) {
             if (i > 0) {
                 buffer.append(", ");
             }
             formatHeaderElement(buffer, elems[i], quote);
         }
-
-        return buffer;
-    }
-
-
-    /**
-     * Estimates the length of formatted header elements.
-     *
-     * @param elems     the header elements to format, or {@code null}
-     *
-     * @return  a length estimate, in number of characters
-     */
-    protected int estimateElementsLen(final HeaderElement[] elems) {
-        if ((elems == null) || (elems.length < 1)) {
-            return 0;
-        }
-
-        int result = (elems.length-1) * 2; // elements separated by ", "
-        for (final HeaderElement elem : elems) {
-            result += estimateHeaderElementLen(elem);
-        }
-
-        return result;
-    }
-
-
-
-    /**
-     * Formats a header element.
-     *
-     * @param elem      the header element to format
-     * @param quote     {@code true} to always format with quoted values,
-     *                  {@code false} to use quotes only when necessary
-     * @param formatter         the formatter to use, or {@code null}
-     *                          for the {@link #INSTANCE default}
-     *
-     * @return  the formatted header element
-     */
-    public static
-        String formatHeaderElement(final HeaderElement elem,
-                                   final boolean quote,
-                                   final HeaderValueFormatter formatter) {
-        return (formatter != null ? formatter : BasicHeaderValueFormatter.INSTANCE)
-                .formatHeaderElement(null, elem, quote).toString();
     }
 
-
-    // non-javadoc, see interface HeaderValueFormatter
     @Override
-    public CharArrayBuffer formatHeaderElement(final CharArrayBuffer charBuffer,
-                                               final HeaderElement elem,
-                                               final boolean quote) {
+    public void formatHeaderElement(
+            final CharArrayBuffer buffer, final HeaderElement elem, final boolean quote) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(elem, "Header element");
-        final int len = estimateHeaderElementLen(elem);
-        CharArrayBuffer buffer = charBuffer;
-        if (buffer == null) {
-            buffer = new CharArrayBuffer(len);
-        } else {
-            buffer.ensureCapacity(len);
-        }
 
         buffer.append(elem.getName());
         final String value = elem.getValue();
         if (value != null) {
             buffer.append('=');
-            doFormatValue(buffer, value, quote);
+            formatValue(buffer, value, quote);
         }
 
-        final int parcnt = elem.getParameterCount();
-        if (parcnt > 0) {
-            for (int i=0; i<parcnt; i++) {
+        final int c = elem.getParameterCount();
+        if (c > 0) {
+            for (int i = 0; i < c; i++) {
                 buffer.append("; ");
                 formatNameValuePair(buffer, elem.getParameter(i), quote);
             }
         }
-
-        return buffer;
-    }
-
-
-    /**
-     * Estimates the length of a formatted header element.
-     *
-     * @param elem      the header element to format, or {@code null}
-     *
-     * @return  a length estimate, in number of characters
-     */
-    protected int estimateHeaderElementLen(final HeaderElement elem) {
-        if (elem == null) {
-            return 0;
-        }
-
-        int result = elem.getName().length(); // name
-        final String value = elem.getValue();
-        if (value != null) {
-            // assume quotes, but no escaped characters
-            result += 3 + value.length(); // ="value"
-        }
-
-        final int parcnt = elem.getParameterCount();
-        if (parcnt > 0) {
-            for (int i=0; i<parcnt; i++) {
-                result += 2 +                   // ; <param>
-                    estimateNameValuePairLen(elem.getParameter(i));
-            }
-        }
-
-        return result;
-    }
-
-
-
-
-    /**
-     * Formats a set of parameters.
-     *
-     * @param nvps      the parameters to format
-     * @param quote     {@code true} to always format with quoted values,
-     *                  {@code false} to use quotes only when necessary
-     * @param formatter         the formatter to use, or {@code null}
-     *                          for the {@link #INSTANCE default}
-     *
-     * @return  the formatted parameters
-     */
-    public static
-        String formatParameters(final NameValuePair[] nvps,
-                                final boolean quote,
-                                final HeaderValueFormatter formatter) {
-        return (formatter != null ? formatter : BasicHeaderValueFormatter.INSTANCE)
-                .formatParameters(null, nvps, quote).toString();
     }
 
-
-    // non-javadoc, see interface HeaderValueFormatter
     @Override
-    public CharArrayBuffer formatParameters(final CharArrayBuffer charBuffer,
-                                            final NameValuePair[] nvps,
-                                            final boolean quote) {
+    public void formatParameters(
+            final CharArrayBuffer buffer, final NameValuePair[] nvps, final boolean quote) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(nvps, "Header parameter array");
-        final int len = estimateParametersLen(nvps);
-        CharArrayBuffer buffer = charBuffer;
-        if (buffer == null) {
-            buffer = new CharArrayBuffer(len);
-        } else {
-            buffer.ensureCapacity(len);
-        }
 
         for (int i = 0; i < nvps.length; i++) {
             if (i > 0) {
@@ -255,112 +98,23 @@ public class BasicHeaderValueFormatter i
             }
             formatNameValuePair(buffer, nvps[i], quote);
         }
-
-        return buffer;
-    }
-
-
-    /**
-     * Estimates the length of formatted parameters.
-     *
-     * @param nvps      the parameters to format, or {@code null}
-     *
-     * @return  a length estimate, in number of characters
-     */
-    protected int estimateParametersLen(final NameValuePair[] nvps) {
-        if ((nvps == null) || (nvps.length < 1)) {
-            return 0;
-        }
-
-        int result = (nvps.length-1) * 2; // "; " between the parameters
-        for (final NameValuePair nvp : nvps) {
-            result += estimateNameValuePairLen(nvp);
-        }
-
-        return result;
-    }
-
-
-    /**
-     * Formats a name-value pair.
-     *
-     * @param nvp       the name-value pair to format
-     * @param quote     {@code true} to always format with a quoted value,
-     *                  {@code false} to use quotes only when necessary
-     * @param formatter         the formatter to use, or {@code null}
-     *                          for the {@link #INSTANCE default}
-     *
-     * @return  the formatted name-value pair
-     */
-    public static
-        String formatNameValuePair(final NameValuePair nvp,
-                                   final boolean quote,
-                                   final HeaderValueFormatter formatter) {
-        return (formatter != null ? formatter : BasicHeaderValueFormatter.INSTANCE)
-                .formatNameValuePair(null, nvp, quote).toString();
     }
 
-
-    // non-javadoc, see interface HeaderValueFormatter
     @Override
-    public CharArrayBuffer formatNameValuePair(final CharArrayBuffer charBuffer,
-                                               final NameValuePair nvp,
-                                               final boolean quote) {
+    public void formatNameValuePair(
+            final CharArrayBuffer buffer, final NameValuePair nvp, final boolean quote) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(nvp, "Name / value pair");
-        final int len = estimateNameValuePairLen(nvp);
-        CharArrayBuffer buffer = charBuffer;
-        if (buffer == null) {
-            buffer = new CharArrayBuffer(len);
-        } else {
-            buffer.ensureCapacity(len);
-        }
 
         buffer.append(nvp.getName());
         final String value = nvp.getValue();
         if (value != null) {
             buffer.append('=');
-            doFormatValue(buffer, value, quote);
-        }
-
-        return buffer;
-    }
-
-
-    /**
-     * Estimates the length of a formatted name-value pair.
-     *
-     * @param nvp       the name-value pair to format, or {@code null}
-     *
-     * @return  a length estimate, in number of characters
-     */
-    protected int estimateNameValuePairLen(final NameValuePair nvp) {
-        if (nvp == null) {
-            return 0;
+            formatValue(buffer, value, quote);
         }
-
-        int result = nvp.getName().length(); // name
-        final String value = nvp.getValue();
-        if (value != null) {
-            // assume quotes, but no escaped characters
-            result += 3 + value.length(); // ="value"
-        }
-        return result;
     }
 
-
-    /**
-     * Actually formats the value of a name-value pair.
-     * This does not include a leading = character.
-     * Called from {@link #formatNameValuePair formatNameValuePair}.
-     *
-     * @param buffer    the buffer to append to, never {@code null}
-     * @param value     the value to append, never {@code null}
-     * @param quote     {@code true} to always format with quotes,
-     *                  {@code false} to use quotes only when necessary
-     */
-    protected void doFormatValue(final CharArrayBuffer buffer,
-                                 final String value,
-                                 final boolean quote) {
+    void formatValue(final CharArrayBuffer buffer, final String value, final boolean quote) {
 
         boolean quoteFlag = quote;
         if (!quoteFlag) {
@@ -384,31 +138,12 @@ public class BasicHeaderValueFormatter i
         }
     }
 
-
-    /**
-     * Checks whether a character is a {@link #SEPARATORS separator}.
-     *
-     * @param ch        the character to check
-     *
-     * @return  {@code true} if the character is a separator,
-     *          {@code false} otherwise
-     */
-    protected boolean isSeparator(final char ch) {
+    boolean isSeparator(final char ch) {
         return SEPARATORS.indexOf(ch) >= 0;
     }
 
-
-    /**
-     * Checks whether a character is {@link #UNSAFE_CHARS unsafe}.
-     *
-     * @param ch        the character to check
-     *
-     * @return  {@code true} if the character is unsafe,
-     *          {@code false} otherwise
-     */
-    protected boolean isUnsafe(final char ch) {
+    boolean isUnsafe(final char ch) {
         return UNSAFE_CHARS.indexOf(ch) >= 0;
     }
 
-
-} // class BasicHeaderValueFormatter
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java Sat Nov  1 14:40:07 2014
@@ -33,15 +33,12 @@ import java.util.List;
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
-import org.apache.http.ParseException;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
 /**
- * Basic implementation for parsing header values into elements.
- * Instances of this class are stateless and thread-safe.
- * Derived classes are expected to maintain these properties.
+ * Default {@link org.apache.http.message.HeaderValueParser} implementation.
  *
  * @since 4.0
  */
@@ -64,32 +61,8 @@ public class BasicHeaderValueParser impl
         this.tokenParser = TokenParser.INSTANCE;
     }
 
-    /**
-     * Parses elements with the given parser.
-     *
-     * @param value     the header value to parse
-     * @param parser    the parser to use, or {@code null} for default
-     *
-     * @return  array holding the header elements, never {@code null}
-     * @throws ParseException in case of a parsing error
-     */
-    public static
-        HeaderElement[] parseElements(final String value,
-                                      final HeaderValueParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicHeaderValueParser.INSTANCE)
-            .parseElements(buffer, cursor);
-    }
-
-
-    // non-javadoc, see interface HeaderValueParser
     @Override
-    public HeaderElement[] parseElements(final CharArrayBuffer buffer,
-                                         final ParserCursor cursor) {
+    public HeaderElement[] parseElements(final CharArrayBuffer buffer, final ParserCursor cursor) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
         final List<HeaderElement> elements = new ArrayList<HeaderElement>();
@@ -102,32 +75,8 @@ public class BasicHeaderValueParser impl
         return elements.toArray(new HeaderElement[elements.size()]);
     }
 
-
-    /**
-     * Parses an element with the given parser.
-     *
-     * @param value     the header element to parse
-     * @param parser    the parser to use, or {@code null} for default
-     *
-     * @return  the parsed header element
-     */
-    public static
-        HeaderElement parseHeaderElement(final String value,
-                                         final HeaderValueParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicHeaderValueParser.INSTANCE)
-                .parseHeaderElement(buffer, cursor);
-    }
-
-
-    // non-javadoc, see interface HeaderValueParser
     @Override
-    public HeaderElement parseHeaderElement(final CharArrayBuffer buffer,
-                                            final ParserCursor cursor) {
+    public HeaderElement parseHeaderElement(final CharArrayBuffer buffer, final ParserCursor cursor) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
         final NameValuePair nvp = parseNameValuePair(buffer, cursor);
@@ -138,50 +87,11 @@ public class BasicHeaderValueParser impl
                 params = parseParameters(buffer, cursor);
             }
         }
-        return createHeaderElement(nvp.getName(), nvp.getValue(), params);
+        return new BasicHeaderElement(nvp.getName(), nvp.getValue(), params);
     }
 
-
-    /**
-     * Creates a header element.
-     * Called from {@link #parseHeaderElement}.
-     *
-     * @return  a header element representing the argument
-     */
-    protected HeaderElement createHeaderElement(
-            final String name,
-            final String value,
-            final NameValuePair[] params) {
-        return new BasicHeaderElement(name, value, params);
-    }
-
-
-    /**
-     * Parses parameters with the given parser.
-     *
-     * @param value     the parameter list to parse
-     * @param parser    the parser to use, or {@code null} for default
-     *
-     * @return  array holding the parameters, never {@code null}
-     */
-    public static
-        NameValuePair[] parseParameters(final String value,
-                                        final HeaderValueParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicHeaderValueParser.INSTANCE)
-                .parseParameters(buffer, cursor);
-    }
-
-
-
-    // non-javadoc, see interface HeaderValueParser
     @Override
-    public NameValuePair[] parseParameters(final CharArrayBuffer buffer,
-                                           final ParserCursor cursor) {
+    public NameValuePair[] parseParameters(final CharArrayBuffer buffer, final ParserCursor cursor) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
         tokenParser.skipWhiteSpace(buffer, cursor);
@@ -197,31 +107,8 @@ public class BasicHeaderValueParser impl
         return params.toArray(new NameValuePair[params.size()]);
     }
 
-    /**
-     * Parses a name-value-pair with the given parser.
-     *
-     * @param value     the NVP to parse
-     * @param parser    the parser to use, or {@code null} for default
-     *
-     * @return  the parsed name-value pair
-     */
-    public static
-       NameValuePair parseNameValuePair(final String value,
-                                        final HeaderValueParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicHeaderValueParser.INSTANCE)
-                .parseNameValuePair(buffer, cursor);
-    }
-
-
-    // non-javadoc, see interface HeaderValueParser
     @Override
-    public NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
-                                            final ParserCursor cursor) {
+    public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, final ParserCursor cursor) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
 
@@ -232,25 +119,12 @@ public class BasicHeaderValueParser impl
         final int delim = buffer.charAt(cursor.getPos());
         cursor.updatePos(cursor.getPos() + 1);
         if (delim != '=') {
-            return createNameValuePair(name, null);
+            return new BasicNameValuePair(name, null);
         }
         final String value = tokenParser.parseValue(buffer, cursor, VALUE_DELIMS);
         if (!cursor.atEnd()) {
             cursor.updatePos(cursor.getPos() + 1);
         }
-        return createNameValuePair(name, value);
-    }
-
-    /**
-     * Creates a name-value pair.
-     * Called from {@link #parseNameValuePair}.
-     *
-     * @param name      the name
-     * @param value     the value, or {@code null}
-     *
-     * @return  a name-value pair representing the arguments
-     */
-    protected NameValuePair createNameValuePair(final String name, final String value) {
         return new BasicNameValuePair(name, value);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineFormatter.java Sat Nov  1 14:40:07 2014
@@ -27,7 +27,6 @@
 
 package org.apache.http.message;
 
-import org.apache.http.FormattedHeader;
 import org.apache.http.Header;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.RequestLine;
@@ -37,14 +36,7 @@ import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
 /**
- * Interface for formatting elements of the HEAD section of an HTTP message.
- * This is the complement to {@link LineParser}.
- * There are individual methods for formatting a request line, a
- * status line, or a header line. The formatting does <i>not</i> include the
- * trailing line break sequence CR-LF.
- * The formatted lines are returned in memory, the formatter does not depend
- * on any specific IO mechanism.
- * Instances of this interface are expected to be stateless and thread-safe.
+ * Default {@link org.apache.http.message.LineFormatter} implementation.
  *
  * @since 4.0
  */
@@ -57,256 +49,51 @@ public class BasicLineFormatter implemen
         super();
     }
 
-    /**
-     * Obtains a buffer for formatting.
-     *
-     * @param charBuffer a buffer already available, or {@code null}
-     *
-     * @return  the cleared argument buffer if there is one, or
-     *          a new empty buffer that can be used for formatting
-     */
-    protected CharArrayBuffer initBuffer(final CharArrayBuffer charBuffer) {
-        CharArrayBuffer buffer = charBuffer;
-        if (buffer != null) {
-            buffer.clear();
-        } else {
-            buffer = new CharArrayBuffer(64);
-        }
-        return buffer;
-    }
-
-
-    /**
-     * Formats a protocol version.
-     *
-     * @param version           the protocol version to format
-     * @param formatter         the formatter to use, or
-     *                          {@code null} for the
-     *                          {@link #INSTANCE default}
-     *
-     * @return  the formatted protocol version
-     */
-    public static
-        String formatProtocolVersion(final ProtocolVersion version,
-                                     final LineFormatter formatter) {
-        return (formatter != null ? formatter : BasicLineFormatter.INSTANCE)
-                .appendProtocolVersion(null, version).toString();
-    }
-
-
-    // non-javadoc, see interface LineFormatter
-    @Override
-    public CharArrayBuffer appendProtocolVersion(final CharArrayBuffer buffer,
-                                                 final ProtocolVersion version) {
-        Args.notNull(version, "Protocol version");
-        // can't use initBuffer, that would clear the argument!
-        CharArrayBuffer result = buffer;
-        final int len = estimateProtocolVersionLen(version);
-        if (result == null) {
-            result = new CharArrayBuffer(len);
-        } else {
-            result.ensureCapacity(len);
-        }
-
-        result.append(version.getProtocol());
-        result.append('/');
-        result.append(Integer.toString(version.getMajor()));
-        result.append('.');
-        result.append(Integer.toString(version.getMinor()));
-
-        return result;
-    }
-
-
-    /**
-     * Guesses the length of a formatted protocol version.
-     * Needed to guess the length of a formatted request or status line.
-     *
-     * @param version   the protocol version to format, or {@code null}
-     *
-     * @return  the estimated length of the formatted protocol version,
-     *          in characters
-     */
-    protected int estimateProtocolVersionLen(final ProtocolVersion version) {
-        return version.getProtocol().length() + 4; // room for "HTTP/1.1"
-    }
-
-
-    /**
-     * Formats a request line.
-     *
-     * @param reqline           the request line to format
-     * @param formatter         the formatter to use, or
-     *                          {@code null} for the
-     *                          {@link #INSTANCE default}
-     *
-     * @return  the formatted request line
-     */
-    public static String formatRequestLine(final RequestLine reqline,
-                                           final LineFormatter formatter) {
-        return (formatter != null ? formatter : BasicLineFormatter.INSTANCE)
-                .formatRequestLine(null, reqline).toString();
+    void formatProtocolVersion(final CharArrayBuffer buffer, final ProtocolVersion version) {
+        buffer.append(version.getProtocol());
+        buffer.append('/');
+        buffer.append(Integer.toString(version.getMajor()));
+        buffer.append('.');
+        buffer.append(Integer.toString(version.getMinor()));
     }
 
-
-    // non-javadoc, see interface LineFormatter
     @Override
-    public CharArrayBuffer formatRequestLine(final CharArrayBuffer buffer,
-                                             final RequestLine reqline) {
+    public void formatRequestLine(final CharArrayBuffer buffer, final RequestLine reqline) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(reqline, "Request line");
-        final CharArrayBuffer result = initBuffer(buffer);
-        doFormatRequestLine(result, reqline);
-
-        return result;
-    }
-
-
-    /**
-     * Actually formats a request line.
-     * Called from {@link #formatRequestLine}.
-     *
-     * @param buffer    the empty buffer into which to format,
-     *                  never {@code null}
-     * @param reqline   the request line to format, never {@code null}
-     */
-    protected void doFormatRequestLine(final CharArrayBuffer buffer,
-                                       final RequestLine reqline) {
-        final String method = reqline.getMethod();
-        final String uri    = reqline.getUri();
-
-        // room for "GET /index.html HTTP/1.1"
-        final int len = method.length() + 1 + uri.length() + 1 +
-            estimateProtocolVersionLen(reqline.getProtocolVersion());
-        buffer.ensureCapacity(len);
-
-        buffer.append(method);
+        buffer.append(reqline.getMethod());
         buffer.append(' ');
-        buffer.append(uri);
+        buffer.append(reqline.getUri());
         buffer.append(' ');
-        appendProtocolVersion(buffer, reqline.getProtocolVersion());
-    }
-
-
-
-    /**
-     * Formats a status line.
-     *
-     * @param statline          the status line to format
-     * @param formatter         the formatter to use, or
-     *                          {@code null} for the
-     *                          {@link #INSTANCE default}
-     *
-     * @return  the formatted status line
-     */
-    public static String formatStatusLine(final StatusLine statline,
-                                          final LineFormatter formatter) {
-        return (formatter != null ? formatter : BasicLineFormatter.INSTANCE)
-                .formatStatusLine(null, statline).toString();
+        formatProtocolVersion(buffer, reqline.getProtocolVersion());
     }
 
-
-    // non-javadoc, see interface LineFormatter
     @Override
-    public CharArrayBuffer formatStatusLine(final CharArrayBuffer buffer,
-                                            final StatusLine statline) {
+    public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statline) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(statline, "Status line");
-        final CharArrayBuffer result = initBuffer(buffer);
-        doFormatStatusLine(result, statline);
-
-        return result;
-    }
 
-
-    /**
-     * Actually formats a status line.
-     * Called from {@link #formatStatusLine}.
-     *
-     * @param buffer    the empty buffer into which to format,
-     *                  never {@code null}
-     * @param statline  the status line to format, never {@code null}
-     */
-    protected void doFormatStatusLine(final CharArrayBuffer buffer,
-                                      final StatusLine statline) {
-
-        int len = estimateProtocolVersionLen(statline.getProtocolVersion())
-            + 1 + 3 + 1; // room for "HTTP/1.1 200 "
-        final String reason = statline.getReasonPhrase();
-        if (reason != null) {
-            len += reason.length();
-        }
-        buffer.ensureCapacity(len);
-
-        appendProtocolVersion(buffer, statline.getProtocolVersion());
+        formatProtocolVersion(buffer, statline.getProtocolVersion());
         buffer.append(' ');
         buffer.append(Integer.toString(statline.getStatusCode()));
         buffer.append(' '); // keep whitespace even if reason phrase is empty
-        if (reason != null) {
-            buffer.append(reason);
+        final String reasonPhrase = statline.getReasonPhrase();
+        if (reasonPhrase != null) {
+            buffer.append(reasonPhrase);
         }
     }
 
-
-    /**
-     * Formats a header.
-     *
-     * @param header            the header to format
-     * @param formatter         the formatter to use, or
-     *                          {@code null} for the
-     *                          {@link #INSTANCE default}
-     *
-     * @return  the formatted header
-     */
-    public static String formatHeader(final Header header,
-                                      final LineFormatter formatter) {
-        return (formatter != null ? formatter : BasicLineFormatter.INSTANCE)
-                .formatHeader(null, header).toString();
-    }
-
-
-    // non-javadoc, see interface LineFormatter
     @Override
-    public CharArrayBuffer formatHeader(final CharArrayBuffer buffer,
-                                        final Header header) {
+    public void formatHeader(final CharArrayBuffer buffer, final Header header) {
+        Args.notNull(buffer, "Char array buffer");
         Args.notNull(header, "Header");
-        final CharArrayBuffer result;
-
-        if (header instanceof FormattedHeader) {
-            // If the header is backed by a buffer, re-use the buffer
-            result = ((FormattedHeader)header).getBuffer();
-        } else {
-            result = initBuffer(buffer);
-            doFormatHeader(result, header);
-        }
-        return result;
-
-    } // formatHeader
-
-
-    /**
-     * Actually formats a header.
-     * Called from {@link #formatHeader}.
-     *
-     * @param buffer    the empty buffer into which to format,
-     *                  never {@code null}
-     * @param header    the header to format, never {@code null}
-     */
-    protected void doFormatHeader(final CharArrayBuffer buffer,
-                                  final Header header) {
-        final String name = header.getName();
-        final String value = header.getValue();
-
-        int len = name.length() + 2;
-        if (value != null) {
-            len += value.length();
-        }
-        buffer.ensureCapacity(len);
 
-        buffer.append(name);
+        buffer.append(header.getName());
         buffer.append(": ");
+        final String value = header.getValue();
         if (value != null) {
             buffer.append(value);
         }
     }
 
-
-} // class BasicLineFormatter
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineParser.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicLineParser.java Sat Nov  1 14:40:07 2014
@@ -27,6 +27,8 @@
 
 package org.apache.http.message;
 
+import java.util.BitSet;
+
 import org.apache.http.Header;
 import org.apache.http.HttpVersion;
 import org.apache.http.ParseException;
@@ -34,25 +36,12 @@ import org.apache.http.ProtocolVersion;
 import org.apache.http.RequestLine;
 import org.apache.http.StatusLine;
 import org.apache.http.annotation.Immutable;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
+import org.apache.http.util.TextUtils;
 
 /**
- * Basic parser for lines in the head section of an HTTP message.
- * There are individual methods for parsing a request line, a
- * status line, or a header line.
- * The lines to parse are passed in memory, the parser does not depend
- * on any specific IO mechanism.
- * Instances of this class are stateless and thread-safe.
- * Derived classes MUST maintain these properties.
- *
- * <p>
- * Note: This class was created by refactoring parsing code located in
- * various other classes. The author tags from those other classes have
- * been replicated here, although the association with the parsing code
- * taken from there has not been traced.
- * </p>
+ * Default {@link org.apache.http.message.LineParser} implementation.
  *
  * @since 4.0
  */
@@ -61,12 +50,18 @@ public class BasicLineParser implements 
 
     public final static BasicLineParser INSTANCE = new BasicLineParser();
 
+    // IMPORTANT!
+    // These private static variables must be treated as immutable and never exposed outside this class
+    private static final BitSet FULL_STOP = TokenParser.INIT_BITSET('.');
+    private static final BitSet BLANKS = TokenParser.INIT_BITSET(' ', '\t');
+    private static final BitSet COLON = TokenParser.INIT_BITSET(':');
+
     /**
      * A version of the protocol to parse.
      * The version is typically not relevant, but the protocol name.
      */
-    protected final ProtocolVersion protocol;
-
+    private final ProtocolVersion protocol;
+    private final TokenParser tokenParser;
 
     /**
      * Creates a new line parser for the given HTTP-like protocol.
@@ -77,9 +72,9 @@ public class BasicLineParser implements 
      */
     public BasicLineParser(final ProtocolVersion proto) {
         this.protocol = proto != null? proto : HttpVersion.HTTP_1_1;
+        this.tokenParser = TokenParser.INSTANCE;
     }
 
-
     /**
      * Creates a new line parser for HTTP.
      */
@@ -87,171 +82,56 @@ public class BasicLineParser implements 
         this(null);
     }
 
-
-    public static
-        ProtocolVersion parseProtocolVersion(final String value,
-                                             final LineParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicLineParser.INSTANCE)
-                .parseProtocolVersion(buffer, cursor);
-    }
-
-
-    // non-javadoc, see interface LineParser
-    @Override
-    public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer,
-                                                final ParserCursor cursor) throws ParseException {
-        Args.notNull(buffer, "Char array buffer");
-        Args.notNull(cursor, "Parser cursor");
+    ProtocolVersion parseProtocolVersion(
+            final CharArrayBuffer buffer,
+            final ParserCursor cursor) throws ParseException {
         final String protoname = this.protocol.getProtocol();
         final int protolength  = protoname.length();
 
-        final int indexFrom = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        skipWhitespace(buffer, cursor);
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
 
-        int i = cursor.getPos();
+        final int pos = cursor.getPos();
 
         // long enough for "HTTP/1.1"?
-        if (i + protolength + 4 > indexTo) {
-            throw new ParseException
-                ("Not a valid protocol version: " +
-                 buffer.substring(indexFrom, indexTo));
+        if (pos + protolength + 4 > cursor.getUpperBound()) {
+            throw new ParseException("Invalid protocol version: " + buffer.toString());
         }
 
         // check the protocol name and slash
         boolean ok = true;
-        for (int j=0; ok && (j<protolength); j++) {
-            ok = (buffer.charAt(i+j) == protoname.charAt(j));
+        for (int i = 0; ok && (i < protolength); i++) {
+            ok = buffer.charAt(pos + i) == protoname.charAt(i);
         }
         if (ok) {
-            ok = (buffer.charAt(i+protolength) == '/');
+            ok = buffer.charAt(pos + protolength) == '/';
         }
         if (!ok) {
-            throw new ParseException
-                ("Not a valid protocol version: " +
-                 buffer.substring(indexFrom, indexTo));
+            throw new ParseException("Invalid protocol version: " + buffer.toString());
         }
 
-        i += protolength+1;
-
-        final int period = buffer.indexOf('.', i, indexTo);
-        if (period == -1) {
-            throw new ParseException
-                ("Invalid protocol version number: " +
-                 buffer.substring(indexFrom, indexTo));
-        }
+        cursor.updatePos(pos + protolength + 1);
+
+        final String token1 = this.tokenParser.parseToken(buffer, cursor, FULL_STOP);
         final int major;
         try {
-            major = Integer.parseInt(buffer.substringTrimmed(i, period));
+            major = Integer.parseInt(token1);
         } catch (final NumberFormatException e) {
-            throw new ParseException
-                ("Invalid protocol major version number: " +
-                 buffer.substring(indexFrom, indexTo));
-        }
-        i = period + 1;
-
-        int blank = buffer.indexOf(' ', i, indexTo);
-        if (blank == -1) {
-            blank = indexTo;
+            throw new ParseException("Invalid protocol major version number: " + buffer.toString());
+        }
+        if (cursor.atEnd()) {
+            throw new ParseException("Invalid protocol version: " + buffer.toString());
         }
+        cursor.updatePos(cursor.getPos() + 1);
+        final String token2 = this.tokenParser.parseToken(buffer, cursor, BLANKS);
         final int minor;
         try {
-            minor = Integer.parseInt(buffer.substringTrimmed(i, blank));
+            minor = Integer.parseInt(token2);
         } catch (final NumberFormatException e) {
-            throw new ParseException(
-                "Invalid protocol minor version number: " +
-                buffer.substring(indexFrom, indexTo));
+            throw new ParseException("Invalid protocol minor version number: " + buffer.toString());
         }
-
-        cursor.updatePos(blank);
-
-        return createProtocolVersion(major, minor);
-
-    } // parseProtocolVersion
-
-
-    /**
-     * Creates a protocol version.
-     * Called from {@link #parseProtocolVersion}.
-     *
-     * @param major     the major version number, for example 1 in HTTP/1.0
-     * @param minor     the minor version number, for example 0 in HTTP/1.0
-     *
-     * @return  the protocol version
-     */
-    protected ProtocolVersion createProtocolVersion(final int major, final int minor) {
-        return protocol.forVersion(major, minor);
-    }
-
-
-
-    // non-javadoc, see interface LineParser
-    @Override
-    public boolean hasProtocolVersion(final CharArrayBuffer buffer,
-                                      final ParserCursor cursor) {
-        Args.notNull(buffer, "Char array buffer");
-        Args.notNull(cursor, "Parser cursor");
-        int index = cursor.getPos();
-
-        final String protoname = this.protocol.getProtocol();
-        final int  protolength = protoname.length();
-
-        if (buffer.length() < protolength+4)
-         {
-            return false; // not long enough for "HTTP/1.1"
-        }
-
-        if (index < 0) {
-            // end of line, no tolerance for trailing whitespace
-            // this works only for single-digit major and minor version
-            index = buffer.length() -4 -protolength;
-        } else if (index == 0) {
-            // beginning of line, tolerate leading whitespace
-            while ((index < buffer.length()) &&
-                    HTTP.isWhitespace(buffer.charAt(index))) {
-                 index++;
-             }
-        } // else within line, don't tolerate whitespace
-
-
-        if (index + protolength + 4 > buffer.length()) {
-            return false;
-        }
-
-
-        // just check protocol name and slash, no need to analyse the version
-        boolean ok = true;
-        for (int j=0; ok && (j<protolength); j++) {
-            ok = (buffer.charAt(index+j) == protoname.charAt(j));
-        }
-        if (ok) {
-            ok = (buffer.charAt(index+protolength) == '/');
-        }
-
-        return ok;
-    }
-
-
-
-    public static
-        RequestLine parseRequestLine(final String value,
-                                     final LineParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicLineParser.INSTANCE)
-            .parseRequestLine(buffer, cursor);
+        return new HttpVersion(major, minor);
     }
 
-
     /**
      * Parses a request line.
      *
@@ -262,189 +142,64 @@ public class BasicLineParser implements 
      * @throws ParseException        in case of a parse error
      */
     @Override
-    public RequestLine parseRequestLine(final CharArrayBuffer buffer,
-                                        final ParserCursor cursor) throws ParseException {
-
+    public RequestLine parseRequestLine(final CharArrayBuffer buffer) throws ParseException {
         Args.notNull(buffer, "Char array buffer");
-        Args.notNull(cursor, "Parser cursor");
-        final int indexFrom = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        try {
-            skipWhitespace(buffer, cursor);
-            int i = cursor.getPos();
-
-            int blank = buffer.indexOf(' ', i, indexTo);
-            if (blank < 0) {
-                throw new ParseException("Invalid request line: " +
-                        buffer.substring(indexFrom, indexTo));
-            }
-            final String method = buffer.substringTrimmed(i, blank);
-            cursor.updatePos(blank);
-
-            skipWhitespace(buffer, cursor);
-            i = cursor.getPos();
 
-            blank = buffer.indexOf(' ', i, indexTo);
-            if (blank < 0) {
-                throw new ParseException("Invalid request line: " +
-                        buffer.substring(indexFrom, indexTo));
-            }
-            final String uri = buffer.substringTrimmed(i, blank);
-            cursor.updatePos(blank);
-
-            final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
-
-            skipWhitespace(buffer, cursor);
-            if (!cursor.atEnd()) {
-                throw new ParseException("Invalid request line: " +
-                        buffer.substring(indexFrom, indexTo));
-            }
-
-            return createRequestLine(method, uri, ver);
-        } catch (final IndexOutOfBoundsException e) {
-            throw new ParseException("Invalid request line: " +
-                                     buffer.substring(indexFrom, indexTo));
+        final ParserCursor cursor = new ParserCursor(0, buffer.length());
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        final String method = this.tokenParser.parseToken(buffer, cursor, BLANKS);
+        if (TextUtils.isEmpty(method)) {
+            throw new ParseException("Invalid request line: " + buffer.toString());
+        }
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        final String uri = this.tokenParser.parseToken(buffer, cursor, BLANKS);
+        if (TextUtils.isEmpty(uri)) {
+            throw new ParseException("Invalid request line: " + buffer.toString());
+        }
+        final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        if (!cursor.atEnd()) {
+            throw new ParseException("Invalid request line: " + buffer.toString());
         }
-    } // parseRequestLine
-
-
-    /**
-     * Instantiates a new request line.
-     * Called from {@link #parseRequestLine}.
-     *
-     * @param method    the request method
-     * @param uri       the requested URI
-     * @param ver       the protocol version
-     *
-     * @return  a new status line with the given data
-     */
-    protected RequestLine createRequestLine(final String method,
-                                            final String uri,
-                                            final ProtocolVersion ver) {
         return new BasicRequestLine(method, uri, ver);
     }
 
-
-
-    public static
-        StatusLine parseStatusLine(final String value,
-                                   final LineParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        final ParserCursor cursor = new ParserCursor(0, value.length());
-        return (parser != null ? parser : BasicLineParser.INSTANCE)
-                .parseStatusLine(buffer, cursor);
-    }
-
-
-    // non-javadoc, see interface LineParser
     @Override
-    public StatusLine parseStatusLine(final CharArrayBuffer buffer,
-                                      final ParserCursor cursor) throws ParseException {
+    public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException {
         Args.notNull(buffer, "Char array buffer");
-        Args.notNull(cursor, "Parser cursor");
-        final int indexFrom = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        try {
-            // handle the HTTP-Version
-            final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
 
-            // handle the Status-Code
-            skipWhitespace(buffer, cursor);
-            int i = cursor.getPos();
-
-            int blank = buffer.indexOf(' ', i, indexTo);
-            if (blank < 0) {
-                blank = indexTo;
+        final ParserCursor cursor = new ParserCursor(0, buffer.length());
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        final String s = this.tokenParser.parseToken(buffer, cursor, BLANKS);
+        for (int i = 0; i < s.length(); i++) {
+            if (!Character.isDigit(s.charAt(i))) {
+                throw new ParseException("Status line contains invalid status code: " + buffer.toString());
             }
-            final int statusCode;
-            final String s = buffer.substringTrimmed(i, blank);
-            for (int j = 0; j < s.length(); j++) {
-                if (!Character.isDigit(s.charAt(j))) {
-                    throw new ParseException(
-                            "Status line contains invalid status code: "
-                            + buffer.substring(indexFrom, indexTo));
-                }
-            }
-            try {
-                statusCode = Integer.parseInt(s);
-            } catch (final NumberFormatException e) {
-                throw new ParseException(
-                        "Status line contains invalid status code: "
-                        + buffer.substring(indexFrom, indexTo));
-            }
-            //handle the Reason-Phrase
-            i = blank;
-            final String reasonPhrase;
-            if (i < indexTo) {
-                reasonPhrase = buffer.substringTrimmed(i, indexTo);
-            } else {
-                reasonPhrase = "";
-            }
-            return createStatusLine(ver, statusCode, reasonPhrase);
-
-        } catch (final IndexOutOfBoundsException e) {
-            throw new ParseException("Invalid status line: " +
-                                     buffer.substring(indexFrom, indexTo));
         }
-    } // parseStatusLine
-
-
-    /**
-     * Instantiates a new status line.
-     * Called from {@link #parseStatusLine}.
-     *
-     * @param ver       the protocol version
-     * @param status    the status code
-     * @param reason    the reason phrase
-     *
-     * @return  a new status line with the given data
-     */
-    protected StatusLine createStatusLine(final ProtocolVersion ver,
-                                          final int status,
-                                          final String reason) {
-        return new BasicStatusLine(ver, status, reason);
-    }
-
-
-
-    public static
-        Header parseHeader(final String value,
-                           final LineParser parser) throws ParseException {
-        Args.notNull(value, "Value");
-
-        final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
-        buffer.append(value);
-        return (parser != null ? parser : BasicLineParser.INSTANCE)
-                .parseHeader(buffer);
+        final int statusCode;
+        try {
+            statusCode = Integer.parseInt(s);
+        } catch (final NumberFormatException e) {
+            throw new ParseException("Status line contains invalid status code: " + buffer.toString());
+        }
+        final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound());
+        return new BasicStatusLine(ver, statusCode, text);
     }
 
-
-    // non-javadoc, see interface LineParser
     @Override
-    public Header parseHeader(final CharArrayBuffer buffer)
-        throws ParseException {
-
-        // the actual parser code is in the constructor of BufferedHeader
-        return new BufferedHeader(buffer);
-    }
-
+    public Header parseHeader(final CharArrayBuffer buffer) throws ParseException {
+        Args.notNull(buffer, "Char array buffer");
 
-    /**
-     * Helper to skip whitespace.
-     */
-    protected void skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor) {
-        int pos = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-        while ((pos < indexTo) &&
-               HTTP.isWhitespace(buffer.charAt(pos))) {
-            pos++;
+        final ParserCursor cursor = new ParserCursor(0, buffer.length());
+        this.tokenParser.skipWhiteSpace(buffer, cursor);
+        final String name = this.tokenParser.parseToken(buffer, cursor, COLON);
+        if (TextUtils.isEmpty(name) || cursor.atEnd()) {
+            throw new ParseException("Invalid header: " + buffer.toString());
         }
-        cursor.updatePos(pos);
+        final String value = buffer.substringTrimmed(cursor.getPos() + 1, cursor.getUpperBound());
+        return new BasicHeader(name, value);
     }
 
-} // class BasicLineParser
+}

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java?rev=1635959&r1=1635958&r2=1635959&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicRequestLine.java Sat Nov  1 14:40:07 2014
@@ -74,8 +74,9 @@ public class BasicRequestLine implements
 
     @Override
     public String toString() {
-        // no need for non-default formatting in toString()
-        return BasicLineFormatter.INSTANCE.formatRequestLine(null, this).toString();
+        final StringBuilder buf = new StringBuilder();
+        buf.append(this.method).append(" ").append(this.uri).append(" ").append(this.protoversion);
+        return buf.toString();
     }
 
     @Override



Mime
View raw message