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

File diff suppressed because it is too large Load Diff

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

File diff suppressed because it is too large Load Diff

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
}
}
}

File diff suppressed because it is too large Load Diff

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
}
}
}