This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11;
import org.junit.Test;
public class TestAbstractHttp11Protocol {
@Test
public void testGetSslProtocol() {
Http11Nio2Protocol protocol = new Http11Nio2Protocol();
protocol.getSSLProtocol();
}
}

View File

@@ -0,0 +1,691 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.SimpleHttpClient;
import org.apache.catalina.startup.TesterServlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestHttp11InputBuffer extends TomcatBaseTest {
/**
* Test case for https://bz.apache.org/bugzilla/show_bug.cgi?id=48839
*/
@Test
public void testBug48839() {
Bug48839Client client = new Bug48839Client();
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Bug 48839 test client.
*/
private class Bug48839Client extends SimpleHttpClient {
private Exception doRequest() {
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
Tomcat.addServlet(root, "Bug48839", new Bug48839Servlet());
root.addServletMappingDecoded("/test", "Bug48839");
try {
tomcat.start();
setPort(tomcat.getConnector().getLocalPort());
// Open connection
connect();
String[] request = new String[1];
request[0] =
"GET http://localhost:8080/test HTTP/1.1" + CRLF +
"Host: localhost:8080" + CRLF +
"X-Bug48839: abcd" + CRLF +
"\tefgh" + CRLF +
"Connection: close" + CRLF +
CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
if (!getResponseBody().contains("abcd\tefgh")) {
return false;
}
return true;
}
}
private static class Bug48839Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Only interested in the request headers from a GET request
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Just echo the header value back as plain text
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
Enumeration<String> values = req.getHeaders("X-Bug48839");
while (values.hasMoreElements()) {
out.println(values.nextElement());
}
}
}
@Test
public void testBug51557Valid() {
Bug51557Client client = new Bug51557Client("X-Bug51557Valid", "1234");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("1234abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
@Test
public void testBug51557Invalid() {
Bug51557Client client = new Bug51557Client("X-Bug51557=Invalid", "1234", true);
client.doRequest();
Assert.assertTrue(client.isResponse400());
}
@Test
public void testBug51557NoColon() {
Bug51557Client client = new Bug51557Client("X-Bug51557NoColon");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
@Test
public void testBug51557SeparatorsInName() throws Exception {
char httpSeparators[] = new char[] {
'\t', ' ', '\"', '(', ')', ',', '/', ':', ';', '<',
'=', '>', '?', '@', '[', '\\', ']', '{', '}' };
for (char s : httpSeparators) {
doTestBug51557CharInName(s);
tearDown();
setUp();
}
}
@Test
public void testBug51557CtlInName() throws Exception {
for (int i = 0; i < 31; i++) {
doTestBug51557CharInName((char) i);
tearDown();
setUp();
}
doTestBug51557CharInName((char) 127);
}
@Test
public void testBug51557CtlInValue() throws Exception {
for (int i = 0; i < 31; i++) {
if (i == '\t') {
// TAB is allowed
continue;
}
doTestBug51557InvalidCharInValue((char) i);
tearDown();
setUp();
}
doTestBug51557InvalidCharInValue((char) 127);
}
@Test
public void testBug51557ObsTextInValue() throws Exception {
for (int i = 128; i < 255; i++) {
doTestBug51557ValidCharInValue((char) i);
tearDown();
setUp();
}
}
@Test
public void testBug51557Continuation() {
Bug51557Client client = new Bug51557Client("X-Bug=51557NoColon",
"foo" + SimpleHttpClient.CRLF + " bar");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
@Test
public void testBug51557BoundaryStart() {
Bug51557Client client = new Bug51557Client("=X-Bug51557",
"invalid");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
@Test
public void testBug51557BoundaryEnd() {
Bug51557Client client = new Bug51557Client("X-Bug51557=",
"invalid");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
private void doTestBug51557CharInName(char s) {
Bug51557Client client =
new Bug51557Client("X-Bug" + s + "51557", "invalid");
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertEquals("abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
private void doTestBug51557InvalidCharInValue(char s) {
Bug51557Client client =
new Bug51557Client("X-Bug51557-Invalid", "invalid" + s + "invalid");
client.doRequest();
Assert.assertTrue("Testing [" + (int) s + "]", client.isResponse200());
Assert.assertEquals("Testing [" + (int) s + "]", "abcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
private void doTestBug51557ValidCharInValue(char s) {
Bug51557Client client =
new Bug51557Client("X-Bug51557-Valid", "valid" + s + "valid");
client.doRequest();
Assert.assertTrue("Testing [" + (int) s + "]", client.isResponse200());
Assert.assertEquals("Testing [" + (int) s + "]", "valid" + s + "validabcd", client.getResponseBody());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Bug 51557 test client.
*/
private class Bug51557Client extends SimpleHttpClient {
private final String headerName;
private final String headerLine;
private final boolean rejectIllegalHeader;
public Bug51557Client(String headerName) {
this.headerName = headerName;
this.headerLine = headerName;
this.rejectIllegalHeader = false;
}
public Bug51557Client(String headerName, String headerValue) {
this(headerName, headerValue, false);
}
public Bug51557Client(String headerName, String headerValue,
boolean rejectIllegalHeader) {
this.headerName = headerName;
this.headerLine = headerName + ": " + headerValue;
this.rejectIllegalHeader = rejectIllegalHeader;
}
private Exception doRequest() {
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
Tomcat.addServlet(root, "Bug51557",
new Bug51557Servlet(headerName));
root.addServletMappingDecoded("/test", "Bug51557");
try {
Connector connector = tomcat.getConnector();
Assert.assertTrue(connector.setProperty(
"rejectIllegalHeader", Boolean.toString(rejectIllegalHeader)));
tomcat.start();
setPort(connector.getLocalPort());
// Open connection
connect();
String[] request = new String[1];
request[0] =
"GET http://localhost:8080/test HTTP/1.1" + CRLF +
"Host: localhost:8080" + CRLF +
headerLine + CRLF +
"X-Bug51557: abcd" + CRLF +
"Connection: close" + CRLF +
CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
if (!getResponseBody().contains("abcd")) {
return false;
}
return true;
}
}
private static class Bug51557Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String invalidHeaderName;
/**
* @param invalidHeaderName The header name should be invalid and
* therefore ignored by the header parsing code
*/
public Bug51557Servlet(String invalidHeaderName) {
this.invalidHeaderName = invalidHeaderName;
}
/**
* Only interested in the request headers from a GET request
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Just echo the header value back as plain text
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
processHeaders(invalidHeaderName, req, out);
processHeaders("X-Bug51557", req, out);
}
private void processHeaders(String header, HttpServletRequest req,
PrintWriter out) {
Enumeration<String> values = req.getHeaders(header);
while (values.hasMoreElements()) {
out.println(values.nextElement());
}
}
}
/**
* Test case for new lines at the start of a request. RFC2616
* does not permit any, but Tomcat is tolerant of them if they are present.
*/
@Test
public void testNewLines() {
NewLinesClient client = new NewLinesClient(10);
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Test case for new lines at the start of a request. RFC2616
* does not permit any, but Tomcat is tolerant of them if they are present.
*/
@Test
public void testNewLinesExcessive() {
NewLinesClient client = new NewLinesClient(10000);
// If the connection is closed fast enough, writing the request will
// fail and the response won't be read.
Exception e = client.doRequest();
if (e == null) {
Assert.assertTrue(client.getResponseLine(), client.isResponse400());
}
Assert.assertFalse(client.isResponseBodyOK());
}
private class NewLinesClient extends SimpleHttpClient {
private final String newLines;
private NewLinesClient(int count) {
StringBuilder sb = new StringBuilder(count * 2);
for (int i = 0; i < count; i++) {
sb.append(CRLF);
}
newLines = sb.toString();
}
private Exception doRequest() {
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
Tomcat.addServlet(root, "test", new TesterServlet());
root.addServletMappingDecoded("/test", "test");
try {
tomcat.start();
setPort(tomcat.getConnector().getLocalPort());
// Open connection
connect();
String[] request = new String[1];
request[0] =
newLines +
"GET http://localhost:8080/test HTTP/1.1" + CRLF +
"Host: localhost:8080" + CRLF +
"X-Bug48839: abcd" + CRLF +
"\tefgh" + CRLF +
"Connection: close" + CRLF +
CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
if (!getResponseBody().contains("OK")) {
return false;
}
return true;
}
}
/**
* Test case for https://bz.apache.org/bugzilla/show_bug.cgi?id=54947
*/
@Test
public void testBug54947() {
Bug54947Client client = new Bug54947Client();
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Bug 54947 test client.
*/
private class Bug54947Client extends SimpleHttpClient {
private Exception doRequest() {
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
Tomcat.addServlet(root, "Bug54947", new TesterServlet());
root.addServletMappingDecoded("/test", "Bug54947");
try {
tomcat.start();
setPort(tomcat.getConnector().getLocalPort());
// Open connection
connect();
String[] request = new String[2];
request[0] = "GET http://localhost:8080/test HTTP/1.1" + CR;
request[1] = LF +
"Host: localhost:8080" + CRLF +
"Connection: close" + CRLF +
CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
if (!getResponseBody().contains("OK")) {
return false;
}
return true;
}
}
/**
* Test case for https://bz.apache.org/bugzilla/show_bug.cgi?id=59089
*/
@Test
public void testBug59089() {
Bug59089Client client = new Bug59089Client();
client.doRequest();
Assert.assertTrue(client.isResponse200());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Bug 59089 test client.
*/
private class Bug59089Client extends SimpleHttpClient {
private Exception doRequest() {
// Ensure body is read correctly
setUseContentLength(true);
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
Tomcat.addServlet(root, "Bug59089", new TesterServlet());
root.addServletMappingDecoded("/test", "Bug59089");
try {
Connector connector = tomcat.getConnector();
Assert.assertTrue(connector.setProperty("rejectIllegalHeader", "false"));
tomcat.start();
setPort(connector.getLocalPort());
// Open connection
connect();
String[] request = new String[1];
request[0] = "GET http://localhost:8080/test HTTP/1.1" + CRLF +
"Host: localhost:8080" + CRLF +
"X-Header: Ignore" + CRLF +
"X-Header" + (char) 130 + ": Broken" + CRLF + CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
if (!getResponseBody().contains("OK")) {
return false;
}
return true;
}
}
@Test
public void testInvalidMethod() {
InvalidMethodClient client = new InvalidMethodClient();
client.doRequest();
Assert.assertTrue(client.getResponseLine(), client.isResponse400());
Assert.assertTrue(client.isResponseBodyOK());
}
/**
* Bug 48839 test client.
*/
private class InvalidMethodClient extends SimpleHttpClient {
private Exception doRequest() {
Tomcat tomcat = getTomcatInstance();
tomcat.addContext("", TEMP_DIR);
try {
tomcat.start();
setPort(tomcat.getConnector().getLocalPort());
// Open connection
connect();
String[] request = new String[1];
request[0] =
"GET" + (char) 0 + " /test HTTP/1.1" + CRLF +
"Host: localhost:8080" + CRLF +
"Connection: close" + CRLF +
CRLF;
setRequest(request);
processRequest(); // blocks until response has been read
// Close the connection
disconnect();
} catch (Exception e) {
return e;
}
return null;
}
@Override
public boolean isResponseBodyOK() {
if (getResponseBody() == null) {
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.SimpleHttpClient;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestHttp11OutputBuffer extends TomcatBaseTest {
@Test
public void testSendAck() 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");
tomcat.start();
ExpectationClient client = new ExpectationClient();
client.setPort(tomcat.getConnector().getLocalPort());
// Expected content doesn't end with a CR-LF so if it isn't chunked make
// sure the content length is used as reading it line-by-line will fail
// since there is no "line".
client.setUseContentLength(true);
client.connect();
client.doRequestHeaders();
Assert.assertTrue(client.isResponse100());
client.doRequestBody();
Assert.assertTrue(client.isResponse200());
Assert.assertTrue(client.isResponseBodyOK());
}
private static class ExpectationClient extends SimpleHttpClient {
private static final String BODY = "foo=bar";
public void doRequestHeaders() throws Exception {
StringBuilder requestHeaders = new StringBuilder();
requestHeaders.append("POST /echo HTTP/1.1").append(CRLF);
requestHeaders.append("Host: localhost").append(CRLF);
requestHeaders.append("Expect: 100-continue").append(CRLF);
requestHeaders.append("Content-Type: application/x-www-form-urlencoded").append(CRLF);
String len = Integer.toString(BODY.length());
requestHeaders.append("Content-length: ").append(len).append(CRLF);
requestHeaders.append(CRLF);
setRequest(new String[] {requestHeaders.toString()});
processRequest(false);
}
public void doRequestBody() throws Exception {
setRequest(new String[] { BODY });
processRequest(true);
}
@Override
public boolean isResponseBodyOK() {
return BODY.equals(getResponseBody());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,547 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11.filters;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.SimpleHttpClient;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestChunkedInputFilter extends TomcatBaseTest {
private static final String LF = "\n";
private static final int EXT_SIZE_LIMIT = 10;
@Test
public void testChunkHeaderCRLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, true, true);
}
@Test
public void testChunkHeaderLF() throws Exception {
doTestChunkingCRLF(false, true, true, true, true, false);
}
@Test
public void testChunkCRLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, true, true);
}
@Test
public void testChunkLF() throws Exception {
doTestChunkingCRLF(true, false, true, true, true, false);
}
@Test
public void testFirstTrailingHeadersCRLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, true, true);
}
@Test
public void testFirstTrailingHeadersLF() throws Exception {
doTestChunkingCRLF(true, true, false, true, true, true);
}
@Test
public void testSecondTrailingHeadersCRLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, true, true);
}
@Test
public void testSecondTrailingHeadersLF() throws Exception {
doTestChunkingCRLF(true, true, true, false, true, true);
}
@Test
public void testEndCRLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, true, true);
}
@Test
public void testEndLF() throws Exception {
doTestChunkingCRLF(true, true, true, true, false, false);
}
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
Assert.assertTrue(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());
}
}
}
@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
Assert.assertTrue(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
Assert.assertTrue(client.isResponse500());
}
@Test
public void testExtensionSizeLimitOneBelow() throws Exception {
doTestExtensionSizeLimit(EXT_SIZE_LIMIT - 1, true);
}
@Test
public void testExtensionSizeLimitExact() throws Exception {
doTestExtensionSizeLimit(EXT_SIZE_LIMIT, true);
}
@Test
public void testExtensionSizeLimitOneOver() throws Exception {
doTestExtensionSizeLimit(EXT_SIZE_LIMIT + 1, false);
}
private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
Assert.assertTrue(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());
}
}
@Test
public void testNoTrailingHeaders() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(true));
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: 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 +
SimpleHttpClient.CRLF;
TrailerClient client =
new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] {request});
client.connect();
client.processRequest();
Assert.assertEquals("nullnull7nullnull", client.getResponseBody());
}
@Test
public void testChunkSizeZero() throws Exception {
doTestChunkSize(true, true, "", 10, 0);
}
@Test
public void testChunkSizeAbsent() throws Exception {
doTestChunkSize(false, false, SimpleHttpClient.CRLF, 10, 0);
}
@Test
public void testChunkSizeTwentyFive() throws Exception {
doTestChunkSize(true, true, "19" + SimpleHttpClient.CRLF
+ "Hello World!Hello World!!" + SimpleHttpClient.CRLF, 40, 25);
}
@Test
public void testChunkSizeEightDigit() throws Exception {
doTestChunkSize(true, true, "0000000C" + SimpleHttpClient.CRLF
+ "Hello World!" + SimpleHttpClient.CRLF, 20, 12);
}
@Test
public void testChunkSizeNineDigit() throws Exception {
doTestChunkSize(false, false, "00000000C" + SimpleHttpClient.CRLF
+ "Hello World!" + SimpleHttpClient.CRLF, 20, 12);
}
@Test
public void testChunkSizeLong() throws Exception {
doTestChunkSize(true, false, "7fFFffFF" + SimpleHttpClient.CRLF
+ "Hello World!" + SimpleHttpClient.CRLF, 10, 10);
}
@Test
public void testChunkSizeIntegerMinValue() throws Exception {
doTestChunkSize(false, false, "80000000" + SimpleHttpClient.CRLF
+ "Hello World!" + SimpleHttpClient.CRLF, 10, 10);
}
@Test
public void testChunkSizeMinusOne() throws Exception {
doTestChunkSize(false, false, "ffffffff" + SimpleHttpClient.CRLF
+ "Hello World!" + SimpleHttpClient.CRLF, 10, 10);
}
/**
* @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());
}
}
private static class EchoHeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private boolean exceptionDuringRead = false;
private final boolean expectPass;
public EchoHeaderServlet(boolean expectPass) {
this.expectPass = expectPass;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
PrintWriter pw = resp.getWriter();
// Headers not visible yet, body not processed
dumpHeader("x-trailer1", req, pw);
dumpHeader("x-trailer2", req, pw);
// Read the body - quick and dirty
InputStream is = req.getInputStream();
int count = 0;
try {
while (is.read() > -1) {
count++;
}
} catch (IOException ioe) {
exceptionDuringRead = true;
if (!expectPass) { // as expected
log(ioe.toString());
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
throw ioe;
}
pw.write(Integer.toString(count));
// Headers should be visible now
dumpHeader("x-trailer1", req, pw);
dumpHeader("x-trailer2", req, pw);
}
public boolean getExceptionDuringRead() {
return exceptionDuringRead;
}
private void dumpHeader(String headerName, HttpServletRequest req,
PrintWriter pw) {
String value = req.getHeader(headerName);
if (value == null) {
value = "null";
}
pw.write(value);
}
}
private static class BodyReadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private boolean exceptionDuringRead = false;
private int countRead = 0;
private final boolean expectPass;
private final int readLimit;
public BodyReadServlet(boolean expectPass, int readLimit) {
this.expectPass = expectPass;
this.readLimit = readLimit;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
PrintWriter pw = resp.getWriter();
// Read the body - quick and dirty
InputStream is = req.getInputStream();
try {
while (is.read() > -1 && countRead < readLimit) {
countRead++;
}
} catch (IOException ioe) {
exceptionDuringRead = true;
if (!expectPass) { // as expected
log(ioe.toString());
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
throw ioe;
}
pw.write(Integer.toString(countRead));
}
public boolean getExceptionDuringRead() {
return exceptionDuringRead;
}
public int getCountRead() {
return countRead;
}
}
private static class TrailerClient extends SimpleHttpClient {
public TrailerClient(int port) {
setPort(port);
}
@Override
public boolean isResponseBodyOK() {
return getResponseBody().contains("TestTestTest");
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11.filters;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.zip.GZIPOutputStream;
import org.junit.Assert;
import org.junit.Test;
import org.apache.coyote.Response;
/**
* Test case to demonstrate the interaction between gzip and flushing in the
* output filter.
*/
public class TestGzipOutputFilter {
/*
* Test the interaction between gzip and flushing. The idea is to: 1. create
* a internal output buffer, response, and attach an active gzipoutputfilter
* to the output buffer 2. set the output stream of the internal buffer to
* be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
* chunk out using the gzipoutputfilter and invoke a flush on the
* InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
* what's being written out (flushed) 5. find out what's expected by writing
* to GZIPOutputStream and close it (to force flushing) 6. Compare the size
* of the two arrays, they should be close (instead of one being much
* shorter than the other one)
*
* @throws Exception
*/
@Test
public void testFlushingWithGzip() throws Exception {
// set up response, InternalOutputBuffer, and ByteArrayOutputStream
Response res = new Response();
TesterOutputBuffer tob = new TesterOutputBuffer(res, 8 * 1024);
res.setOutputBuffer(tob);
// set up GzipOutputFilter to attach to the TesterOutputBuffer
GzipOutputFilter gf = new GzipOutputFilter();
tob.addFilter(gf);
tob.addActiveFilter(gf);
// write a chunk out
byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
ByteBuffer bb = ByteBuffer.wrap(d);
tob.doWrite(bb);
// flush the InternalOutputBuffer
tob.flush();
// read from the ByteArrayOutputStream to find out what's being written
// out (flushed)
byte[] dataFound = tob.toByteArray();
// find out what's expected by writing to GZIPOutputStream and close it
// (to force flushing)
ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
GZIPOutputStream gos = new GZIPOutputStream(gbos);
gos.write(d);
gos.close();
// read the expected data
byte[] dataExpected = gbos.toByteArray();
// most of the data should have been flushed out
Assert.assertTrue(dataFound.length >= (dataExpected.length - 20));
}
}

View File

@@ -0,0 +1,137 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11.filters;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.coyote.Response;
import org.apache.coyote.http11.Http11OutputBuffer;
import org.apache.coyote.http11.HttpOutputBuffer;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.net.SocketWrapperBase;
/**
* Output buffer for use in unit tests. This is a minimal implementation.
*/
public class TesterOutputBuffer extends Http11OutputBuffer {
/**
* Underlying output stream.
*/
private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
public TesterOutputBuffer(Response response, int headerBufferSize) {
super(response, headerBufferSize, false);
outputStreamOutputBuffer = new OutputStreamOutputBuffer();
}
// --------------------------------------------------------- Public Methods
@Override
public void init(SocketWrapperBase<?> socketWrapper) {
// NO-OP: Unused
}
/**
* Recycle the output buffer. This should be called when closing the
* connection.
*/
@Override
public void recycle() {
super.recycle();
outputStream = null;
}
// ------------------------------------------------ HTTP/1.1 Output Methods
/**
* Send an acknowledgement.
*/
@Override
public void sendAck() {
// NO-OP: Unused
}
@Override
protected void commit() {
// NO-OP: Unused
}
@Override
protected boolean flushBuffer(boolean block) throws IOException {
// Blocking IO so ignore block parameter as this will always use
// blocking IO.
// Always blocks so never any data left over.
return false;
}
/*
* Expose data written for use by unit tests.
*/
byte[] toByteArray() {
return outputStream.toByteArray();
}
/**
* This class is an output buffer which will write data to an output
* stream.
*/
protected class OutputStreamOutputBuffer implements HttpOutputBuffer {
@Override
public int doWrite(ByteChunk chunk) throws IOException {
int length = chunk.getLength();
outputStream.write(chunk.getBuffer(), chunk.getStart(), length);
byteCount += chunk.getLength();
return chunk.getLength();
}
@Override
public int doWrite(ByteBuffer chunk) throws IOException {
int length = chunk.remaining();
outputStream.write(chunk.array(), chunk.arrayOffset() + chunk.position(), length);
byteCount += length;
return length;
}
@Override
public long getBytesWritten() {
return byteCount;
}
@Override
public void flush() throws IOException {
// NO-OP: Unused
}
@Override
public void end() throws IOException {
// NO-OP: Unused
}
}
}

View File

@@ -0,0 +1,519 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11.upgrade;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import javax.net.SocketFactory;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.WebConnection;
import org.junit.Assert;
import org.junit.Test;
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestUpgrade extends TomcatBaseTest {
private static final String MESSAGE = "This is a test.";
@Test
public void testSimpleUpgradeBlocking() throws Exception {
UpgradeConnection uc = doUpgrade(EchoBlocking.class);
uc.shutdownInput();
uc.shutdownOutput();
}
@Test
public void testSimpleUpgradeNonBlocking() throws Exception {
UpgradeConnection uc = doUpgrade(EchoNonBlocking.class);
uc.shutdownInput();
uc.shutdownOutput();
}
@Test
public void testMessagesBlocking() throws Exception {
doTestMessages(EchoBlocking.class);
}
@Test
public void testMessagesNonBlocking() throws Exception {
doTestMessages(EchoNonBlocking.class);
}
@Test
public void testSetNullReadListener() throws Exception {
doTestCheckClosed(SetNullReadListener.class);
}
@Test
public void testSetNullWriteListener() throws Exception {
doTestCheckClosed(SetNullWriteListener.class);
}
@Test
public void testSetReadListenerTwice() throws Exception {
doTestCheckClosed(SetReadListenerTwice.class);
}
@Test
public void testSetWriteListenerTwice() throws Exception {
doTestCheckClosed(SetWriteListenerTwice.class);
}
@Test
public void testFirstCallToOnWritePossible() throws Exception {
doTestFixedResponse(FixedResponseNonBlocking.class);
}
private void doTestCheckClosed(
Class<? extends HttpUpgradeHandler> upgradeHandlerClass)
throws Exception {
UpgradeConnection conn = doUpgrade(upgradeHandlerClass);
Reader r = conn.getReader();
int c = r.read();
Assert.assertEquals(-1, c);
}
private void doTestFixedResponse(
Class<? extends HttpUpgradeHandler> upgradeHandlerClass)
throws Exception {
UpgradeConnection conn = doUpgrade(upgradeHandlerClass);
Reader r = conn.getReader();
int c = r.read();
Assert.assertEquals(FixedResponseNonBlocking.FIXED_RESPONSE, c);
}
private void doTestMessages (
Class<? extends HttpUpgradeHandler> upgradeHandlerClass)
throws Exception {
UpgradeConnection uc = doUpgrade(upgradeHandlerClass);
PrintWriter pw = new PrintWriter(uc.getWriter());
BufferedReader reader = uc.getReader();
pw.println(MESSAGE);
pw.flush();
Thread.sleep(500);
pw.println(MESSAGE);
pw.flush();
uc.shutdownOutput();
// Note: BufferedReader.readLine() strips new lines
// ServletInputStream.readLine() does not strip new lines
String response = reader.readLine();
Assert.assertEquals(MESSAGE, response);
response = reader.readLine();
Assert.assertEquals(MESSAGE, response);
uc.shutdownInput();
pw.close();
}
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;
}
private static class UpgradeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final Class<? extends HttpUpgradeHandler> upgradeHandlerClass;
public UpgradeServlet(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) {
this.upgradeHandlerClass = upgradeHandlerClass;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.upgrade(upgradeHandlerClass);
}
}
private static class UpgradeConnection {
private final Socket socket;
private final Writer writer;
private final BufferedReader reader;
public UpgradeConnection(Socket socket) {
this.socket = socket;
InputStream is;
OutputStream os;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
} catch (IOException ioe) {
throw new IllegalArgumentException(ioe);
}
BufferedReader reader =
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
this.writer = writer;
this.reader = reader;
}
public Writer getWriter() {
return writer;
}
public BufferedReader getReader() {
return reader;
}
public void shutdownOutput() throws IOException {
writer.flush();
socket.shutdownOutput();
}
public void shutdownInput() throws IOException {
socket.shutdownInput();
}
}
public static class EchoBlocking implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
try (ServletInputStream sis = connection.getInputStream();
ServletOutputStream sos = connection.getOutputStream()){
byte[] buffer = new byte[8192];
int read;
while ((read = sis.read(buffer)) >= 0) {
sos.write(buffer, 0, read);
sos.flush();
}
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
@Override
public void destroy() {
// NO-OP
}
}
public static class EchoNonBlocking implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
ServletInputStream sis;
ServletOutputStream sos;
try {
sis = connection.getInputStream();
sos = connection.getOutputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
EchoListener echoListener = new EchoListener(sis, sos);
sis.setReadListener(echoListener);
sos.setWriteListener(echoListener);
}
@Override
public void destroy() {
// NO-OP
}
private static class EchoListener implements ReadListener, WriteListener {
private final ServletInputStream sis;
private final ServletOutputStream sos;
private final byte[] buffer = new byte[8192];
public EchoListener(ServletInputStream sis, ServletOutputStream sos) {
this.sis = sis;
this.sos = sos;
}
@Override
public void onWritePossible() throws IOException {
if (sis.isFinished()) {
sis.close();
sos.close();
}
while (sis.isReady()) {
int read = sis.read(buffer);
if (read > 0) {
sos.write(buffer, 0, read);
if (!sos.isReady()) {
break;
}
}
}
}
@Override
public void onDataAvailable() throws IOException {
if (sos.isReady()) {
onWritePossible();
}
}
@Override
public void onAllDataRead() throws IOException {
if (sos.isReady()) {
onWritePossible();
}
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
}
}
public static class SetNullReadListener implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
ServletInputStream sis;
try {
sis = connection.getInputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
sis.setReadListener(null);
}
@Override
public void destroy() {
// NO-OP
}
}
public static class SetNullWriteListener implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
ServletOutputStream sos;
try {
sos = connection.getOutputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
sos.setWriteListener(null);
}
@Override
public void destroy() {
// NO-OP
}
}
public static class SetReadListenerTwice implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
ServletInputStream sis;
ServletOutputStream sos;
try {
sis = connection.getInputStream();
sos = connection.getOutputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
sos.setWriteListener(new NoOpWriteListener());
ReadListener rl = new NoOpReadListener();
sis.setReadListener(rl);
sis.setReadListener(rl);
}
@Override
public void destroy() {
// NO-OP
}
}
public static class SetWriteListenerTwice implements HttpUpgradeHandler {
@Override
public void init(WebConnection connection) {
ServletInputStream sis;
ServletOutputStream sos;
try {
sis = connection.getInputStream();
sos = connection.getOutputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
sis.setReadListener(new NoOpReadListener());
WriteListener wl = new NoOpWriteListener();
sos.setWriteListener(wl);
sos.setWriteListener(wl);
}
@Override
public void destroy() {
// NO-OP
}
}
public static class FixedResponseNonBlocking implements HttpUpgradeHandler {
public static final char FIXED_RESPONSE = 'F';
private ServletInputStream sis;
private ServletOutputStream sos;
@Override
public void init(WebConnection connection) {
try {
sis = connection.getInputStream();
sos = connection.getOutputStream();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
sis.setReadListener(new NoOpReadListener());
sos.setWriteListener(new FixedResponseWriteListener());
}
@Override
public void destroy() {
// NO-OP
}
private class FixedResponseWriteListener extends NoOpWriteListener {
@Override
public void onWritePossible() {
try {
sos.write(FIXED_RESPONSE);
sos.flush();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
}
}
private static class NoOpReadListener implements ReadListener {
@Override
public void onDataAvailable() {
// NO-OP
}
@Override
public void onAllDataRead() {
// Always NO-OP for HTTP Upgrade
}
@Override
public void onError(Throwable throwable) {
// NO-OP
}
}
private static class NoOpWriteListener implements WriteListener {
@Override
public void onWritePossible() {
// NO-OP
}
@Override
public void onError(Throwable throwable) {
// NO-OP
}
}
}

View File

@@ -0,0 +1,276 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http11.upgrade;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import javax.net.SocketFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.WebConnection;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.SSLSupport;
import org.apache.tomcat.util.net.SocketEvent;
import org.apache.tomcat.util.net.SocketWrapperBase;
import org.apache.tomcat.util.net.SocketWrapperBase.BlockingMode;
import org.apache.tomcat.util.net.SocketWrapperBase.CompletionState;
public class TestUpgradeInternalHandler extends TomcatBaseTest {
private static final String MESSAGE = "This is a test.";
@Test
public void testUpgradeInternal() throws Exception {
Assume.assumeTrue(
"Only supported on NIO 2",
getTomcatInstance().getConnector().getProtocolHandlerClassName().contains("Nio2"));
UpgradeConnection uc = doUpgrade(EchoAsync.class);
PrintWriter pw = new PrintWriter(uc.getWriter());
BufferedReader reader = uc.getReader();
// Add extra sleep to avoid completing inline
Thread.sleep(500);
pw.println(MESSAGE);
pw.flush();
Thread.sleep(500);
uc.shutdownOutput();
// Note: BufferedReader.readLine() strips new lines
// ServletInputStream.readLine() does not strip new lines
String response = reader.readLine();
Assert.assertEquals(MESSAGE, response);
uc.shutdownInput();
pw.close();
}
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;
}
private static class UpgradeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final Class<? extends HttpUpgradeHandler> upgradeHandlerClass;
public UpgradeServlet(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) {
this.upgradeHandlerClass = upgradeHandlerClass;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.upgrade(upgradeHandlerClass);
}
}
private static class UpgradeConnection {
private final Socket socket;
private final Writer writer;
private final BufferedReader reader;
public UpgradeConnection(Socket socket) {
this.socket = socket;
InputStream is;
OutputStream os;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
} catch (IOException ioe) {
throw new IllegalArgumentException(ioe);
}
BufferedReader reader =
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
this.writer = writer;
this.reader = reader;
}
public Writer getWriter() {
return writer;
}
public BufferedReader getReader() {
return reader;
}
public void shutdownOutput() throws IOException {
writer.flush();
socket.shutdownOutput();
}
public void shutdownInput() throws IOException {
socket.shutdownInput();
}
}
public static class EchoAsync implements InternalHttpUpgradeHandler {
private SocketWrapperBase<?> wrapper;
@Override
public void init(WebConnection connection) {
System.out.println("Init: " + connection);
// Arbitrarily located in the init, could be in the initial read event, asynchronous, etc.
// Note: the completion check used will not call the completion handler if the IO completed inline and without error.
// Using a completion check that always calls complete would be easier here since the action is the same even with inline completion.
final ByteBuffer buffer = ByteBuffer.allocate(1024);
CompletionState state = wrapper.read(BlockingMode.NON_BLOCK, 10, TimeUnit.SECONDS, null, SocketWrapperBase.READ_DATA, new CompletionHandler<Long, Void>() {
@Override
public void completed(Long result, Void attachment) {
System.out.println("Read: " + result.longValue());
write(buffer);
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
}, buffer);
System.out.println("CompletionState: " + state);
if (state == CompletionState.INLINE) {
write(buffer);
}
}
private void write(ByteBuffer buffer) {
buffer.flip();
CompletionState state = wrapper.write(BlockingMode.BLOCK, 10, TimeUnit.SECONDS, null, SocketWrapperBase.COMPLETE_WRITE, new CompletionHandler<Long, Void>() {
@Override
public void completed(Long result, Void attachment) {
System.out.println("Write: " + result.longValue());
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
}, buffer);
System.out.println("CompletionState: " + state);
}
@Override
public void pause() {
// NO-OP
}
@Override
public void destroy() {
// NO-OP
}
@Override
public SocketState upgradeDispatch(SocketEvent status) {
System.out.println("Processing: " + status);
switch (status) {
case OPEN_READ:
// Note: there's always an initial read event at the moment (reading should be skipped since it ends up in the internal buffer)
break;
case OPEN_WRITE:
break;
case STOP:
case DISCONNECT:
case ERROR:
case TIMEOUT:
case CONNECT_FAIL:
return SocketState.CLOSED;
}
return SocketState.UPGRADED;
}
@Override
public void timeoutAsync(long now) {
// NO-OP
}
@Override
public void setSocketWrapper(SocketWrapperBase<?> wrapper) {
this.wrapper = wrapper;
}
@Override
public void setSslSupport(SSLSupport sslSupport) {
// NO-OP
}
}
}