shinseongsu opened a new pull request, #952:
URL: https://github.com/apache/httpcomponents-client/pull/952

   I found this while comparing date parsing across HTTP libraries for a Netty 
PR (netty/netty#18429). Two of the three HTTP-date formats don't parse 
correctly in `DateUtils`.
   
   **asctime never parses.** `FORMATTER_ASCTIME` has no zone, so `Instant.from` 
always fails and `parseStandardDate("Thu Feb 22 17:20:18 2024")` returns null. 
This is the input from HTTPCLIENT-2319. That issue only asked about the 
exception, and the fix made it return null, so the value still can't be parsed. 
The formatter also rejects the space-padded day that asctime uses (`Nov  6`). 
RFC 9110 section 5.6.7 says a recipient MUST accept all three formats, and that 
asctime values are in UTC.
   
   **RFC 850 years 70-99 fail.** `yy` reads the year as 2000-2099, so 
`Thursday, 01-Jan-70 00:00:00 GMT` becomes 2070-01-01. That day is a Wednesday, 
so the weekday check fails and the value is rejected. A date in 1970 is a 
common way to delete a cookie. With a two-digit year it is rejected, and the 
default classic client (strict cookie spec, 
`BasicExpiresHandler(DateUtils.STANDARD_PATTERNS)`) keeps the cookie. With this 
change, 70-99 is read as 1970-1999 and 00-69 as 2000-2069. This is the RFC 6265 
section 5.1.1 rule, and `LaxExpiresHandler` already uses it.
   
   End-to-end, with the default classic client against a raw socket server 
(master `28844736b`):
   
   | Server sends | master | this PR |
   |---|---|---|
   | `Set-Cookie: session=deleted; Expires=Thursday, 01-Jan-70 00:00:00 GMT` | 
cookie kept, WARN `Invalid 'expires' attribute` | deleted |
   | `Set-Cookie: session=deleted; Expires=Friday, 31-Dec-99 23:59:59 GMT` | 
cookie kept | deleted |
   | `Set-Cookie: id=1; Expires=Sat Nov  6 08:49:37 2094` | cookie dropped | 
stored, 2094-11-06T08:49:37Z |
   | `503` + `Retry-After: <asctime, now+4s>` | retried after 1008 ms (date 
ignored) | retried after 3988 ms |
   
   The same parser is used for `Date`/`Expires` in httpclient5-cache. There, an 
asctime value was treated as missing.
   
   Changes:
   - `FORMATTER_ASCTIME` parses in UTC and accepts a space-padded day.
     - Formatting now writes `Nov  6` instead of `Nov 6`, which is the asctime 
layout.
     - Formatting a non-UTC `ZonedDateTime` directly with this formatter now 
prints the UTC time instead of the local time, and formatting an `Instant` 
works instead of throwing. `DateUtils.formatDate` already converted to GMT, so 
its output is unchanged apart from the padding.
   - `FORMATTER_RFC1036` uses base year 1970 for two-digit years.
   - I left `PATTERN_ASCTIME`, `PATTERN_RFC1036` and the deprecated 
`parseDate(String, String[])` alone. A pattern string can't express a base 
year, so that path keeps the old behavior.
   
   One thing to check: RFC 9110 uses a sliding window for two-digit years (more 
than 50 years in the future means the past). Today that window would read `70` 
as 2070. I used the fixed RFC 6265 rule because the same formatters parse 
cookie `Expires`, and it matches `LaxExpiresHandler`. I can change it to the 
sliding window if you prefer.
   
   A question, not in this PR: the formatters use `ResolverStyle.SMART`, so a 
day that doesn't exist is moved to the last day of the month. `Sun, 31 Feb 2094 
08:49:37 GMT` is stored as 2094-02-28, and `Fri, 31 Apr 2094` as 2094-04-30. 
With a different weekday (`Thu, 31 Apr 2094`), the whole cookie is dropped 
instead. RFC 6265 section 5.1.1 says to fail when the date doesn't exist. This 
is the same issue as the Netty PR. Should `DateUtils` reject these? Rejecting 
them would change behavior, so I left it out of this PR.
   
   Tested: the new `TestDateUtils` tests fail on master and pass with this 
change. `mvn -pl httpclient5,httpclient5-cache verify` passes (1162 and 745 
tests).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to