Search in sources :

Example 1 with StatusLine

use of com.squareup.okhttp.internal.http.StatusLine in project grpc-java by grpc.

the class OkHttpClientTransport method createHttpProxySocket.

private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress, String proxyUsername, String proxyPassword) throws IOException, StatusException {
    try {
        Socket sock = new Socket(proxyAddress.getAddress(), 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();
    }
}
Also used : StatusLine(com.squareup.okhttp.internal.http.StatusLine) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) BufferedSink(okio.BufferedSink) ByteString(okio.ByteString) IOException(java.io.IOException) Socket(java.net.Socket) BufferedSource(okio.BufferedSource) Source(okio.Source) HttpUrl(com.squareup.okhttp.HttpUrl)

Aggregations

HttpUrl (com.squareup.okhttp.HttpUrl)1 Request (com.squareup.okhttp.Request)1 StatusLine (com.squareup.okhttp.internal.http.StatusLine)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 Buffer (okio.Buffer)1 BufferedSink (okio.BufferedSink)1 BufferedSource (okio.BufferedSource)1 ByteString (okio.ByteString)1 Source (okio.Source)1