* Fixed a problem where 'Expires' cookie attribute is misinterpreted
* CookieEncoder now uses Cookie.getVersion() to determine the cookie version instead of constructor parameter
This commit is contained in:
parent
32ddc4541d
commit
d1bafbc65f
@ -21,6 +21,10 @@
|
||||
*/
|
||||
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;
|
||||
@ -33,10 +37,6 @@ 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 java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Andy Taylor (andy.taylor@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(2);
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder();
|
||||
httpCookieEncoder.addCookie("my-cookie", "foo");
|
||||
httpCookieEncoder.addCookie("another-cookie", "bar");
|
||||
request.addHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
|
||||
|
@ -21,6 +21,10 @@
|
||||
*/
|
||||
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;
|
||||
@ -41,10 +45,6 @@ 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(2);
|
||||
CookieEncoder cookieEncoder = new CookieEncoder();
|
||||
for (Cookie cookie : cookies.values()) {
|
||||
cookieEncoder.addCookie(cookie);
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* JBoss, Home of Professional Open Source
|
||||
*
|
||||
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
|
||||
* by the @author tags. 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 java.text.SimpleDateFormat;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Trustin Lee (tlee@redhat.com)
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
final class CookieDateFormat extends SimpleDateFormat {
|
||||
|
||||
private static final long serialVersionUID = 1789486337887402640L;
|
||||
|
||||
CookieDateFormat() {
|
||||
super("E, d-MMM-y H:m:s z");
|
||||
setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@ -106,9 +107,18 @@ public class CookieDecoder {
|
||||
path = value;
|
||||
}
|
||||
else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) {
|
||||
// FIXME: Expires attribute has different representation from Max-Age.
|
||||
// Format: Wdy, DD-Mon-YYYY HH:MM:SS GMT
|
||||
maxAge = Integer.valueOf(value);
|
||||
try {
|
||||
long maxAgeMillis =
|
||||
new CookieDateFormat().parse(value).getTime() - System.currentTimeMillis();
|
||||
if (maxAgeMillis <= 0) {
|
||||
maxAge = 0;
|
||||
} else {
|
||||
maxAge = (int) (maxAgeMillis / 1000) +
|
||||
(maxAgeMillis % 1000 != 0? 1 : 0);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
maxAge = 0;
|
||||
}
|
||||
}
|
||||
else if (CookieHeaderNames.MAX_AGE.equalsIgnoreCase(name)) {
|
||||
maxAge = Integer.valueOf(value);
|
||||
|
@ -22,6 +22,7 @@
|
||||
package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@ -39,25 +40,11 @@ public class CookieEncoder {
|
||||
|
||||
private final String charset;
|
||||
|
||||
private final int encodingVersion;
|
||||
|
||||
public CookieEncoder() {
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET, 0);
|
||||
}
|
||||
|
||||
public CookieEncoder(int encodingVersion) {
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET, encodingVersion);
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
@ -82,9 +69,16 @@ public class CookieEncoder {
|
||||
Cookie cookie = cookies.get(cookieName);
|
||||
add(sb, cookieName, QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
|
||||
|
||||
// FIXME: Expires attribute has different representation from Max-Age.
|
||||
// Format: Wdy, DD-Mon-YYYY HH:MM:SS GMT
|
||||
add(sb, CookieHeaderNames.getMaxAgeString(encodingVersion), cookie.getMaxAge());
|
||||
if (cookie.getMaxAge() >= 0) {
|
||||
if (cookie.getVersion() == 0) {
|
||||
add(sb, CookieHeaderNames.EXPIRES,
|
||||
new CookieDateFormat().format(
|
||||
new Date(System.currentTimeMillis() +
|
||||
cookie.getMaxAge() * 1000L)));
|
||||
} else {
|
||||
add(sb, CookieHeaderNames.MAX_AGE, cookie.getMaxAge());
|
||||
}
|
||||
}
|
||||
|
||||
if (cookie.getPath() != null) {
|
||||
add(sb, CookieHeaderNames.PATH, cookie.getPath());
|
||||
@ -97,18 +91,17 @@ public class CookieEncoder {
|
||||
sb.append(CookieHeaderNames.SECURE);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
if (encodingVersion >= 1) {
|
||||
if (cookie.getVersion() >= 1) {
|
||||
if (cookie.getComment() != null) {
|
||||
add(sb, CookieHeaderNames.COMMENT, cookie.getComment());
|
||||
}
|
||||
|
||||
add(sb, CookieHeaderNames.VERSION, 1);
|
||||
}
|
||||
|
||||
if (encodingVersion == 2) {
|
||||
if (cookie.getCommentUrl() != null) {
|
||||
addQuoted(sb, CookieHeaderNames.COMMENTURL, cookie.getCommentUrl());
|
||||
}
|
||||
|
||||
if(!cookie.getPorts().isEmpty()) {
|
||||
sb.append(CookieHeaderNames.PORT);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
|
@ -43,7 +43,7 @@ public class DefaultCookie implements Cookie {
|
||||
private boolean discard;
|
||||
private Set<Integer> ports = Collections.emptySet();
|
||||
private Set<Integer> unmodifiablePorts = ports;
|
||||
private int maxAge;
|
||||
private int maxAge = -1;
|
||||
private int version;
|
||||
private boolean secure;
|
||||
|
||||
|
@ -23,6 +23,7 @@ package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
@ -36,7 +37,9 @@ import org.junit.Test;
|
||||
public class CookieDecoderTest {
|
||||
@Test
|
||||
public void testDecodingSingleCookieV0() {
|
||||
String cookieString = "myCookie=myValue;expires=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
cookieString = cookieString.replace("XXX", new CookieDateFormat().format(new Date(System.currentTimeMillis() + 50000)));
|
||||
|
||||
CookieDecoder cookieDecoder = new CookieDecoder();
|
||||
Map<String, Cookie> cookieMap = cookieDecoder.decode(cookieString);
|
||||
assertEquals(1, cookieMap.size());
|
||||
|
@ -23,6 +23,8 @@ package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
@ -35,9 +37,9 @@ import org.junit.Test;
|
||||
public class CookieEncoderTest {
|
||||
@Test
|
||||
public void testEncodingSingleCookieV0() {
|
||||
String result = "myCookie=myValue;expires=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
String result = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(0);
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentUrl("http://aurl.com");
|
||||
@ -48,21 +50,20 @@ public class CookieEncoderTest {
|
||||
cookie.setPorts(80, 8080);
|
||||
cookie.setSecure(true);
|
||||
String encodedCookie = encoder.encode();
|
||||
result = result.replace("XXX", new CookieDateFormat().format(new Date(System.currentTimeMillis() + 50000)));
|
||||
assertEquals(result, encodedCookie);
|
||||
}
|
||||
@Test
|
||||
public void testEncodingSingleCookieV1() {
|
||||
String result = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(1);
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentUrl("http://aurl.com");
|
||||
cookie.setDomain(".adomainsomewhere");
|
||||
cookie.setDiscard(true);
|
||||
cookie.setMaxAge(50);
|
||||
cookie.setPath("/apathsomewhere");
|
||||
cookie.setPorts(80, 8080);
|
||||
cookie.setSecure(true);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(result, encodedCookie);
|
||||
@ -71,8 +72,9 @@ public class CookieEncoderTest {
|
||||
public void testEncodingSingleCookieV2() {
|
||||
String result = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;commentURL=\"http://aurl.com\";port=\"80,8080\";discard;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder(2);
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentUrl("http://aurl.com");
|
||||
cookie.setDomain(".adomainsomewhere");
|
||||
@ -88,10 +90,11 @@ public class CookieEncoderTest {
|
||||
@Test
|
||||
public void testEncodingMultipleCookies() {
|
||||
String c1 = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;commentURL=\"http://aurl.com\";port=\"80,8080\";discard;";
|
||||
String c2 = "myCookie2=myValue2;max-age=0;path=/anotherpathsomewhere;domain=.anotherdomainsomewhere;comment=this is another comment;version=1;commentURL=\"http://anotherurl.com\";";
|
||||
String c3 = "myCookie3=myValue3;max-age=0;version=1;";
|
||||
CookieEncoder encoder = new CookieEncoder(2);
|
||||
String c2 = "myCookie2=myValue2;path=/anotherpathsomewhere;domain=.anotherdomainsomewhere;comment=this is another comment;version=1;commentURL=\"http://anotherurl.com\";";
|
||||
String c3 = "myCookie3=myValue3;version=1;";
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentUrl("http://aurl.com");
|
||||
cookie.setDomain(".adomainsomewhere");
|
||||
@ -102,6 +105,7 @@ public class CookieEncoderTest {
|
||||
cookie.setSecure(true);
|
||||
encoder.addCookie(cookie);
|
||||
Cookie cookie2 = new DefaultCookie("myCookie2", "myValue2");
|
||||
cookie2.setVersion(1);
|
||||
cookie2.setComment("this is another comment");
|
||||
cookie2.setCommentUrl("http://anotherurl.com");
|
||||
cookie2.setDomain(".anotherdomainsomewhere");
|
||||
@ -110,6 +114,7 @@ public class CookieEncoderTest {
|
||||
cookie2.setSecure(false);
|
||||
encoder.addCookie(cookie2);
|
||||
Cookie cookie3 = new DefaultCookie("myCookie3", "myValue3");
|
||||
cookie3.setVersion(1);
|
||||
encoder.addCookie(cookie3);
|
||||
String encodedCookie = encoder.encode();
|
||||
assertEquals(c1 + c2 + c3, encodedCookie);
|
||||
|
Loading…
Reference in New Issue
Block a user