http-client-branch: review comment: small fixes to BufferingSubscriber, and add a User-Agent header http-client-branch
authorchegar
Wed, 06 Dec 2017 12:05:38 +0000
branchhttp-client-branch
changeset 55959 7fd1a84f4fae
parent 55958 24e637188a68
child 55960 7553e90ba43c
child 55963 ae2750657f98
http-client-branch: review comment: small fixes to BufferingSubscriber, and add a User-Agent header
src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/BufferingSubscriber.java
src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpRequestImpl.java
--- a/src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/BufferingSubscriber.java	Wed Dec 06 10:33:27 2017 +0000
+++ b/src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/BufferingSubscriber.java	Wed Dec 06 12:05:38 2017 +0000
@@ -89,8 +89,8 @@
     }
 
     /** Returns the number of bytes remaining in the given buffers. */
-    private static final int remaining(List<ByteBuffer> buffers) {
-        return buffers.stream().mapToInt(ByteBuffer::remaining).sum();
+    private static final long remaining(List<ByteBuffer> buffers) {
+        return buffers.stream().mapToLong(ByteBuffer::remaining).sum();
     }
 
     /**
@@ -190,8 +190,8 @@
                 try {
                     Throwable t = throwable;
                     if (t != null) {
+                        pushDemandedScheduler.stop(); // stop the demand scheduler
                         downstreamSubscriber.onError(t);
-                        pushDemandedScheduler.stop(); // stop the demand scheduler
                         return;
                     }
 
@@ -219,8 +219,9 @@
                         complete = state == COMPLETE && internalBuffers.isEmpty();
                     }
                     if (complete) {
+                        assert internalBuffers.isEmpty();
+                        pushDemandedScheduler.stop(); // stop the demand scheduler
                         downstreamSubscriber.onComplete();
-                        pushDemandedScheduler.stop(); // stop the demand scheduler
                         return;
                     }
                 } catch (Throwable t) {
--- a/src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpRequestImpl.java	Wed Dec 06 10:33:27 2017 +0000
+++ b/src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpRequestImpl.java	Wed Dec 06 12:05:38 2017 +0000
@@ -59,6 +59,15 @@
     private final Duration timeout;  // may be null
     private final Optional<HttpClient.Version> version;
 
+    private static String userAgent() {
+        PrivilegedAction<String> pa = () -> System.getProperty("java.version");
+        String version = AccessController.doPrivileged(pa);
+        return "Java-http-client/" + version;
+    }
+
+    /** The value of the User-Agent header for all requests sent by the client. */
+    public static final String USER_AGENT = userAgent();
+
     /**
      * Creates an HttpRequestImpl from the given builder.
      */
@@ -90,6 +99,7 @@
         } else {
             this.systemHeaders = new HttpHeadersImpl();
         }
+        this.systemHeaders.setHeader("User-Agent", USER_AGENT);
         this.uri = request.uri();
         if (isWebSocket) {
             // WebSocket determines and sets the proxy itself