Java Code Examples for org.apache.catalina.startup.SimpleHttpClient#CRLF

The following examples show how to use org.apache.catalina.startup.SimpleHttpClient#CRLF . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testWithTEBuffered() throws Exception {
    getTomcatInstanceTestWebapp(false, true);

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: buffered" + SimpleHttpClient.CRLF +
        "Content-Length: 9" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
                SimpleHttpClient.CRLF +
        "test=data";

    Client client = new Client(getPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse501());
}
 
Example 2
Source File: TestAbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithUnknownExpectation() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // Use the normal Tomcat ROOT context
    File root = new File("test/webapp-3.0");
    tomcat.addWebapp("", root.getAbsolutePath());

    tomcat.start();

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Expect: unknoen" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse417());
}
 
Example 3
Source File: TestAbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void doTestWithTEChunked(boolean withCL)
        throws Exception {

    Tomcat tomcat = getTomcatInstance();

    // Use the normal Tomcat ROOT context
    File root = new File("test/webapp-3.0");
    tomcat.addWebapp("", root.getAbsolutePath());

    tomcat.start();

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        (withCL ? "Content-length: 1" + SimpleHttpClient.CRLF : "") +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "9" + SimpleHttpClient.CRLF +
        "test=data" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse200());
    assertTrue(client.getResponseBody().contains("test - data"));
}
 
Example 4
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testConsistentHostHeader03() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new ServerNameTesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");

    tomcat.start();

    String request =
            "GET http://user:pwd@a/foo HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host: a" + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 200 response.
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals("request.getServerName() is [a] and request.getServerPort() is 80", client.getResponseBody());
}
 
Example 5
Source File: TestAbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithTESavedRequest() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // Use the normal Tomcat ROOT context
    File root = new File("test/webapp-3.0");
    tomcat.addWebapp("", root.getAbsolutePath());

    tomcat.start();

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: savedrequest" + SimpleHttpClient.CRLF +
        "Content-Length: 9" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "test=data";

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse501());
}
 
Example 6
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBlankHostHeader02() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new ServerNameTesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");

    tomcat.start();

    String request =
            "GET /foo HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host:      " + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 200 response.
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals("request.getServerName() is [] and request.getServerPort() is " + getPort(), client.getResponseBody());
}
 
Example 7
Source File: TestAbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithTEUnsupported() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // Use the normal Tomcat ROOT context
    File root = new File("test/webapp-3.0");
    tomcat.addWebapp("", root.getAbsolutePath());

    tomcat.start();

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: unsupported" + SimpleHttpClient.CRLF +
        "Content-Length: 9" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "test=data";

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse501());
}
 
Example 8
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testInconsistentHostHeader03() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    tomcat.getConnector().setAttribute("allowHostHeaderMismatch", "false");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");

    tomcat.start();

    String request =
            "GET http://user:pwd@a/foo HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host: b" + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 400 response.
    Assert.assertTrue(client.isResponse400());
}
 
Example 9
Source File: TestInternalInputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug51557Continuation() {

    Bug51557Client client = new Bug51557Client("X-Bug=51557NoColon",
            "foo" + SimpleHttpClient.CRLF + " bar");

    client.doRequest();
    assertTrue(client.isResponse200());
    assertEquals("abcd", client.getResponseBody());
    assertTrue(client.isResponseBodyOK());
}
 
Example 10
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testConsistentHostHeader02() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new ServerNameTesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");

    tomcat.start();

    String request =
            "GET http://a:8080/foo HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host: a:8080" + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 200 response.
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals("request.getServerName() is [a] and request.getServerPort() is 8080", client.getResponseBody());

}
 
Example 11
Source File: TestAbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithTEBuffered() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // Use the normal Tomcat ROOT context
    File root = new File("test/webapp-3.0");
    tomcat.addWebapp("", root.getAbsolutePath());

    tomcat.start();

    String request =
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: buffered" + SimpleHttpClient.CRLF +
        "Content-Length: 9" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "test=data";

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse501());
}
 
Example 12
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testInconsistentHostHeader01() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    tomcat.getConnector().setAttribute("allowHostHeaderMismatch", "false");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");

    tomcat.start();

    String request =
            "GET http://a/foo HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host: b" + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 400 response.
    Assert.assertTrue(client.isResponse400());
}
 
Example 13
Source File: TestChunkedInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    tomcat.getConnector().setProperty(
            "maxExtensionSize", Integer.toString(EXT_SIZE_LIMIT));

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(ok));
    ctx.addServletMapping("/", "servlet");

    tomcat.start();

    String extName = ";foo=";
    StringBuilder extValue = new StringBuilder(len);
    for (int i = 0; i < (len - extName.length()); i++) {
        extValue.append("x");
    }

    String[] request = new String[]{
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "3" + extName + extValue.toString() + SimpleHttpClient.CRLF +
        "a=0" + SimpleHttpClient.CRLF +
        "4" + SimpleHttpClient.CRLF +
        "&b=1" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF };

    TrailerClient client =
            new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);

    client.connect();
    client.processRequest();

    if (ok) {
        assertTrue(client.isResponse200());
    } else {
        assertTrue(client.isResponse500());
    }
}
 
Example 14
Source File: TestChunkedInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testTrailingHeadersSizeLimit() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(false));
    ctx.addServletMapping("/", "servlet");

    // Limit the size of the trailing header
    tomcat.getConnector().setProperty("maxTrailerSize", "10");
    tomcat.start();

    String[] request = new String[]{
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "3" + SimpleHttpClient.CRLF +
        "a=0" + SimpleHttpClient.CRLF +
        "4" + SimpleHttpClient.CRLF +
        "&b=1" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        "x-trailer: Test" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF };

    TrailerClient client =
            new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);

    client.connect();
    client.processRequest();
    // Expected to fail because the trailers are longer
    // than the set limit of 10 bytes
    assertTrue(client.isResponse500());
}
 
Example 15
Source File: TestChunkedInputFilter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testTrailingHeadersSizeLimit() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(false));
    ctx.addServletMapping("/", "servlet");

    // Limit the size of the trailing header
    tomcat.getConnector().setProperty("maxTrailerSize", "10");
    tomcat.start();

    String[] request = new String[]{
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "3" + SimpleHttpClient.CRLF +
        "a=0" + SimpleHttpClient.CRLF +
        "4" + SimpleHttpClient.CRLF +
        "&b=1" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        "x-trailer: Test" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF };

    TrailerClient client =
            new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);

    client.connect();
    client.processRequest();
    // Expected to fail because the trailers are longer
    // than the set limit of 10 bytes
    assertTrue(client.isResponse500());
}
 
Example 16
Source File: TestChunkedInputFilter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    tomcat.getConnector().setProperty(
            "maxExtensionSize", Integer.toString(EXT_SIZE_LIMIT));

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(ok));
    ctx.addServletMappingDecoded("/", "servlet");

    tomcat.start();

    String extName = ";foo=";
    StringBuilder extValue = new StringBuilder(len);
    for (int i = 0; i < (len - extName.length()); i++) {
        extValue.append("x");
    }

    String[] request = new String[]{
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "3" + extName + extValue.toString() + SimpleHttpClient.CRLF +
        "a=0" + SimpleHttpClient.CRLF +
        "4" + SimpleHttpClient.CRLF +
        "&b=1" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF };

    TrailerClient client =
            new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);

    client.connect();
    client.processRequest();

    if (ok) {
        Assert.assertTrue(client.isResponse200());
    } else {
        Assert.assertTrue(client.isResponse500());
    }
}
 
Example 17
Source File: TestChunkedInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * @param expectPass
 *            If the servlet is expected to process the request
 * @param expectReadWholeBody
 *            If the servlet is expected to fully read the body and reliably
 *            deliver a response
 * @param chunks
 *            Text of chunks
 * @param readLimit
 *            Do not read more than this many bytes
 * @param expectReadCount
 *            Expected count of read bytes
 * @throws Exception
 *             Unexpected
 */
private void doTestChunkSize(boolean expectPass,
        boolean expectReadWholeBody, String chunks, int readLimit,
        int expectReadCount) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    BodyReadServlet servlet = new BodyReadServlet(expectPass, readLimit);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMapping("/", "servlet");

    tomcat.start();

    String request = "POST /echo-params.jsp HTTP/1.1"
            + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF
            + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF
            + "Content-Type: text/plain" + SimpleHttpClient.CRLF;
    if (expectPass) {
        request += "Connection: close" + SimpleHttpClient.CRLF;
    }
    request += SimpleHttpClient.CRLF + chunks + "0" + SimpleHttpClient.CRLF
            + SimpleHttpClient.CRLF;

    TrailerClient client = new TrailerClient(tomcat.getConnector()
            .getLocalPort());
    client.setRequest(new String[] { request });

    Exception processException = null;
    client.connect();
    try {
        client.processRequest();
    } catch (Exception e) {
        // Socket was probably closed before client had a chance to read
        // response
        processException = e;
    }
    if (expectPass) {
        if (expectReadWholeBody) {
            assertNull(processException);
        }
        if (processException == null) {
            assertTrue(client.getResponseLine(), client.isResponse200());
            assertEquals(String.valueOf(expectReadCount),
                    client.getResponseBody());
        }
        assertEquals(expectReadCount, servlet.getCountRead());
    } else {
        if (processException == null) {
            assertTrue(client.getResponseLine(), client.isResponse500());
        }
        assertEquals(0, servlet.getCountRead());
        assertTrue(servlet.getExceptionDuringRead());
    }
}
 
Example 18
Source File: TestChunkedInputFilter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doTestChunkingCRLF(boolean chunkHeaderUsesCRLF,
        boolean chunkUsesCRLF, boolean firstheaderUsesCRLF,
        boolean secondheaderUsesCRLF, boolean endUsesCRLF,
        boolean expectPass) throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Configure allowed trailer headers
    tomcat.getConnector().setProperty("allowedTrailerHeaders", "X-Trailer1,X-Trailer2");

    EchoHeaderServlet servlet = new EchoHeaderServlet(expectPass);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");

    tomcat.start();

    String[] request = new String[]{
        "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF +
        "Host: any" + SimpleHttpClient.CRLF +
        "Transfer-encoding: chunked" + SimpleHttpClient.CRLF +
        "Content-Type: application/x-www-form-urlencoded" +
                SimpleHttpClient.CRLF +
        "Connection: close" + SimpleHttpClient.CRLF +
        SimpleHttpClient.CRLF +
        "3" + (chunkHeaderUsesCRLF ? SimpleHttpClient.CRLF : LF) +
        "a=0" + (chunkUsesCRLF ? SimpleHttpClient.CRLF : LF) +
        "4" + SimpleHttpClient.CRLF +
        "&b=1" + SimpleHttpClient.CRLF +
        "0" + SimpleHttpClient.CRLF +
        "x-trailer1: Test", "Value1" +
        (firstheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) +
        "x-trailer2: TestValue2" +
        (secondheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) +
        (endUsesCRLF ? SimpleHttpClient.CRLF : LF) };

    TrailerClient client =
            new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);

    client.connect();
    Exception processException = null;
    try {
        client.processRequest();
    } catch (Exception e) {
        // Socket was probably closed before client had a chance to read
        // response
        processException = e;
    }

    if (expectPass) {
        Assert.assertTrue(client.isResponse200());
        Assert.assertEquals("nullnull7TestValue1TestValue2",
                client.getResponseBody());
        Assert.assertNull(processException);
        Assert.assertFalse(servlet.getExceptionDuringRead());
    } else {
        if (processException == null) {
            Assert.assertTrue(client.getResponseLine(), client.isResponse500());
        } else {
            // Use fall-back for checking the error occurred
            Assert.assertTrue(servlet.getExceptionDuringRead());
        }
    }
}
 
Example 19
Source File: TestHttp11Processor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testResponseWithErrorChunked() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add protected servlet
    Tomcat.addServlet(ctx, "ChunkedResponseWithErrorServlet",
            new ResponseWithErrorServlet(true));
    ctx.addServletMappingDecoded("/*", "ChunkedResponseWithErrorServlet");

    tomcat.start();

    String request =
            "GET /anything HTTP/1.1" + SimpleHttpClient.CRLF +
            "Host: any" + SimpleHttpClient.CRLF +
             SimpleHttpClient.CRLF;

    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] {request});

    client.connect();
    client.processRequest();

    // Expected response is a 200 response followed by an incomplete chunked
    // body.
    Assert.assertTrue(client.isResponse200());
    // Should use chunked encoding
    String transferEncoding = null;
    for (String header : client.getResponseHeaders()) {
         if (header.startsWith("Transfer-Encoding:")) {
            transferEncoding = header.substring(18).trim();
        }
    }
    Assert.assertEquals("chunked", transferEncoding);
    // There should not be an end chunk
    Assert.assertFalse(client.getResponseBody().endsWith("0"));
    // The last portion of text should be there
    Assert.assertTrue(client.getResponseBody().endsWith("line03"));
}
 
Example 20
Source File: TestChunkedInputFilter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * @param expectPass
 *            If the servlet is expected to process the request
 * @param expectReadWholeBody
 *            If the servlet is expected to fully read the body and reliably
 *            deliver a response
 * @param chunks
 *            Text of chunks
 * @param readLimit
 *            Do not read more than this many bytes
 * @param expectReadCount
 *            Expected count of read bytes
 * @throws Exception
 *             Unexpected
 */
private void doTestChunkSize(boolean expectPass,
        boolean expectReadWholeBody, String chunks, int readLimit,
        int expectReadCount) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    BodyReadServlet servlet = new BodyReadServlet(expectPass, readLimit);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");

    tomcat.start();

    String request = "POST /echo-params.jsp HTTP/1.1"
            + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF
            + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF
            + "Content-Type: text/plain" + SimpleHttpClient.CRLF;
    if (expectPass) {
        request += "Connection: close" + SimpleHttpClient.CRLF;
    }
    request += SimpleHttpClient.CRLF + chunks + "0" + SimpleHttpClient.CRLF
            + SimpleHttpClient.CRLF;

    TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
    // Need to use the content length here as variations in Connector and
    // JVM+OS behaviour mean that in some circumstances the client may see
    // an IOException rather than the response body when the server closes
    // the connection.
    client.setUseContentLength(true);
    client.setRequest(new String[] { request });

    Exception processException = null;
    client.connect();
    try {
        client.processRequest();
        client.disconnect();
    } catch (Exception e) {
        // Socket was probably closed before client had a chance to read
        // response
        processException = e;
    }
    if (expectPass) {
        if (expectReadWholeBody) {
            Assert.assertNull(processException);
        }
        if (processException == null) {
            Assert.assertTrue(client.getResponseLine(), client.isResponse200());
            Assert.assertEquals(String.valueOf(expectReadCount),
                    client.getResponseBody());
        }
        Assert.assertEquals(expectReadCount, servlet.getCountRead());
    } else {
        if (processException == null) {
            Assert.assertTrue(client.getResponseLine(), client.isResponse500());
        }
        Assert.assertEquals(0, servlet.getCountRead());
        Assert.assertTrue(servlet.getExceptionDuringRead());
    }
}