Skip to content

Commit d8f9f64

Browse files
committed
Introduce HttpHeaderDateFormatter, close AsyncHttpClient#1308
1 parent 5b0e807 commit d8f9f64

File tree

7 files changed

+527
-210
lines changed

7 files changed

+527
-210
lines changed

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
import org.slf4j.LoggerFactory;
1919

2020
import java.nio.CharBuffer;
21+
import java.util.Date;
2122

2223
import static org.asynchttpclient.cookie.CookieUtil.*;
2324

2425
public class CookieDecoder {
2526

2627
private static final Logger LOGGER = LoggerFactory.getLogger(CookieDecoder.class);
27-
28+
2829
/**
2930
* Decodes the specified HTTP header value into {@link Cookie}.
3031
*
@@ -185,31 +186,23 @@ private long mergeMaxAgeAndExpires() {
185186
// max age has precedence over expires
186187
if (maxAge != Long.MIN_VALUE) {
187188
return maxAge;
188-
} else {
189-
String expires = computeValue(expiresStart, expiresEnd);
190-
if (expires != null) {
191-
long expiresMillis = computeExpires(expires);
192-
if (expiresMillis != Long.MIN_VALUE) {
193-
long maxAgeMillis = expiresMillis - System.currentTimeMillis();
194-
return maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0 ? 1 : 0);
195-
}
189+
} else if (isValueDefined(expiresStart, expiresEnd)) {
190+
Date expiresDate = HttpHeaderDateFormatter.parse(header, expiresStart, expiresEnd);
191+
if (expiresDate != null) {
192+
long maxAgeMillis = expiresDate.getTime() - System.currentTimeMillis();
193+
return maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0 ? 1 : 0);
196194
}
197195
}
198196
return Long.MIN_VALUE;
199197
}
200-
198+
201199
/**
202-
* Parse and store a key-value pair. First one is considered to be the
203-
* cookie name/value. Unknown attribute names are silently discarded.
200+
* Parse and store a key-value pair. First one is considered to be the cookie name/value. Unknown attribute names are silently discarded.
204201
*
205-
* @param keyStart
206-
* where the key starts in the header
207-
* @param keyEnd
208-
* where the key ends in the header
209-
* @param valueStart
210-
* where the value starts in the header
211-
* @param valueEnd
212-
* where the value ends in the header
202+
* @param keyStart where the key starts in the header
203+
* @param keyEnd where the key ends in the header
204+
* @param valueStart where the value starts in the header
205+
* @param valueEnd where the value ends in the header
213206
*/
214207
public void appendAttribute(int keyStart, int keyEnd, int valueStart, int valueEnd) {
215208
setCookieAttribute(keyStart, keyEnd, valueStart, valueEnd);
@@ -263,17 +256,21 @@ private void parse8(int nameStart, int valueStart, int valueEnd) {
263256
}
264257
}
265258

259+
private static boolean isValueDefined(int valueStart, int valueEnd) {
260+
return valueStart != -1 && valueStart != valueEnd;
261+
}
262+
266263
private String computeValue(int valueStart, int valueEnd) {
267-
if (valueStart == -1 || valueStart == valueEnd) {
268-
return null;
269-
} else {
264+
if (isValueDefined(valueStart, valueEnd)) {
270265
while (valueStart < valueEnd && header.charAt(valueStart) <= ' ') {
271266
valueStart++;
272267
}
273268
while (valueStart < valueEnd && (header.charAt(valueEnd - 1) <= ' ')) {
274269
valueEnd--;
275270
}
276271
return valueStart == valueEnd ? null : header.substring(valueStart, valueEnd);
272+
} else {
273+
return null;
277274
}
278275
}
279276
}

client/src/main/java/org/asynchttpclient/cookie/CookieUtil.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.asynchttpclient.util.Assertions.*;
1616

1717
import java.util.BitSet;
18-
import java.util.Date;
1918

2019
public class CookieUtil {
2120

@@ -132,16 +131,6 @@ static CharSequence unwrapValue(CharSequence cs) {
132131
return cs;
133132
}
134133

135-
static long computeExpires(String expires) {
136-
if (expires != null) {
137-
Date expiresDate = DateParser.parse(expires);
138-
if (expiresDate != null)
139-
return expiresDate.getTime();
140-
}
141-
142-
return Long.MIN_VALUE;
143-
}
144-
145134
private CookieUtil() {
146135
// Unused
147136
}

client/src/main/java/org/asynchttpclient/cookie/DateParser.java

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

0 commit comments

Comments
 (0)