hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-core git commit: BasicHttpRequest to handle absolute URIs passed as a path constructor argument
Date Fri, 08 Dec 2017 09:37:51 GMT
Repository: httpcomponents-core
Updated Branches:
  refs/heads/master 3ef097ca9 -> d4fbef3cc


BasicHttpRequest to handle absolute URIs passed as a path constructor argument


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

Branch: refs/heads/master
Commit: d4fbef3cca1bcd263cbd70cff47737de746fffe5
Parents: 3ef097c
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Fri Dec 8 10:26:47 2017 +0100
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Fri Dec 8 10:33:24 2017 +0100

----------------------------------------------------------------------
 .../hc/core5/http/message/BasicHttpRequest.java | 49 ++++++++++++--------
 .../core5/http/message/TestBasicMessages.java   | 10 ++++
 2 files changed, 40 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d4fbef3c/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
index 495b237..d530e84 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
@@ -60,7 +60,15 @@ public class BasicHttpRequest extends HeaderGroup implements HttpRequest
{
      * @param path request path.
      */
     public BasicHttpRequest(final String method, final String path) {
-        this(method, null, path);
+        super();
+        this.method = method;
+        if (path != null) {
+            try {
+                setUri(new URI(path));
+            } catch (final URISyntaxException ex) {
+                this.path = path;
+            }
+        }
     }
 
     /**
@@ -91,24 +99,7 @@ public class BasicHttpRequest extends HeaderGroup implements HttpRequest
{
     public BasicHttpRequest(final String method, final URI requestUri) {
         super();
         this.method = Args.notNull(method, "Method name");
-        Args.notNull(requestUri, "Request URI");
-        this.scheme = requestUri.getScheme();
-        this.authority = requestUri.getHost() != null ? new URIAuthority(
-                requestUri.getRawUserInfo(),
-                requestUri.getHost(),
-                requestUri.getPort()) : null;
-        final StringBuilder buf = new StringBuilder();
-        final String path = requestUri.getRawPath();
-        if (!TextUtils.isBlank(path)) {
-            buf.append(path);
-        } else {
-            buf.append("/");
-        }
-        final String query = requestUri.getRawQuery();
-        if (query != null) {
-            buf.append('?').append(query);
-        }
-        this.path = buf.toString();
+        setUri(Args.notNull(requestUri, "Request URI"));
     }
 
     @Override
@@ -176,6 +167,26 @@ public class BasicHttpRequest extends HeaderGroup implements HttpRequest
{
         return getPath();
     }
 
+    void setUri(final URI requestUri) {
+        this.scheme = requestUri.getScheme();
+        this.authority = requestUri.getHost() != null ? new URIAuthority(
+                requestUri.getRawUserInfo(),
+                requestUri.getHost(),
+                requestUri.getPort()) : null;
+        final StringBuilder buf = new StringBuilder();
+        final String path = requestUri.getRawPath();
+        if (!TextUtils.isBlank(path)) {
+            buf.append(path);
+        } else {
+            buf.append("/");
+        }
+        final String query = requestUri.getRawQuery();
+        if (query != null) {
+            buf.append('?').append(query);
+        }
+        this.path = buf.toString();
+    }
+
     @Override
     public URI getUri() throws URISyntaxException {
         if (this.requestUri == null) {

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d4fbef3c/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
index 5e1baab..49542cd 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
@@ -157,6 +157,16 @@ public class TestBasicMessages {
     }
 
     @Test
+    public void testRequestWithAbsoluteURIAsPath() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", "https://host:9443/stuff?param=value");
+        Assert.assertEquals("GET", request.getMethod());
+        Assert.assertEquals("/stuff?param=value", request.getPath());
+        Assert.assertEquals(new URIAuthority("host", 9443), request.getAuthority());
+        Assert.assertEquals("https", request.getScheme());
+        Assert.assertEquals(new URI("https://host:9443/stuff?param=value"), request.getUri());
+    }
+
+    @Test
     public void testRequestWithNoPath() throws Exception {
         final HttpRequest request = new BasicHttpRequest("GET", new URI("http://host"));
         Assert.assertEquals("GET", request.getMethod());


Mime
View raw message