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));
}
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();
}
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();
}
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);
}
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();
}
Aggregations