You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/10/20 12:27:49 UTC

httpcomponents-core git commit: HTTPCORE-495: add context details to ParseException [Forced Update!]

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master abfca7ed5 -> d5021cc65 (forced update)


HTTPCORE-495: add context details to ParseException


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/d5021cc6
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/d5021cc6
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/d5021cc6

Branch: refs/heads/master
Commit: d5021cc65e052135600c4c7b7b111db2d583b240
Parents: e948fb1
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Fri Oct 20 13:36:42 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Fri Oct 20 14:27:21 2017 +0200

----------------------------------------------------------------------
 .../apache/hc/core5/http/ParseException.java    | 32 +++++++++++++++++++
 .../hc/core5/http/message/BasicLineParser.java  | 33 +++++++++++++-------
 .../hc/core5/http/message/BufferedHeader.java   |  6 ++--
 3 files changed, 57 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d5021cc6/httpcore5/src/main/java/org/apache/hc/core5/http/ParseException.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/ParseException.java b/httpcore5/src/main/java/org/apache/hc/core5/http/ParseException.java
index 3f78759..1f44a7d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/ParseException.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/ParseException.java
@@ -36,11 +36,14 @@ public class ParseException extends ProtocolException {
 
     private static final long serialVersionUID = -7288819855864183578L;
 
+    private final int errorOffset;
+
     /**
      * Creates a {@link ParseException} without details.
      */
     public ParseException() {
         super();
+        this.errorOffset = -1;
     }
 
     /**
@@ -50,6 +53,35 @@ public class ParseException extends ProtocolException {
      */
     public ParseException(final String message) {
         super(message);
+        this.errorOffset = -1;
+    }
+
+    /**
+     * Creates a {@link ParseException} with parsing context details.
+     *
+     * @since 5.0
+     */
+    public ParseException(final String description, final CharSequence text, final int off, final int len, final int errorOffset) {
+        super(description +
+                (errorOffset >= 0 ? "; error at offset " + errorOffset : "") +
+                (text != null && len < 1024 ? ": <" + text.subSequence(off, off + len) + ">" : ""));
+        this.errorOffset = errorOffset;
+    }
+
+    /**
+     * Creates a {@link ParseException} with parsing context details.
+     *
+     * @since 5.0
+     */
+    public ParseException(final String description, final CharSequence text, final int off, final int len) {
+        this(description, text, off, len, -1);
+    }
+
+    /**
+     * @since 5.0
+     */
+    public int getErrorOffset() {
+        return errorOffset;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d5021cc6/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicLineParser.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicLineParser.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicLineParser.java
index 2810d8d..69a087f 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicLineParser.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicLineParser.java
@@ -93,7 +93,8 @@ public class BasicLineParser implements LineParser {
 
         // long enough for "HTTP/1.1"?
         if (pos + protolength + 4 > cursor.getUpperBound()) {
-            throw new ParseException("Invalid protocol version: " + buffer.toString());
+            throw new ParseException("Invalid protocol version",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
 
         // check the protocol name and slash
@@ -105,7 +106,8 @@ public class BasicLineParser implements LineParser {
             ok = buffer.charAt(pos + protolength) == '/';
         }
         if (!ok) {
-            throw new ParseException("Invalid protocol version: " + buffer.toString());
+            throw new ParseException("Invalid protocol version",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
 
         cursor.updatePos(pos + protolength + 1);
@@ -115,10 +117,12 @@ public class BasicLineParser implements LineParser {
         try {
             major = Integer.parseInt(token1);
         } catch (final NumberFormatException e) {
-            throw new ParseException("Invalid protocol major version number: " + buffer.toString());
+            throw new ParseException("Invalid protocol major version number",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         if (cursor.atEnd()) {
-            throw new ParseException("Invalid protocol version: " + buffer.toString());
+            throw new ParseException("Invalid protocol version",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         cursor.updatePos(cursor.getPos() + 1);
         final String token2 = this.tokenParser.parseToken(buffer, cursor, BLANKS);
@@ -126,7 +130,8 @@ public class BasicLineParser implements LineParser {
         try {
             minor = Integer.parseInt(token2);
         } catch (final NumberFormatException e) {
-            throw new ParseException("Invalid protocol minor version number: " + buffer.toString());
+            throw new ParseException("Invalid protocol minor version number",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         return new HttpVersion(major, minor);
     }
@@ -148,17 +153,20 @@ public class BasicLineParser implements LineParser {
         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());
+            throw new ParseException("Invalid request line",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         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());
+            throw new ParseException("Invalid request line",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
         this.tokenParser.skipWhiteSpace(buffer, cursor);
         if (!cursor.atEnd()) {
-            throw new ParseException("Invalid request line: " + buffer.toString());
+            throw new ParseException("Invalid request line",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         return new RequestLine(method, uri, ver);
     }
@@ -174,14 +182,16 @@ public class BasicLineParser implements LineParser {
         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());
+                throw new ParseException("Status line contains invalid status code",
+                        buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
             }
         }
         final int statusCode;
         try {
             statusCode = Integer.parseInt(s);
         } catch (final NumberFormatException e) {
-            throw new ParseException("Status line contains invalid status code: " + buffer.toString());
+            throw new ParseException("Status line contains invalid status code",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound());
         return new StatusLine(ver, statusCode, text);
@@ -198,7 +208,8 @@ public class BasicLineParser implements LineParser {
                 buffer.charAt(cursor.getPos()) != ':' ||
                 TextUtils.isEmpty(name) ||
                 TokenParser.isWhitespace(buffer.charAt(cursor.getPos() - 1))) {
-            throw new ParseException("Invalid header: " + buffer.toString());
+            throw new ParseException("Invalid header",
+                    buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
         }
         final String value = buffer.substringTrimmed(cursor.getPos() + 1, cursor.getUpperBound());
         return new BasicHeader(name, value);

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d5021cc6/httpcore5/src/main/java/org/apache/hc/core5/http/message/BufferedHeader.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BufferedHeader.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BufferedHeader.java
index 65fb363..10099b9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BufferedHeader.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BufferedHeader.java
@@ -88,14 +88,14 @@ public class BufferedHeader implements FormattedHeader, Serializable {
         Args.notNull(buffer, "Char array buffer");
         final int colon = buffer.indexOf(':');
         if (colon <= 0) {
-            throw new ParseException("Invalid header: " + buffer.toString());
+            throw new ParseException("Invalid header", buffer, 0, buffer.length());
         }
         if (strict && TokenParser.isWhitespace(buffer.charAt(colon - 1))) {
-            throw new ParseException("Invalid header: " + buffer.toString());
+            throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1);
         }
         final String s = buffer.substringTrimmed(0, colon);
         if (s.length() == 0) {
-            throw new ParseException("Invalid header: " + buffer.toString());
+            throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon);
         }
         this.buffer = buffer;
         this.name = s;