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/08/01 17:28:50 UTC

[1/3] httpcomponents-client git commit: [HTTPASYNC-124] Add doPrivileged blocks to async client and connection manager builders. [Forced Update!]

Repository: httpcomponents-client
Updated Branches:
  refs/heads/master 888cefb7c -> 9efcba873 (forced update)


[HTTPASYNC-124] Add doPrivileged blocks to async client and connection
manager builders.


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

Branch: refs/heads/master
Commit: fe6b90a8c622adbc8469d15730be4a1b9a622c4a
Parents: 643ea6c
Author: Jay Modi <jay at elastic dot co>
Authored: Thu Jul 20 20:52:39 2017 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Tue Aug 1 19:12:02 2017 +0200

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |  3 +++
 .../http/impl/async/HttpAsyncClientBuilder.java | 23 +++++++++++++++++---
 ...lingAsyncClientConnectionManagerBuilder.java | 22 +++++++++++++++----
 3 files changed, 41 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fe6b90a8/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index a0dd7d4..0fe9886 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -6,7 +6,10 @@
   Contributed by Gary Gregory <ggregory at apache.org>
 
 * [HTTPCLIENT-1858] Alleviate GC pressure due to wire logging.
+  Contributed by Gary Gregory <ggregory at apache.org>
 
+* [HTTPASYNC-124] Add doPrivileged blocks to async client and connection manager builders
+  Contributed by Jay Modi <jay at elastic dot co>
 
 Release 5.0-ALPHA2
 -------------------

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fe6b90a8/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
index 0a7e02d..6884849 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
@@ -30,6 +30,8 @@ package org.apache.hc.client5.http.impl.async;
 import java.io.Closeable;
 import java.io.IOException;
 import java.net.ProxySelector;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -749,7 +751,7 @@ public class HttpAsyncClientBuilder {
         String userAgentCopy = this.userAgent;
         if (userAgentCopy == null) {
             if (systemProperties) {
-                userAgentCopy = System.getProperty("http.agent");
+                userAgentCopy = getProperty("http.agent", null);
             }
             if (userAgentCopy == null) {
                 userAgentCopy = VersionInfo.getSoftwareInfo("Apache-HttpAsyncClient",
@@ -831,8 +833,14 @@ public class HttpAsyncClientBuilder {
             if (proxy != null) {
                 routePlannerCopy = new DefaultProxyRoutePlanner(proxy, schemePortResolverCopy);
             } else if (systemProperties) {
+                final ProxySelector defaultProxySelector = AccessController.doPrivileged(new PrivilegedAction<ProxySelector>() {
+                    @Override
+                    public ProxySelector run() {
+                        return ProxySelector.getDefault();
+                    }
+                });
                 routePlannerCopy = new SystemDefaultRoutePlanner(
-                        schemePortResolverCopy, ProxySelector.getDefault());
+                        schemePortResolverCopy, defaultProxySelector);
             } else {
                 routePlannerCopy = new DefaultRoutePlanner(schemePortResolverCopy);
             }
@@ -874,7 +882,7 @@ public class HttpAsyncClientBuilder {
         ConnectionReuseStrategy reuseStrategyCopy = this.reuseStrategy;
         if (reuseStrategyCopy == null) {
             if (systemProperties) {
-                final String s = System.getProperty("http.keepAlive", "true");
+                final String s = getProperty("http.keepAlive", "true");
                 if ("true".equalsIgnoreCase(s)) {
                     reuseStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE;
                 } else {
@@ -998,4 +1006,13 @@ public class HttpAsyncClientBuilder {
                 closeablesCopy);
     }
 
+    private String getProperty(final String key, final String defaultValue) {
+        return AccessController.doPrivileged(new PrivilegedAction<String>() {
+            @Override
+            public String run() {
+                return System.getProperty(key, defaultValue);
+            }
+        });
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fe6b90a8/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManagerBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManagerBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManagerBuilder.java
index cce713d..2af512c 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManagerBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManagerBuilder.java
@@ -37,6 +37,9 @@ import org.apache.hc.core5.pool.ConnPoolListener;
 import org.apache.hc.core5.pool.ConnPoolPolicy;
 import org.apache.hc.core5.util.TimeValue;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
 /**
  * Builder for {@link PoolingAsyncClientConnectionManager} instances.
  * <p>
@@ -176,10 +179,7 @@ public class PoolingAsyncClientConnectionManagerBuilder {
         @SuppressWarnings("resource")
         final PoolingAsyncClientConnectionManager poolingmgr = new PoolingAsyncClientConnectionManager(
                 RegistryBuilder.<TlsStrategy>create()
-                        .register("https", tlsStrategy != null ? tlsStrategy :
-                                (systemProperties ?
-                                        H2TlsStrategy.getSystemDefault() :
-                                        H2TlsStrategy.getDefault()))
+                        .register("https", getTlsStrategy())
                         .build(),
                 schemePortResolver,
                 dnsResolver,
@@ -196,4 +196,18 @@ public class PoolingAsyncClientConnectionManagerBuilder {
         return poolingmgr;
     }
 
+    private TlsStrategy getTlsStrategy() {
+        if (tlsStrategy != null) {
+            return tlsStrategy;
+        } else if (systemProperties) {
+            return AccessController.doPrivileged(new PrivilegedAction<TlsStrategy>() {
+                @Override
+                public TlsStrategy run() {
+                    return H2TlsStrategy.getSystemDefault();
+                }
+            });
+        } else {
+            return H2TlsStrategy.getDefault();
+        }
+    }
 }


[2/3] httpcomponents-client git commit: [HTTPCLIENT-1858] Clone some code from Log4j 2 to cache a StringBuilder in a ThreadLocal. Update to use the StringBuilder's capacity instead of its length to measure upper bound.

Posted by ol...@apache.org.
[HTTPCLIENT-1858] Clone some code from Log4j 2 to cache a StringBuilder
in a ThreadLocal. Update to use the StringBuilder's capacity instead of
its length to measure upper bound.

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

Branch: refs/heads/master
Commit: f2146cab622b63ff69a005040635c14fed509e54
Parents: fe6b90a
Author: Gary Gregory <gg...@apache.org>
Authored: Tue Jul 25 15:25:20 2017 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Tue Aug 1 19:12:15 2017 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/hc/client5/http/impl/logging/Wire.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f2146cab/httpclient5/src/main/java/org/apache/hc/client5/http/impl/logging/Wire.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/logging/Wire.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/logging/Wire.java
index e6b09b1..8bd9e4f 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/logging/Wire.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/logging/Wire.java
@@ -64,7 +64,7 @@ class Wire {
      */
     // TODO Delete wheb Log4j's 2.9 (see #trimToMaxSize(StringBuild))
     private static void trimToMaxSize(final StringBuilder stringBuilder, final int maxSize) {
-        if (stringBuilder != null && stringBuilder.length() > maxSize) {
+        if (stringBuilder != null && stringBuilder.capacity() > maxSize) {
             stringBuilder.setLength(maxSize);
             stringBuilder.trimToSize();
         }


[3/3] httpcomponents-client git commit: [HTTPCLIENT-1865] DefaultServiceUnavailableRetryStrategy does not respect HttpEntity#isRepeatable.

Posted by ol...@apache.org.
[HTTPCLIENT-1865] DefaultServiceUnavailableRetryStrategy does not
respect HttpEntity#isRepeatable.


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

Branch: refs/heads/master
Commit: 9efcba87302dc76fe31b33d685bca1fea34cf7f2
Parents: f2146ca
Author: Tomas Celaya <tj...@joyent.com>
Authored: Mon Jul 31 18:10:03 2017 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Tue Aug 1 19:25:17 2017 +0200

----------------------------------------------------------------------
 .../impl/sync/ServiceUnavailableRetryExec.java  |  5 +++
 .../sync/TestServiceUnavailableRetryExec.java   | 32 ++++++++++++++++++++
 2 files changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9efcba87/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ServiceUnavailableRetryExec.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ServiceUnavailableRetryExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ServiceUnavailableRetryExec.java
index 42fcff3..721f98d 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ServiceUnavailableRetryExec.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ServiceUnavailableRetryExec.java
@@ -39,6 +39,7 @@ import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.util.Args;
 import org.apache.logging.log4j.LogManager;
@@ -82,6 +83,10 @@ final class ServiceUnavailableRetryExec implements ExecChainHandler {
         for (int c = 1;; c++) {
             final ClassicHttpResponse response = chain.proceed(currentRequest, scope);
             try {
+                final HttpEntity entity = request.getEntity();
+                if (entity != null && !entity.isRepeatable()) {
+                    return response;
+                }
                 if (this.retryStrategy.retryRequest(response, c, context)) {
                     response.close();
                     final long nextInterval = this.retryStrategy.getRetryInterval(response, context);

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9efcba87/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestServiceUnavailableRetryExec.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestServiceUnavailableRetryExec.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestServiceUnavailableRetryExec.java
index 60b8fd3..1ce4920 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestServiceUnavailableRetryExec.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/sync/TestServiceUnavailableRetryExec.java
@@ -26,17 +26,22 @@
  */
 package org.apache.hc.client5.http.impl.sync;
 
+import java.io.ByteArrayInputStream;
+
 import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.entity.EntityBuilder;
 import org.apache.hc.client5.http.protocol.HttpClientContext;
 import org.apache.hc.client5.http.sync.ExecChain;
 import org.apache.hc.client5.http.sync.ExecRuntime;
 import org.apache.hc.client5.http.sync.ServiceUnavailableRetryStrategy;
 import org.apache.hc.client5.http.sync.methods.HttpGet;
+import org.apache.hc.client5.http.sync.methods.HttpPost;
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
@@ -113,4 +118,31 @@ public class TestServiceUnavailableRetryExec {
             throw ex;
         }
     }
+
+    @Test
+    public void testNonRepeatableEntityResponseReturnedImmediately() throws Exception {
+        final HttpRoute route = new HttpRoute(target);
+
+        final HttpPost request = new HttpPost("/test");
+        request.setEntity(EntityBuilder.create()
+                .setStream(new ByteArrayInputStream(new byte[]{}))
+                .build());
+        final HttpClientContext context = HttpClientContext.create();
+
+        final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
+        Mockito.when(chain.proceed(
+                Mockito.<ClassicHttpRequest>any(),
+                Mockito.<ExecChain.Scope>any())).thenReturn(response);
+        Mockito.when(retryStrategy.retryRequest(
+                Mockito.<HttpResponse>any(),
+                Mockito.anyInt(),
+                Mockito.<HttpContext>any())).thenReturn(Boolean.TRUE, Boolean.FALSE);
+
+        final ExecChain.Scope scope = new ExecChain.Scope(route, request, endpoint, context);
+        final ClassicHttpResponse finalResponse = retryExec.execute(request, scope, chain);
+
+        Assert.assertSame(response, finalResponse);
+        Mockito.verify(response, Mockito.times(0)).close();
+    }
+
 }