public Entry(Source in) throws IOException { try { int i; BufferedSource source = Okio.buffer(in); this.url = source.readUtf8LineStrict(); this.requestMethod = source.readUtf8LineStrict(); Builder varyHeadersBuilder = new Builder(); int varyRequestHeaderLineCount = Cache.readInt(source); for (i = 0; i < varyRequestHeaderLineCount; i++) { varyHeadersBuilder.addLenient(source.readUtf8LineStrict()); } this.varyHeaders = varyHeadersBuilder.build(); StatusLine statusLine = StatusLine.parse(source.readUtf8LineStrict()); this.protocol = statusLine.protocol; this.code = statusLine.code; this.message = statusLine.message; Builder responseHeadersBuilder = new Builder(); int responseHeaderLineCount = Cache.readInt(source); for (i = 0; i < responseHeaderLineCount; i++) { responseHeadersBuilder.addLenient(source.readUtf8LineStrict()); } this.responseHeaders = responseHeadersBuilder.build(); if (isHttps()) { String blank = source.readUtf8LineStrict(); if (blank.length() > 0) { throw new IOException("expected \"\" but was \"" + blank + a.e); } this.handshake = Handshake.get(source.readUtf8LineStrict(), readCertificateList(source), readCertificateList(source)); } else { this.handshake = null; } in.close(); } catch (Throwable th) { in.close(); } }
public final String getHeaderField(String fieldName) { if (fieldName != null) { return getHeaders().get(fieldName); } try { return StatusLine.get(getResponse().getResponse()).toString(); } catch (IOException e) { return null; } }
public final Map<String, List<String>> getHeaderFields() { try { return OkHeaders.toMultimap(getHeaders(), StatusLine.get(getResponse().getResponse()) .toString()); } catch (IOException e) { return Collections.emptyMap(); } }
public void writeTo(DiskLruCache.Editor editor) throws IOException { OutputStream out = editor.newOutputStream(ENTRY_METADATA); Writer writer = new BufferedWriter(new OutputStreamWriter(out, UTF_8)); writer.write(url); writer.write('\n'); writer.write(requestMethod); writer.write('\n'); writer.write(Integer.toString(varyHeaders.size())); writer.write('\n'); for (int i = 0; i < varyHeaders.size(); i++) { writer.write(varyHeaders.name(i)); writer.write(": "); writer.write(varyHeaders.value(i)); writer.write('\n'); } writer.write(new StatusLine(protocol, code, message).toString()); writer.write('\n'); writer.write(Integer.toString(responseHeaders.size())); writer.write('\n'); for (int i = 0; i < responseHeaders.size(); i++) { writer.write(responseHeaders.name(i)); writer.write(": "); writer.write(responseHeaders.value(i)); writer.write('\n'); } if (isHttps()) { writer.write('\n'); writer.write(handshake.cipherSuite()); writer.write('\n'); writeCertArray(writer, handshake.peerCertificates()); writeCertArray(writer, handshake.localCertificates()); } writer.close(); }
/** * Returns the value of the field corresponding to the {@code fieldName}, or * null if there is no such field. If the field has multiple values, the * last value is returned. */ @Override public final String getHeaderField(String fieldName) { try { return fieldName == null ? StatusLine.get(getResponse().getResponse()).toString() : getHeaders().get(fieldName); } catch (IOException e) { return null; } }
@Override public final Map<String, List<String>> getHeaderFields() { try { return OkHeaders.toMultimap(getHeaders(), StatusLine.get(getResponse().getResponse()).toString()); } catch (IOException e) { return Collections.emptyMap(); } }
@Override public String getHeaderField(int position) { // Deal with index 0 meaning "status line" if (position < 0) { throw new IllegalArgumentException("Invalid header index: " + position); } if (position == 0) { return StatusLine.get(response).toString(); } return response.headers().value(position - 1); }
/** * Creates an OkHttp {@link Response} using the supplied {@link URI} and {@link URLConnection} * to supply the data. The URLConnection is assumed to already be connected. */ public static Response createOkResponse(URI uri, URLConnection urlConnection) throws IOException { HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; Response.Builder okResponseBuilder = new Response.Builder(); // Request: Create one from the URL connection. // A connected HttpURLConnection does not permit access to request headers. Map<String, List<String>> requestHeaders = null; Request okRequest = createOkRequest(uri, httpUrlConnection.getRequestMethod(), requestHeaders); okResponseBuilder.request(okRequest); // Status line StatusLine statusLine = StatusLine.parse(extractStatusLine(httpUrlConnection)); okResponseBuilder.protocol(statusLine.protocol); okResponseBuilder.code(statusLine.code); okResponseBuilder.message(statusLine.message); // Response headers Headers okHeaders = extractOkResponseHeaders(httpUrlConnection); okResponseBuilder.headers(okHeaders); // Response body ResponseBody okBody = createOkBody(okHeaders, urlConnection.getInputStream()); okResponseBuilder.body(okBody); // Handle SSL handshake information as needed. if (httpUrlConnection instanceof HttpsURLConnection) { HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) httpUrlConnection; Certificate[] peerCertificates; try { peerCertificates = httpsUrlConnection.getServerCertificates(); } catch (SSLPeerUnverifiedException e) { peerCertificates = null; } Certificate[] localCertificates = httpsUrlConnection.getLocalCertificates(); Handshake handshake = Handshake.get( httpsUrlConnection.getCipherSuite(), nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates)); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
/** * Creates an OkHttp {@link Response} using the supplied {@link Request} and {@link CacheResponse} * to supply the data. */ static Response createOkResponse(Request request, CacheResponse javaResponse) throws IOException { Response.Builder okResponseBuilder = new Response.Builder(); // Request: Use the one provided. okResponseBuilder.request(request); // Status line: Java has this as one of the headers. StatusLine statusLine = StatusLine.parse(extractStatusLine(javaResponse)); okResponseBuilder.protocol(statusLine.protocol); okResponseBuilder.code(statusLine.code); okResponseBuilder.message(statusLine.message); // Response headers Headers okHeaders = extractOkHeaders(javaResponse); okResponseBuilder.headers(okHeaders); // Response body ResponseBody okBody = createOkBody(okHeaders, javaResponse.getBody()); okResponseBuilder.body(okBody); // Handle SSL handshake information as needed. if (javaResponse instanceof SecureCacheResponse) { SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse; // Handshake doesn't support null lists. List<Certificate> peerCertificates; try { peerCertificates = javaSecureCacheResponse.getServerCertificateChain(); } catch (SSLPeerUnverifiedException e) { peerCertificates = Collections.emptyList(); } List<Certificate> localCertificates = javaSecureCacheResponse.getLocalCertificateChain(); if (localCertificates == null) { localCertificates = Collections.emptyList(); } Handshake handshake = Handshake.get( javaSecureCacheResponse.getCipherSuite(), peerCertificates, localCertificates); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
@Override public String getHeaderField(String fieldName) { return fieldName == null ? StatusLine.get(response).toString() : response.headers().get(fieldName); }
@Override public Map<String, List<String>> getHeaderFields() { return OkHeaders.toMultimap(response.headers(), StatusLine.get(response).toString()); }
private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress, String proxyUsername, String proxyPassword) throws IOException, StatusException { try { Socket sock; // The proxy address may not be resolved if (proxyAddress.getAddress() != null) { sock = new Socket(proxyAddress.getAddress(), proxyAddress.getPort()); } else { sock = new Socket(proxyAddress.getHostName(), proxyAddress.getPort()); } sock.setTcpNoDelay(true); Source source = Okio.source(sock); BufferedSink sink = Okio.buffer(Okio.sink(sock)); // Prepare headers and request method line Request proxyRequest = createHttpProxyRequest(address, proxyUsername, proxyPassword); HttpUrl url = proxyRequest.httpUrl(); String requestLine = String.format("CONNECT %s:%d HTTP/1.1", url.host(), url.port()); // Write request to socket sink.writeUtf8(requestLine).writeUtf8("\r\n"); for (int i = 0, size = proxyRequest.headers().size(); i < size; i++) { sink.writeUtf8(proxyRequest.headers().name(i)) .writeUtf8(": ") .writeUtf8(proxyRequest.headers().value(i)) .writeUtf8("\r\n"); } sink.writeUtf8("\r\n"); // Flush buffer (flushes socket and sends request) sink.flush(); // Read status line, check if 2xx was returned StatusLine statusLine = StatusLine.parse(readUtf8LineStrictUnbuffered(source)); // Drain rest of headers while (!readUtf8LineStrictUnbuffered(source).equals("")) {} if (statusLine.code < 200 || statusLine.code >= 300) { Buffer body = new Buffer(); try { sock.shutdownOutput(); source.read(body, 1024); } catch (IOException ex) { body.writeUtf8("Unable to read body: " + ex.toString()); } try { sock.close(); } catch (IOException ignored) { // ignored } String message = String.format( "Response returned from proxy was not successful (expected 2xx, got %d %s). " + "Response body:\n%s", statusLine.code, statusLine.message, body.readUtf8()); throw Status.UNAVAILABLE.withDescription(message).asException(); } return sock; } catch (IOException e) { throw Status.UNAVAILABLE.withDescription("Failed trying to connect with proxy").withCause(e) .asException(); } }