View | Details | Raw Unified | Return to bug 64267
Collapse All | Expand All

(-)a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (-2 / +86 lines)
Lines 24-35 import java.io.OutputStream; Link Here
24
import java.io.UnsupportedEncodingException;
24
import java.io.UnsupportedEncodingException;
25
import java.net.InetAddress;
25
import java.net.InetAddress;
26
import java.net.InetSocketAddress;
26
import java.net.InetSocketAddress;
27
import java.net.MalformedURLException;
27
import java.net.URI;
28
import java.net.URI;
28
import java.net.URISyntaxException;
29
import java.net.URISyntaxException;
29
import java.net.URL;
30
import java.net.URL;
30
import java.net.URLDecoder;
31
import java.net.URLDecoder;
31
import java.nio.charset.Charset;
32
import java.nio.charset.Charset;
32
import java.security.GeneralSecurityException;
33
import java.security.GeneralSecurityException;
34
import java.security.Principal;
33
import java.security.PrivilegedActionException;
35
import java.security.PrivilegedActionException;
34
import java.security.PrivilegedExceptionAction;
36
import java.security.PrivilegedExceptionAction;
35
import java.util.ArrayList;
37
import java.util.ArrayList;
Lines 62-67 import org.apache.http.auth.AuthScope; Link Here
62
import org.apache.http.auth.AuthState;
64
import org.apache.http.auth.AuthState;
63
import org.apache.http.auth.Credentials;
65
import org.apache.http.auth.Credentials;
64
import org.apache.http.auth.NTCredentials;
66
import org.apache.http.auth.NTCredentials;
67
import org.apache.http.auth.UsernamePasswordCredentials;
65
import org.apache.http.client.AuthCache;
68
import org.apache.http.client.AuthCache;
66
import org.apache.http.client.AuthenticationStrategy;
69
import org.apache.http.client.AuthenticationStrategy;
67
import org.apache.http.client.CredentialsProvider;
70
import org.apache.http.client.CredentialsProvider;
Lines 119-124 import org.apache.http.impl.client.HttpClients; Link Here
119
import org.apache.http.impl.client.LaxRedirectStrategy;
122
import org.apache.http.impl.client.LaxRedirectStrategy;
120
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
123
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
121
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
124
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
125
import org.apache.http.impl.client.TargetAuthenticationStrategy;
122
import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator;
126
import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator;
123
import org.apache.http.impl.conn.DefaultSchemePortResolver;
127
import org.apache.http.impl.conn.DefaultSchemePortResolver;
124
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
128
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
Lines 205-210 public class HTTPHC4Impl extends HTTPHCAbstractImpl { Link Here
205
209
206
    private static final InputStreamFactory BROTLI = BrotliInputStream::new;
210
    private static final InputStreamFactory BROTLI = BrotliInputStream::new;
207
211
212
    private static final class ManagedCredentialsProvider implements CredentialsProvider {
213
        private AuthManager authManager;
214
        private Credentials proxyCredentials;
215
        private AuthScope proxyAuthScope;
216
217
        public ManagedCredentialsProvider(AuthManager authManager, AuthScope proxyAuthScope, Credentials proxyCredentials) {
218
            this.authManager = authManager;
219
            this.proxyAuthScope = proxyAuthScope;
220
            this.proxyCredentials = proxyCredentials;
221
        }
222
223
        @Override
224
        public void setCredentials(AuthScope authscope, Credentials credentials) {
225
            log.debug("Store creds {} for {}", credentials, authscope);
226
        }
227
228
        @Override
229
        public Credentials getCredentials(AuthScope authScope) {
230
            log.info("Get creds for {}", authScope);
231
            if (this.proxyAuthScope != null && authScope.equals(proxyAuthScope)) {
232
                return proxyCredentials;
233
            }
234
            final Authorization authorization = getAuthorizationForAuthScope(authScope);
235
            if (authorization == null) {
236
                return null;
237
            }
238
            return new UsernamePasswordCredentials(authorization.getUser(), authorization.getPass());
239
        }
240
241
        /**
242
         * Find the Authorization for the given AuthScope. We can't ask the AuthManager
243
         * by the URL, as we didn't get the scheme or path of the URL. Therefore we do a
244
         * best guess on the information we have
245
         *
246
         * @param authScope information which destination we want to get credentials for
247
         * @return matching authorization information entry from the AuthManager
248
         */
249
        private Authorization getAuthorizationForAuthScope(AuthScope authScope) {
250
            if (authScope == null) {
251
                return null;
252
            }
253
            for (JMeterProperty authProp : authManager.getAuthObjects()) {
254
                Object authObject = authProp.getObjectValue();
255
                if (authObject instanceof Authorization) {
256
                    Authorization auth = (Authorization) authObject;
257
                    if (!authScope.getRealm().equals(auth.getRealm())) {
258
                        continue;
259
                    }
260
                    try {
261
                        URL authUrl = new URL(auth.getURL());
262
                        if (authUrl.getHost().equals(authScope.getHost()) && getPort(authUrl) == authScope.getPort()) {
263
                            return auth;
264
                        }
265
                    } catch (MalformedURLException e) {
266
                        log.debug("Invalid URL {} in authManager", auth.getURL());
267
                    }
268
                }
269
            }
270
            return null;
271
        }
272
273
        private int getPort(URL url) {
274
            if (url.getPort() == -1) {
275
                return url.getProtocol().equals("https") ? 443 : 80;
276
            }
277
            return url.getPort();
278
        }
279
280
        @Override
281
        public void clear() {
282
            log.debug("clear creds");
283
        }
284
    }
285
208
    private static final class PreemptiveAuthRequestInterceptor implements HttpRequestInterceptor {
286
    private static final class PreemptiveAuthRequestInterceptor implements HttpRequestInterceptor {
209
        @Override
287
        @Override
210
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
288
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
Lines 1050-1070 public class HTTPHC4Impl extends HTTPHCAbstractImpl { Link Here
1050
            }
1131
            }
1051
1132
1052
            // Set up proxy details
1133
            // Set up proxy details
1134
            AuthScope proxyAuthScope = null;
1135
            NTCredentials proxyCredentials = null;
1053
            if (key.hasProxy) {
1136
            if (key.hasProxy) {
1054
                HttpHost proxy = new HttpHost(key.proxyHost, key.proxyPort, key.proxyScheme);
1137
                HttpHost proxy = new HttpHost(key.proxyHost, key.proxyPort, key.proxyScheme);
1055
                builder.setProxy(proxy);
1138
                builder.setProxy(proxy);
1056
1139
1057
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
1140
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
1058
                if (!key.proxyUser.isEmpty()) {
1141
                if (!key.proxyUser.isEmpty()) {
1142
                    proxyAuthScope = new AuthScope(key.proxyHost, key.proxyPort);
1143
                    proxyCredentials = new NTCredentials(key.proxyUser, key.proxyPass, LOCALHOST, PROXY_DOMAIN);
1059
                    credsProvider.setCredentials(
1144
                    credsProvider.setCredentials(
1060
                            new AuthScope(key.proxyHost, key.proxyPort),
1145
                            proxyAuthScope,
1061
                            new NTCredentials(key.proxyUser, key.proxyPass, LOCALHOST, PROXY_DOMAIN));
1146
                            proxyCredentials);
1062
                }
1147
                }
1063
                builder.setDefaultCredentialsProvider(credsProvider);
1148
                builder.setDefaultCredentialsProvider(credsProvider);
1064
            }
1149
            }
1065
            builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING);
1150
            builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING);
1066
            if(BASIC_AUTH_PREEMPTIVE) {
1151
            if(BASIC_AUTH_PREEMPTIVE) {
1067
                builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR);
1152
                builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR);
1153
            } else {
1154
                builder.setDefaultCredentialsProvider(new ManagedCredentialsProvider(getAuthManager(), proxyAuthScope, proxyCredentials));
1068
            }
1155
            }
1069
            httpClient = builder.build();
1156
            httpClient = builder.build();
1070
            if (log.isDebugEnabled()) {
1157
            if (log.isDebugEnabled()) {

Return to bug 64267