Search in sources :

Example 41 with ProtocolException

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

the class Http2Server method run.

private void run() throws Exception {
    ServerSocket serverSocket = new ServerSocket(8888);
    serverSocket.setReuseAddress(true);
    while (true) {
        Socket socket = null;
        try {
            socket = serverSocket.accept();
            SSLSocket sslSocket = doSsl(socket);
            String protocolString = Platform.get().getSelectedProtocol(sslSocket);
            Protocol protocol = protocolString != null ? Protocol.get(protocolString) : null;
            if (protocol != Protocol.HTTP_2) {
                throw new ProtocolException("Protocol " + protocol + " unsupported");
            }
            Http2Connection connection = new Http2Connection.Builder(false).socket(sslSocket).listener(this).build();
            connection.start();
        } catch (IOException e) {
            logger.log(Level.INFO, "Http2Server connection failure: " + e);
            Util.closeQuietly(socket);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Http2Server unexpected failure", e);
            Util.closeQuietly(socket);
        }
    }
}
Also used : ProtocolException(java.net.ProtocolException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Protocol(okhttp3.Protocol) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 42 with ProtocolException

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

the class URLConnectionTest method doesNotAttemptAuthorization21Times.

@Test
public void doesNotAttemptAuthorization21Times() throws Exception {
    for (int i = 0; i < 21; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    String credential = Credentials.basic("jesse", "peanutbutter");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getInputStream();
        fail();
    } catch (ProtocolException expected) {
        assertEquals(401, connection.getResponseCode());
        assertEquals("Too many follow-up requests: 21", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 43 with ProtocolException

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

the class URLConnectionTest method contentDisagreesWithChunkedHeaderBodyTooShort.

@Test
public void contentDisagreesWithChunkedHeaderBodyTooShort() throws IOException {
    MockResponse mockResponse = new MockResponse();
    mockResponse.setChunkedBody("abcde", 5);
    Buffer truncatedBody = new Buffer();
    Buffer fullBody = mockResponse.getBody();
    truncatedBody.write(fullBody, fullBody.indexOf((byte) 'e'));
    mockResponse.setBody(truncatedBody);
    mockResponse.clearHeaders();
    mockResponse.addHeader("Transfer-encoding: chunked");
    mockResponse.setSocketPolicy(DISCONNECT_AT_END);
    server.enqueue(mockResponse);
    try {
        readAscii(urlFactory.open(server.url("/").url()).getInputStream(), 5);
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) Test(org.junit.Test)

Example 44 with ProtocolException

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

the class URLConnectionTest method getResponseCodeNoResponseBody.

@Test
public void getResponseCodeNoResponseBody() throws Exception {
    server.enqueue(new MockResponse().addHeader("abc: def"));
    URL url = server.url("/").url();
    HttpURLConnection conn = urlFactory.open(url);
    conn.setDoInput(false);
    assertEquals("def", conn.getHeaderField("abc"));
    assertEquals(200, conn.getResponseCode());
    try {
        conn.getInputStream();
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 45 with ProtocolException

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

the class URLConnectionTest method getOutputStreamOnGetFails.

@Test
public void getOutputStreamOnGetFails() throws Exception {
    server.enqueue(new MockResponse());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getOutputStream();
        fail();
    } catch (ProtocolException expected) {
    }
    connection.getInputStream().close();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) Test(org.junit.Test)

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