Merge branch 'jetty-9.4.x' into jetty-9.4.x-2232-HttpParser-cleanup

This commit is contained in:
Greg Wilkins 2018-03-14 16:22:29 +11:00
commit 4812ab7e05
4 changed files with 59 additions and 21 deletions

View File

@ -619,9 +619,9 @@ public class Response implements HttpServletResponse
}
_mimeType=null;
_characterEncoding=null;
_outputType = OutputType.NONE;
setContentType(null);
setCharacterEncoding(null);
setHeader(HttpHeader.EXPIRES,null);
setHeader(HttpHeader.LAST_MODIFIED,null);
setHeader(HttpHeader.CACHE_CONTROL,null);

View File

@ -19,30 +19,34 @@
package org.eclipse.jetty.server;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AdvancedRunner.class)
public class ErrorHandlerTest
{
Server server;
LocalConnector connector;
static Server server;
static LocalConnector connector;
@Before
public void before() throws Exception
@BeforeClass
public static void before() throws Exception
{
server = new Server();
connector = new LocalConnector(server);
@ -72,17 +76,36 @@ public class ErrorHandlerTest
.append("}");
break;
}
case "text/plain":
{
baseRequest.setHandled(true);
response.setContentType("text/plain");
response.getOutputStream().print(response.getContentType());
break;
}
default:
super.generateAcceptableResponse(baseRequest,request,response,code,message,mimeType);
}
}
});
server.setHandler(new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if(target.startsWith("/charencoding/"))
{
response.setCharacterEncoding("utf-8");
response.sendError(404);
}
}
});
server.start();
}
@After
public void after() throws Exception
@AfterClass
public static void after() throws Exception
{
server.stop();
}
@ -260,4 +283,19 @@ public class ErrorHandlerTest
assertThat(response,containsString("Content-Type: text/json"));
}
@Test
public void testCharEncoding() throws Exception
{
String rawResponse = connector.getResponse(
"GET /charencoding/foo HTTP/1.1\r\n"+
"Host: Localhost\r\n"+
"Accept: text/plain\r\n"+
"\r\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat("Response status code", response.getStatus(), is(404));
HttpField contentType = response.getField(HttpHeader.CONTENT_TYPE);
assertThat("Response Content-Type", contentType, is(notNullValue()));
assertThat("Response Content-Type value", contentType.getValue(), not(containsString("null")));
}
}

View File

@ -218,28 +218,28 @@ public class LowResourcesMonitorTest
@Test
public void testMaxLowResourceTime() throws Exception
{
_lowResourcesMonitor.setMaxLowResourcesTime(2000);
_lowResourcesMonitor.setMaxLowResourcesTime(5000);
Assert.assertFalse(_lowResourcesMonitor.isLowOnResources());
try(Socket socket0 = new Socket("localhost",_connector.getLocalPort()))
{
_lowResourcesMonitor.setMaxMemory(1);
Thread.sleep(1200);
Thread.sleep(2400);
Assert.assertTrue(_lowResourcesMonitor.isLowOnResources());
try(Socket socket1 = new Socket("localhost",_connector.getLocalPort()))
{
Thread.sleep(1200);
Thread.sleep(2400);
Assert.assertTrue(_lowResourcesMonitor.isLowOnResources());
Assert.assertEquals(-1,socket0.getInputStream().read());
socket1.getOutputStream().write("G".getBytes(StandardCharsets.UTF_8));
Thread.sleep(1200);
Thread.sleep(2400);
Assert.assertTrue(_lowResourcesMonitor.isLowOnResources());
socket1.getOutputStream().write("E".getBytes(StandardCharsets.UTF_8));
Thread.sleep(1200);
Thread.sleep(2400);
Assert.assertTrue(_lowResourcesMonitor.isLowOnResources());
Assert.assertEquals(-1,socket1.getInputStream().read());
}

View File

@ -156,11 +156,11 @@ public class ExecutionStrategyTest
Producer producer = new TestProducer()
{
int tasks=TASKS;
AtomicInteger tasks = new AtomicInteger(TASKS);
@Override
public Runnable produce()
{
final int id = --tasks;
final int id = tasks.decrementAndGet();
if (id>=0)
{
while(_threads.isRunning())