|
18 | 18 | import org.slf4j.LoggerFactory;
|
19 | 19 |
|
20 | 20 | import java.nio.CharBuffer;
|
| 21 | +import java.util.Date; |
21 | 22 |
|
22 | 23 | import static org.asynchttpclient.cookie.CookieUtil.*;
|
23 | 24 |
|
24 | 25 | public class CookieDecoder {
|
25 | 26 |
|
26 | 27 | private static final Logger LOGGER = LoggerFactory.getLogger(CookieDecoder.class);
|
27 |
| - |
| 28 | + |
28 | 29 | /**
|
29 | 30 | * Decodes the specified HTTP header value into {@link Cookie}.
|
30 | 31 | *
|
@@ -185,31 +186,23 @@ private long mergeMaxAgeAndExpires() {
|
185 | 186 | // max age has precedence over expires
|
186 | 187 | if (maxAge != Long.MIN_VALUE) {
|
187 | 188 | 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); |
196 | 194 | }
|
197 | 195 | }
|
198 | 196 | return Long.MIN_VALUE;
|
199 | 197 | }
|
200 |
| - |
| 198 | + |
201 | 199 | /**
|
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. |
204 | 201 | *
|
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 |
213 | 206 | */
|
214 | 207 | public void appendAttribute(int keyStart, int keyEnd, int valueStart, int valueEnd) {
|
215 | 208 | setCookieAttribute(keyStart, keyEnd, valueStart, valueEnd);
|
@@ -263,17 +256,21 @@ private void parse8(int nameStart, int valueStart, int valueEnd) {
|
263 | 256 | }
|
264 | 257 | }
|
265 | 258 |
|
| 259 | + private static boolean isValueDefined(int valueStart, int valueEnd) { |
| 260 | + return valueStart != -1 && valueStart != valueEnd; |
| 261 | + } |
| 262 | + |
266 | 263 | private String computeValue(int valueStart, int valueEnd) {
|
267 |
| - if (valueStart == -1 || valueStart == valueEnd) { |
268 |
| - return null; |
269 |
| - } else { |
| 264 | + if (isValueDefined(valueStart, valueEnd)) { |
270 | 265 | while (valueStart < valueEnd && header.charAt(valueStart) <= ' ') {
|
271 | 266 | valueStart++;
|
272 | 267 | }
|
273 | 268 | while (valueStart < valueEnd && (header.charAt(valueEnd - 1) <= ' ')) {
|
274 | 269 | valueEnd--;
|
275 | 270 | }
|
276 | 271 | return valueStart == valueEnd ? null : header.substring(valueStart, valueEnd);
|
| 272 | + } else { |
| 273 | + return null; |
277 | 274 | }
|
278 | 275 | }
|
279 | 276 | }
|
|
0 commit comments