Skip to content

Fork Netty's Cookie Decoder, close #283 #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@
<exclude>**/NettyAsyncHttpProvider$*</exclude>
<exclude>**/NettyResponse</exclude>
<exclude>**/AsyncHttpProviderUtils</exclude>
<exclude>**/Cookie</exclude>
</excludes>
</configuration>
<executions>
Expand Down
203 changes: 169 additions & 34 deletions src/main/java/com/ning/http/client/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,93 @@
import java.util.Set;
import java.util.TreeSet;

public class Cookie {
public class Cookie implements Comparable<Cookie>{
private final String domain;
private final String name;
private final String value;
private final String path;
private final int maxAge;
private final boolean secure;
private final int version;
private final boolean httpOnly;
private final boolean discard;
private final String comment;
private final String commentUrl;

private Set<Integer> ports = Collections.emptySet();
private Set<Integer> unmodifiablePorts = ports;

@Deprecated
public Cookie(String domain, String name, String value, String path, int maxAge, boolean secure) {
this.domain = domain;
this.name = name;
this.value = value;
this.path = path;
this.maxAge = maxAge;
this.secure = secure;
this.version = 1;
this(domain, name, value, path, maxAge, secure, 1);
}

@Deprecated
public Cookie(String domain, String name, String value, String path, int maxAge, boolean secure, int version) {
this.domain = domain;
this(domain, name, value, path, maxAge, secure, version, false, false, null, null, Collections.<Integer> emptySet());
}

public Cookie(String domain, String name, String value, String path, int maxAge, boolean secure, int version, boolean httpOnly, boolean discard, String comment, String commentUrl, Iterable<Integer> ports) {

if (name == null) {
throw new NullPointerException("name");
}
name = name.trim();
if (name.length() == 0) {
throw new IllegalArgumentException("empty name");
}

for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c > 127) {
throw new IllegalArgumentException("name contains non-ascii character: " + name);
}

// Check prohibited characters.
switch (c) {
case '\t':
case '\n':
case 0x0b:
case '\f':
case '\r':
case ' ':
case ',':
case ';':
case '=':
throw new IllegalArgumentException("name contains one of the following prohibited characters: " + "=,; \\t\\r\\n\\v\\f: " + name);
}
}

if (name.charAt(0) == '$') {
throw new IllegalArgumentException("name starting with '$' not allowed: " + name);
}

if (value == null) {
throw new NullPointerException("value");
}

this.name = name;
this.value = value;
this.path = path;
this.domain = validateValue("domain", domain);
this.path = validateValue("path", path);
this.maxAge = maxAge;
this.secure = secure;
this.version = version;
this.httpOnly = httpOnly;

if (version > 0) {
this.comment = validateValue("comment", comment);
} else {
this.comment = null;
}
if (version > 1) {
this.discard = discard;
this.commentUrl = validateValue("commentUrl", commentUrl);
setPorts(ports);
} else {
this.discard = false;
this.commentUrl = null;
}
}

public String getDomain() {
Expand Down Expand Up @@ -79,35 +137,30 @@ public int getVersion() {
return version;
}

public String getComment() {
return this.comment;
}

public String getCommentUrl() {
return this.commentUrl;
}

public boolean isHttpOnly() {
return httpOnly;
}

public boolean isDiscard() {
return discard;
}

public Set<Integer> getPorts() {
if (unmodifiablePorts == null) {
unmodifiablePorts = Collections.unmodifiableSet(ports);
}
return unmodifiablePorts;
}

public void setPorts(int... ports) {
if (ports == null) {
throw new NullPointerException("ports");
}

int[] portsCopy = ports.clone();
if (portsCopy.length == 0) {
unmodifiablePorts = this.ports = Collections.emptySet();
} else {
Set<Integer> newPorts = new TreeSet<Integer>();
for (int p : portsCopy) {
if (p <= 0 || p > 65535) {
throw new IllegalArgumentException("port out of range: " + p);
}
newPorts.add(Integer.valueOf(p));
}
this.ports = newPorts;
unmodifiablePorts = null;
}
}

public void setPorts(Iterable<Integer> ports) {
private void setPorts(Iterable<Integer> ports) {
Set<Integer> newPorts = new TreeSet<Integer>();
for (int p : ports) {
if (p <= 0 || p > 65535) {
Expand All @@ -125,7 +178,89 @@ public void setPorts(Iterable<Integer> ports) {

@Override
public String toString() {
return String.format("Cookie: domain=%s, name=%s, value=%s, path=%s, maxAge=%d, secure=%s",
domain, name, value, path, maxAge, secure);
StringBuilder buf = new StringBuilder();
buf.append(getName());
buf.append('=');
buf.append(getValue());
if (getDomain() != null) {
buf.append("; domain=");
buf.append(getDomain());
}
if (getPath() != null) {
buf.append("; path=");
buf.append(getPath());
}
if (getComment() != null) {
buf.append("; comment=");
buf.append(getComment());
}
if (getMaxAge() >= 0) {
buf.append("; maxAge=");
buf.append(getMaxAge());
buf.append('s');
}
if (isSecure()) {
buf.append("; secure");
}
if (isHttpOnly()) {
buf.append("; HTTPOnly");
}
return buf.toString();
}

private String validateValue(String name, String value) {
if (value == null) {
return null;
}
value = value.trim();
if (value.length() == 0) {
return null;
}
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
switch (c) {
case '\r':
case '\n':
case '\f':
case 0x0b:
case ';':
throw new IllegalArgumentException(name + " contains one of the following prohibited characters: " + ";\\r\\n\\f\\v (" + value + ')');
}
}
return value;
}

public int compareTo(Cookie c) {
int v;
v = getName().compareToIgnoreCase(c.getName());
if (v != 0) {
return v;
}

if (getPath() == null) {
if (c.getPath() != null) {
return -1;
}
} else if (c.getPath() == null) {
return 1;
} else {
v = getPath().compareTo(c.getPath());
if (v != 0) {
return v;
}
}

if (getDomain() == null) {
if (c.getDomain() != null) {
return -1;
}
} else if (c.getDomain() == null) {
return 1;
} else {
v = getDomain().compareToIgnoreCase(c.getDomain());
return v;
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static com.ning.http.util.MiscUtil.isNonEmpty;

import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
import com.ning.http.client.Cookie;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.HttpResponseBodyPart;
Expand All @@ -31,7 +32,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;

import java.util.Set;

public class ApacheResponse implements Response {
private final static String DEFAULT_CHARSET = "ISO-8859-1";
Expand Down Expand Up @@ -161,8 +162,8 @@ public List<Cookie> getCookies() {
// TODO: ask for parsed header
List<String> v = header.getValue();
for (String value : v) {
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
localCookies.add(cookie);
Set<Cookie> cookies = CookieDecoder.decode(value);
localCookies.addAll(cookies);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

import static com.ning.http.util.MiscUtil.isNonEmpty;

import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpProvider;
import com.ning.http.client.AsyncHttpProviderConfig;
import com.ning.http.client.Body;
import com.ning.http.client.BodyGenerator;
import com.ning.http.client.ConnectionPoolKeyStrategy;
import com.ning.http.client.ConnectionsPool;
import com.ning.http.client.Cookie;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
Expand Down Expand Up @@ -1667,8 +1667,9 @@ private static Request newRequest(final URI uri,
builder.setQueryParameters(null);
}
for (String cookieStr : response.getHeaders().values(Header.Cookie)) {
Cookie c = AsyncHttpProviderUtils.parseCookie(cookieStr);
builder.addOrReplaceCookie(c);
for (Cookie c : CookieDecoder.decode(cookieStr)) {
builder.addOrReplaceCookie(c);
}
}
return builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static com.ning.http.util.MiscUtil.isNonEmpty;

import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
import com.ning.http.client.Cookie;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.HttpResponseBodyPart;
Expand All @@ -32,6 +33,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;


Expand Down Expand Up @@ -175,8 +177,8 @@ public List<Cookie> getCookies() {
// TODO: ask for parsed header
List<String> v = header.getValue();
for (String value : v) {
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
localCookies.add(cookie);
Set<Cookie> cookies = CookieDecoder.decode(value);
localCookies.addAll(cookies);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.ning.http.util.MiscUtil.isNonEmpty;

import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHandler.STATE;
import com.ning.http.client.AsyncHttpClientConfig;
Expand Down Expand Up @@ -585,7 +586,7 @@ public void operationComplete(ChannelFuture cf) {
int delay = Math.min(config.getIdleConnectionTimeoutInMs(), requestTimeout(config, future.getRequest().getPerRequestConfig()));
if (delay != -1 && !future.isDone() && !future.isCancelled()) {
ReaperFuture reaperFuture = new ReaperFuture(future);
Future scheduledFuture = config.reaper().scheduleAtFixedRate(reaperFuture, 0, delay, TimeUnit.MILLISECONDS);
Future<?> scheduledFuture = config.reaper().scheduleAtFixedRate(reaperFuture, 0, delay, TimeUnit.MILLISECONDS);
reaperFuture.setScheduledFuture(scheduledFuture);
future.setReaperFuture(reaperFuture);
}
Expand Down Expand Up @@ -2083,13 +2084,15 @@ private boolean redirect(Request request,

log.debug("Redirecting to {}", newUrl);
for (String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE)) {
Cookie c = AsyncHttpProviderUtils.parseCookie(cookieStr);
nBuilder.addOrReplaceCookie(c);
for (Cookie c : CookieDecoder.decode(cookieStr)) {
nBuilder.addOrReplaceCookie(c);
}
}

for (String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE2)) {
Cookie c = AsyncHttpProviderUtils.parseCookie(cookieStr);
nBuilder.addOrReplaceCookie(c);
for (Cookie c : CookieDecoder.decode(cookieStr)) {
nBuilder.addOrReplaceCookie(c);
}
}

AsyncCallable ac = new AsyncCallable(future) {
Expand Down
Loading