427128 - Cookies are not sent to the server.

Implemented/fixed handling of cookies sent by client to server.
This commit is contained in:
Simone Bordet 2014-02-03 12:49:58 +01:00
parent 5e3c8821bb
commit cfe248c67b
5 changed files with 151 additions and 10 deletions

View File

@ -18,9 +18,9 @@
package org.eclipse.jetty.websocket.jsr356;
import java.net.HttpCookie;
import java.util.List;
import java.util.Map;
import javax.websocket.ClientEndpointConfig.Configurator;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
@ -46,7 +46,23 @@ public class JsrUpgradeListener implements UpgradeListener
Map<String, List<String>> headers = request.getHeaders();
configurator.beforeRequest(headers);
request.setHeaders(headers);
// Handle cookies
for (String name : headers.keySet())
{
if ("cookie".equalsIgnoreCase(name))
{
List<String> values = headers.get(name);
if (values != null)
{
for (String cookie : values)
{
List<HttpCookie> cookies = HttpCookie.parse(cookie);
request.getCookies().addAll(cookies);
}
}
}
}
}
@Override

View File

@ -0,0 +1,122 @@
//
// ========================================================================
// Copyright (c) 1995-2014 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.jsr356;
import java.net.HttpCookie;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.ContainerProvider;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
public class CookiesTest
{
private Server server;
private ServerConnector connector;
protected void startServer(Handler handler) throws Exception
{
server = new Server();
connector = new ServerConnector(server);
server.addConnector(connector);
ContextHandler context = new ContextHandler();
context.setContextPath("/");
context.setHandler(handler);
server.setHandler(context);
server.start();
}
@After
public void stopServer() throws Exception
{
server.stop();
}
@Test
public void testCookiesAreSentToServer() throws Exception
{
final String cookieName = "name";
final String cookieValue = "value";
final String cookieString = cookieName + "=" + cookieValue;
startServer(new EchoHandler()
{
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response)
{
List<HttpCookie> cookies = request.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(cookieName, cookie.getName());
Assert.assertEquals(cookieValue, cookie.getValue());
Map<String, List<String>> headers = request.getHeaders();
// Test case insensitivity
Assert.assertTrue(headers.containsKey("cookie"));
List<String> values = headers.get("Cookie");
Assert.assertNotNull(values);
Assert.assertEquals(1, values.size());
Assert.assertEquals(cookieString, values.get(0));
return super.createWebSocket(request, response);
}
});
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
builder.configurator(new ClientEndpointConfig.Configurator()
{
@Override
public void beforeRequest(Map<String, List<String>> headers)
{
headers.put("Cookie", Collections.singletonList(cookieString));
}
});
ClientEndpointConfig config = builder.build();
Endpoint endPoint = new Endpoint()
{
@Override
public void onOpen(Session session, EndpointConfig config)
{
}
};
Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
session.close();
}
}

View File

@ -262,7 +262,8 @@ public class UpgradeRequest
public void setCookies(List<HttpCookie> cookies)
{
this.cookies = cookies;
this.cookies.clear();
this.cookies.addAll(cookies);
}
public void setExtensions(List<ExtensionConfig> configs)

View File

@ -24,10 +24,10 @@ import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ThreadLocalRandom;
import org.eclipse.jetty.util.B64Code;
@ -44,15 +44,17 @@ import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
*/
public class ClientUpgradeRequest extends UpgradeRequest
{
private final static Logger LOG = Log.getLogger(ClientUpgradeRequest.class);
private final static int MAX_KEYS = -1; // maximum number of parameter keys to decode
private static final Logger LOG = Log.getLogger(ClientUpgradeRequest.class);
private static final int MAX_KEYS = -1; // maximum number of parameter keys to decode
private static final Set<String> FORBIDDEN_HEADERS;
static
{
// headers not allowed to be set in ClientUpgradeRequest.headers
FORBIDDEN_HEADERS = new HashSet<>();
// Headers not allowed to be set in ClientUpgradeRequest.headers.
FORBIDDEN_HEADERS = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
// Cookies are handled explicitly, avoid to add them twice.
FORBIDDEN_HEADERS.add("cookie");
// Headers that cannot be set by applications.
FORBIDDEN_HEADERS.add("upgrade");
FORBIDDEN_HEADERS.add("host");
FORBIDDEN_HEADERS.add("connection");
@ -176,7 +178,7 @@ public class ClientUpgradeRequest extends UpgradeRequest
{
if (FORBIDDEN_HEADERS.contains(key))
{
LOG.warn("Skipping forbidden header - {}",key);
LOG.debug("Skipping forbidden header - {}",key);
continue; // skip
}
request.append(key).append(": ");

View File

@ -264,7 +264,7 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
}
super.doStop();
LOG.info("Stopped {}",this);
LOG.debug("Stopped {}",this);
}
/**