Skip to content

Commit 20a9c3f

Browse files
committed
Reformat, improve cookie parsing robusness
1 parent 0ae1a68 commit 20a9c3f

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

src/main/java/com/ning/http/client/RequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ public RequestBuilder setFollowRedirects(boolean followRedirects) {
163163

164164
@Override
165165
public RequestBuilder addOrReplaceCookie(Cookie c) {
166-
return super.addOrReplaceCookie(c);
166+
return super.addOrReplaceCookie(c);
167167
}
168168
}

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@
2323
import java.io.File;
2424
import java.io.InputStream;
2525
import java.io.UnsupportedEncodingException;
26-
import java.net.MalformedURLException;
2726
import java.net.URI;
2827
import java.net.URLDecoder;
29-
import java.util.ArrayList;
30-
import java.util.Collection;
31-
import java.util.Collections;
32-
import java.util.Iterator;
33-
import java.util.List;
34-
import java.util.Map;
28+
import java.util.*;
3529
import java.util.Map.Entry;
3630

3731
/**
@@ -563,23 +557,24 @@ private boolean allowBody(String method) {
563557
return true;
564558
}
565559
}
566-
567-
public T addOrReplaceCookie(Cookie cookie) {
568-
String cookieKey=cookie.getName();
569-
boolean replace=false;
570-
int index=0;
571-
for(Cookie c : request.cookies) {
572-
if(c.getName().equals(cookieKey)){
573-
replace=true;
574-
break;
575-
};
576-
index++;
577-
}
578-
if(replace) {
579-
((ArrayList<Cookie>)request.cookies).set(index, cookie);
580-
} else {
560+
561+
public T addOrReplaceCookie(Cookie cookie) {
562+
String cookieKey = cookie.getName();
563+
boolean replace = false;
564+
int index = 0;
565+
for (Cookie c : request.cookies) {
566+
if (c.getName().equals(cookieKey)) {
567+
replace = true;
568+
break;
569+
}
570+
571+
index++;
572+
}
573+
if (replace) {
574+
((ArrayList<Cookie>) request.cookies).set(index, cookie);
575+
} else {
581576
request.cookies.add(cookie);
582-
}
577+
}
583578
return derived.cast(this);
584579
}
585580
}

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,11 +1162,12 @@ public Object call() throws Exception {
11621162

11631163
log.debug("Redirecting to {}", newUrl);
11641164
for(String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE)){
1165-
Cookie c=AsyncHttpProviderUtils.parseCookie(cookieStr);
1165+
Cookie c = AsyncHttpProviderUtils.parseCookie(cookieStr);
11661166
nBuilder.addOrReplaceCookie(c);
11671167
}
1168+
11681169
for(String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE2)){
1169-
Cookie c=AsyncHttpProviderUtils.parseCookie(cookieStr);
1170+
Cookie c = AsyncHttpProviderUtils.parseCookie(cookieStr);
11701171
nBuilder.addOrReplaceCookie(c);
11711172
}
11721173

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public static Cookie parseCookie(String value) {
415415
try {
416416
maxAge = convertExpireField(f[1]);
417417
}
418-
catch (ParseException e) {
418+
catch (Exception e) {
419419
// original behavior, is this correct at all (expires field with max-age semantics)?
420420
try {
421421
maxAge = Integer.valueOf(f[1]);
@@ -440,20 +440,33 @@ public static Cookie parseCookie(String value) {
440440
return new Cookie(domain, cookieName, cookieValue, path, maxAge, secure);
441441
}
442442

443-
private static int convertExpireField(String timestring) throws ParseException {
444-
ParseException exception = null;
443+
private static int convertExpireField(String timestring) throws Exception {
444+
Exception exception = null;
445445
for (SimpleDateFormat sdf : RFC2822_LIKE_DATE_FORMATS) {
446446
try {
447-
long expire = sdf.parse(timestring).getTime();
447+
long expire = sdf.parse(removeQuote(timestring.trim())).getTime();
448448
return (int) (expire - System.currentTimeMillis()) / 1000;
449449
} catch (ParseException e) {
450450
exception = e;
451+
} catch (NumberFormatException e) {
452+
exception = e;
451453
}
452454
}
453455

454456
throw exception;
455457
}
456458

459+
private final static String removeQuote(String s) {
460+
if (s.startsWith("\"")) {
461+
s = s.substring(1);
462+
}
463+
464+
if (s.endsWith("\"")) {
465+
s = s.substring(0,s.length() -1);
466+
}
467+
return s;
468+
}
469+
457470
public static void checkBodyParts(int statusCode, Collection<HttpResponseBodyPart> bodyParts) {
458471
if (bodyParts == null || bodyParts.size() == 0) {
459472

src/test/java/com/ning/http/client/async/netty/NettyFollowingThreadTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public class NettyFollowingThreadTest extends FollowingThreadTest {
2222
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
2323
return ProviderUtil.nettyProvider(config);
2424
}
25+
2526
}
27+

0 commit comments

Comments
 (0)