Merge remote-tracking branch 'origin/jetty-9.4.x'
This commit is contained in:
commit
764307b9a7
|
@ -344,7 +344,7 @@ public class Response implements HttpServletResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the set cookie
|
// add the set cookie
|
||||||
_fields.add(HttpHeader.SET_COOKIE.toString(), buf.toString());
|
_fields.add(HttpHeader.SET_COOKIE, buf.toString());
|
||||||
|
|
||||||
// Expire responses with set-cookie headers so they do not get cached.
|
// Expire responses with set-cookie headers so they do not get cached.
|
||||||
_fields.put(__EXPIRES_01JAN1970);
|
_fields.put(__EXPIRES_01JAN1970);
|
||||||
|
|
|
@ -18,6 +18,11 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.server;
|
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.nullValue;
|
||||||
|
import static org.hamcrest.Matchers.startsWith;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -42,7 +47,9 @@ import javax.servlet.ServletOutputStream;
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpField;
|
||||||
import org.eclipse.jetty.http.HttpFields;
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
import org.eclipse.jetty.http.HttpHeader;
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
import org.eclipse.jetty.http.HttpURI;
|
import org.eclipse.jetty.http.HttpURI;
|
||||||
|
@ -139,7 +146,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testContentType() throws Exception
|
public void testContentType() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
assertEquals(null, response.getContentType());
|
assertEquals(null, response.getContentType());
|
||||||
|
|
||||||
|
@ -244,7 +251,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testStrangeContentType() throws Exception
|
public void testStrangeContentType() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
assertEquals(null, response.getContentType());
|
assertEquals(null, response.getContentType());
|
||||||
|
|
||||||
|
@ -258,7 +265,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testLocale() throws Exception
|
public void testLocale() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
ContextHandler context = new ContextHandler();
|
ContextHandler context = new ContextHandler();
|
||||||
context.addLocaleEncoding(Locale.ENGLISH.toString(), "ISO-8859-1");
|
context.addLocaleEncoding(Locale.ENGLISH.toString(), "ISO-8859-1");
|
||||||
|
@ -281,7 +288,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testContentTypeCharacterEncoding() throws Exception
|
public void testContentTypeCharacterEncoding() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setContentType("foo/bar");
|
response.setContentType("foo/bar");
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
|
@ -309,7 +316,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testCharacterEncodingContentType() throws Exception
|
public void testCharacterEncodingContentType() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.setContentType("foo/bar");
|
response.setContentType("foo/bar");
|
||||||
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
||||||
|
@ -336,7 +343,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testContentTypeWithCharacterEncoding() throws Exception
|
public void testContentTypeWithCharacterEncoding() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setCharacterEncoding("utf16");
|
response.setCharacterEncoding("utf16");
|
||||||
response.setContentType("foo/bar; charset=UTF-8");
|
response.setContentType("foo/bar; charset=UTF-8");
|
||||||
|
@ -372,10 +379,45 @@ public class ResponseTest
|
||||||
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testResetWithNewSession() throws Exception
|
||||||
|
{
|
||||||
|
Response response = getResponse();
|
||||||
|
Request request = response.getHttpChannel().getRequest();
|
||||||
|
|
||||||
|
SessionHandler session_handler = new SessionHandler();
|
||||||
|
session_handler.setServer(_server);
|
||||||
|
session_handler.setUsingCookies(true);
|
||||||
|
session_handler.start();
|
||||||
|
request.setSessionHandler(session_handler);
|
||||||
|
HttpSession session = request.getSession(true);
|
||||||
|
|
||||||
|
assertThat(session,not(nullValue()));
|
||||||
|
assertTrue(session.isNew());
|
||||||
|
|
||||||
|
HttpField set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
|
||||||
|
assertThat(set_cookie,not(nullValue()));
|
||||||
|
assertThat(set_cookie.getValue(),startsWith("JSESSIONID"));
|
||||||
|
assertThat(set_cookie.getValue(),containsString(session.getId()));
|
||||||
|
response.setHeader("Some","Header");
|
||||||
|
response.addCookie(new Cookie("Some","Cookie"));
|
||||||
|
response.getOutputStream().print("X");
|
||||||
|
assertThat(response.getHttpFields().size(),is(4));
|
||||||
|
|
||||||
|
response.reset();
|
||||||
|
|
||||||
|
set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
|
||||||
|
assertThat(set_cookie,not(nullValue()));
|
||||||
|
assertThat(set_cookie.getValue(),startsWith("JSESSIONID"));
|
||||||
|
assertThat(set_cookie.getValue(),containsString(session.getId()));
|
||||||
|
assertThat(response.getHttpFields().size(),is(2));
|
||||||
|
response.getWriter();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testResetContentTypeWithoutCharacterEncoding() throws Exception
|
public void testResetContentTypeWithoutCharacterEncoding() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.setContentType("wrong/answer");
|
response.setContentType("wrong/answer");
|
||||||
|
@ -390,7 +432,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testResetContentTypeWithCharacterEncoding() throws Exception
|
public void testResetContentTypeWithCharacterEncoding() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setContentType("wrong/answer;charset=utf-8");
|
response.setContentType("wrong/answer;charset=utf-8");
|
||||||
response.setContentType("foo/bar");
|
response.setContentType("foo/bar");
|
||||||
|
@ -407,7 +449,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testContentTypeWithOther() throws Exception
|
public void testContentTypeWithOther() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setContentType("foo/bar; other=xyz");
|
response.setContentType("foo/bar; other=xyz");
|
||||||
assertEquals("foo/bar; other=xyz", response.getContentType());
|
assertEquals("foo/bar; other=xyz", response.getContentType());
|
||||||
|
@ -430,7 +472,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testContentTypeWithCharacterEncodingAndOther() throws Exception
|
public void testContentTypeWithCharacterEncodingAndOther() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.setCharacterEncoding("utf16");
|
response.setCharacterEncoding("utf16");
|
||||||
response.setContentType("foo/bar; charset=utf-8 other=xyz");
|
response.setContentType("foo/bar; charset=utf-8 other=xyz");
|
||||||
|
@ -458,26 +500,26 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testStatusCodes() throws Exception
|
public void testStatusCodes() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.sendError(404);
|
response.sendError(404);
|
||||||
assertEquals(404, response.getStatus());
|
assertEquals(404, response.getStatus());
|
||||||
assertEquals("Not Found", response.getReason());
|
assertEquals("Not Found", response.getReason());
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.sendError(500, "Database Error");
|
response.sendError(500, "Database Error");
|
||||||
assertEquals(500, response.getStatus());
|
assertEquals(500, response.getStatus());
|
||||||
assertEquals("Database Error", response.getReason());
|
assertEquals("Database Error", response.getReason());
|
||||||
assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeader.CACHE_CONTROL.asString()));
|
assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeader.CACHE_CONTROL.asString()));
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.setStatus(200);
|
response.setStatus(200);
|
||||||
assertEquals(200, response.getStatus());
|
assertEquals(200, response.getStatus());
|
||||||
assertEquals(null, response.getReason());
|
assertEquals(null, response.getReason());
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.sendError(406, "Super Nanny");
|
response.sendError(406, "Super Nanny");
|
||||||
assertEquals(406, response.getStatus());
|
assertEquals(406, response.getStatus());
|
||||||
|
@ -489,26 +531,26 @@ public class ResponseTest
|
||||||
public void testStatusCodesNoErrorHandler() throws Exception
|
public void testStatusCodesNoErrorHandler() throws Exception
|
||||||
{
|
{
|
||||||
_server.removeBean(_server.getBean(ErrorHandler.class));
|
_server.removeBean(_server.getBean(ErrorHandler.class));
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
response.sendError(404);
|
response.sendError(404);
|
||||||
assertEquals(404, response.getStatus());
|
assertEquals(404, response.getStatus());
|
||||||
assertEquals("Not Found", response.getReason());
|
assertEquals("Not Found", response.getReason());
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.sendError(500, "Database Error");
|
response.sendError(500, "Database Error");
|
||||||
assertEquals(500, response.getStatus());
|
assertEquals(500, response.getStatus());
|
||||||
assertEquals("Database Error", response.getReason());
|
assertEquals("Database Error", response.getReason());
|
||||||
assertThat(response.getHeader(HttpHeader.CACHE_CONTROL.asString()),Matchers.nullValue());
|
assertThat(response.getHeader(HttpHeader.CACHE_CONTROL.asString()),Matchers.nullValue());
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.setStatus(200);
|
response.setStatus(200);
|
||||||
assertEquals(200, response.getStatus());
|
assertEquals(200, response.getStatus());
|
||||||
assertEquals(null, response.getReason());
|
assertEquals(null, response.getReason());
|
||||||
|
|
||||||
response = newResponse();
|
response = getResponse();
|
||||||
|
|
||||||
response.sendError(406, "Super Nanny");
|
response.sendError(406, "Super Nanny");
|
||||||
assertEquals(406, response.getStatus());
|
assertEquals(406, response.getStatus());
|
||||||
|
@ -519,7 +561,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testWriteRuntimeIOException() throws Exception
|
public void testWriteRuntimeIOException() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
PrintWriter writer = response.getWriter();
|
PrintWriter writer = response.getWriter();
|
||||||
writer.println("test");
|
writer.println("test");
|
||||||
|
@ -546,7 +588,7 @@ public class ResponseTest
|
||||||
public void testEncodeRedirect()
|
public void testEncodeRedirect()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
Request request = response.getHttpChannel().getRequest();
|
Request request = response.getHttpChannel().getRequest();
|
||||||
request.setAuthority("myhost",8888);
|
request.setAuthority("myhost",8888);
|
||||||
request.setContextPath("/path");
|
request.setContextPath("/path");
|
||||||
|
@ -626,7 +668,7 @@ public class ResponseTest
|
||||||
{
|
{
|
||||||
for (int i=0;i<tests.length;i++)
|
for (int i=0;i<tests.length;i++)
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
Request request = response.getHttpChannel().getRequest();
|
Request request = response.getHttpChannel().getRequest();
|
||||||
|
|
||||||
request.setScheme("http");
|
request.setScheme("http");
|
||||||
|
@ -660,7 +702,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testSetBufferSizeAfterHavingWrittenContent() throws Exception
|
public void testSetBufferSizeAfterHavingWrittenContent() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
response.setBufferSize(20 * 1024);
|
response.setBufferSize(20 * 1024);
|
||||||
response.getWriter().print("hello");
|
response.getWriter().print("hello");
|
||||||
try
|
try
|
||||||
|
@ -677,7 +719,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testZeroContent() throws Exception
|
public void testZeroContent() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
PrintWriter writer = response.getWriter();
|
PrintWriter writer = response.getWriter();
|
||||||
response.setContentLength(0);
|
response.setContentLength(0);
|
||||||
assertTrue(!response.isCommitted());
|
assertTrue(!response.isCommitted());
|
||||||
|
@ -746,7 +788,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testAddCookie() throws Exception
|
public void testAddCookie() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
Cookie cookie = new Cookie("name", "value");
|
Cookie cookie = new Cookie("name", "value");
|
||||||
cookie.setDomain("domain");
|
cookie.setDomain("domain");
|
||||||
|
@ -765,7 +807,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testCookiesWithReset() throws Exception
|
public void testCookiesWithReset() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
|
|
||||||
Cookie cookie=new Cookie("name","value");
|
Cookie cookie=new Cookie("name","value");
|
||||||
cookie.setDomain("domain");
|
cookie.setDomain("domain");
|
||||||
|
@ -800,7 +842,7 @@ public class ResponseTest
|
||||||
@Test
|
@Test
|
||||||
public void testFlushAfterFullContent() throws Exception
|
public void testFlushAfterFullContent() throws Exception
|
||||||
{
|
{
|
||||||
Response response = newResponse();
|
Response response = getResponse();
|
||||||
byte[] data = new byte[]{(byte)0xCA, (byte)0xFE};
|
byte[] data = new byte[]{(byte)0xCA, (byte)0xFE};
|
||||||
ServletOutputStream output = response.getOutputStream();
|
ServletOutputStream output = response.getOutputStream();
|
||||||
response.setContentLength(data.length);
|
response.setContentLength(data.length);
|
||||||
|
@ -810,7 +852,6 @@ public class ResponseTest
|
||||||
output.flush();
|
output.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetCookie() throws Exception
|
public void testSetCookie() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -927,14 +968,13 @@ public class ResponseTest
|
||||||
response.addSetCookie("name","value%=",null,null,-1,null,false,false,0);
|
response.addSetCookie("name","value%=",null,null,-1,null,false,false,0);
|
||||||
setCookie=fields.get("Set-Cookie");
|
setCookie=fields.get("Set-Cookie");
|
||||||
assertEquals("name=value%=",setCookie);
|
assertEquals("name=value%=",setCookie);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response newResponse()
|
private Response getResponse()
|
||||||
{
|
{
|
||||||
_channel.recycle();
|
_channel.recycle();
|
||||||
_channel.getRequest().setMetaData(new MetaData.Request("GET",new HttpURI("/path/info"),HttpVersion.HTTP_1_0,new HttpFields()));
|
_channel.getRequest().setMetaData(new MetaData.Request("GET",new HttpURI("/path/info"),HttpVersion.HTTP_1_0,new HttpFields()));
|
||||||
return new Response(_channel, _channel.getResponse().getHttpOutput());
|
return _channel.getResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestSession extends Session
|
private static class TestSession extends Session
|
||||||
|
|
|
@ -81,8 +81,9 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
||||||
public void sendError(int statusCode, String message) throws IOException
|
public void sendError(int statusCode, String message) throws IOException
|
||||||
{
|
{
|
||||||
setSuccess(false);
|
setSuccess(false);
|
||||||
commitHeaders();
|
applyHeaders();
|
||||||
response.sendError(statusCode, message);
|
response.sendError(statusCode, message);
|
||||||
|
response.flushBuffer(); // commit response
|
||||||
response = null;
|
response = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,8 +91,9 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
||||||
public void sendForbidden(String message) throws IOException
|
public void sendForbidden(String message) throws IOException
|
||||||
{
|
{
|
||||||
setSuccess(false);
|
setSuccess(false);
|
||||||
commitHeaders();
|
applyHeaders();
|
||||||
response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
|
response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
|
||||||
|
response.flushBuffer(); // commit response
|
||||||
response = null;
|
response = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,11 +113,11 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
||||||
|
|
||||||
public void complete()
|
public void complete()
|
||||||
{
|
{
|
||||||
commitHeaders();
|
applyHeaders();
|
||||||
response = null;
|
response = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commitHeaders()
|
private void applyHeaders()
|
||||||
{
|
{
|
||||||
// Transfer all headers to the real HTTP response
|
// Transfer all headers to the real HTTP response
|
||||||
for (Map.Entry<String, List<String>> entry : getHeaders().entrySet())
|
for (Map.Entry<String, List<String>> entry : getHeaders().entrySet())
|
||||||
|
|
Loading…
Reference in New Issue