diff --git a/lib/cookie.js b/lib/cookie.js index 19953927..027d9fb8 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -87,7 +87,6 @@ function parseDate(str,strict) { if (!str) { return; } - var found_time, found_dom, found_month, found_year; /* RFC6265 S5.1.1: * 2. Process each date-token sequentially in the order the date-tokens @@ -98,6 +97,13 @@ function parseDate(str,strict) { return; } + var hour = null; + var minutes = null; + var seconds = null; + var day = null; + var month = null; + var year = null; + var date = new Date(); date.setMilliseconds(0); @@ -115,12 +121,12 @@ function parseDate(str,strict) { * the date-token, respectively. Skip the remaining sub-steps and continue * to the next date-token. */ - if (!found_time) { + if (seconds === null) { result = TIME.exec(token); if (result) { - var hour = parseInt(result[1], 10); - var minutes = parseInt(result[2], 10); - var seconds = parseInt(result[3], 10); + hour = parseInt(result[1], 10); + minutes = parseInt(result[2], 10); + seconds = parseInt(result[3], 10); /* RFC6265 S5.1.1.5: * [fail if] * * the hour-value is greater than 23, @@ -131,10 +137,6 @@ function parseDate(str,strict) { return; } - found_time = true; - date.setUTCHours(result[1]); - date.setUTCMinutes(result[2]); - date.setUTCSeconds(result[3]); continue; } } @@ -144,18 +146,16 @@ function parseDate(str,strict) { * the day-of-month-value to the number denoted by the date-token. Skip * the remaining sub-steps and continue to the next date-token. */ - if (!found_dom) { + if (day === null) { result = DAY_OF_MONTH.exec(token); if (result) { - var dom = parseInt(result, 10); + day = parseInt(result, 10); /* RFC6265 S5.1.1.5: * [fail if] the day-of-month-value is less than 1 or greater than 31 */ - if(dom < 1 || dom > 31) { + if(day < 1 || day > 31) { return; } - found_dom = true; - date.setUTCDate(result[1]); continue; } } @@ -165,11 +165,10 @@ function parseDate(str,strict) { * the month denoted by the date-token. Skip the remaining sub-steps and * continue to the next date-token. */ - if (!found_month) { + if (month === null) { result = MONTH.exec(token); if (result) { - found_month = true; - date.setUTCMonth(MONTH_TO_NUM[result[1].toLowerCase()]); + month = MONTH_TO_NUM[result[1].toLowerCase()]; continue; } } @@ -179,10 +178,10 @@ function parseDate(str,strict) { * denoted by the date-token. Skip the remaining sub-steps and continue to * the next date-token. */ - if (!found_year) { + if (year === null) { result = YEAR.exec(token); if (result) { - var year = parseInt(result[0], 10); + year = parseInt(result[0], 10); /* From S5.1.1: * 3. If the year-value is greater than or equal to 70 and less * than or equal to 99, increment the year-value by 1900. @@ -198,20 +197,16 @@ function parseDate(str,strict) { if (year < 1601) { return; // 5. ... the year-value is less than 1601 } - - found_year = true; - date.setUTCFullYear(year); - continue; } } } - if (!(found_time && found_dom && found_month && found_year)) { + if (seconds === null || day === null || month === null || year === null) { return; // 5. ... at least one of the found-day-of-month, found-month, found- // year, or found-time flags is not set, } - return date; + return new Date(Date.UTC(year, month, day, hour, minutes, seconds)); } function formatDate(date) {