MXS-1220: Expand HttpResponse class

The class now generates default headers. The ETag and Last-Modified tags
do not represent any actual modification time or resource hash.

The basic functionality of the HTTP responses is tested by the core test
suite. More advanced testing of the whole REST API is still required.

Removed the static `create` functions as only the JSON parsing version
could generated errors and even then the errors were unlikely. By
replacing the static creator function with a normal constructor, the
HttpResponse class can now also be created on the stack making its use
easier.
This commit is contained in:
Markus Mäkelä
2017-04-17 03:13:22 +03:00
committed by Markus Mäkelä
parent a73d3e9276
commit 9d0d394361
7 changed files with 114 additions and 42 deletions

View File

@ -19,6 +19,7 @@
#include <maxscale/thread.h>
#include "http.hh"
#include "adminclient.hh"
using std::deque;
@ -29,8 +30,9 @@ typedef deque<SAdminClient> ClientList;
/** The admin interface configuration */
struct AdminConfig
{
string host;
uint16_t port;
string host;
uint16_t port;
enum http_auth auth;
};
class AdminListener
@ -87,4 +89,4 @@ void mxs_admin_shutdown();
*
* @return A reference to the administrative interface configuration
*/
AdminConfig& mxs_admin_get_config();
AdminConfig& mxs_admin_get_config();

View File

@ -71,8 +71,40 @@ enum http_code
HTTP_507_INSUFFICIENT_STORAGE,
HTTP_508_LOOP_DETECTED,
HTTP_510_NOT_EXTENDED
};
/** Supported authentication types */
enum http_auth
{
HTTP_AUTH_NONE,
HTTP_AUTH_BASIC,
HTTP_AUTH_DIGEST
} ;
/**
* @brief Convert auth type to string
*
* @param auth Authentication value to convert
*
* @return Converted value in string format
*/
static inline const char* http_auth_to_string(enum http_auth auth)
{
switch (auth)
{
case HTTP_AUTH_NONE:
return "None";
case HTTP_AUTH_BASIC:
return "Basic";
case HTTP_AUTH_DIGEST:
return "Digest";
default:
ss_dassert(false);
return "";
}
}
/**
* @brief Convert string to HTTP verb
*
@ -226,3 +258,20 @@ static inline const char* http_code_to_string(enum http_code code)
return "500 Internal Server Error";
}
}
/**
* @brief Return the current HTTP-date
*
* @return The RFC 1123 compliant date
*/
static inline string get_http_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 string(buf);
}

View File

@ -34,12 +34,12 @@ class HttpResponse
{
public:
/**
* @brief Create a new HTTP response
*
* @param request Response body
*/
static HttpResponse* create(string response = "", enum http_code code = HTTP_200_OK);
static HttpResponse* create(json_t* response, enum http_code code = HTTP_200_OK);
* @brief Create new HTTP response
*
* @param response Response body
* @param code HTTP return code
*/
HttpResponse(string response = "", enum http_code code = HTTP_200_OK);
~HttpResponse();
@ -59,11 +59,10 @@ public:
string get_response() const;
private:
HttpResponse(string response, enum http_code code);
HttpResponse(const HttpResponse&);
HttpResponse& operator = (const HttpResponse&);
string m_body; /**< Message body */
map<string, string> m_headers; /**< Message headers */
http_code m_code; /**< The HTTP code for the response */
};
};