Search in sources :

Example 16 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingSmallContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingSmallContent() throws Exception {
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final int chunk1 = 'q';
    final int chunk2 = 'w';
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            response.flushBuffer();
            // Wait for the client to receive this chunk.
            await(chunk1Latch, 5000);
            // Send second chunk, must not be received by proxy.
            output.write(chunk2);
        }

        private boolean await(CountDownLatch latch, long ms) throws IOException {
            try {
                return latch.await(ms, TimeUnit.MILLISECONDS);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    final long proxyTimeout = 1000;
    Map<String, String> proxyParams = new HashMap<>();
    proxyParams.put("timeout", String.valueOf(proxyTimeout));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    // Make the proxy request fail; given the small content, the
    // proxy-to-client response is not committed yet so it will be reset.
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(504, response.getStatus());
    // Make sure there is no content, as the proxy-to-client response has been reset.
    InputStream input = listener.getInputStream();
    Assert.assertEquals(-1, input.read());
    chunk1Latch.countDown();
    // Result succeeds because a 504 is a valid HTTP response.
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertTrue(result.isSucceeded());
    // Make sure the proxy does not receive chunk2.
    Assert.assertEquals(-1, input.read());
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 17 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ConnectHandlerTest method testCONNECTAndGETPipelined.

@Test
public void testCONNECTAndGETPipelined() throws Exception {
    String hostPort = "localhost:" + serverConnector.getLocalPort();
    String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
    try (Socket socket = newSocket()) {
        OutputStream output = socket.getOutputStream();
        InputStream input = socket.getInputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        // Expect 200 OK from the CONNECT request
        HttpTester.Response response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        // The pipelined request must have gone up to the server as is
        response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        Assert.assertEquals("GET /echo", response.getContent());
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 18 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ConnectHandlerTest method testCONNECTAndMultipleGETs.

@Test
public void testCONNECTAndMultipleGETs() throws Exception {
    String hostPort = "localhost:" + serverConnector.getLocalPort();
    String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
    try (Socket socket = newSocket()) {
        OutputStream output = socket.getOutputStream();
        InputStream input = socket.getInputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        // Expect 200 OK from the CONNECT request
        HttpTester.Response response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        for (int i = 0; i < 10; ++i) {
            request = "" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
            output.write(request.getBytes(StandardCharsets.UTF_8));
            output.flush();
            response = readResponse(input);
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            Assert.assertEquals("GET /echo", response.getContent());
        }
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) EndPoint(org.eclipse.jetty.io.EndPoint) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 19 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ConnectHandlerTest method testCONNECT10AndGET.

@Test
public void testCONNECT10AndGET() throws Exception {
    String hostPort = "localhost:" + serverConnector.getLocalPort();
    String request = "" + "CONNECT " + hostPort + " HTTP/1.0\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
    try (Socket socket = newSocket()) {
        OutputStream output = socket.getOutputStream();
        InputStream input = socket.getInputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        // Expect 200 OK from the CONNECT request
        HttpTester.Response response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        request = "" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        Assert.assertEquals("GET /echo", response.getContent());
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 20 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ConnectHandlerTest method testCONNECTAndGETServerStop.

@Test
public void testCONNECTAndGETServerStop() throws Exception {
    String hostPort = "localhost:" + serverConnector.getLocalPort();
    String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
    try (Socket socket = newSocket()) {
        OutputStream output = socket.getOutputStream();
        InputStream input = socket.getInputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        // Expect 200 OK from the CONNECT request
        HttpTester.Response response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        request = "" + "GET /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        Assert.assertEquals("GET /echo", response.getContent());
        // Idle server is shut down
        disposeServer();
        int read = input.read();
        Assert.assertEquals(-1, read);
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) EndPoint(org.eclipse.jetty.io.EndPoint) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)33681 IOException (java.io.IOException)12617 ByteArrayInputStream (java.io.ByteArrayInputStream)7643 Test (org.junit.Test)7105 FileInputStream (java.io.FileInputStream)6956 File (java.io.File)5058 InputStreamReader (java.io.InputStreamReader)3045 URL (java.net.URL)3010 OutputStream (java.io.OutputStream)2893 BufferedInputStream (java.io.BufferedInputStream)2558 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2228 ArrayList (java.util.ArrayList)2193 FileOutputStream (java.io.FileOutputStream)2191 BufferedReader (java.io.BufferedReader)2039 Properties (java.util.Properties)1679 FileNotFoundException (java.io.FileNotFoundException)1503 HashMap (java.util.HashMap)1264 HttpURLConnection (java.net.HttpURLConnection)1144 Map (java.util.Map)866 Document (org.w3c.dom.Document)847