Search in sources :

Example 36 with ProtocolException

use of java.net.ProtocolException in project okhttp by square.

the class RetryAndFollowUpInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()), callStackTrace);
    int followUpCount = 0;
    Response priorResponse = null;
    while (true) {
        if (canceled) {
            streamAllocation.release();
            throw new IOException("Canceled");
        }
        Response response = null;
        boolean releaseConnection = true;
        try {
            response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
            releaseConnection = false;
        } catch (RouteException e) {
            // The attempt to connect via a route failed. The request will not have been sent.
            if (!recover(e.getLastConnectException(), false, request)) {
                throw e.getLastConnectException();
            }
            releaseConnection = false;
            continue;
        } catch (IOException e) {
            // An attempt to communicate with a server failed. The request may have been sent.
            boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
            if (!recover(e, requestSendStarted, request))
                throw e;
            releaseConnection = false;
            continue;
        } finally {
            // We're throwing an unchecked exception. Release any resources.
            if (releaseConnection) {
                streamAllocation.streamFailed(null);
                streamAllocation.release();
            }
        }
        // Attach the prior response if it exists. Such responses never have a body.
        if (priorResponse != null) {
            response = response.newBuilder().priorResponse(priorResponse.newBuilder().body(null).build()).build();
        }
        Request followUp = followUpRequest(response);
        if (followUp == null) {
            if (!forWebSocket) {
                streamAllocation.release();
            }
            return response;
        }
        closeQuietly(response.body());
        if (++followUpCount > MAX_FOLLOW_UPS) {
            streamAllocation.release();
            throw new ProtocolException("Too many follow-up requests: " + followUpCount);
        }
        if (followUp.body() instanceof UnrepeatableRequestBody) {
            streamAllocation.release();
            throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
        }
        if (!sameConnection(response, followUp.url())) {
            streamAllocation.release();
            streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(followUp.url()), callStackTrace);
        } else if (streamAllocation.codec() != null) {
            throw new IllegalStateException("Closing the body of " + response + " didn't close its backing stream. Bad interceptor?");
        }
        request = followUp;
        priorResponse = response;
    }
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation) Response(okhttp3.Response) RouteException(okhttp3.internal.connection.RouteException) ProtocolException(java.net.ProtocolException) ConnectionShutdownException(okhttp3.internal.http2.ConnectionShutdownException) Request(okhttp3.Request) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpRetryException(java.net.HttpRetryException)

Example 37 with ProtocolException

use of java.net.ProtocolException in project okhttp by square.

the class RealWebSocket method connect.

public void connect(OkHttpClient client) {
    client = client.newBuilder().protocols(ONLY_HTTP1).build();
    final int pingIntervalMillis = client.pingIntervalMillis();
    final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
    call = Internal.instance.newWebSocketCall(client, request);
    call.enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) {
            try {
                checkResponse(response);
            } catch (ProtocolException e) {
                failWebSocket(e, response);
                closeQuietly(response);
                return;
            }
            // Promote the HTTP streams into web socket streams.
            StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
            // Prevent connection pooling!
            streamAllocation.noNewStreams();
            Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
            // Process all web socket messages.
            try {
                listener.onOpen(RealWebSocket.this, response);
                String name = "OkHttp WebSocket " + request.url().redact();
                initReaderAndWriter(name, pingIntervalMillis, streams);
                streamAllocation.connection().socket().setSoTimeout(0);
                loopReader();
            } catch (Exception e) {
                failWebSocket(e, null);
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            failWebSocket(e, null);
        }
    });
}
Also used : Response(okhttp3.Response) StreamAllocation(okhttp3.internal.connection.StreamAllocation) Call(okhttp3.Call) ProtocolException(java.net.ProtocolException) Callback(okhttp3.Callback) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 38 with ProtocolException

use of java.net.ProtocolException in project okhttp by square.

the class WebSocketReader method readControlFrame.

private void readControlFrame() throws IOException {
    Buffer buffer = new Buffer();
    if (frameBytesRead < frameLength) {
        if (isClient) {
            source.readFully(buffer, frameLength);
        } else {
            while (frameBytesRead < frameLength) {
                int toRead = (int) Math.min(frameLength - frameBytesRead, maskBuffer.length);
                int read = source.read(maskBuffer, 0, toRead);
                if (read == -1)
                    throw new EOFException();
                toggleMask(maskBuffer, read, maskKey, frameBytesRead);
                buffer.write(maskBuffer, 0, read);
                frameBytesRead += read;
            }
        }
    }
    switch(opcode) {
        case OPCODE_CONTROL_PING:
            frameCallback.onReadPing(buffer.readByteString());
            break;
        case OPCODE_CONTROL_PONG:
            frameCallback.onReadPong(buffer.readByteString());
            break;
        case OPCODE_CONTROL_CLOSE:
            int code = CLOSE_NO_STATUS_CODE;
            String reason = "";
            long bufferSize = buffer.size();
            if (bufferSize == 1) {
                throw new ProtocolException("Malformed close payload length of 1.");
            } else if (bufferSize != 0) {
                code = buffer.readShort();
                reason = buffer.readUtf8();
                String codeExceptionMessage = WebSocketProtocol.closeCodeExceptionMessage(code);
                if (codeExceptionMessage != null)
                    throw new ProtocolException(codeExceptionMessage);
            }
            frameCallback.onReadClose(code, reason);
            closed = true;
            break;
        default:
            throw new ProtocolException("Unknown control opcode: " + toHexString(opcode));
    }
}
Also used : Buffer(okio.Buffer) ProtocolException(java.net.ProtocolException) EOFException(java.io.EOFException) Integer.toHexString(java.lang.Integer.toHexString) ByteString(okio.ByteString)

Example 39 with ProtocolException

use of java.net.ProtocolException in project okhttp by square.

the class WebSocketReader method readMessage.

/**
   * Reads a message body into across one or more frames. Control frames that occur between
   * fragments will be processed. If the message payload is masked this will unmask as it's being
   * processed.
   */
private void readMessage(Buffer sink) throws IOException {
    while (true) {
        if (closed)
            throw new IOException("closed");
        if (frameBytesRead == frameLength) {
            // We are exhausted and have no continuations.
            if (isFinalFrame)
                return;
            readUntilNonControlFrame();
            if (opcode != OPCODE_CONTINUATION) {
                throw new ProtocolException("Expected continuation opcode. Got: " + toHexString(opcode));
            }
            if (isFinalFrame && frameLength == 0) {
                // Fast-path for empty final frame.
                return;
            }
        }
        long toRead = frameLength - frameBytesRead;
        long read;
        if (isMasked) {
            toRead = Math.min(toRead, maskBuffer.length);
            read = source.read(maskBuffer, 0, (int) toRead);
            if (read == -1)
                throw new EOFException();
            toggleMask(maskBuffer, read, maskKey, frameBytesRead);
            sink.write(maskBuffer, 0, (int) read);
        } else {
            read = source.read(sink, toRead);
            if (read == -1)
                throw new EOFException();
        }
        frameBytesRead += read;
    }
}
Also used : ProtocolException(java.net.ProtocolException) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 40 with ProtocolException

use of java.net.ProtocolException in project okhttp by square.

the class WebSocketReader method readMessageFrame.

private void readMessageFrame() throws IOException {
    int opcode = this.opcode;
    if (opcode != OPCODE_TEXT && opcode != OPCODE_BINARY) {
        throw new ProtocolException("Unknown opcode: " + toHexString(opcode));
    }
    Buffer message = new Buffer();
    readMessage(message);
    if (opcode == OPCODE_TEXT) {
        frameCallback.onReadMessage(message.readUtf8());
    } else {
        frameCallback.onReadMessage(message.readByteString());
    }
}
Also used : Buffer(okio.Buffer) ProtocolException(java.net.ProtocolException)

Aggregations

ProtocolException (java.net.ProtocolException)214 IOException (java.io.IOException)78 URL (java.net.URL)62 HttpURLConnection (java.net.HttpURLConnection)60 MalformedURLException (java.net.MalformedURLException)44 InputStreamReader (java.io.InputStreamReader)32 BufferedReader (java.io.BufferedReader)31 InputStream (java.io.InputStream)21 BufferedInputStream (java.io.BufferedInputStream)20 FileInputStream (java.io.FileInputStream)20 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileNotFoundException (java.io.FileNotFoundException)18 OutputStream (java.io.OutputStream)18 Test (org.junit.Test)16 DataInputStream (java.io.DataInputStream)14 Map (java.util.Map)13 AtomicFile (android.util.AtomicFile)12