MXS-1220: Create HTTP response class

The HTTP response class simplifies the response creation. The next step is
to add generation of all the default headers that are needed by the REST
API.
This commit is contained in:
Markus Mäkelä 2017-04-17 02:20:13 +03:00 committed by Markus Mäkelä
parent 55b52b8ab1
commit a73d3e9276
4 changed files with 150 additions and 5 deletions

View File

@ -15,6 +15,7 @@ add_library(maxscale-common SHARED
hint.cc
housekeeper.cc
httprequest.cc
httpresponse.cc
listener.cc
load_utils.cc
log_manager.cc

View File

@ -0,0 +1,75 @@
/*
* 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/httpresponse.hh"
#include <new>
#include <string>
#include <sstream>
#include <maxscale/alloc.h>
#include <sys/time.h>
using std::string;
using std::stringstream;
HttpResponse::HttpResponse(string response, enum http_code code):
m_body(response),
m_code(code)
{
}
HttpResponse* HttpResponse::create(json_t* response, enum http_code code)
{
HttpResponse* rval = NULL;
char* json = json_dumps(response, 0);
if (json)
{
rval = HttpResponse::create(json, code);
MXS_FREE(json);
}
return rval;
}
HttpResponse* HttpResponse::create(string response, enum http_code code)
{
return new (std::nothrow) HttpResponse(response, code);
}
HttpResponse::~HttpResponse()
{
}
void HttpResponse::add_header(string name, string value)
{
m_headers[name] = value;
}
string HttpResponse::get_response() const
{
stringstream response;
response << "HTTP/1.1 " << http_code_to_string(m_code);
for (map<string, string>::const_iterator it = m_headers.begin();
it != m_headers.end(); it++)
{
response << it->first << ": " << it->second << "\r\n";
}
/** End of headers, add the body */
response << "\r\n" << m_body;
return response.str();
}

View File

@ -124,9 +124,9 @@ private:
HttpRequest(const HttpRequest&);
HttpRequest& operator = (const HttpRequest&);
map<string, string> m_headers;
Closer<json_t*> m_json;
string m_json_string;
string m_resource;
enum http_verb m_verb;
map<string, string> m_headers; /**< Request headers */
Closer<json_t*> m_json; /**< Request body */
string m_json_string; /**< String version of @c m_json */
string m_resource; /**< Requested resource */
enum http_verb m_verb; /**< Request method */
};

View File

@ -0,0 +1,69 @@
#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 <map>
#include <string>
#include <tr1/memory>
#include <maxscale/jansson.hh>
#include "http.hh"
using std::map;
using std::string;
using std::shared_ptr;
class HttpResponse;
typedef shared_ptr<HttpResponse> SHttpResponse;
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);
~HttpResponse();
/**
* @brief Add a header to the response
*
* @param name Header name
* @param value Header value
*/
void add_header(string name, string value);
/**
* @brief Get the response in string format
*
* @return The complete response that can be sent to a client
*/
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 */
};