407386 - Cookies not copied in ServletWebSocketRequest

+ Adding missing to super.setCookies()
+ Adding test case to prevent regression
This commit is contained in:
Joakim Erdfelt 2013-05-21 10:52:28 -07:00
parent b959a742d0
commit f403a1e185
2 changed files with 139 additions and 4 deletions

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.websocket.server;
import java.net.HttpCookie;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
@ -37,7 +38,6 @@ import org.eclipse.jetty.websocket.api.util.QuoteUtil;
public class ServletWebSocketRequest extends UpgradeRequest
{
private Map<String, String> cookieMap;
private HttpServletRequest req;
public ServletWebSocketRequest(HttpServletRequest request)
@ -53,11 +53,14 @@ public class ServletWebSocketRequest extends UpgradeRequest
super.setParameterMap(request.getParameterMap());
// Copy Cookies
cookieMap = new HashMap<String, String>();
for (Cookie cookie : request.getCookies())
List<HttpCookie> cookies = new ArrayList<>();
for (Cookie rcookie : request.getCookies())
{
cookieMap.put(cookie.getName(),cookie.getValue());
HttpCookie hcookie = new HttpCookie(rcookie.getName(),rcookie.getValue());
// no point handling domain/path/expires/secure/httponly on client request cookies
cookies.add(hcookie);
}
super.setCookies(cookies);
// Copy Headers
Enumeration<String> headerNames = request.getHeaderNames();

View File

@ -0,0 +1,132 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.server;
import static org.hamcrest.Matchers.*;
import java.net.HttpCookie;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
import org.eclipse.jetty.websocket.server.helper.EchoSocket;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class RequestHeadersTest
{
private static class EchoCreator implements WebSocketCreator
{
private UpgradeRequest lastRequest;
private UpgradeResponse lastResponse;
private EchoSocket echoSocket = new EchoSocket();
@Override
public Object createWebSocket(UpgradeRequest req, UpgradeResponse resp)
{
this.lastRequest = req;
this.lastResponse = resp;
return echoSocket;
}
public UpgradeRequest getLastRequest()
{
return lastRequest;
}
public UpgradeResponse getLastResponse()
{
return lastResponse;
}
}
public static class EchoRequestServlet extends WebSocketServlet
{
/**
*
*/
private static final long serialVersionUID = -6575001979901924179L;
private final WebSocketCreator creator;
public EchoRequestServlet(WebSocketCreator creator)
{
this.creator = creator;
}
@Override
public void configure(WebSocketServletFactory factory)
{
factory.setCreator(this.creator);
}
}
private static SimpleServletServer server;
private static EchoCreator echoCreator;
@BeforeClass
public static void startServer() throws Exception
{
echoCreator = new EchoCreator();
server = new SimpleServletServer(new EchoRequestServlet(echoCreator));
server.start();
}
@AfterClass
public static void stopServer()
{
server.stop();
}
@Test
public void testAccessRequestCookies() throws Exception
{
BlockheadClient client = new BlockheadClient(server.getServerUri());
client.setTimeout(TimeUnit.SECONDS,1);
try
{
client.connect();
client.addHeader("Cookie: fruit=Pear; type=Anjou\r\n");
client.sendStandardRequest();
client.expectUpgradeResponse();
UpgradeRequest req = echoCreator.getLastRequest();
Assert.assertThat("Last Request",req,notNullValue());
List<HttpCookie> cookies = req.getCookies();
Assert.assertThat("Request cookies",cookies,notNullValue());
Assert.assertThat("Request cookies.size",cookies.size(),is(2));
for (HttpCookie cookie : cookies)
{
Assert.assertThat("Cookie name",cookie.getName(),anyOf(is("fruit"),is("type")));
Assert.assertThat("Cookie value",cookie.getValue(),anyOf(is("Pear"),is("Anjou")));
}
}
finally
{
client.close();
}
}
}