/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private void makeTunnel(TunnelRequest tunnelRequest) throws IOException { RawHeaders requestHeaders = tunnelRequest.getRequestHeaders(); while (true) { out.write(requestHeaders.toBytes()); RawHeaders responseHeaders = RawHeaders.fromBytes(in); switch (responseHeaders.getResponseCode()) { case HTTP_OK: return; case HTTP_PROXY_AUTH: requestHeaders = new RawHeaders(requestHeaders); URL url = new URL("https", tunnelRequest.host, tunnelRequest.port, "/"); boolean credentialsFound = HttpAuthenticator.processAuthHeader( route.address.authenticator, HTTP_PROXY_AUTH, responseHeaders, requestHeaders, route.proxy, url); if (credentialsFound) { continue; } else { throw new IOException("Failed to authenticate with proxy"); } default: throw new IOException( "Unexpected response code for CONNECT: " + responseHeaders.getResponseCode()); } } }
/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private void makeTunnel(TunnelRequest tunnelRequest) throws IOException { HttpConnection tunnelConnection = new HttpConnection(pool, this, socket); Request request = tunnelRequest.getRequest(); String requestLine = tunnelRequest.requestLine(); while (true) { tunnelConnection.writeRequest(request.headers(), requestLine); tunnelConnection.flush(); Response response = tunnelConnection.readResponse().request(request).build(); tunnelConnection.emptyResponseBody(); switch (response.code()) { case HTTP_OK: // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If that // happens, then we will have buffered bytes that are needed by the SSLSocket! if (tunnelConnection.bufferSize() > 0) { throw new IOException("TLS tunnel buffered too many bytes!"); } return; case HTTP_PROXY_AUTH: request = HttpAuthenticator.processAuthHeader( route.address.authenticator, response, route.proxy); if (request != null) continue; throw new IOException("Failed to authenticate with proxy"); default: throw new IOException("Unexpected response code for CONNECT: " + response.code()); } } }
/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private void makeTunnel(TunnelRequest tunnelRequest) throws IOException { Request request = tunnelRequest.getRequest(); String requestLine = tunnelRequest.requestLine(); while (true) { HttpTransport.writeRequest(out, request.headers(), requestLine); Response response = HttpTransport.readResponse(in).request(request).build(); switch (response.code()) { case HTTP_OK: return; case HTTP_PROXY_AUTH: request = HttpAuthenticator.processAuthHeader( route.address.authenticator, response, route.proxy); if (request != null) continue; throw new IOException("Failed to authenticate with proxy"); default: throw new IOException("Unexpected response code for CONNECT: " + response.code()); } } }