use of org.apache.catalina.Context in project tomcat by apache.
the class TestChunkedInputFilter method testTrailingHeadersSizeLimit.
@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.addServletMappingDecoded("/", "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());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestUpgrade method doUpgrade.
private UpgradeConnection doUpgrade(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
UpgradeServlet servlet = new UpgradeServlet(upgradeHandlerClass);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
// Use raw socket so the necessary control is available after the HTTP
// upgrade
Socket socket = SocketFactory.getDefault().createSocket("localhost", getPort());
socket.setSoTimeout(5000);
UpgradeConnection uc = new UpgradeConnection(socket);
uc.getWriter().write("GET / HTTP/1.1" + CRLF);
uc.getWriter().write("Host: whatever" + CRLF);
uc.getWriter().write(CRLF);
uc.getWriter().flush();
String status = uc.getReader().readLine();
Assert.assertNotNull(status);
Assert.assertEquals("101", getStatusCode(status));
// Skip the remaining response headers
String line = uc.getReader().readLine();
while (line != null && line.length() > 0) {
// Skip
line = uc.getReader().readLine();
}
return uc;
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestAbstractAjpProcessor method doTestZeroLengthRequestBody.
private void doTestZeroLengthRequestBody(String method, boolean callAvailable) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ReadBodyServlet servlet = new ReadBodyServlet(callAvailable);
Tomcat.addServlet(ctx, "ReadBody", servlet);
ctx.addServletMappingDecoded("/", "ReadBody");
tomcat.start();
SimpleAjpClient ajpClient = new SimpleAjpClient();
ajpClient.setPort(getPort());
ajpClient.connect();
validateCpong(ajpClient.cping());
ajpClient.setMethod(method);
TesterAjpMessage forwardMessage = ajpClient.createForwardMessage();
forwardMessage.addHeader(0xA008, "0");
forwardMessage.end();
TesterAjpMessage responseHeaders = ajpClient.sendMessage(forwardMessage, null);
// Expect 3 messages: headers, body, end
validateResponseHeaders(responseHeaders, 200, "200");
validateResponseBody(ajpClient.readMessage(), "Request Body length in bytes: 0");
validateResponseEnd(ajpClient.readMessage(), true);
// Double check the connection is still open
validateCpong(ajpClient.cping());
ajpClient.disconnect();
if (callAvailable) {
boolean success = true;
Iterator<Integer> itAvailable = servlet.availableList.iterator();
Iterator<Integer> itRead = servlet.readList.iterator();
while (success && itAvailable.hasNext()) {
success = ((itRead.next().intValue() > 0) == (itAvailable.next().intValue() > 0));
}
if (!success) {
Assert.fail("available() and read() results do not match.\nAvailable: " + servlet.availableList + "\nRead: " + servlet.readList);
}
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestAbstractAjpProcessor method doSnoopTest.
private void doSnoopTest(RequestDescriptor desc) throws Exception {
final int ajpPacketSize = 16000;
Map<String, String> requestInfo = desc.getRequestInfo();
Map<String, String> contextInitParameters = desc.getContextInitParameters();
Map<String, String> contextAttributes = desc.getContextAttributes();
Map<String, String> headers = desc.getHeaders();
Map<String, String> attributes = desc.getAttributes();
Map<String, String> params = desc.getParams();
Tomcat tomcat = getTomcatInstance();
tomcat.getConnector().setProperty("packetSize", Integer.toString(ajpPacketSize));
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "snoop", new SnoopServlet());
ctx.addServletMappingDecoded("/", "snoop");
SimpleAjpClient ajpClient = new SimpleAjpClient(ajpPacketSize);
if (requestInfo.get("REQUEST-QUERY-STRING") != null && params.size() > 0) {
throw (new IllegalArgumentException("Request setting " + "'REQUEST-QUERY-STRING' and explicit params not allowed " + "together"));
}
String value;
int bodySize = 0;
Map<String, String> savedRequestInfo = new HashMap<>();
for (String name : requestInfo.keySet()) {
value = requestInfo.get(name);
switch(name) {
case "REQUEST-METHOD":
ajpClient.setMethod(value);
break;
case "REQUEST-PROTOCOL":
ajpClient.setProtocol(value);
break;
case "REQUEST-URI":
ajpClient.setUri(value);
break;
case "REQUEST-REMOTE-HOST":
/* request.getRemoteHost() will default to
* request.getRemoteAddr() unless enableLookups is set. */
tomcat.getConnector().setEnableLookups(true);
ajpClient.setRemoteHost(value);
break;
case "REQUEST-REMOTE-ADDR":
ajpClient.setRemoteAddr(value);
break;
case "REQUEST-SERVER-NAME":
ajpClient.setServerName(value);
break;
case "REQUEST-SERVER-PORT":
ajpClient.setServerPort(Integer.parseInt(value));
break;
case "REQUEST-IS-SECURE":
ajpClient.setSsl(Boolean.parseBoolean(value));
break;
case "REQUEST-LOCAL-ADDR":
savedRequestInfo.put(name, value);
break;
case "REQUEST-REMOTE-PORT":
savedRequestInfo.put(name, value);
break;
case "REQUEST-REMOTE-USER":
case "REQUEST-ROUTE":
case "REQUEST-SECRET":
case "REQUEST-AUTH-TYPE":
case "REQUEST-QUERY-STRING":
savedRequestInfo.put(name, value);
break;
case "REQUEST-CONTENT-LENGTH":
headers.put("CONTENT-LENGTH", value);
break;
case "REQUEST-BODY-SIZE":
savedRequestInfo.put(name, value);
bodySize = Integer.parseInt(value);
break;
case "REQUEST-CONTENT-TYPE":
headers.put("CONTENT-TYPE", value);
break;
//request.getLocalName()
case "REQUEST-LOCAL-NAME":
//request.getLocalPort()
case "REQUEST-LOCAL-PORT":
//request.getScheme()
case "REQUEST-SCHEME":
//request.getRequestURL()
case "REQUEST-URL":
//request.getContextPath()
case "REQUEST-CONTEXT-PATH":
//request.getServletPath()
case "REQUEST-SERVLET-PATH":
//request.getPathInfo()
case "REQUEST-PATH-INFO":
//request.getPathTranslated()
case "REQUEST-PATH-TRANSLATED":
//request.getUserPrincipal()
case "REQUEST-USER-PRINCIPAL":
//request.getCharacterEncoding()
case "REQUEST-CHARACTER-ENCODING":
//request.getLocale()
case "REQUEST-LOCALE":
//request.getRequestedSessionId()
case "SESSION-REQUESTED-ID":
//request.isRequestedSessionIdFromCookie()
case "SESSION-REQUESTED-ID-COOKIE":
//request.isRequestedSessionIdFromUrl()
case "SESSION-REQUESTED-ID-URL":
//request.isRequestedSessionIdValid()
case "SESSION-REQUESTED-ID-VALID":
default:
throw (new IllegalArgumentException("Request setting '" + name + "' not supported"));
}
}
ServletContext sc = ctx.getServletContext();
for (String name : contextInitParameters.keySet()) {
sc.setInitParameter(name, contextInitParameters.get(name));
}
for (String name : contextAttributes.keySet()) {
sc.setAttribute(name, contextAttributes.get(name));
}
/* Basic request properties must be set before this call */
TesterAjpMessage forwardMessage = ajpClient.createForwardMessage();
for (String name : savedRequestInfo.keySet()) {
value = savedRequestInfo.get(name);
switch(name) {
case "REQUEST-LOCAL-ADDR":
forwardMessage.addAttribute("AJP_LOCAL_ADDR", value);
break;
case "REQUEST-REMOTE-PORT":
forwardMessage.addAttribute("AJP_REMOTE_PORT", value);
break;
case "REQUEST-REMOTE-USER":
/* request.getRemoteUser() will not trust the AJP
* info if tomcatAuthentication is set. */
tomcat.getConnector().setProperty("tomcatAuthentication", "false");
forwardMessage.addAttribute(0x03, value);
break;
case "REQUEST-AUTH-TYPE":
/* request.getAuthType() will not trust the AJP
* info if tomcatAuthentication is set. */
tomcat.getConnector().setProperty("tomcatAuthentication", "false");
forwardMessage.addAttribute(0x04, value);
break;
case "REQUEST-QUERY-STRING":
forwardMessage.addAttribute(0x05, value);
break;
case "REQUEST-ROUTE":
forwardMessage.addAttribute(0x06, value);
break;
case "REQUEST-SECRET":
forwardMessage.addAttribute(0x0C, value);
break;
case "REQUEST-BODY-SIZE":
break;
default:
throw (new IllegalArgumentException("Request setting '" + name + "' not supported"));
}
}
if (params.size() > 0) {
StringBuilder query = new StringBuilder();
boolean sep = false;
for (String name : params.keySet()) {
if (sep) {
query.append("&");
} else {
sep = true;
}
query.append(name);
query.append("=");
query.append(params.get(name));
}
forwardMessage.addAttribute(0x05, query.toString());
}
for (String name : headers.keySet()) {
value = headers.get(name);
name = name.toUpperCase(Locale.ENGLISH);
switch(name) {
case "ACCEPT":
forwardMessage.addHeader(0xA001, value);
break;
case "ACCEPT-CHARSET":
forwardMessage.addHeader(0xA002, value);
break;
case "ACCEPT-ENCODING":
forwardMessage.addHeader(0xA003, value);
break;
case "ACCEPT-LANGUAGE":
forwardMessage.addHeader(0xA004, value);
break;
case "AUTHORIZATION":
forwardMessage.addHeader(0xA005, value);
break;
case "CONNECTION":
forwardMessage.addHeader(0xA006, value);
break;
case "CONTENT-TYPE":
forwardMessage.addHeader(0xA007, value);
break;
case "CONTENT-LENGTH":
forwardMessage.addHeader(0xA008, value);
break;
case "COOKIE":
forwardMessage.addHeader(0xA009, value);
break;
case "COOKIE2":
forwardMessage.addHeader(0xA00A, value);
break;
case "HOST":
forwardMessage.addHeader(0xA00B, value);
break;
case "PRAGMA":
forwardMessage.addHeader(0xA00C, value);
break;
case "REFERER":
forwardMessage.addHeader(0xA00D, value);
break;
case "USER-AGENT":
forwardMessage.addHeader(0xA00E, value);
break;
default:
forwardMessage.addHeader(name, value);
break;
}
}
for (String name : attributes.keySet()) {
value = attributes.get(name);
forwardMessage.addAttribute(name, value);
}
// Complete the message
forwardMessage.end();
tomcat.start();
ajpClient.setPort(getPort());
ajpClient.connect();
TesterAjpMessage responseHeaders = null;
if (bodySize == 0) {
responseHeaders = ajpClient.sendMessage(forwardMessage);
} else {
TesterAjpMessage bodyMessage = ajpClient.createBodyMessage(new byte[bodySize]);
responseHeaders = ajpClient.sendMessage(forwardMessage, bodyMessage);
// Expect back a request for more data (which will be empty and
// trigger end of stream in Servlet)
validateGetBody(responseHeaders);
bodyMessage = ajpClient.createBodyMessage(new byte[0]);
responseHeaders = ajpClient.sendMessage(bodyMessage);
}
// Expect 3 packets: headers, body, end
validateResponseHeaders(responseHeaders, 200, "200");
String body = extractResponseBody(ajpClient.readMessage());
RequestDescriptor result = SnoopResult.parse(body);
/* AJP attributes result in Coyote Request attributes, which are
* not listed by request.getAttributeNames(), so SnoopServlet
* does not see them. Delete attributes before result comparison. */
desc.getAttributes().clear();
result.compare(desc);
validateResponseEnd(ajpClient.readMessage(), true);
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method doTestNon2xxResponseAndExpectation.
private void doTestNon2xxResponseAndExpectation(boolean useExpectation) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "echo", new EchoBodyServlet());
ctx.addServletMappingDecoded("/echo", "echo");
SecurityCollection collection = new SecurityCollection("All", "");
collection.addPatternDecoded("/*");
SecurityConstraint constraint = new SecurityConstraint();
constraint.addAuthRole("Any");
constraint.addCollection(collection);
ctx.addConstraint(constraint);
tomcat.start();
byte[] requestBody = "HelloWorld".getBytes(StandardCharsets.UTF_8);
Map<String, List<String>> reqHeaders = null;
if (useExpectation) {
reqHeaders = new HashMap<>();
List<String> expectation = new ArrayList<>();
expectation.add("100-continue");
reqHeaders.put("Expect", expectation);
}
ByteChunk responseBody = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = postUrl(requestBody, "http://localhost:" + getPort() + "/echo", responseBody, reqHeaders, responseHeaders);
Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, rc);
List<String> connectionHeaders = responseHeaders.get("Connection");
if (useExpectation) {
Assert.assertEquals(1, connectionHeaders.size());
Assert.assertEquals("close", connectionHeaders.get(0).toLowerCase(Locale.ENGLISH));
} else {
Assert.assertNull(connectionHeaders);
}
}
Aggregations