1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

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

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)

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);

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);

View File

@ -26,13 +26,14 @@ testRun(void)
}
// *****************************************************************************************************************************
if (testBegin("httpLastModifiedToTime()"))
if (testBegin("httpDateToTime() and httpDateFromTime()"))
{
TEST_ERROR(httpLastModifiedToTime(STRDEF("Wed, 21 Bog 2015 07:28:00 GMT")), FormatError, "invalid month 'Bog'");
TEST_ERROR(httpDateToTime(STRDEF("Wed, 21 Bog 2015 07:28:00 GMT")), FormatError, "invalid month 'Bog'");
TEST_ERROR(
httpLastModifiedToTime(STRDEF("Wed, 1 Oct 2015 07:28:00 GMT")), FormatError,
"unable to convert base 10 string ' 1' to int");
TEST_RESULT_INT(httpLastModifiedToTime(STRDEF("Wed, 21 Oct 2015 07:28:00 GMT")), 1445412480, "convert gmt datetime");
httpDateToTime(STRDEF("Wed, 1 Oct 2015 07:28:00 GMT")), FormatError, "unable to convert base 10 string ' 1' to int");
TEST_RESULT_INT(httpDateToTime(STRDEF("Wed, 21 Oct 2015 07:28:00 GMT")), 1445412480, "convert HTTP date to time_t");
TEST_RESULT_STR_Z(httpDateFromTime(1592743579), "Sun, 21 Jun 2020 12:46:19 GMT", "convert time_t to HTTP date")
}
// *****************************************************************************************************************************