added cookie v1 and v2 encoding support
This commit is contained in:
parent
83301cd183
commit
395d57b6eb
@ -21,21 +21,21 @@
|
||||
*/
|
||||
package org.jboss.netty.example.http;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||
import org.jboss.netty.channel.Channel;
|
||||
import org.jboss.netty.channel.ChannelFactory;
|
||||
import org.jboss.netty.channel.ChannelFuture;
|
||||
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
||||
import org.jboss.netty.handler.codec.http.CookieEncoder;
|
||||
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
||||
import org.jboss.netty.handler.codec.http.HttpMethod;
|
||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||
import org.jboss.netty.handler.codec.http.HttpVersion;
|
||||
import org.jboss.netty.handler.codec.http.CookieEncoder;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
@ -91,7 +91,7 @@ public class HttpClient {
|
||||
HttpRequest request = new DefaultHttpRequest(
|
||||
HttpVersion.HTTP_1_0, HttpMethod.GET, uri.toASCIIString());
|
||||
request.addHeader(HttpHeaders.Names.HOST, host);
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder();
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder(2);
|
||||
httpCookieEncoder.addCookie("my-cookie", "foo");
|
||||
httpCookieEncoder.addCookie("another-cookie", "bar");
|
||||
request.addHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
|
||||
|
@ -21,10 +21,6 @@
|
||||
*/
|
||||
package org.jboss.netty.example.http;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
import org.jboss.netty.channel.ChannelFuture;
|
||||
@ -45,6 +41,10 @@ import org.jboss.netty.handler.codec.http.HttpResponseStatus;
|
||||
import org.jboss.netty.handler.codec.http.HttpVersion;
|
||||
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||
@ -135,7 +135,7 @@ public class HttpRequestHandler extends SimpleChannelHandler {
|
||||
Map<String, Cookie> cookies = cookieDecoder.decode(cookieString);
|
||||
if(!cookies.isEmpty()) {
|
||||
// Reset the cookies if necessary.
|
||||
CookieEncoder cookieEncoder = new CookieEncoder();
|
||||
CookieEncoder cookieEncoder = new CookieEncoder(2);
|
||||
for (Cookie cookie : cookies.values()) {
|
||||
cookieEncoder.addCookie(cookie);
|
||||
}
|
||||
|
@ -42,4 +42,10 @@ public interface Cookie extends Comparable<Cookie> {
|
||||
void setVersion(int version);
|
||||
boolean isSecure();
|
||||
void setSecure(boolean secure);
|
||||
String getCommentURL();
|
||||
void setCommentURL(String commentURL);
|
||||
boolean isDiscard();
|
||||
void setDiscard(boolean discard);
|
||||
int[] getPortList();
|
||||
void setPortList(int[] portList);
|
||||
}
|
@ -21,12 +21,12 @@
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import org.jboss.netty.util.CaseIgnoringComparator;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.jboss.netty.util.CaseIgnoringComparator;
|
||||
|
||||
/**
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||
@ -38,11 +38,25 @@ public class CookieEncoder {
|
||||
|
||||
private final String charset;
|
||||
|
||||
private final int encodingVersion;
|
||||
|
||||
public CookieEncoder() {
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET);
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET, 0);
|
||||
}
|
||||
|
||||
public CookieEncoder(int encodingVersion) {
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET, encodingVersion);
|
||||
}
|
||||
|
||||
public CookieEncoder(String charset) {
|
||||
this(charset, 0);
|
||||
}
|
||||
|
||||
public CookieEncoder(String charset, int encodingVersion) {
|
||||
if (encodingVersion < 0 || encodingVersion > 2) {
|
||||
throw new IllegalArgumentException("encoding version must be 0,1 or 2");
|
||||
}
|
||||
this.encodingVersion = encodingVersion;
|
||||
if (charset == null) {
|
||||
throw new NullPointerException("charset");
|
||||
}
|
||||
@ -58,21 +72,72 @@ public class CookieEncoder {
|
||||
}
|
||||
|
||||
public String encode() {
|
||||
// FIXME: Support both version 0 and 1 cookies
|
||||
// FIXME: Encode all cookie fields, including domain, path, maxAge, secure, and comment.
|
||||
// FIXME: Check RFC 2109 - http://www.ietf.org/rfc/rfc2109.txt
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Collection<String> cookieNames = cookies.keySet();
|
||||
if(cookieNames.isEmpty()) {
|
||||
if (cookieNames.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
for (String cookieName: cookieNames) {
|
||||
sb.append(cookieName);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append(QueryStringEncoder.encodeComponent(
|
||||
cookies.get(cookieName).getValue(), charset));
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
for (String cookieName : cookieNames) {
|
||||
Cookie cookie = cookies.get(cookieName);
|
||||
add(sb, cookieName, QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
|
||||
|
||||
add(sb, CookieHeaderNames.getMaxAgeString(encodingVersion), cookie.getMaxAge());
|
||||
|
||||
if (cookie.getPath() != null) {
|
||||
add(sb, CookieHeaderNames.PATH, QueryStringEncoder.encodeComponent(cookie.getPath(), charset));
|
||||
}
|
||||
|
||||
if (cookie.getDomain() != null) {
|
||||
add(sb, CookieHeaderNames.DOMAIN, QueryStringEncoder.encodeComponent(cookie.getDomain(), charset));
|
||||
}
|
||||
if (cookie.isSecure()) {
|
||||
sb.append(CookieHeaderNames.SECURE);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
if (encodingVersion >= 1) {
|
||||
if (cookie.getComment() != null) {
|
||||
add(sb, CookieHeaderNames.COMMENT, QueryStringEncoder.encodeComponent(cookie.getComment(), charset));
|
||||
}
|
||||
|
||||
add(sb, CookieHeaderNames.VERSION, encodingVersion);
|
||||
}
|
||||
|
||||
if (encodingVersion == 2) {
|
||||
if (cookie.getCommentURL() != null) {
|
||||
add(sb, CookieHeaderNames.COMMENTURL, QueryStringEncoder.encodeComponent(cookie.getCommentURL(), charset));
|
||||
}
|
||||
if(cookie.getPortList() != null && cookie.getPortList().length > 0) {
|
||||
sb.append(CookieHeaderNames.PORTLIST);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
for (int i = 0; i < cookie.getPortList().length; i++) {
|
||||
int port = cookie.getPortList()[i];
|
||||
if(i > 0) {
|
||||
sb.append((char)HttpCodecUtil.COMMA);
|
||||
}
|
||||
sb.append(port);
|
||||
}
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
if (cookie.isDiscard()) {
|
||||
sb.append(CookieHeaderNames.DISCARD);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void add(StringBuffer sb, String name, String val) {
|
||||
sb.append(name);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append(val);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
|
||||
private void add(StringBuffer sb, String name, int val) {
|
||||
sb.append(name);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append(val);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
|
||||
* by the @authors tag. See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation; either version 2.1 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this software; if not, write to the Free
|
||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.http;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||
*/
|
||||
public class CookieHeaderNames {
|
||||
public static final String PATH = "path";
|
||||
|
||||
private static final String EXPIRES = "expires";
|
||||
|
||||
private static final String MAX_AGE = "max-age";
|
||||
|
||||
public static final String DOMAIN = "domain";
|
||||
|
||||
public static final String SECURE = "secure";
|
||||
|
||||
public static final String COMMENT = "comment";
|
||||
|
||||
public static final String COMMENTURL = "commentURL";
|
||||
|
||||
public static final String DISCARD = "discard";
|
||||
|
||||
public static final String PORTLIST = "port";
|
||||
|
||||
public static final String VERSION = "version";
|
||||
|
||||
public static String getMaxAgeString(int version) {
|
||||
switch (version) {
|
||||
case 0:
|
||||
return EXPIRES;
|
||||
case 1:
|
||||
return MAX_AGE;
|
||||
case 2:
|
||||
return MAX_AGE;
|
||||
default:
|
||||
return EXPIRES;
|
||||
}
|
||||
}
|
||||
}
|
@ -35,6 +35,9 @@ public class DefaultCookie implements Cookie {
|
||||
private String domain;
|
||||
private String path;
|
||||
private String comment;
|
||||
private String commentURL;
|
||||
private boolean discard;
|
||||
private int[] portList;
|
||||
private int maxAge;
|
||||
private int version;
|
||||
private boolean secure;
|
||||
@ -108,6 +111,30 @@ public class DefaultCookie implements Cookie {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public String getCommentURL() {
|
||||
return commentURL;
|
||||
}
|
||||
|
||||
public void setCommentURL(String commentURL) {
|
||||
this.commentURL = commentURL;
|
||||
}
|
||||
|
||||
public boolean isDiscard() {
|
||||
return discard;
|
||||
}
|
||||
|
||||
public void setDiscard(boolean discard) {
|
||||
this.discard = discard;
|
||||
}
|
||||
|
||||
public int[] getPortList() {
|
||||
return portList;
|
||||
}
|
||||
|
||||
public void setPortList(int[] portList) {
|
||||
this.portList = portList;
|
||||
}
|
||||
|
||||
public int getMaxAge() {
|
||||
return maxAge;
|
||||
}
|
||||
|
@ -63,6 +63,11 @@ class HttpCodecUtil {
|
||||
*/
|
||||
static final byte SEMICOLON = 59;
|
||||
|
||||
/**
|
||||
* comma ','
|
||||
*/
|
||||
static final byte COMMA = 44;
|
||||
|
||||
private HttpCodecUtil() {
|
||||
super();
|
||||
}
|
||||
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
|
||||
* by the @authors tag. See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation; either version 2.1 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this software; if not, write to the Free
|
||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||
*/
|
||||
public class HttpCookieEncoderTest {
|
||||
@Test
|
||||
public void testEncodingSingleCookieV0() {
|
||||
String result = "myCookie=myValue;expires=50;path=%2Fapathsomewhere;domain=%2Fadomainsomewhere;secure;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(0);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentURL("http/:aurl.com");
|
||||
cookie.setDomain("/adomainsomewhere");
|
||||
cookie.setDiscard(true);
|
||||
cookie.setMaxAge(50);
|
||||
cookie.setPath("/apathsomewhere");
|
||||
cookie.setPortList(new int[]{80, 8080});
|
||||
cookie.setSecure(true);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(result, encodedCookie);
|
||||
}
|
||||
@Test
|
||||
public void testEncodingSingleCookieV1() {
|
||||
String result = "myCookie=myValue;max-age=50;path=%2Fapathsomewhere;domain=%2Fadomainsomewhere;secure;comment=this%20is%20a%20comment;version=1;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(1);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentURL("http/:aurl.com");
|
||||
cookie.setDomain("/adomainsomewhere");
|
||||
cookie.setDiscard(true);
|
||||
cookie.setMaxAge(50);
|
||||
cookie.setPath("/apathsomewhere");
|
||||
cookie.setPortList(new int[]{80, 8080});
|
||||
cookie.setSecure(true);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(result, encodedCookie);
|
||||
}
|
||||
@Test
|
||||
public void testEncodingSingleCookieV2() {
|
||||
String result = "myCookie=myValue;max-age=50;path=%2Fapathsomewhere;domain=%2Fadomainsomewhere;secure;comment=this%20is%20a%20comment;version=2;commentURL=http%2F%3Aaurl.com;port=80,8080;discard;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(2);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentURL("http/:aurl.com");
|
||||
cookie.setDomain("/adomainsomewhere");
|
||||
cookie.setDiscard(true);
|
||||
cookie.setMaxAge(50);
|
||||
cookie.setPath("/apathsomewhere");
|
||||
cookie.setPortList(new int[]{80, 8080});
|
||||
cookie.setSecure(true);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(result, encodedCookie);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodingMultipleCookies() {
|
||||
String c1 = "myCookie=myValue;max-age=50;path=%2Fapathsomewhere;domain=%2Fadomainsomewhere;secure;comment=this%20is%20a%20comment;version=2;commentURL=http%2F%3Aaurl.com;port=80,8080;discard;";
|
||||
String c2 = "myCookie2=myValue2;max-age=0;path=%2Fanotherpathsomewhere;domain=%2Fanotherdomainsomewhere;comment=this%20is%20another%20comment;version=2;commentURL=http%2F%3Aanotherurl.com;";
|
||||
String c3 = "myCookie3=myValue3;max-age=0;version=2;";
|
||||
CookieEncoder encoder = new CookieEncoder(2);
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentURL("http/:aurl.com");
|
||||
cookie.setDomain("/adomainsomewhere");
|
||||
cookie.setDiscard(true);
|
||||
cookie.setMaxAge(50);
|
||||
cookie.setPath("/apathsomewhere");
|
||||
cookie.setPortList(new int[]{80, 8080});
|
||||
cookie.setSecure(true);
|
||||
encoder.addCookie(cookie);
|
||||
Cookie cookie2 = new DefaultCookie("myCookie2", "myValue2");
|
||||
cookie2.setComment("this is another comment");
|
||||
cookie2.setCommentURL("http/:anotherurl.com");
|
||||
cookie2.setDomain("/anotherdomainsomewhere");
|
||||
cookie2.setDiscard(false);
|
||||
cookie2.setPath("/anotherpathsomewhere");
|
||||
cookie2.setSecure(false);
|
||||
encoder.addCookie(cookie2);
|
||||
Cookie cookie3 = new DefaultCookie("myCookie3", "myValue3");
|
||||
encoder.addCookie(cookie3);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(c1 + c2 + c3, encodedCookie);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user