More fixes to the cookie parser.
- Fix parsing of strings shorter than 24 bytes (which can only happen if the year has only 3 digits, or the day in month, hour, minute or seconds have only 1). - Only allow the GMT and UTC timezone specifiers, as all HTTP dates should use the GMT zone (but still use a format that allows specifying a timezone name). All cookie tests are now passing.
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010 Haiku Inc. All rights reserved.
|
* Copyright 2010-2016 Haiku Inc. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Christophe Huriaux, [email protected]
|
* Christophe Huriaux, [email protected]
|
||||||
|
* Adrien Destugues, [email protected]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <HttpTime.h>
|
#include <HttpTime.h>
|
||||||
@@ -16,6 +17,8 @@
|
|||||||
static const char* kDateFormats[] = {
|
static const char* kDateFormats[] = {
|
||||||
"%a, %d %b %Y %H:%M:%S",
|
"%a, %d %b %Y %H:%M:%S",
|
||||||
"%a, %d-%b-%Y %H:%M:%S",
|
"%a, %d-%b-%Y %H:%M:%S",
|
||||||
|
"%a, %d-%b-%Y %H:%M:%S GMT",
|
||||||
|
"%a, %d-%b-%Y %H:%M:%S UTC",
|
||||||
"%A, %d-%b-%y %H:%M:%S",
|
"%A, %d-%b-%y %H:%M:%S",
|
||||||
"%a %d %b %H:%M:%S %Y"
|
"%a %d %b %H:%M:%S %Y"
|
||||||
};
|
};
|
||||||
@@ -76,16 +79,8 @@ BHttpTime::Parse()
|
|||||||
if (fDateString.Length() < 4)
|
if (fDateString.Length() < 4)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
expireTime.tm_sec = 0;
|
memset(&expireTime, 0, sizeof(struct tm));
|
||||||
expireTime.tm_min = 0;
|
|
||||||
expireTime.tm_hour = 0;
|
|
||||||
expireTime.tm_mday = 0;
|
|
||||||
expireTime.tm_mon = 0;
|
|
||||||
expireTime.tm_year = 0;
|
|
||||||
expireTime.tm_wday = 0;
|
|
||||||
expireTime.tm_yday = 0;
|
|
||||||
expireTime.tm_isdst = 0;
|
|
||||||
|
|
||||||
fDateFormat = B_HTTP_TIME_FORMAT_PARSED;
|
fDateFormat = B_HTTP_TIME_FORMAT_PARSED;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
for (i = 0; i < sizeof(kDateFormats) / sizeof(const char*);
|
for (i = 0; i < sizeof(kDateFormats) / sizeof(const char*);
|
||||||
@@ -93,17 +88,20 @@ BHttpTime::Parse()
|
|||||||
const char* result = strptime(fDateString.String(), kDateFormats[i],
|
const char* result = strptime(fDateString.String(), kDateFormats[i],
|
||||||
&expireTime);
|
&expireTime);
|
||||||
|
|
||||||
// Make sure we parsed enough of the date string, not just a small
|
// We need to parse the complete value for the "Expires" key.
|
||||||
// part of it.
|
// Otherwise, we consider this to be a session cookie (or try another
|
||||||
if (result != NULL && result > fDateString.String() + 24) {
|
// one of the date formats).
|
||||||
|
if (result == fDateString.String() + fDateString.Length()) {
|
||||||
fDateFormat = i;
|
fDateFormat = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Did we identify some valid format?
|
||||||
if (fDateFormat == B_HTTP_TIME_FORMAT_PARSED)
|
if (fDateFormat == B_HTTP_TIME_FORMAT_PARSED)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
// Now convert the struct tm from strptime into a BDateTime.
|
||||||
BTime time(expireTime.tm_hour, expireTime.tm_min, expireTime.tm_sec);
|
BTime time(expireTime.tm_hour, expireTime.tm_min, expireTime.tm_sec);
|
||||||
BDate date(expireTime.tm_year + 1900, expireTime.tm_mon + 1,
|
BDate date(expireTime.tm_year + 1900, expireTime.tm_mon + 1,
|
||||||
expireTime.tm_mday);
|
expireTime.tm_mday);
|
||||||
|
|||||||
Reference in New Issue
Block a user