-
-
Notifications
You must be signed in to change notification settings - Fork 14
fix: do spec-compliant date parsing #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| /** | ||
| * Valid months that are allowed to be in an IMF date | ||
| */ | ||
| const IMF_DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']; | ||
|
|
||
| /** | ||
| * Specific locations that are expected to be spaces in an IMF date | ||
| */ | ||
| const IMF_SPACES = [4, 7, 11, 16, 25]; | ||
|
|
||
| /** | ||
| * Valid months that are allowed to be in an IMF date | ||
| */ | ||
| const IMF_MONTHS = [ | ||
| 'jan', | ||
| 'feb', | ||
| 'mar', | ||
| 'apr', | ||
| 'may', | ||
| 'jun', | ||
| 'jul', | ||
| 'aug', | ||
| 'sep', | ||
| 'oct', | ||
| 'nov', | ||
| 'dec', | ||
| ]; | ||
|
|
||
| /** | ||
| * Specific locations that are expected to be colons in an IMF date | ||
| */ | ||
| const IMF_COLONS = [19, 22]; | ||
|
|
||
| /** | ||
| * Specific locations that are expected to be spaces in an asctime() date | ||
| */ | ||
| const ASCTIME_SPACES = [3, 7, 10, 19]; | ||
|
|
||
| /** | ||
| * Valid days allowed in an RF850 date | ||
| */ | ||
| const RFC850_DAYS = [ | ||
| 'monday', | ||
| 'tuesday', | ||
| 'wednesday', | ||
| 'thursday', | ||
| 'friday', | ||
| 'saturday', | ||
| 'sunday', | ||
| ]; | ||
|
|
||
| /** | ||
| * @see https://www.rfc-editor.org/rfc/rfc9110.html#name-date-time-formats | ||
| */ | ||
| export function parseHttpDate( | ||
| date: string | null, | ||
| now = new Date() | ||
| ): Date | undefined { | ||
| // Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate | ||
| // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format | ||
| // Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format | ||
|
|
||
| if (date === null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| date = date.toLowerCase(); | ||
|
|
||
| switch (date[3]) { | ||
| case ',': | ||
| return parseImfDate(date); | ||
| case ' ': | ||
| return parseAscTimeDate(date); | ||
| default: | ||
| return parseRfc850Date(date, now); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @see https://httpwg.org/specs/rfc9110.html#preferred.date.format | ||
| */ | ||
| function parseImfDate(date: string): Date | undefined { | ||
| if (date.length !== 29) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!date.endsWith('gmt')) { | ||
| // Unsupported timezone | ||
| return undefined; | ||
| } | ||
|
|
||
| // Ensure there are spaces in the expected locations | ||
| for (const spaceInx of IMF_SPACES) { | ||
| if (date[spaceInx] !== ' ') { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| // Ensure there are colons in the expected locations | ||
| for (const colonIdx of IMF_COLONS) { | ||
| if (date[colonIdx] !== ':') { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| const dayName = date.substring(0, 3); | ||
| if (!IMF_DAYS.includes(dayName)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const dayString = date.substring(5, 7); | ||
| const day = Number.parseInt(dayString); | ||
| if (isNaN(day) || (day < 10 && dayString[0] !== '0')) { | ||
| // Not a number, 0, or it's less than 10 and didn't start with a 0 | ||
| return undefined; | ||
| } | ||
|
|
||
| const month = date.substring(8, 11); | ||
| const monthIdx = IMF_MONTHS.indexOf(month); | ||
| if (monthIdx === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const year = Number.parseInt(date.substring(12, 16)); | ||
| if (isNaN(year)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const hourString = date.substring(17, 19); | ||
| const hour = Number.parseInt(hourString); | ||
| if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const minuteString = date.substring(20, 22); | ||
| const minute = Number.parseInt(minuteString); | ||
| if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const secondString = date.substring(23, 25); | ||
| const second = Number.parseInt(secondString); | ||
| if (isNaN(second) || (second < 10 && secondString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return new Date(Date.UTC(year, monthIdx, day, hour, minute, second)); | ||
| } | ||
|
|
||
| /** | ||
| * @see https://httpwg.org/specs/rfc9110.html#obsolete.date.formats | ||
| */ | ||
| function parseAscTimeDate(date: string): Date | undefined { | ||
| // This is assumed to be in UTC | ||
|
|
||
| if (date.length !== 24) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Ensure there are spaces in the expected locations | ||
| for (const spaceIdx of ASCTIME_SPACES) { | ||
| if (date[spaceIdx] !== ' ') { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| const dayName = date.substring(0, 3); | ||
| if (!IMF_DAYS.includes(dayName)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const month = date.substring(4, 7); | ||
| const monthIdx = IMF_MONTHS.indexOf(month); | ||
| if (monthIdx === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const dayString = date.substring(8, 10); | ||
| const day = Number.parseInt(dayString); | ||
| if (isNaN(day) || (day < 10 && dayString[0] !== ' ')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const hourString = date.substring(11, 13); | ||
| const hour = Number.parseInt(hourString); | ||
| if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const minuteString = date.substring(14, 16); | ||
| const minute = Number.parseInt(minuteString); | ||
| if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const secondString = date.substring(17, 19); | ||
| const second = Number.parseInt(secondString); | ||
| if (isNaN(second) || (second < 10 && secondString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const year = Number.parseInt(date.substring(20, 24)); | ||
| if (isNaN(year)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return new Date(Date.UTC(year, monthIdx, day, hour, minute, second)); | ||
| } | ||
|
|
||
| /** | ||
| * @see https://httpwg.org/specs/rfc9110.html#obsolete.date.formats | ||
| */ | ||
| function parseRfc850Date(date: string, now = new Date()): Date | undefined { | ||
| if (!date.endsWith('gmt')) { | ||
| // Unsupported timezone | ||
| return undefined; | ||
| } | ||
|
|
||
| const commaIndex = date.indexOf(','); | ||
| if (commaIndex === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (date.length - commaIndex - 1 !== 23) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const dayName = date.substring(0, commaIndex); | ||
| if (!RFC850_DAYS.includes(dayName)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Ensure there are spaces, dashes, and colons in the expected locations | ||
| if ( | ||
| date[commaIndex + 1] !== ' ' || | ||
| date[commaIndex + 4] !== '-' || | ||
| date[commaIndex + 8] !== '-' || | ||
| date[commaIndex + 11] !== ' ' || | ||
| date[commaIndex + 14] !== ':' || | ||
| date[commaIndex + 17] !== ':' || | ||
| date[commaIndex + 20] !== ' ' | ||
| ) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const dayString = date.substring(commaIndex + 2, commaIndex + 4); | ||
| const day = Number.parseInt(dayString); | ||
| if (isNaN(day) || (day < 10 && dayString[0] !== '0')) { | ||
| // Not a number, or it's less than 10 and didn't start with a 0 | ||
| return undefined; | ||
| } | ||
|
|
||
| const month = date.substring(commaIndex + 5, commaIndex + 8); | ||
| const monthIdx = IMF_MONTHS.indexOf(month); | ||
| if (monthIdx === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // As of this point year is just the decade (i.e. 94) | ||
| let year = Number.parseInt(date.substring(commaIndex + 9, commaIndex + 11)); | ||
| if (isNaN(year)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const currentYear = now.getUTCFullYear(); | ||
| const currentDecade = currentYear % 100; | ||
| const currentCentury = Math.floor(currentYear / 100); | ||
|
|
||
| if (year > currentDecade && year - currentDecade >= 50) { | ||
| // Over 50 years in future, go to previous century | ||
| year += (currentCentury - 1) * 100; | ||
| } else { | ||
| year += currentCentury * 100; | ||
| } | ||
|
|
||
| const hourString = date.substring(commaIndex + 12, commaIndex + 14); | ||
| const hour = Number.parseInt(hourString); | ||
| if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const minuteString = date.substring(commaIndex + 15, commaIndex + 17); | ||
| const minute = Number.parseInt(minuteString); | ||
| if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const secondString = date.substring(commaIndex + 18, commaIndex + 20); | ||
| const second = Number.parseInt(secondString); | ||
| if (isNaN(second) || (second < 10 && secondString[0] !== '0')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return new Date(Date.UTC(year, monthIdx, day, hour, minute, second)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is grabbed straight from undici, do we really need to have a copy of this in our code? Is there a 3rd party library we can use? I really want to avoid us from maintaining such kind of code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nodejs/undici I hope you don't mind the ping, but is this date parsing logic something y'all would consider exposing?
It looks like a couple of utils are currently exposed: https://github.com/nodejs/undici/blob/674ae24d6e61a3ef62b4b3e8677fcae52c0fa068/index.js#L60-L63
So, would it be viable to add https://github.com/nodejs/undici/blob/674ae24d6e61a3ef62b4b3e8677fcae52c0fa068/lib/util/date.js to that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? We have parsing logic already for other things such as the
rangeheader