Skip to content

Commit 29d6cfc

Browse files
author
Stephane Landelle
committed
Drop TimeConverter, stick to RFC 2616, close AsyncHttpClient#778
1 parent 81dae5a commit 29d6cfc

File tree

11 files changed

+221
-809
lines changed

11 files changed

+221
-809
lines changed

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static com.ning.http.client.AsyncHttpClientConfigDefaults.*;
1919

20-
import com.ning.http.client.date.TimeConverter;
2120
import com.ning.http.client.filter.IOExceptionFilter;
2221
import com.ning.http.client.filter.RequestFilter;
2322
import com.ning.http.client.filter.ResponseFilter;
@@ -78,7 +77,6 @@ public class AsyncHttpClientConfig {
7877
protected int maxRequestRetry;
7978
protected boolean disableUrlEncodingForBoundRequests;
8079
protected int ioThreadMultiplier;
81-
protected TimeConverter timeConverter;
8280
protected String[] enabledProtocols;
8381
protected String[] enabledCipherSuites;
8482
protected AsyncHttpProviderConfig<?, ?> providerConfig;
@@ -115,7 +113,6 @@ private AsyncHttpClientConfig(int connectTimeout,//
115113
int maxRequestRetry, //
116114
boolean disableUrlEncodingForBoundedRequests, //
117115
int ioThreadMultiplier, //
118-
TimeConverter timeConverter,//
119116
String[] enabledProtocols,//
120117
String[] enabledCipherSuites,//
121118
AsyncHttpProviderConfig<?, ?> providerConfig) {
@@ -149,7 +146,6 @@ private AsyncHttpClientConfig(int connectTimeout,//
149146
this.maxRequestRetry = maxRequestRetry;
150147
this.disableUrlEncodingForBoundRequests = disableUrlEncodingForBoundedRequests;
151148
this.ioThreadMultiplier = ioThreadMultiplier;
152-
this.timeConverter = timeConverter;
153149
this.enabledProtocols = enabledProtocols;
154150
this.enabledCipherSuites = enabledCipherSuites;
155151
this.providerConfig = providerConfig;
@@ -441,13 +437,6 @@ public int getConnectionTTL() {
441437
return connectionTTL;
442438
}
443439

444-
/**
445-
* since 1.8.2
446-
*/
447-
public TimeConverter getTimeConverter() {
448-
return timeConverter;
449-
}
450-
451440
/**
452441
* since 1.9.0
453442
*/
@@ -506,7 +495,6 @@ public static class Builder {
506495
private int ioThreadMultiplier = defaultIoThreadMultiplier();
507496
private String[] enabledProtocols;
508497
private String[] enabledCipherSuites;
509-
private TimeConverter timeConverter;
510498
private AsyncHttpProviderConfig<?, ?> providerConfig;
511499

512500
public Builder() {
@@ -915,11 +903,6 @@ public Builder setConnectionTTL(int connectionTTL) {
915903
return this;
916904
}
917905

918-
public Builder setTimeConverter(TimeConverter timeConverter) {
919-
this.timeConverter = timeConverter;
920-
return this;
921-
}
922-
923906
public Builder setAcceptAnyCertificate(boolean acceptAnyCertificate) {
924907
this.acceptAnyCertificate = acceptAnyCertificate;
925908
return this;
@@ -974,7 +957,6 @@ public Builder(AsyncHttpClientConfig prototype) {
974957
removeQueryParamOnRedirect = prototype.isRemoveQueryParamOnRedirect();
975958
hostnameVerifier = prototype.getHostnameVerifier();
976959
strict302Handling = prototype.isStrict302Handling();
977-
timeConverter = prototype.timeConverter;
978960
enabledProtocols = prototype.enabledProtocols;
979961
enabledCipherSuites = prototype.enabledCipherSuites;
980962
acceptAnyCertificate = prototype.acceptAnyCertificate;
@@ -1039,7 +1021,6 @@ else if (hostnameVerifier == null)
10391021
maxRequestRetry, //
10401022
disableUrlEncodingForBoundedRequests, //
10411023
ioThreadMultiplier, //
1042-
timeConverter,//
10431024
enabledProtocols, //
10441025
enabledCipherSuites, //
10451026
providerConfig);

src/main/java/com/ning/http/client/cookie/CookieDecoder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,21 @@
1212
*/
1313
package com.ning.http.client.cookie;
1414

15-
import com.ning.http.client.date.CalendarTimeConverter;
16-
import com.ning.http.client.date.TimeConverter;
1715
import com.ning.http.util.StringUtils;
1816

1917
public class CookieDecoder {
2018

21-
public static final TimeConverter DEFAULT_TIME_CONVERTER = new CalendarTimeConverter();
22-
23-
public static Cookie decode(String header) {
24-
return decode(header, DEFAULT_TIME_CONVERTER);
25-
}
26-
2719
/**
2820
* Decodes the specified HTTP header value into {@link Cookie}.
2921
*
3022
* @return the decoded {@link Cookie}
3123
*/
32-
public static Cookie decode(String header, TimeConverter timeConverter) {
33-
34-
if (timeConverter == null)
35-
timeConverter = DEFAULT_TIME_CONVERTER;
24+
public static Cookie decode(String header) {
3625

3726
if (header.length() == 0)
3827
return null;
3928

40-
KeyValuePairsParser pairsParser = new KeyValuePairsParser(timeConverter);
29+
KeyValuePairsParser pairsParser = new KeyValuePairsParser();
4130

4231
final int headerLen = header.length();
4332
loop: for (int i = 0;;) {

src/main/java/com/ning/http/client/cookie/KeyValuePairsParser.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
*/
1313
package com.ning.http.client.cookie;
1414

15-
import com.ning.http.client.date.RFC2616Date;
16-
import com.ning.http.client.date.RFC2616DateParser;
17-
import com.ning.http.client.date.TimeConverter;
15+
import java.text.ParsePosition;
16+
import java.util.Date;
1817

1918
/**
2019
* A companion for CookieDecoder that parses key-value pairs (cookie name/value
@@ -24,28 +23,30 @@
2423
*/
2524
class KeyValuePairsParser {
2625

27-
private final TimeConverter timeBuilder;
2826
private String name;
2927
private String value;
3028
private String rawValue;
3129
private String domain;
3230
private String path;
33-
private long expires = -1L;
31+
private String expires;
3432
private int maxAge = -1;
3533
private boolean secure;
3634
private boolean httpOnly;
3735

38-
/**
39-
* @param timeBuilder used for parsing expires attribute
40-
*/
41-
public KeyValuePairsParser(TimeConverter timeBuilder) {
42-
this.timeBuilder = timeBuilder;
43-
}
44-
4536
public Cookie cookie() {
46-
return name != null ? new Cookie(name, value, rawValue, domain, path, expires, maxAge, secure, httpOnly) : null;
37+
return name != null ? new Cookie(name, value, rawValue, domain, path, computeExpires(), maxAge, secure, httpOnly) : null;
4738
}
4839

40+
private long computeExpires() {
41+
if (expires != null) {
42+
Date expiresDate = RFC2616DateParser.get().parse(expires, new ParsePosition(0));
43+
if (expiresDate != null)
44+
return expiresDate.getTime();
45+
}
46+
47+
return -1L;
48+
}
49+
4950
/**
5051
* Parse and store a key-value pair. First one is considered to be the
5152
* cookie name/value. Unknown attribute names are silently discarded.
@@ -156,15 +157,7 @@ private boolean isMaxAge(char c0, char c1, char c2, char c3, char c4, char c5, c
156157
}
157158

158159
private void setExpire(String value) {
159-
160-
RFC2616Date dateElements = new RFC2616DateParser(value).parse();
161-
if (dateElements != null) {
162-
try {
163-
expires = timeBuilder.toTime(dateElements);
164-
} catch (Exception e1) {
165-
// ignore failure to parse -> treat as session cookie
166-
}
167-
}
160+
expires = value;
168161
}
169162

170163
private void setMaxAge(String value) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.cookie;
14+
15+
import java.text.ParsePosition;
16+
import java.text.SimpleDateFormat;
17+
import java.util.Date;
18+
import java.util.Locale;
19+
import java.util.TimeZone;
20+
21+
/**
22+
* A parser for <a href="http://tools.ietf.org/html/rfc2616#section-3.3">RFC2616
23+
* Date format</a>.
24+
*
25+
* @author slandelle
26+
*/
27+
@SuppressWarnings("serial")
28+
public class RFC2616DateParser extends SimpleDateFormat {
29+
30+
private final SimpleDateFormat format1 = new RFC2616DateParserObsolete1();
31+
private final SimpleDateFormat format2 = new RFC2616DateParserObsolete2();
32+
33+
private static final ThreadLocal<RFC2616DateParser> DATE_FORMAT_HOLDER = new ThreadLocal<RFC2616DateParser>() {
34+
@Override
35+
protected RFC2616DateParser initialValue() {
36+
return new RFC2616DateParser();
37+
}
38+
};
39+
40+
public static RFC2616DateParser get() {
41+
return DATE_FORMAT_HOLDER.get();
42+
}
43+
44+
/**
45+
* Standard date format<p>
46+
* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
47+
*/
48+
private RFC2616DateParser() {
49+
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
50+
setTimeZone(TimeZone.getTimeZone("GMT"));
51+
}
52+
53+
@Override
54+
public Date parse(String text, ParsePosition pos) {
55+
Date date = super.parse(text, pos);
56+
if (date == null) {
57+
date = format1.parse(text, pos);
58+
}
59+
if (date == null) {
60+
date = format2.parse(text, pos);
61+
}
62+
return date;
63+
}
64+
65+
/**
66+
* First obsolete format<p>
67+
* Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
68+
*/
69+
private static final class RFC2616DateParserObsolete1 extends SimpleDateFormat {
70+
private static final long serialVersionUID = -3178072504225114298L;
71+
72+
RFC2616DateParserObsolete1() {
73+
super("E, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH);
74+
setTimeZone(TimeZone.getTimeZone("GMT"));
75+
}
76+
}
77+
78+
/**
79+
* Second obsolete format
80+
* <p>
81+
* Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
82+
*/
83+
private static final class RFC2616DateParserObsolete2 extends SimpleDateFormat {
84+
private static final long serialVersionUID = 3010674519968303714L;
85+
86+
RFC2616DateParserObsolete2() {
87+
super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
88+
setTimeZone(TimeZone.getTimeZone("GMT"));
89+
}
90+
}
91+
}

src/main/java/com/ning/http/client/date/CalendarTimeConverter.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)