Skip to content

Commit 6ce7832

Browse files
author
Stephane Landelle
committed
Drop TimeConverter, stick to RFC 2616, close AsyncHttpClient#778
1 parent 5bd538d commit 6ce7832

File tree

15 files changed

+227
-826
lines changed

15 files changed

+227
-826
lines changed

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import javax.net.ssl.HostnameVerifier;
3030
import javax.net.ssl.SSLContext;
3131

32-
import org.asynchttpclient.date.TimeConverter;
3332
import org.asynchttpclient.filter.IOExceptionFilter;
3433
import org.asynchttpclient.filter.RequestFilter;
3534
import org.asynchttpclient.filter.ResponseFilter;
@@ -103,7 +102,6 @@ public class AsyncHttpClientConfig {
103102
protected int maxRequestRetry;
104103
protected boolean disableUrlEncodingForBoundRequests;
105104
protected int ioThreadMultiplier;
106-
protected TimeConverter timeConverter;
107105
protected String[] enabledProtocols;
108106
protected String[] enabledCipherSuites;
109107
protected AsyncHttpProviderConfig<?, ?> providerConfig;
@@ -145,7 +143,6 @@ private AsyncHttpClientConfig(int connectTimeout,//
145143
int maxRequestRetry, //
146144
boolean disableUrlEncodingForBoundRequests, //
147145
int ioThreadMultiplier, //
148-
TimeConverter timeConverter,//
149146
String[] enabledProtocols,//
150147
String[] enabledCipherSuites,//
151148
AsyncHttpProviderConfig<?, ?> providerConfig,//
@@ -182,7 +179,6 @@ private AsyncHttpClientConfig(int connectTimeout,//
182179
this.maxRequestRetry = maxRequestRetry;
183180
this.disableUrlEncodingForBoundRequests = disableUrlEncodingForBoundRequests;
184181
this.ioThreadMultiplier = ioThreadMultiplier;
185-
this.timeConverter = timeConverter;
186182
this.enabledProtocols = enabledProtocols;
187183
this.enabledCipherSuites = enabledCipherSuites;
188184
this.providerConfig = providerConfig;
@@ -517,15 +513,6 @@ public int getConnectionTTL() {
517513
return connectionTTL;
518514
}
519515

520-
/**
521-
* @return the TimeConverter used for converting RFC2616Dates into time
522-
*
523-
* since 1.9.0
524-
*/
525-
public TimeConverter getTimeConverter() {
526-
return timeConverter;
527-
}
528-
529516
public boolean isAcceptAnyCertificate() {
530517
return acceptAnyCertificate;
531518
}
@@ -579,7 +566,6 @@ public static class Builder {
579566
private int maxRequestRetry = defaultMaxRequestRetry();
580567
private boolean disableUrlEncodingForBoundRequests = defaultDisableUrlEncodingForBoundRequests();
581568
private int ioThreadMultiplier = defaultIoThreadMultiplier();
582-
private TimeConverter timeConverter;
583569
private String[] enabledProtocols;
584570
private String[] enabledCipherSuites;
585571
private AsyncHttpProviderConfig<?, ?> providerConfig;
@@ -1041,11 +1027,6 @@ public Builder setSpdyMaxConcurrentStreams(int spdyMaxConcurrentStreams) {
10411027
return this;
10421028
}
10431029

1044-
public Builder setTimeConverter(TimeConverter timeConverter) {
1045-
this.timeConverter = timeConverter;
1046-
return this;
1047-
}
1048-
10491030
public Builder setAcceptAnyCertificate(boolean acceptAnyCertificate) {
10501031
this.acceptAnyCertificate = acceptAnyCertificate;
10511032
return this;
@@ -1100,7 +1081,6 @@ public Builder(AsyncHttpClientConfig prototype) {
11001081
removeQueryParamOnRedirect = prototype.isRemoveQueryParamOnRedirect();
11011082
hostnameVerifier = prototype.getHostnameVerifier();
11021083
strict302Handling = prototype.isStrict302Handling();
1103-
timeConverter = prototype.timeConverter;
11041084
acceptAnyCertificate = prototype.acceptAnyCertificate;
11051085
enabledProtocols = prototype.enabledProtocols;
11061086
enabledCipherSuites = prototype.enabledCipherSuites;
@@ -1160,7 +1140,6 @@ else if (hostnameVerifier == null)
11601140
maxRequestRetry, //
11611141
disableUrlEncodingForBoundRequests, //
11621142
ioThreadMultiplier, //
1163-
timeConverter,//
11641143
enabledProtocols, //
11651144
enabledCipherSuites, //
11661145
providerConfig, //

api/src/main/java/org/asynchttpclient/cookie/CookieDecoder.java

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

15-
import org.asynchttpclient.date.CalendarTimeConverter;
16-
import org.asynchttpclient.date.TimeConverter;
1715
import org.asynchttpclient.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.isEmpty())
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;;) {

api/src/main/java/org/asynchttpclient/cookie/KeyValuePairsParser.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
*/
1313
package org.asynchttpclient.cookie;
1414

15-
import org.asynchttpclient.date.RFC2616Date;
16-
import org.asynchttpclient.date.RFC2616DateParser;
17-
import org.asynchttpclient.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,26 +23,28 @@
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;
36+
public Cookie cookie() {
37+
return name != null ? new Cookie(name, value, rawValue, domain, path, computeExpires(), maxAge, secure, httpOnly) : null;
4338
}
4439

45-
public Cookie cookie() {
46-
return name != null ? new Cookie(name, value, rawValue, domain, path, expires, maxAge, secure, httpOnly) : null;
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;
4748
}
4849

4950
/**
@@ -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 org.asynchttpclient.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+
}

api/src/main/java/org/asynchttpclient/date/CalendarTimeConverter.java

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

0 commit comments

Comments
 (0)