MXS-1220: Make the http.hh header public
As it contains utility functions for formatting time_t values to HTTP-date values, there's no real need to make it an internal header.
This commit is contained in:
89
include/maxscale/http.hh
Normal file
89
include/maxscale/http.hh
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2019-07-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#include <maxscale/cppdefs.hh>
|
||||
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
|
||||
#include <maxscale/debug.h>
|
||||
|
||||
/**
|
||||
* @brief Return the current HTTP-date
|
||||
*
|
||||
* @return The RFC 1123 compliant date
|
||||
*/
|
||||
static inline std::string http_get_date()
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
char buf[200]; // Enough to store all dates
|
||||
|
||||
gmtime_r(&now, &tm);
|
||||
strftime(buf, sizeof(buf), "%a, %d %b %y %T GMT", &tm);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a time_t value into a HTTP-date string
|
||||
*
|
||||
* @param t Time to convert
|
||||
*
|
||||
* @return The time converted to a HTTP-date string
|
||||
*/
|
||||
static inline std::string http_to_date(time_t t)
|
||||
{
|
||||
struct tm tm;
|
||||
char buf[200]; // Enough to store all dates
|
||||
|
||||
gmtime_r(&t, &tm);
|
||||
strftime(buf, sizeof(buf), "%a, %d %b %Y %T GMT", &tm);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a HTTP-date string into time_t
|
||||
*
|
||||
* @param str HTTP-date formatted string to convert
|
||||
*
|
||||
* @return The time converted to time_t
|
||||
*/
|
||||
static inline time_t http_from_date(const std::string& str)
|
||||
{
|
||||
struct tm tm = {};
|
||||
|
||||
/** First get the GMT time in time_t format */
|
||||
strptime(str.c_str(), "%a, %d %b %Y %T GMT", &tm);
|
||||
time_t t = mktime(&tm);
|
||||
|
||||
/** Then convert it to local time by calculating the difference between
|
||||
* the local time and the GMT time */
|
||||
struct tm local_tm = {};
|
||||
struct tm gmt_tm = {};
|
||||
time_t epoch = 0;
|
||||
|
||||
/** Call tzset() for the sake of portability */
|
||||
tzset();
|
||||
gmtime_r(&epoch, &gmt_tm);
|
||||
localtime_r(&epoch, &local_tm);
|
||||
|
||||
time_t gmt_t = mktime(&gmt_tm);
|
||||
time_t local_t = mktime(&local_tm);
|
||||
|
||||
/** The value of `(gmt_t - local_t)` will be the number of seconds west
|
||||
* from GMT. For timezones east of GMT, it will be negative. */
|
||||
return t - (gmt_t - local_t);
|
||||
}
|
Reference in New Issue
Block a user