@Override protected void execute() { try { logger.info(MockWebServer.this + " starting to accept connections"); acceptConnections(); } catch (Throwable e) { logger.log(Level.WARNING, MockWebServer.this + " failed unexpectedly", e); } // Release all sockets and all threads, even if any close fails. closeQuietly(serverSocket); for (Iterator<Socket> s = openClientSockets.iterator(); s.hasNext(); ) { closeQuietly(s.next()); s.remove(); } for (Iterator<Http2Connection> s = openConnections.iterator(); s.hasNext(); ) { closeQuietly(s.next()); s.remove(); } dispatcher.shutdown(); executor.shutdown(); }
@Override public MockResponse peek() { MockResponse peek = responseQueue.peek(); if (peek != null) return peek; if (failFastResponse != null) return failFastResponse; return super.peek(); }
private void dispatchBookkeepingRequest(int sequenceNumber, Socket socket) throws InterruptedException { RecordedRequest request = new RecordedRequest( null, null, null, -1, null, sequenceNumber, socket); requestCount.incrementAndGet(); requestQueue.add(request); dispatcher.dispatch(request); }
private void acceptConnections() throws Exception { while (true) { Socket socket; try { socket = serverSocket.accept(); } catch (SocketException e) { logger.info(MockWebServer.this + " done accepting connections: " + e.getMessage()); return; } SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (socketPolicy == DISCONNECT_AT_START) { dispatchBookkeepingRequest(0, socket); socket.close(); } else { openClientSockets.add(socket); serveConnection(socket); } } } });
requestQueue.add(request); MockResponse response = dispatcher.dispatch(request); if (response.getSocketPolicy() == DISCONNECT_AFTER_REQUEST) { socket.close();
/** * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response is * dispatched. */ private void createTunnel() throws IOException, InterruptedException { BufferedSource source = Okio.buffer(Okio.source(raw)); BufferedSink sink = Okio.buffer(Okio.sink(raw)); while (true) { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (!processOneRequest(raw, source, sink)) { throw new IllegalStateException("Tunnel without any CONNECT!"); } if (socketPolicy == UPGRADE_TO_SSL_AT_END) return; } }
private void dispatchBookkeepingRequest(int sequenceNumber, Socket socket) throws InterruptedException { RecordedRequest request = new RecordedRequest( null, null, null, -1, null, sequenceNumber, socket); requestCount.incrementAndGet(); requestQueue.add(request); dispatcher.dispatch(request); }
@Override protected void execute() { try { logger.info(MockWebServer.this + " starting to accept connections"); acceptConnections(); } catch (Throwable e) { logger.log(Level.WARNING, MockWebServer.this + " failed unexpectedly", e); } // Release all sockets and all threads, even if any close fails. closeQuietly(serverSocket); for (Iterator<Socket> s = openClientSockets.iterator(); s.hasNext(); ) { closeQuietly(s.next()); s.remove(); } for (Iterator<Http2Connection> s = openConnections.iterator(); s.hasNext(); ) { closeQuietly(s.next()); s.remove(); } dispatcher.shutdown(); executor.shutdown(); }
final SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (expectContinue && socketPolicy == EXPECT_CONTINUE || socketPolicy == CONTINUE_ALWAYS) { sink.writeUtf8("HTTP/1.1 100 Continue\r\n"); TruncatingBuffer requestBody = new TruncatingBuffer(bodyLimit); List<Integer> chunkSizes = new ArrayList<>(); MockResponse policy = dispatcher.peek(); if (contentLength != -1) { hasBody = contentLength > 0;
requestQueue.add(request); MockResponse response = dispatcher.dispatch(request); if (response.getSocketPolicy() == DISCONNECT_AFTER_REQUEST) { socket.close();
public void processConnection() throws Exception { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); Protocol protocol = Protocol.HTTP_1_1; Socket socket;
@Test public void interceptDispatch() throws InterruptedException, IOException { Dispatcher dispatcher = spy(new QueueDispatcher()); server.setDispatcher(dispatcher); server.enqueue(new MockResponse()); execute(); verify(dispatcher).dispatch(any(RecordedRequest.class)); }
MockResponse peek = dispatcher.peek(); if (peek.isDuplex()) { readBody = false;
@Override public MockResponse peek() { MockResponse peek = responseQueue.peek(); if (peek != null) return peek; if (failFastResponse != null) return failFastResponse; return super.peek(); }
private void acceptConnections() throws Exception { while (true) { Socket socket; try { socket = serverSocket.accept(); } catch (SocketException e) { logger.info(MockWebServer.this + " done accepting connections: " + e.getMessage()); return; } SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (socketPolicy == DISCONNECT_AT_START) { dispatchBookkeepingRequest(0, socket); socket.close(); } else { openClientSockets.add(socket); serveConnection(socket); } } } });
/** * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response is * dispatched. */ private void createTunnel() throws IOException, InterruptedException { BufferedSource source = Okio.buffer(Okio.source(raw)); BufferedSink sink = Okio.buffer(Okio.sink(raw)); while (true) { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (!processOneRequest(raw, source, sink)) { throw new IllegalStateException("Tunnel without any CONNECT!"); } if (socketPolicy == UPGRADE_TO_SSL_AT_END) return; } }
if (expectContinue && dispatcher.peek().getSocketPolicy() == EXPECT_CONTINUE) { sink.writeUtf8("HTTP/1.1 100 Continue\r\n"); sink.writeUtf8("Content-Length: 0\r\n"); TruncatingBuffer requestBody = new TruncatingBuffer(bodyLimit); List<Integer> chunkSizes = new ArrayList<>(); MockResponse policy = dispatcher.peek(); if (contentLength != -1) { hasBody = contentLength > 0;
createTunnel(); SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (socketPolicy == FAIL_HANDSHAKE) { dispatchBookkeepingRequest(sequenceNumber, raw);