Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/http_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 4.1.3-wip

* **BREAKING:** Apply the RFC 9110 50-year rule when parsing RFC 850 dates.
Previously, two-digit years were always interpreted as years between 1900
and 1999.
* Replace reference to `dart:web` with `package:web` in README.md.

## 4.1.2
Expand Down
11 changes: 9 additions & 2 deletions pkgs/http_parser/lib/src/http_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ String formatHttpDate(DateTime date) {

/// Parses an HTTP-formatted date into a UTC [DateTime].
///
/// This follows [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3).
/// This follows [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.7).
/// It will throw a [FormatException] if [date] is invalid.
DateTime parseHttpDate(String date) =>
wrapFormatException('HTTP date', date, () {
Expand All @@ -68,7 +68,14 @@ DateTime parseHttpDate(String date) =>
scanner.expect('-');
final month = _parseMonth(scanner);
scanner.expect('-');
final year = 1900 + _parseInt(scanner, 2);
// RFC 9110, Section 5.6.7 says:
// Recipients of a timestamp value in rfc850-date format, which uses a
// two-digit year, MUST interpret a timestamp that appears to be more
// than 50 years in the future as representing the most recent year
// in the past that had the same last two digits.
final currentYear = DateTime.now().toUtc().year;
Comment thread
medz marked this conversation as resolved.
var year = currentYear - currentYear % 100 + _parseInt(scanner, 2);
if (year > currentYear + 50) year -= 100;
scanner.expect(' ');
final time = _parseTime(scanner);
scanner.expect(' GMT');
Expand Down
7 changes: 7 additions & 0 deletions pkgs/http_parser/test/http_date_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ void main() {
expect(date.timeZoneName, equals('UTC'));
});

test('applies the two-digit year rule', () {
expect(
parseHttpDate('Sunday, 21-Jun-26 07:28:00 GMT'),
DateTime.utc(2026, DateTime.june, 21, 7, 28),
);
});

test('whitespace is required', () {
expect(() => parseHttpDate('Sunday,06-Nov-94 08:49:37 GMT'),
throwsFormatException);
Expand Down
Loading