Add httpDateFromTime().

Also rename httpLastModifiedToTime() to httpDateToTime() since the RFC-2822 date format used by HTTP is used in all Date headers.
This commit is contained in:
David Steele
2020-06-21 11:07:18 -04:00
parent fbff29957c
commit 911384d9b9
4 changed files with 33 additions and 14 deletions
+23 -6
View File
@@ -13,8 +13,11 @@ Http Common
Convert the time using the format specified in https://tools.ietf.org/html/rfc7231#section-7.1.1.1 which is used by HTTP 1.1 (the
only version we support).
***********************************************************************************************************************************/
static const char *httpCommonMonthList[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static const char *httpCommonDayList[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t
httpLastModifiedToTime(const String *lastModified)
httpDateToTime(const String *lastModified)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, lastModified);
@@ -25,18 +28,16 @@ httpLastModifiedToTime(const String *lastModified)
MEM_CONTEXT_TEMP_BEGIN()
{
// Find the month
static const char *monthList[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
const char *month = strPtr(strSubN(lastModified, 8, 3));
unsigned int monthIdx = 0;
for (; monthIdx < sizeof(monthList) / sizeof(char *); monthIdx++)
for (; monthIdx < sizeof(httpCommonMonthList) / sizeof(char *); monthIdx++)
{
if (strcmp(month, monthList[monthIdx]) == 0)
if (strcmp(month, httpCommonMonthList[monthIdx]) == 0)
break;
}
if (monthIdx == sizeof(monthList) / sizeof(char *))
if (monthIdx == sizeof(httpCommonMonthList) / sizeof(char *))
THROW_FMT(FormatError, "invalid month '%s'", month);
// Convert to time_t
@@ -50,6 +51,22 @@ httpLastModifiedToTime(const String *lastModified)
FUNCTION_TEST_RETURN(result);
}
String *
httpDateFromTime(time_t time)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(TIME, time);
FUNCTION_TEST_END();
struct tm *timePart = gmtime(&time);
FUNCTION_TEST_RETURN(
strNewFmt(
"%s, %02d %s %04d %02d:%02d:%02d GMT", httpCommonDayList[timePart->tm_wday], timePart->tm_mday,
httpCommonMonthList[timePart->tm_mon], timePart->tm_year + 1900, timePart->tm_hour, timePart->tm_min,
timePart->tm_sec));
}
/**********************************************************************************************************************************/
String *
httpUriEncode(const String *uri, bool path)
+3 -2
View File
@@ -13,8 +13,9 @@ Http common functions.
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
// Convert Last-Modified header to time_t
time_t httpLastModifiedToTime(const String *lastModified);
// Convert HTTP date to time_t and vice versa
time_t httpDateToTime(const String *lastModified);
String *httpDateFromTime(time_t time);
// Encode string to conform with URI specifications. If a path is being encoded then / characters won't be encoded.
String *httpUriEncode(const String *uri, bool path);
+1 -1
View File
@@ -549,7 +549,7 @@ storageS3Info(THIS_VOID, const String *file, StorageInfoLevel level, StorageInte
{
result.type = storageTypeFile;
result.size = cvtZToUInt64(strPtr(httpHeaderGet(httpResult.responseHeader, HTTP_HEADER_CONTENT_LENGTH_STR)));
result.timeModified = httpLastModifiedToTime(httpHeaderGet(httpResult.responseHeader, HTTP_HEADER_LAST_MODIFIED_STR));
result.timeModified = httpDateToTime(httpHeaderGet(httpResult.responseHeader, HTTP_HEADER_LAST_MODIFIED_STR));
}
FUNCTION_LOG_RETURN(STORAGE_INFO, result);