protected void rewriteRequestURI( final RequestWrapper request, final HttpRoute route) throws ProtocolException { try { URI uri = request.getURI(); if (route.getProxyHost() != null && !route.isTunnelled()) { // Make sure the request URI is absolute if (!uri.isAbsolute()) { HttpHost target = route.getTargetHost(); uri = URIUtils.rewriteURI(uri, target); request.setURI(uri); } } else { // Make sure the request URI is relative if (uri.isAbsolute()) { uri = URIUtils.rewriteURI(uri, null); request.setURI(uri); } } } catch (URISyntaxException ex) { throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex); } }
private void requestDidNotExpect100ContinueButResponseIsOne( HttpRequest request, HttpResponse response) throws ClientProtocolException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) { return; } if (!requestWasWrapped(request)) { return; } ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request); if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) { return; } if (originalRequestDidNotExpectContinue((RequestWrapper) request)) { throw new ClientProtocolException( "The incoming request did not contain a " + "100-continue header, but the response was a Status 100, continue."); } }
@Override protected void setHost(HttpRequest request, String host) { request.setHeader("Host", host); if (request instanceof HttpRequestWrapper) { setHost(((HttpRequestWrapper) request).getOriginal(), host); } else if (request instanceof RequestWrapper) { setHost(((RequestWrapper) request).getOriginal(), host); } else { URI oldUri = ((HttpRequestBase) request).getURI(); try { URI newUri = replaceHost(oldUri, host); ((HttpRequestBase) request).setURI(newUri); } catch (URISyntaxException e) { throw new RuntimeException(e); } } }
private static RequestWrapper wrapRequest(HttpUriRequest paramHttpUriRequest) throws IOException { // Byte code: // 0: aload_0 // 1: instanceof 217 // 4: ifeq +21 -> 25 // 7: new 219 org/apache/http/impl/client/EntityEnclosingRequestWrapper // 10: dup // 11: aload_0 // 12: checkcast 217 org/apache/http/HttpEntityEnclosingRequest // 15: invokespecial 222 org/apache/http/impl/client/EntityEnclosingRequestWrapper:<init> (Lorg/apache/http/HttpEntityEnclosingRequest;)V // 18: astore_2 // 19: aload_2 // 20: invokevirtual 225 org/apache/http/impl/client/RequestWrapper:resetHeaders ()V // 23: aload_2 // 24: areturn // 25: new 173 org/apache/http/impl/client/RequestWrapper // 28: dup // 29: aload_0 // 30: invokespecial 228 org/apache/http/impl/client/RequestWrapper:<init> (Lorg/apache/http/HttpRequest;)V // 33: astore_2 // 34: goto -15 -> 19 // 37: astore_1 // 38: new 230 org/apache/http/client/ClientProtocolException // 41: dup // 42: aload_1 // 43: invokespecial 233 org/apache/http/client/ClientProtocolException:<init> (Ljava/lang/Throwable;)V // 46: athrow // Local variable table: // start length slot name signature // 0 47 0 paramHttpUriRequest HttpUriRequest // 37 6 1 localProtocolException org.apache.http.ProtocolException // 18 16 2 localObject Object // Exception table: // from to target type // 0 19 37 org/apache/http/ProtocolException // 19 23 37 org/apache/http/ProtocolException // 25 34 37 org/apache/http/ProtocolException }
private static RequestWrapper wrapRequest(HttpRequest paramHttpRequest) throws IOException { // Byte code: // 0: aload_0 // 1: instanceof 254 // 4: ifeq +21 -> 25 // 7: new 256 org/apache/http/impl/client/EntityEnclosingRequestWrapper // 10: dup // 11: aload_0 // 12: checkcast 254 org/apache/http/HttpEntityEnclosingRequest // 15: invokespecial 259 org/apache/http/impl/client/EntityEnclosingRequestWrapper:<init> (Lorg/apache/http/HttpEntityEnclosingRequest;)V // 18: astore_2 // 19: aload_2 // 20: invokevirtual 264 org/apache/http/impl/client/RequestWrapper:resetHeaders ()V // 23: aload_2 // 24: areturn // 25: new 261 org/apache/http/impl/client/RequestWrapper // 28: dup // 29: aload_0 // 30: invokespecial 267 org/apache/http/impl/client/RequestWrapper:<init> (Lorg/apache/http/HttpRequest;)V // 33: astore_2 // 34: goto -15 -> 19 // 37: astore_1 // 38: new 269 org/apache/http/client/ClientProtocolException // 41: dup // 42: aload_1 // 43: invokespecial 272 org/apache/http/client/ClientProtocolException:<init> (Ljava/lang/Throwable;)V // 46: athrow // Local variable table: // start length slot name signature // 0 47 0 paramHttpRequest HttpRequest // 37 6 1 localProtocolException org.apache.http.ProtocolException // 18 16 2 localObject Object // Exception table: // from to target type // 0 19 37 org/apache/http/ProtocolException // 19 23 37 org/apache/http/ProtocolException // 25 34 37 org/apache/http/ProtocolException }
private static RequestWrapper wrapRequest(HttpUriRequest paramHttpUriRequest) throws IOException { // Byte code: // 0: aload_0 // 1: instanceof 254 // 4: ifeq +21 -> 25 // 7: new 256 org/apache/http/impl/client/EntityEnclosingRequestWrapper // 10: dup // 11: aload_0 // 12: checkcast 254 org/apache/http/HttpEntityEnclosingRequest // 15: invokespecial 259 org/apache/http/impl/client/EntityEnclosingRequestWrapper:<init> (Lorg/apache/http/HttpEntityEnclosingRequest;)V // 18: astore_2 // 19: aload_2 // 20: invokevirtual 264 org/apache/http/impl/client/RequestWrapper:resetHeaders ()V // 23: aload_2 // 24: areturn // 25: new 261 org/apache/http/impl/client/RequestWrapper // 28: dup // 29: aload_0 // 30: invokespecial 267 org/apache/http/impl/client/RequestWrapper:<init> (Lorg/apache/http/HttpRequest;)V // 33: astore_2 // 34: goto -15 -> 19 // 37: astore_1 // 38: new 269 org/apache/http/client/ClientProtocolException // 41: dup // 42: aload_1 // 43: invokespecial 272 org/apache/http/client/ClientProtocolException:<init> (Ljava/lang/Throwable;)V // 46: athrow // Local variable table: // start length slot name signature // 0 47 0 paramHttpUriRequest HttpUriRequest // 37 6 1 localProtocolException org.apache.http.ProtocolException // 18 16 2 localObject Object // Exception table: // from to target type // 0 19 37 org/apache/http/ProtocolException // 19 23 37 org/apache/http/ProtocolException // 25 34 37 org/apache/http/ProtocolException }
public final <T> T execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest, ResponseHandler<? extends T> paramResponseHandler) throws IOException, ClientProtocolException { CookieSourceApplier localCookieSourceApplier = this.mCookieSourceApplier; RequestWrapper localRequestWrapper = wrapRequest(paramHttpRequest); SetCookie localSetCookie = new SetCookie(localCookieSourceApplier.this$0, paramResponseHandler, localRequestWrapper, localCookieSourceApplier.mCookieSource, (byte)0); return localCookieSourceApplier.mClient.execute(paramHttpHost, PseudonymousCookieSource.Helper.setRequestCookie(localRequestWrapper, localCookieSourceApplier.mCookieSource), localSetCookie); }
public final <T> T execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest, ResponseHandler<? extends T> paramResponseHandler, HttpContext paramHttpContext) throws IOException, ClientProtocolException { CookieSourceApplier localCookieSourceApplier = this.mCookieSourceApplier; RequestWrapper localRequestWrapper = wrapRequest(paramHttpRequest); SetCookie localSetCookie = new SetCookie(localCookieSourceApplier.this$0, paramResponseHandler, localRequestWrapper, localCookieSourceApplier.mCookieSource, (byte)0); return localCookieSourceApplier.mClient.execute(paramHttpHost, PseudonymousCookieSource.Helper.setRequestCookie(localRequestWrapper, localCookieSourceApplier.mCookieSource), localSetCookie, paramHttpContext); }
public final HttpResponse execute(HttpUriRequest paramHttpUriRequest, HttpContext paramHttpContext) throws IOException { String str1 = paramHttpUriRequest.getURI().toString(); UrlRules.Rule localRule = UrlRules.getRules(this.mResolver).matchRule(str1); String str2 = localRule.apply(str1); if (str2 == null) { Log.w("GoogleHttpClient", "Blocked by " + localRule.mName + ": " + str1); throw new BlockedRequestException(localRule); } if (str2 == str1) { return executeWithoutRewriting(paramHttpUriRequest, paramHttpContext); } try { URI localURI = new URI(str2); RequestWrapper localRequestWrapper = wrapRequest(paramHttpUriRequest); localRequestWrapper.setURI(localURI); if (Log.isLoggable("GoogleHttpClient", 3)) { Log.d("GoogleHttpClient", "Rule " + localRule.mName + ": " + str1 + " -> " + str2); } return executeWithoutRewriting(localRequestWrapper, paramHttpContext); } catch (URISyntaxException localURISyntaxException) { throw new RuntimeException("Bad URL from rule: " + localRule.mName, localURISyntaxException); } }
private RequestWrapper wrapRequest( final HttpRequest request) throws ProtocolException { if (request instanceof HttpEntityEnclosingRequest) { return new EntityEnclosingRequestWrapper( (HttpEntityEnclosingRequest) request); } else { return new RequestWrapper( request); } }
@Deprecated protected boolean requestIsAborted(final HttpRequest request) { if (request == null) return false; HttpRequest req = request; if (request instanceof RequestWrapper) { // does not forward request to // original req = ((RequestWrapper) request).getOriginal(); } return (req instanceof HttpUriRequest && ((HttpUriRequest) req).isAborted()); }
private HttpUriRequest getHttpUriRequest(HttpRequest request) { while (request instanceof RequestWrapper) { HttpRequest original = ((RequestWrapper) request).getOriginal(); if (original == request) { break; } request = original; } if (request instanceof HttpUriRequest) { return (HttpUriRequest) request; } return null; }
private HttpRequest upgradeRequestTo(HttpRequest request, ProtocolVersion version) throws ProtocolException { RequestWrapper newRequest = new RequestWrapper(request); newRequest.setProtocolVersion(version); return newRequest; }
private HttpRequest downgradeRequestTo(HttpRequest request, ProtocolVersion version) throws ProtocolException { RequestWrapper newRequest = new RequestWrapper(request); newRequest.setProtocolVersion(version); return newRequest; }
private void transferEncodingIsNotReturnedTo1_0Client(HttpRequest request, HttpResponse response) { if (!requestWasWrapped(request)) { return; } ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request); if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) { return; } removeResponseTransferEncoding(response); }
private boolean originalRequestDidNotExpectContinue(RequestWrapper request) { try { HttpEntityEnclosingRequest original = (HttpEntityEnclosingRequest) request .getOriginal(); return !original.expectContinue(); } catch (ClassCastException ex) { return false; } }
/** * When a {@link HttpCacheEntry} is stale but 'might' be used as a response * to an {@link HttpRequest} we will attempt to revalidate the entry with * the origin. Build the origin {@link HttpRequest} here and return it. * * @param request * the original request from the caller * @param cacheEntry * the entry that needs to be revalidated * @return the wrapped request * @throws ProtocolException * when I am unable to build a new origin request. */ public HttpRequest buildConditionalRequest(HttpRequest request, HttpCacheEntry cacheEntry) throws ProtocolException { RequestWrapper wrapperRequest = new RequestWrapper(request); wrapperRequest.resetHeaders(); Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG); if (eTag != null) { wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue()); } Header lastModified = cacheEntry .getFirstHeader(HeaderConstants.LAST_MODIFIED); if (lastModified != null) { wrapperRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue()); } boolean mustRevalidate = false; for (Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) { for (HeaderElement elt : h.getElements()) { if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE .equalsIgnoreCase(elt.getName()) || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE .equalsIgnoreCase(elt.getName())) { mustRevalidate = true; break; } } } if (mustRevalidate) { wrapperRequest.addHeader("Cache-Control", "max-age=0"); } return wrapperRequest; }
/** * When a {@link HttpCacheEntry} does not exist for a specific * {@link HttpRequest} we attempt to see if an existing * {@link HttpCacheEntry} is appropriate by building a conditional * {@link HttpRequest} using the variants' ETag values. If no such values * exist, the request is unmodified * * @param request * the original request from the caller * @param cacheEntry * the entry that needs to be revalidated * @return the wrapped request * @throws ProtocolException * when I am unable to build a new origin request. */ public HttpRequest buildConditionalRequestFromVariants(HttpRequest request, Set<HttpCacheEntry> variantEntries) throws ProtocolException { RequestWrapper wrapperRequest = new RequestWrapper(request); wrapperRequest.resetHeaders(); // we do not support partial content so all etags are used StringBuilder etags = new StringBuilder(); boolean first = true; for (HttpCacheEntry entry : variantEntries) { Header etagHeader = entry.getFirstHeader(HeaderConstants.ETAG); if (etagHeader != null) { if (first) { etags.append(etagHeader.getValue()); first = false; } else { etags.append(",").append(etagHeader.getValue()); } } } // if first is still true than no variants had a cache entry, return // unmodified wrapped request if (first) { return wrapperRequest; } wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString()); return wrapperRequest; }
protected boolean requestIsAborted(final HttpRequest request) { HttpRequest req = request; if (request instanceof RequestWrapper) { // does not forward request to original req = ((RequestWrapper) request).getOriginal(); } return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted()); }
/** * Generates a cURL command equivalent to the given request. */ private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException { StringBuilder builder = new StringBuilder(); builder.append("curl "); for (Header header: request.getAllHeaders()) { if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) { continue; } builder.append("--header \""); builder.append(header.toString().trim()); builder.append("\" "); } URI uri = request.getURI(); // If this is a wrapped request, use the URI from the original // request instead. getURI() on the wrapper seems to return a // relative URI. We want an absolute URI. if (request instanceof RequestWrapper) { HttpRequest original = ((RequestWrapper) request).getOriginal(); if (original instanceof HttpUriRequest) { uri = ((HttpUriRequest) original).getURI(); } } builder.append("\""); builder.append(uri); builder.append("\""); if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity = entityRequest.getEntity(); if (entity != null && entity.isRepeatable()) { if (entity.getContentLength() < 1024) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); entity.writeTo(stream); String entityString = stream.toString(); // TODO: Check the content type, too. builder.append(" --data-ascii \"") .append(entityString) .append("\""); } else { builder.append(" [TOO MUCH DATA TO INCLUDE]"); } } } return builder.toString(); }
public static HttpUriRequest setRequestCookie(HttpUriRequest paramHttpUriRequest, PseudonymousCookieSource paramPseudonymousCookieSource) throws IOException { if (cookieSourceApplies(paramHttpUriRequest, paramPseudonymousCookieSource)) { String str1 = paramPseudonymousCookieSource.getCookieName(); String str2 = paramPseudonymousCookieSource.getCookieValue(); int i = 0; Header[] arrayOfHeader = paramHttpUriRequest.getHeaders("Cookie"); int j = arrayOfHeader.length; int k = 0; if (k < j) { Header localHeader = arrayOfHeader[k]; String str3 = localHeader.getValue(); Matcher localMatcher = COOKIE_PAIR.matcher(str3); do { if (!localMatcher.find()) { break; } } while (!localMatcher.group(2).equals(str1)); for (int m = 1;; m = 0) { if (m != 0) { if ((i == 0) && (!(paramHttpUriRequest instanceof RequestWrapper))) { paramHttpUriRequest = wrapRequest(paramHttpUriRequest); } i = 1; paramHttpUriRequest.removeHeader(localHeader); paramHttpUriRequest.addHeader(new BasicHeader(localHeader.getName(), replaceCookie(localHeader.getValue(), str1, str2))); } k++; break; } } if (i == 0) { paramHttpUriRequest.addHeader(new BasicHeader("Cookie", str1 + "=" + wrapInQuotesIfNeeded(str2))); } } return paramHttpUriRequest; }
/** * Generates a cURL command equivalent to the given request. */ private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException { StringBuilder builder = new StringBuilder(); builder.append("curl "); for (Header header : request.getAllHeaders()) { if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) { continue; } builder.append("--header \""); builder.append(header.toString().trim()); builder.append("\" "); } URI uri = request.getURI(); // If this is a wrapped request, use the URI from the original // request instead. getURI() on the wrapper seems to return a // relative URI. We want an absolute URI. if (request instanceof RequestWrapper) { HttpRequest original = ((RequestWrapper) request).getOriginal(); if (original instanceof HttpUriRequest) { uri = ((HttpUriRequest) original).getURI(); } } builder.append("\""); builder.append(uri); builder.append("\""); if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity = entityRequest.getEntity(); if (entity != null && entity.isRepeatable()) { if (entity.getContentLength() < 1024) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); entity.writeTo(stream); String entityString = stream.toString(); // TODO: Check the content type, too. builder.append(" --data-ascii \"") .append(entityString) .append("\""); } else { builder.append(" [TOO MUCH DATA TO INCLUDE]"); } } } return builder.toString(); }
/** * Generates a cURL command equivalent to the given request. */ private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException { StringBuilder builder = new StringBuilder(); builder.append("curl "); // add in the method builder.append("-X "); builder.append(request.getMethod()); builder.append(" "); for (Header header : request.getAllHeaders()) { if (!logAuthToken && (header.getName().equals("Authorization") || header .getName().equals("Cookie"))) { continue; } builder.append("--header \""); builder.append(header.toString().trim()); builder.append("\" "); } URI uri = request.getURI(); // If this is a wrapped request, use the URI from the original // request instead. getURI() on the wrapper seems to return a // relative URI. We want an absolute URI. if (request instanceof RequestWrapper) { HttpRequest original = ((RequestWrapper) request).getOriginal(); if (original instanceof HttpUriRequest) { uri = ((HttpUriRequest) original).getURI(); } } builder.append("\""); builder.append(uri); builder.append("\""); if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity = entityRequest.getEntity(); if (entity != null && entity.isRepeatable()) { if (entity.getContentLength() < 1024) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); entity.writeTo(stream); if (isBinaryContent(request)) { String base64 = Base64.encodeToString( stream.toByteArray(), Base64.NO_WRAP); builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; "); builder.append(" --data-binary @/tmp/$$.bin"); } else { String entityString = stream.toString(); builder.append(" --data-ascii \"").append(entityString) .append("\""); } } else { builder.append(" [TOO MUCH DATA TO INCLUDE]"); } } } return builder.toString(); }
/** * Execute request and retry in case of a recoverable I/O failure */ private HttpResponse tryExecute( final RoutedRequest req, final HttpContext context) throws HttpException, IOException { RequestWrapper wrapper = req.getRequest(); HttpRoute route = req.getRoute(); HttpResponse response = null; Exception retryReason = null; for (;;) { // Increment total exec count (with redirects) execCount++; // Increment exec count for this particular request wrapper.incrementExecCount(); if (!wrapper.isRepeatable()) { if (retryReason != null) { throw new NonRepeatableRequestException("Cannot retry request " + "with a non-repeatable request entity. The cause lists the " + "reason the original request failed." + retryReason); } else { throw new NonRepeatableRequestException("Cannot retry request " + "with a non-repeatable request entity."); } } try { if (!managedConn.isOpen()) { // If we have a direct route to the target host // just re-open connection and re-try the request if (!route.isTunnelled()) { if (DEBUG) { Logger.debug("Reopening the direct connection."); } managedConn.open(route, context, params); } else { // otherwise give up if (DEBUG) { Logger.debug("Proxied connection. Need to start over."); } break; } } response = requestExec.execute(wrapper, managedConn, context); break; } catch (IOException ex) { try { managedConn.close(); } catch (IOException ignore) { } if (retryHandler.retryRequest(ex, wrapper.getExecCount(), context)) { retryReason = ex; } else { throw ex; } } } return response; }
private ProtocolVersion getOriginalRequestProtocol(RequestWrapper request) { return request.getOriginal().getProtocolVersion(); }