Skip to content

Loading…

Client side 'set cookie' feature #89

Merged
merged 3 commits into from

3 participants

@dennisbyrne

No description provided.

@aslakhellesoy
webbit member

Nice one Dennis!

@joewalnes joewalnes merged commit 647546a into webbit:master
@joewalnes
webbit member

LGTM. Merged. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 19, 2012
  1. allow cookies to be set client side

    Dennis Byrne committed
  2. patch mods per conversation w/ jw

    Dennis Byrne committed
View
3 src/main/java/org/webbitserver/HttpRequest.java
@@ -15,7 +15,7 @@
*/
public interface HttpRequest extends DataHolder {
- String COOKIE_HEADER = "Cookie";
+ public String COOKIE_HEADER = "Cookie";
String uri();
@@ -155,5 +155,4 @@
* Timestamp (millis since epoch) of when this request was first received by the server.
*/
long timestamp();
-
}
View
5 src/main/java/org/webbitserver/netty/NettyHttpRequest.java
@@ -9,10 +9,7 @@
import java.net.HttpCookie;
import java.net.SocketAddress;
import java.net.URI;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
public class NettyHttpRequest implements org.webbitserver.HttpRequest {
View
26 src/main/java/org/webbitserver/netty/WebSocketClient.java
@@ -33,16 +33,20 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.io.InputStream;
+import java.net.HttpCookie;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static org.jboss.netty.channel.Channels.pipeline;
public class WebSocketClient implements WebSocket {
@@ -74,12 +78,13 @@
private Thread.UncaughtExceptionHandler exceptionHandler;
private Thread.UncaughtExceptionHandler ioExceptionHandler;
private SslFactory sslFactory;
+ private final List<HttpCookie> cookies = new ArrayList<HttpCookie>();
public WebSocketClient(URI uri, WebSocketHandler webSocketHandler) {
- this(uri, webSocketHandler, Executors.newSingleThreadExecutor());
+ this(uri, webSocketHandler, newSingleThreadExecutor());
}
- public WebSocketClient(URI uri, WebSocketHandler webSocketHandler, Executor executor) {
+ public WebSocketClient(URI uri, WebSocketHandler webSocketHandler, Executor executor){
this.uri = uri;
this.webSocketHandler = webSocketHandler;
this.executor = executor;
@@ -102,6 +107,11 @@ public WebSocketClient(URI uri, WebSocketHandler webSocketHandler, Executor exec
connectionExceptionHandler(new SilentExceptionHandler());
}
+ public WebSocketClient cookie(HttpCookie httpCookie){
+ cookies.add(httpCookie);
+ return this;
+ }
+
private String getPath(URI uri) {
String path = uri.getPath();
return "".equals(path) ? "/" : path;
@@ -200,6 +210,18 @@ private HttpRequest createNettyHttpRequest(String uri, String host) {
request.setHeader(HttpHeaders.Names.UPGRADE, "websocket");
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
request.setHeader(Hybi.SEC_WEBSOCKET_VERSION, VERSION);
+ StringBuilder builder = new StringBuilder();
+ boolean first = true;
+ for(HttpCookie cookie : cookies){
+ if(!first){
+ builder.append("; ");
+ }
+ builder.append(cookie.toString());
+ first = false;
+ }
+ if(!first){
+ request.setHeader(org.webbitserver.HttpRequest.COOKIE_HEADER, builder.toString());
+ }
base64Nonce = base64Nonce();
request.setHeader(Hybi.SEC_WEBSOCKET_KEY, base64Nonce);
Something went wrong with that request. Please try again.