Merged branch 'jetty-12.0.x' into 'jetty-12.1.x'.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2024-09-10 11:09:32 +02:00
commit 3d8a62d3b3
No known key found for this signature in database
GPG Key ID: 1677D141BCF3584D
4 changed files with 163 additions and 54 deletions

View File

@ -14,16 +14,23 @@
package org.eclipse.jetty.ee10.servlet;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.MappingMatch;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -33,6 +40,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ServletTest
{
@ -42,7 +50,7 @@ public class ServletTest
private LocalConnector _connector;
@BeforeEach
public void beforeEach() throws Exception
public void beforeEach()
{
_server = new Server();
_connector = new LocalConnector(_server);
@ -67,7 +75,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.getWriter().println("Hello!");
}
@ -90,7 +98,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
try
{
@ -125,7 +133,7 @@ public class ServletTest
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String input = IO.toString(req.getInputStream());
resp.getWriter().println("Hello " + input);
resp.getWriter().print("Hello " + input);
}
}, "/post");
@ -138,24 +146,67 @@ public class ServletTest
POST /ctx/post HTTP/1.1
Host: local
Content-Length: 10
""";
endPoint.addInput(request);
endPoint.addInput("1234567890\n");
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
endPoint.addInput("1234567890\n");
response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
response = endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS);
assertThat(response, containsString(" 500 "));
assertThat(response, containsString("Connection: close"));
// Do not send the content.
response = HttpTester.parseResponse(endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS));
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
}
}
@Test
public void testSimpleIdleReadNetwork() throws Exception
{
ServerConnector networkConnector = new ServerConnector(_server, 1, 1);
_server.addConnector(networkConnector);
long idleTimeout = 1000;
_context.addServlet(new HttpServlet()
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String input = IO.toString(req.getInputStream());
resp.getWriter().print("Hello " + input);
}
}, "/post");
networkConnector.setIdleTimeout(idleTimeout);
_server.start();
try (SocketChannel client = SocketChannel.open(new InetSocketAddress("localhost", networkConnector.getLocalPort())))
{
String request = """
POST /ctx/post HTTP/1.1
Host: local
Content-Length: 10
""";
client.write(StandardCharsets.UTF_8.encode(request));
client.write(StandardCharsets.UTF_8.encode("1234567890"));
HttpTester.Response response = HttpTester.parseResponse(client);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
client.write(StandardCharsets.UTF_8.encode(request));
// Do not send the content.
response = HttpTester.parseResponse(client);
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
}
}
@ -165,7 +216,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.getWriter().println("Hello!");
}
@ -180,7 +231,7 @@ public class ServletTest
String request = """
GET /ctx/get HTTP/1.1
Host: local
""";
endPoint.addInput(request);
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
@ -203,7 +254,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
HttpServletMapping mapping = req.getHttpServletMapping();
assertThat(mapping.getMappingMatch(), is(MappingMatch.EXACT));

View File

@ -1,4 +1,3 @@
# Jetty Logging using jetty-slf4j-impl
#org.eclipse.jetty.LEVEL=DEBUG
#org.eclipse.jetty.http.LEVEL=DEBUG
#org.eclipse.jetty.server.LEVEL=DEBUG

View File

@ -14,16 +14,23 @@
package org.eclipse.jetty.ee11.servlet;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -34,7 +41,9 @@ import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ServletTest
{
@ -44,7 +53,7 @@ public class ServletTest
private LocalConnector _connector;
@BeforeEach
public void beforeEach() throws Exception
public void beforeEach()
{
_server = new Server();
_connector = new LocalConnector(_server);
@ -69,7 +78,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.getWriter().println("Hello!");
}
@ -92,7 +101,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
try
{
@ -127,7 +136,7 @@ public class ServletTest
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String input = IO.toString(req.getInputStream());
resp.getWriter().println("Hello " + input);
resp.getWriter().print("Hello " + input);
}
}, "/post");
@ -140,24 +149,67 @@ public class ServletTest
POST /ctx/post HTTP/1.1
Host: local
Content-Length: 10
""";
endPoint.addInput(request);
endPoint.addInput("1234567890\n");
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
endPoint.addInput("1234567890\n");
response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
response = endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS);
assertThat(response, containsString(" 500 "));
assertThat(response, containsString("Connection: close"));
// Do not send the content.
response = HttpTester.parseResponse(endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS));
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
}
}
@Test
public void testSimpleIdleReadNetwork() throws Exception
{
ServerConnector networkConnector = new ServerConnector(_server, 1, 1);
_server.addConnector(networkConnector);
long idleTimeout = 1000;
_context.addServlet(new HttpServlet()
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String input = IO.toString(req.getInputStream());
resp.getWriter().print("Hello " + input);
}
}, "/post");
networkConnector.setIdleTimeout(idleTimeout);
_server.start();
try (SocketChannel client = SocketChannel.open(new InetSocketAddress("localhost", networkConnector.getLocalPort())))
{
String request = """
POST /ctx/post HTTP/1.1
Host: local
Content-Length: 10
""";
client.write(StandardCharsets.UTF_8.encode(request));
client.write(StandardCharsets.UTF_8.encode("1234567890"));
HttpTester.Response response = HttpTester.parseResponse(client);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
client.write(StandardCharsets.UTF_8.encode(request));
// Do not send the content.
response = HttpTester.parseResponse(client);
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
}
}
@ -167,7 +219,7 @@ public class ServletTest
_context.addServlet(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.getWriter().println("Hello!");
}
@ -182,7 +234,7 @@ public class ServletTest
String request = """
GET /ctx/get HTTP/1.1
Host: local
""";
endPoint.addInput(request);
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
@ -226,7 +278,7 @@ public class ServletTest
ServletHolder holder = new ServletHolder("MyServlet", new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
HttpServletMapping mapping = req.getHttpServletMapping();
resp.getWriter().println("""

View File

@ -20,6 +20,10 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.IO;
@ -29,7 +33,9 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ServletTest
{
@ -122,7 +128,7 @@ public class ServletTest
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String input = IO.toString(req.getInputStream());
resp.getWriter().println("Hello " + input);
resp.getWriter().print("Hello " + input);
}
}), "/post");
@ -135,24 +141,25 @@ public class ServletTest
POST /ctx/post HTTP/1.1
Host: local
Content-Length: 10
""";
endPoint.addInput(request);
endPoint.addInput("1234567890\n");
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
endPoint.addInput("1234567890\n");
response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
assertThat(response, containsString(" 200 OK"));
assertThat(response, containsString("Hello 1234567890"));
endPoint.addInput("1234567890");
response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), is("Hello 1234567890"));
endPoint.addInputAndExecute(request);
response = endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS);
assertThat(response, containsString(" 500 "));
assertThat(response, containsString("Connection: close"));
// Do not send the content.
response = HttpTester.parseResponse(endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS));
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
}
}