/** * @see #avoidProxy(ProxyServer, String) */ public static boolean avoidProxy(final ProxyServer proxyServer, final Request request) { return avoidProxy(proxyServer, request.getURI().getHost()); }
HttpTransactionContext(final GrizzlyAsyncHttpProvider provider, final GrizzlyResponseFuture future, final Request request, final AsyncHandler handler) { this.provider = provider; this.future = future; this.request = request; this.handler = handler; redirectsAllowed = provider.clientConfig.isFollowRedirect(); maxRedirectCount = provider.clientConfig.getMaxRedirects(); this.requestUri = request.getURI(); }
private static String getPoolKey(Request request, ProxyServer proxyServer) { return request.getConnectionPoolKeyStrategy().getKey(request.getURI(), proxyServer); }
private HttpURLConnection createUrlConnection(Request request) throws IOException, URISyntaxException { ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); Proxy proxy = null; if (proxyServer != null || realm != null) { try { proxy = configureProxyAndAuth(proxyServer, realm); } catch (AuthenticationException e) { throw new IOException(e.getMessage()); } } HttpURLConnection urlConnection = (HttpURLConnection) request.getURI().toURI().toURL().openConnection(proxy == null ? Proxy.NO_PROXY : proxy); if (request.getURI().getScheme().equals("https")) { HttpsURLConnection secure = (HttpsURLConnection) urlConnection; SSLContext sslContext = config.getSSLContext(); if (sslContext == null) { try { sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate()); } catch (NoSuchAlgorithmException e) { throw new IOException(e.getMessage()); } catch (GeneralSecurityException e) { throw new IOException(e.getMessage()); } } secure.setSSLSocketFactory(sslContext.getSocketFactory()); secure.setHostnameVerifier(config.getHostnameVerifier()); } return urlConnection; }
/** * Invoke this API if you want to set the Range header on your {@link Request} based on the last valid bytes * position. * * @param request {@link Request} * @return a {@link Request} with the Range header properly set. */ public Request adjustRequestRange(Request request) { Long ri = resumableIndex.get(request.getURI().toUrl()); if (ri != null) { byteTransferred.set(ri); } // The Resumbale if (resumableListener != null && resumableListener.length() > 0 && byteTransferred.get() != resumableListener.length()) { byteTransferred.set(resumableListener.length()); } RequestBuilder builder = new RequestBuilder(request); if (request.getHeaders().get("Range") == null && byteTransferred.get() != 0) { builder.setHeader("Range", "bytes=" + byteTransferred.get() + "-"); } return builder.build(); }
/** * @param config the global config * @param request the request * @return the proxy server to be used for this request (can be null) */ public static ProxyServer getProxyServer(AsyncHttpClientConfig config, Request request) { ProxyServer proxyServer = request.getProxyServer(); if (proxyServer == null) { ProxyServerSelector selector = config.getProxyServerSelector(); if (selector != null) { proxyServer = selector.select(request.getURI()); } } return ProxyUtils.avoidProxy(proxyServer, request) ? null : proxyServer; }
@Override public void calculateAndAddSignature(Request request, RequestBuilderBase<?> requestBuilder) { String nonce = generateNonce(); long timestamp = System.currentTimeMillis() / 1000L; String signature = calculateSignature(request.getMethod(), request.getURI(), timestamp, nonce, request.getFormParams(), request.getQueryParams()); String headerValue = constructAuthHeader(signature, nonce, timestamp); requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue); }
private Future<Response> execute(RequestBuilder rb, BodyConsumer bodyConsumer, ThrowableHandler throwableHandler) throws IOException { if (throwableHandler == null) { throwableHandler = defaultThrowableHandler; } Request request = rb.build(); ProgressAsyncHandler<Response> handler = new BodyConsumerAsyncHandler(bodyConsumer, throwableHandler, errorDocumentBehaviour, request.getURI(), listener); if (resumeEnabled && request.getMethod().equals("GET") && bodyConsumer != null && bodyConsumer instanceof ResumableBodyConsumer) { ResumableBodyConsumer fileBodyConsumer = (ResumableBodyConsumer) bodyConsumer; long length = fileBodyConsumer.getTransferredBytes(); fileBodyConsumer.resume(); handler = new ResumableBodyConsumerAsyncHandler(length, handler); } return asyncHttpClient().executeRequest(request, handler); }
public <T> ListenableFuture<T> sendRequest(final Request request,// final AsyncHandler<T> asyncHandler,// NettyResponseFuture<T> future,// boolean reclaimCache) throws IOException { if (closed.get()) throw new IOException("Closed"); UriComponents uri = request.getURI(); // FIXME really useful? Why not do this check when building the request? if (uri.getScheme().startsWith(WEBSOCKET) && !validateWebSocketRequest(request, asyncHandler)) throw new IOException("WebSocket method must be a GET"); ProxyServer proxyServer = getProxyServer(config, request); boolean resultOfAConnect = future != null && future.getNettyRequest() != null && future.getNettyRequest().getHttpRequest().getMethod() == HttpMethod.CONNECT; boolean useProxy = proxyServer != null && !resultOfAConnect; if (useProxy && useProxyConnect(uri)) // SSL proxy, have to handle CONNECT if (future != null && future.isConnectAllowed()) // CONNECT forced return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, true, true); else return sendRequestThroughSslProxy(request, asyncHandler, future, reclaimCache, uri, proxyServer); else return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, useProxy, false); }
ClientBootstrap bootstrap = channelManager.getBootstrap(request.getURI().getScheme(), useProxy, useSSl);
final UriComponents uri = request.getURI(); String host = ((proxy != null) ? proxy.getHost() : uri.getHost()); int port = ((proxy != null) ? proxy.getPort() : uri.getPort());
private Connection obtainConnection0(final Request request, final GrizzlyResponseFuture requestFuture) throws IOException, ExecutionException, InterruptedException, TimeoutException { final UriComponents uri = request.getURI(); final ProxyServer proxy = requestFuture.getProxy(); String host = (proxy != null) ? proxy.getHost() : uri.getHost(); int port = (proxy != null) ? proxy.getPort() : uri.getPort(); int cTimeout = provider.clientConfig.getConnectionTimeout(); FutureImpl<Connection> future = Futures.createSafeFuture(); CompletionHandler<Connection> ch = Futures.toCompletionHandler(future, createConnectionCompletionHandler(request, requestFuture, null)); if (cTimeout > 0) { connectionHandler.connect(new InetSocketAddress(host, getPort(uri, port)), ch); return future.get(cTimeout, TimeUnit.MILLISECONDS); } else { connectionHandler.connect(new InetSocketAddress(host, getPort(uri, port)), ch); return future.get(); } }
private Realm ntlmProxyChallenge(List<String> wwwAuth, Request request, ProxyServer proxyServer, FluentCaseInsensitiveStringsMap headers, Realm realm, NettyResponseFuture<?> future, boolean proxyInd) throws NTLMEngineException { future.getAndSetAuth(false); headers.remove(HttpHeaders.Names.PROXY_AUTHORIZATION); addType3NTLMAuthorizationHeader(wwwAuth, headers, proxyServer.getPrincipal(), proxyServer.getPassword(), proxyServer.getNtlmDomain(), proxyServer.getHost(), proxyInd); return newRealmBuilder(realm)// // .setScheme(realm.getAuthScheme()) .setUri(request.getURI())// .setMethodName(request.getMethod()).build(); }
public RequestImpl(Request prototype) { if (prototype != null) { this.method = prototype.getMethod(); this.uri = prototype.getURI(); this.address = prototype.getInetAddress(); this.localAddress = prototype.getLocalAddress(); this.headers = new FluentCaseInsensitiveStringsMap(prototype.getHeaders()); this.cookies = new ArrayList<Cookie>(prototype.getCookies()); this.byteData = prototype.getByteData(); this.stringData = prototype.getStringData(); this.streamData = prototype.getStreamData(); this.bodyGenerator = prototype.getBodyGenerator(); this.formParams = prototype.getFormParams() == null ? null : new ArrayList<Param>(prototype.getFormParams()); this.parts = prototype.getParts() == null ? null : new ArrayList<Part>(prototype.getParts()); this.virtualHost = prototype.getVirtualHost(); this.length = prototype.getContentLength(); this.proxyServer = prototype.getProxyServer(); this.realm = prototype.getRealm(); this.file = prototype.getFile(); this.followRedirects = prototype.getFollowRedirect(); this.requestTimeout = prototype.getRequestTimeout(); this.rangeOffset = prototype.getRangeOffset(); this.charset = prototype.getBodyEncoding(); this.connectionPoolKeyStrategy = prototype.getConnectionPoolKeyStrategy(); } }
private boolean exitAfterHandlingConnect(// final Channel channel,// final NettyResponseFuture<?> future,// final Request request,// ProxyServer proxyServer,// int statusCode,// HttpRequest httpRequest) throws IOException { if (statusCode == OK.getCode() && httpRequest.getMethod() == HttpMethod.CONNECT) { if (future.isKeepAlive()) future.attachChannel(channel, true); try { UriComponents requestURI = request.getURI(); String scheme = requestURI.getScheme(); String host = requestURI.getHost(); int port = getDefaultPort(requestURI); logger.debug("Connecting to proxy {} for scheme {}", proxyServer, scheme); channelManager.upgradeProtocol(channel.getPipeline(), scheme, host, port); } catch (Throwable ex) { requestSender.abort(future, ex); } future.setReuseChannel(true); future.setConnectAllowed(false); requestSender.sendNextRequest(new RequestBuilder(future.getRequest()).build(), future); return true; } return false; }
private Realm kerberosChallenge(List<String> proxyAuth, Request request, ProxyServer proxyServer, FluentCaseInsensitiveStringsMap headers, Realm realm, NettyResponseFuture<?> future, boolean proxyInd) throws NTLMEngineException { UriComponents uri = request.getURI(); String host = request.getVirtualHost() == null ? uri.getHost() : request.getVirtualHost(); String server = proxyServer == null ? host : proxyServer.getHost(); try { String challengeHeader = SpnegoEngine.INSTANCE.generateToken(server); headers.remove(HttpHeaders.Names.AUTHORIZATION); headers.add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challengeHeader); return newRealmBuilder(realm)// .setUri(uri)// .setMethodName(request.getMethod())// .setScheme(Realm.AuthScheme.KERBEROS)// .build(); } catch (Throwable throwable) { if (isNTLM(proxyAuth)) { return ntlmChallenge(proxyAuth, request, proxyServer, headers, realm, future, proxyInd); } requestSender.abort(future, throwable); return null; } }
if (request.getURI().getScheme().startsWith(WEBSOCKET)) { newUrl = newUrl.replaceFirst(HTTP, WEBSOCKET);
private Realm ntlmChallenge(List<String> wwwAuth, Request request, ProxyServer proxyServer, FluentCaseInsensitiveStringsMap headers, Realm realm, NettyResponseFuture<?> future, boolean proxyInd) throws NTLMEngineException { boolean useRealm = proxyServer == null && realm != null; String ntlmDomain = useRealm ? realm.getNtlmDomain() : proxyServer.getNtlmDomain(); String ntlmHost = useRealm ? realm.getNtlmHost() : proxyServer.getHost(); String principal = useRealm ? realm.getPrincipal() : proxyServer.getPrincipal(); String password = useRealm ? realm.getPassword() : proxyServer.getPassword(); UriComponents uri = request.getURI(); if (realm != null && !realm.isNtlmMessageType2Received()) { String challengeHeader = NTLMEngine.INSTANCE.generateType1Msg(ntlmDomain, ntlmHost); addNTLMAuthorizationHeader(headers, challengeHeader, proxyInd); future.getAndSetAuth(false); return newRealmBuilder(realm)// .setScheme(realm.getAuthScheme())// .setUri(uri)// .setMethodName(request.getMethod())// .setNtlmMessageType2Received(true)// .build(); } else { addType3NTLMAuthorizationHeader(wwwAuth, headers, principal, password, ntlmDomain, ntlmHost, proxyInd); Realm.AuthScheme authScheme = realm != null ? realm.getAuthScheme() : Realm.AuthScheme.NTLM; return newRealmBuilder(realm)// .setScheme(authScheme)// .setUri(uri)// .setMethodName(request.getMethod())// .build(); } }