Search in sources :

Example 1 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class WsWebSocketContainer method processResponse.

/**
 * Process response, blocking until HTTP response has been fully received.
 * @throws ExecutionException if there is an exception reading the response
 * @throws InterruptedException if the thread is interrupted while reading
 *         the response
 * @throws DeploymentException if the response status line is not correctly
 *         formatted
 * @throws TimeoutException if the response was not read within the expected
 *         timeout
 */
private HttpResponse processResponse(ByteBuffer response, AsyncChannelWrapper channel, long timeout) throws InterruptedException, ExecutionException, DeploymentException, EOFException, TimeoutException {
    Map<String, List<String>> headers = new CaseInsensitiveKeyMap<>();
    int status = 0;
    boolean readStatus = false;
    boolean readHeaders = false;
    String line = null;
    while (!readHeaders) {
        // On entering loop buffer will be empty and at the start of a new
        // loop the buffer will have been fully read.
        response.clear();
        // Blocking read
        Future<Integer> read = channel.read(response);
        Integer bytesRead;
        try {
            bytesRead = read.get(timeout, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            TimeoutException te = new TimeoutException(sm.getString("wsWebSocketContainer.responseFail", Integer.toString(status), headers));
            te.initCause(e);
            throw te;
        }
        if (bytesRead.intValue() == -1) {
            throw new EOFException(sm.getString("wsWebSocketContainer.responseFail", Integer.toString(status), headers));
        }
        response.flip();
        while (response.hasRemaining() && !readHeaders) {
            if (line == null) {
                line = readLine(response);
            } else {
                line += readLine(response);
            }
            if ("\r\n".equals(line)) {
                readHeaders = true;
            } else if (line.endsWith("\r\n")) {
                if (readStatus) {
                    parseHeaders(line, headers);
                } else {
                    status = parseStatus(line);
                    readStatus = true;
                }
                line = null;
            }
        }
    }
    return new HttpResponse(status, new WsHandshakeResponse(headers));
}
Also used : EOFException(java.io.EOFException) List(java.util.List) ArrayList(java.util.ArrayList) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class HttpServletDoHeadBaseTest method testDoHead.

@Test
public void testDoHead() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    configureAndStartWebApplication();
    Map<String, List<String>> getHeaders = new CaseInsensitiveKeyMap<>();
    String path = "http://localhost:" + getPort() + "/test";
    ByteChunk out = new ByteChunk();
    int rc = getUrl(path, out, getHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    out.recycle();
    Map<String, List<String>> headHeaders = new HashMap<>();
    rc = headUrl(path, out, headHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Headers should be the same (apart from Date)
    Assert.assertEquals(getHeaders.size(), headHeaders.size());
    for (Map.Entry<String, List<String>> getHeader : getHeaders.entrySet()) {
        String headerName = getHeader.getKey();
        if ("date".equalsIgnoreCase(headerName)) {
            continue;
        }
        Assert.assertTrue(headerName, headHeaders.containsKey(headerName));
        List<String> getValues = getHeader.getValue();
        List<String> headValues = headHeaders.get(headerName);
        Assert.assertEquals(getValues.size(), headValues.size());
        for (String value : getValues) {
            Assert.assertTrue(headValues.contains(value));
        }
    }
    tomcat.stop();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) HashMap(java.util.HashMap) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestHttpServlet method doTestHead.

private void doTestHead(Servlet servlet) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    Wrapper w = Tomcat.addServlet(ctx, "TestServlet", servlet);
    // Not all need/use this but it is simpler to set it for all
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "TestServlet");
    tomcat.start();
    Map<String, List<String>> getHeaders = new CaseInsensitiveKeyMap<>();
    String path = "http://localhost:" + getPort() + "/test";
    ByteChunk out = new ByteChunk();
    int rc = getUrl(path, out, getHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    out.recycle();
    Map<String, List<String>> headHeaders = new HashMap<>();
    rc = headUrl(path, out, headHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Headers should be the same (apart from Date)
    Assert.assertEquals(getHeaders.size(), headHeaders.size());
    for (Map.Entry<String, List<String>> getHeader : getHeaders.entrySet()) {
        String headerName = getHeader.getKey();
        if ("date".equalsIgnoreCase(headerName)) {
            continue;
        }
        Assert.assertTrue(headerName, headHeaders.containsKey(headerName));
        List<String> getValues = getHeader.getValue();
        List<String> headValues = headHeaders.get(headerName);
        Assert.assertEquals(getValues.size(), headValues.size());
        for (String value : getValues) {
            Assert.assertTrue(headValues.contains(value));
        }
    }
    tomcat.stop();
}
Also used : Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) StandardContext(org.apache.catalina.core.StandardContext) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) HashMap(java.util.HashMap) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Map(java.util.Map)

Example 4 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestResponse method testBug49598.

@Test
public void testBug49598() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "servlet", new Bug49598Servlet());
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    Map<String, List<String>> headers = new CaseInsensitiveKeyMap<>();
    getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers);
    // Check for headers without a name
    for (Map.Entry<String, List<String>> header : headers.entrySet()) {
        if (header.getKey() == null) {
            // Expected if this is the response line
            List<String> values = header.getValue();
            if (values.size() == 1 && values.get(0).startsWith("HTTP/1.1")) {
                continue;
            }
            Assert.fail("Null header name detected for value " + values);
        }
    }
    // Check for exactly one Set-Cookie header
    int count = 0;
    for (String headerName : headers.keySet()) {
        if ("Set-Cookie".equals(headerName)) {
            count++;
        }
    }
    Assert.assertEquals(1, count);
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Map(java.util.Map) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestHttpServlet method testBug57602.

/*
     * Verifies that the same Content-Length is returned for both GET and HEAD
     * operations when a Servlet includes content from another Servlet
     */
@Test
public void testBug57602() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    Bug57602ServletOuter outer = new Bug57602ServletOuter();
    Tomcat.addServlet(ctx, "Bug57602ServletOuter", outer);
    ctx.addServletMappingDecoded("/outer", "Bug57602ServletOuter");
    Bug57602ServletInner inner = new Bug57602ServletInner();
    Tomcat.addServlet(ctx, "Bug57602ServletInner", inner);
    ctx.addServletMappingDecoded("/inner", "Bug57602ServletInner");
    tomcat.start();
    Map<String, List<String>> resHeaders = new CaseInsensitiveKeyMap<>();
    String path = "http://localhost:" + getPort() + "/outer";
    ByteChunk out = new ByteChunk();
    int rc = getUrl(path, out, resHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    String length = getSingleHeader("Content-Length", resHeaders);
    Assert.assertEquals(Long.parseLong(length), out.getLength());
    out.recycle();
    rc = headUrl(path, out, resHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertEquals(0, out.getLength());
    Assert.assertEquals(length, resHeaders.get("Content-Length").get(0));
    tomcat.stop();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardContext(org.apache.catalina.core.StandardContext) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

List (java.util.List)8 CaseInsensitiveKeyMap (org.apache.tomcat.util.collections.CaseInsensitiveKeyMap)8 Tomcat (org.apache.catalina.startup.Tomcat)7 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)7 Test (org.junit.Test)6 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 Context (org.apache.catalina.Context)4 Map (java.util.Map)3 Wrapper (org.apache.catalina.Wrapper)3 TesterContext (org.apache.tomcat.unittest.TesterContext)3 AsyncContext (jakarta.servlet.AsyncContext)2 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)2 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 StandardContext (org.apache.catalina.core.StandardContext)2 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)2 ClientEndpoint (jakarta.websocket.ClientEndpoint)1 Endpoint (jakarta.websocket.Endpoint)1 EOFException (java.io.EOFException)1