public synchronized void cancel(Object tag) { for (AsyncCall call : this.readyCalls) { if (Util.equal(tag, call.tag())) { call.cancel(); } } for (AsyncCall call2 : this.runningCalls) { if (Util.equal(tag, call2.tag())) { call2.get().canceled = true; HttpEngine engine = call2.get().engine; if (engine != null) { engine.cancel(); } } } for (Call call3 : this.executedCalls) { if (Util.equal(tag, call3.tag())) { call3.cancel(); } } }
private void update(CacheResponse conditionalCacheHit, HttpURLConnection httpConnection) throws IOException { HttpEngine httpEngine = getHttpEngine(httpConnection); URI uri = httpEngine.getUri(); ResponseHeaders response = httpEngine.getResponseHeaders(); RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields()); Entry entry = new Entry(uri, varyHeaders, httpConnection); DiskLruCache.Snapshot snapshot = (conditionalCacheHit instanceof EntryCacheResponse) ? ((EntryCacheResponse) conditionalCacheHit).snapshot : ((EntrySecureCacheResponse) conditionalCacheHit).snapshot; DiskLruCache.Editor editor = null; try { editor = snapshot.edit(); // returns null if snapshot is not current if (editor != null) { entry.writeTo(editor); editor.commit(); } } catch (IOException e) { abortQuietly(editor); } }
public final InputStream getErrorStream() { InputStream inputStream = null; try { HttpEngine response = getResponse(); if (HttpEngine.hasBody(response.getResponse()) && response.getResponse().code() >= 400) { inputStream = response.getResponse().body().byteStream(); } } catch (IOException e) { } return inputStream; }
public final InputStream getInputStream() throws IOException { if (this.doInput) { HttpEngine response = getResponse(); if (getResponseCode() < 400) { return response.getResponse().body().byteStream(); } throw new FileNotFoundException(this.url.toString()); } throw new ProtocolException("This protocol does not support input"); }
private HttpEngine newHttpEngine(String method, StreamAllocation streamAllocation, RetryableSink requestBody, Response priorResponse) throws MalformedURLException, UnknownHostException { Request.Builder builder = new Request.Builder().url(Internal.instance.getHttpUrlChecked (getURL().toString())).method(method, HttpMethod.requiresRequestBody(method) ? EMPTY_REQUEST_BODY : null); Headers headers = this.requestHeaders.build(); int size = headers.size(); for (int i = 0; i < size; i++) { builder.addHeader(headers.name(i), headers.value(i)); } boolean bufferRequestBody = false; if (HttpMethod.permitsRequestBody(method)) { if (this.fixedContentLength != -1) { builder.header("Content-Length", Long.toString(this.fixedContentLength)); } else if (this.chunkLength > 0) { builder.header("Transfer-Encoding", "chunked"); } else { bufferRequestBody = true; } if (headers.get("Content-Type") == null) { builder.header("Content-Type", Client.FormMime); } } if (headers.get(Network.USER_AGENT) == null) { builder.header(Network.USER_AGENT, defaultUserAgent()); } Request request = builder.build(); OkHttpClient engineClient = this.client; if (!(Internal.instance.internalCache(engineClient) == null || getUseCaches())) { engineClient = this.client.clone().setCache(null); } return new HttpEngine(engineClient, request, bufferRequestBody, true, false, streamAllocation, requestBody, priorResponse); }
private HttpEngine getResponse() throws IOException { initHttpEngine(); if (this.httpEngine.hasResponse()) { return this.httpEngine; } while (true) { if (execute(true)) { Response response = this.httpEngine.getResponse(); Request followUp = this.httpEngine.followUpRequest(); if (followUp == null) { this.httpEngine.releaseStreamAllocation(); return this.httpEngine; } int i = this.followUpCount + 1; this.followUpCount = i; if (i > 20) { throw new ProtocolException("Too many follow-up requests: " + this .followUpCount); } this.url = followUp.url(); this.requestHeaders = followUp.headers().newBuilder(); Sink requestBody = this.httpEngine.getRequestBody(); if (!followUp.method().equals(this.method)) { requestBody = null; } if (requestBody == null || (requestBody instanceof RetryableSink)) { StreamAllocation streamAllocation = this.httpEngine.close(); if (!this.httpEngine.sameConnection(followUp.httpUrl())) { streamAllocation.release(); streamAllocation = null; } this.httpEngine = newHttpEngine(followUp.method(), streamAllocation, (RetryableSink) requestBody, response); } else { throw new HttpRetryException("Cannot retry streamed HTTP body", this .responseCode); } } } }
HttpEngine newEngine(Connection connection) throws IOException { String protocol = request.url().getProtocol(); RawHeaders requestHeaders = request.rawHeaders(); if (protocol.equals("http")) { return new HttpEngine(client, this, request.method(), requestHeaders, connection, null); } else if (protocol.equals("https")) { return new HttpsEngine(client, this, request.method(), requestHeaders, connection, null); } else { throw new AssertionError(); } }
private HttpEngine getHttpEngine(URLConnection httpConnection) { if (httpConnection instanceof HttpURLConnectionImpl) { return ((HttpURLConnectionImpl) httpConnection).getHttpEngine(); } else if (httpConnection instanceof HttpsURLConnectionImpl) { return ((HttpsURLConnectionImpl) httpConnection).getHttpEngine(); } else { return null; } }
/** * Returns the SSL socket used by {@code httpConnection} for HTTPS, nor null * if the connection isn't using HTTPS. Since we permit redirects across * protocols (HTTP to HTTPS or vice versa), the implementation type of the * connection doesn't necessarily match the implementation type of its HTTP * engine. */ private SSLSocket getSslSocket(HttpURLConnection httpConnection) { HttpEngine engine = httpConnection instanceof HttpsURLConnectionImpl ? ((HttpsURLConnectionImpl) httpConnection).getHttpEngine() : ((HttpURLConnectionImpl) httpConnection).getHttpEngine(); return engine instanceof HttpsEngine ? ((HttpsEngine) engine).getSslSocket() : null; }