Search in sources :

Example 36 with Connection

use of okhttp3.Connection 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 37 with Connection

use of okhttp3.Connection in project okhttp by square.

the class MockWebServer method handleWebSocketUpgrade.

private void handleWebSocketUpgrade(Socket socket, BufferedSource source, BufferedSink sink, RecordedRequest request, MockResponse response) throws IOException {
    String key = request.getHeader("Sec-WebSocket-Key");
    response.setHeader("Sec-WebSocket-Accept", WebSocketProtocol.acceptHeader(key));
    writeHttpResponse(socket, sink, response);
    // Adapt the request and response into our Request and Response domain model.
    String scheme = request.getTlsVersion() != null ? "https" : "http";
    // Has host and port.
    String authority = request.getHeader("Host");
    final Request fancyRequest = new Request.Builder().url(scheme + "://" + authority + "/").headers(request.getHeaders()).build();
    final Response fancyResponse = new Response.Builder().code(Integer.parseInt(response.getStatus().split(" ")[1])).message(response.getStatus().split(" ", 3)[2]).headers(response.getHeaders()).request(fancyRequest).protocol(Protocol.HTTP_1_1).build();
    final CountDownLatch connectionClose = new CountDownLatch(1);
    RealWebSocket.Streams streams = new RealWebSocket.Streams(false, source, sink) {

        @Override
        public void close() {
            connectionClose.countDown();
        }
    };
    RealWebSocket webSocket = new RealWebSocket(fancyRequest, response.getWebSocketListener(), new SecureRandom());
    response.getWebSocketListener().onOpen(webSocket, fancyResponse);
    String name = "MockWebServer WebSocket " + request.getPath();
    webSocket.initReaderAndWriter(name, 0, streams);
    try {
        webSocket.loopReader();
        // Even if messages are no longer being read we need to wait for the connection close signal.
        try {
            connectionClose.await();
        } catch (InterruptedException ignored) {
        }
    } catch (IOException e) {
        webSocket.failWebSocket(e, null);
    } finally {
        closeQuietly(sink);
        closeQuietly(source);
    }
}
Also used : Response(okhttp3.Response) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Request(okhttp3.Request) SecureRandom(java.security.SecureRandom) ByteString(okio.ByteString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 38 with Connection

use of okhttp3.Connection in project okhttp by square.

the class CacheAdapterTest method put_httpsGet.

@Test
public void put_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpsURLConnection);
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection;
                HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection;
                assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite());
                assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal());
                assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates());
                assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates());
                assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal());
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 39 with Connection

use of okhttp3.Connection in project okhttp by square.

the class CacheAdapterTest method get_httpGet.

@Test
public void get_httpGet() throws Exception {
    final URL serverUrl = configureServer(new MockResponse());
    assertEquals("http", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 40 with Connection

use of okhttp3.Connection in project okhttp by square.

the class CacheAdapterTest method get_httpsGet.

@Test
public void get_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals("https", uri.getScheme());
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)79 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)43 Connection (com.trilead.ssh2.Connection)40 InputStream (java.io.InputStream)40 Request (okhttp3.Request)38 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)26 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 ResponseBody (okhttp3.ResponseBody)14