MXS-1220: Clean up resource, request and response headers

Cleaned up various parts of the resource, request and response class
headers.

Moved `using` declarations into .cc files.

Made the Resource class non-copyable as it isn't really meant to be
copied.
This commit is contained in:
Markus Mäkelä
2017-04-19 18:58:40 +03:00
committed by Markus Mäkelä
parent 80104d6dad
commit ebc9e4bd3b
7 changed files with 67 additions and 78 deletions

View File

@ -16,6 +16,9 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
using std::string;
using std::deque;
/** TODO: Move this to a C++ string utility header */ /** TODO: Move this to a C++ string utility header */
namespace maxscale namespace maxscale
{ {
@ -50,7 +53,7 @@ static inline string& trim(string& str)
} }
} }
static void process_uri(string& uri, deque<string>& uri_parts) static void process_uri(string& uri, std::deque<string>& uri_parts)
{ {
/** Clean up trailing slashes in requested resource */ /** Clean up trailing slashes in requested resource */
while (uri.length() > 1 && *uri.rbegin() == '/') while (uri.length() > 1 && *uri.rbegin() == '/')

View File

@ -12,7 +12,6 @@
*/ */
#include "maxscale/httpresponse.hh" #include "maxscale/httpresponse.hh"
#include "maxscale/admin.hh"
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -20,6 +19,8 @@
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <sys/time.h> #include <sys/time.h>
#include "maxscale/admin.hh"
using std::string; using std::string;
using std::stringstream; using std::stringstream;

View File

@ -18,14 +18,12 @@
#include <maxscale/debug.h> #include <maxscale/debug.h>
using std::string;
/** /**
* @brief Return the current HTTP-date * @brief Return the current HTTP-date
* *
* @return The RFC 1123 compliant date * @return The RFC 1123 compliant date
*/ */
static inline string http_get_date() static inline std::string http_get_date()
{ {
time_t now = time(NULL); time_t now = time(NULL);
struct tm tm; struct tm tm;
@ -34,5 +32,5 @@ static inline string http_get_date()
gmtime_r(&now, &tm); gmtime_r(&now, &tm);
strftime(buf, sizeof(buf), "%a, %d %b %y %T GMT", &tm); strftime(buf, sizeof(buf), "%a, %d %b %y %T GMT", &tm);
return string(buf); return std::string(buf);
} }

View File

@ -26,23 +26,12 @@
#include "http.hh" #include "http.hh"
using std::shared_ptr;
using std::string;
using std::map;
using std::deque;
using mxs::Closer;
class HttpRequest;
/** Typedef for managed pointer */
typedef std::shared_ptr<HttpRequest> SHttpRequest;
static int value_iterator(void *cls, static int value_iterator(void *cls,
enum MHD_ValueKind kind, enum MHD_ValueKind kind,
const char *key, const char *key,
const char *value) const char *value)
{ {
std::pair<string, string>* cmp = (std::pair<string, string>*)cls; std::pair<std::string, std::string>* cmp = (std::pair<std::string, std::string>*)cls;
if (cmp->first == key) if (cmp->first == key)
{ {
@ -55,6 +44,8 @@ static int value_iterator(void *cls,
class HttpRequest class HttpRequest
{ {
HttpRequest(const HttpRequest&);
HttpRequest& operator = (const HttpRequest);
public: public:
/** /**
* @brief Parse a request * @brief Parse a request
@ -63,7 +54,7 @@ public:
* *
* @return Parsed statement or NULL if request is not valid * @return Parsed statement or NULL if request is not valid
*/ */
HttpRequest(struct MHD_Connection *connection, string url, string method, json_t* data); HttpRequest(struct MHD_Connection *connection, std::string url, std::string method, json_t* data);
~HttpRequest(); ~HttpRequest();
@ -72,7 +63,7 @@ public:
* *
* @return One of the HTTP verb values * @return One of the HTTP verb values
*/ */
const string& get_verb() const const std::string& get_verb() const
{ {
return m_verb; return m_verb;
} }
@ -84,9 +75,9 @@ public:
* *
* @return Header value or empty string if the header was not found * @return Header value or empty string if the header was not found
*/ */
string get_header(const string header) const std::string get_header(const std::string& header) const
{ {
std::pair<string, string> p; std::pair<std::string, std::string> p;
p.first = header; p.first = header;
MHD_get_connection_values(m_connection, MHD_HEADER_KIND, MHD_get_connection_values(m_connection, MHD_HEADER_KIND,
@ -102,9 +93,9 @@ public:
* *
* @return Option value or empty string if the option was not found * @return Option value or empty string if the option was not found
*/ */
string get_option(const string option) const std::string get_option(const std::string& option) const
{ {
std::pair<string, string> p; std::pair<std::string, std::string> p;
p.first = option; p.first = option;
MHD_get_connection_values(m_connection, MHD_GET_ARGUMENT_KIND, MHD_get_connection_values(m_connection, MHD_GET_ARGUMENT_KIND,
@ -118,7 +109,7 @@ public:
* *
* @return Request body or empty string if no body is defined * @return Request body or empty string if no body is defined
*/ */
const string& get_json_str() const const std::string& get_json_str() const
{ {
return m_json_string; return m_json_string;
} }
@ -138,7 +129,7 @@ public:
* *
* @return The complete request URI * @return The complete request URI
*/ */
const string& get_uri() const const std::string& get_uri() const
{ {
return m_resource; return m_resource;
} }
@ -150,7 +141,7 @@ public:
* *
* @return The request URI part or empty string if no part was found * @return The request URI part or empty string if no part was found
*/ */
const string uri_part(uint32_t idx) const const std::string uri_part(uint32_t idx) const
{ {
return m_resource_parts.size() > idx ? m_resource_parts[idx] : ""; return m_resource_parts.size() > idx ? m_resource_parts[idx] : "";
} }
@ -171,12 +162,12 @@ public:
} }
private: private:
map<string, string> m_options; /**< Request options */ std::map<std::string, std::string> m_options; /**< Request options */
Closer<json_t*> m_json; /**< Request body */ mxs::Closer<json_t*> m_json; /**< Request body */
string m_json_string; /**< String version of @c m_json */ std::string m_json_string; /**< String version of @c m_json */
string m_resource; /**< Requested resource */ std::string m_resource; /**< Requested resource */
deque<string> m_resource_parts; /**< @c m_resource split into parts */ std::deque<std::string> m_resource_parts; /**< @c m_resource split into parts */
string m_verb; /**< Request method */ std::string m_verb; /**< Request method */
string m_hostname; /**< The value of the Host header */ std::string m_hostname; /**< The value of the Host header */
struct MHD_Connection* m_connection; struct MHD_Connection* m_connection;
}; };

View File

@ -23,14 +23,6 @@
#include "http.hh" #include "http.hh"
using std::map;
using std::string;
using std::shared_ptr;
class HttpResponse;
typedef shared_ptr<HttpResponse> SHttpResponse;
class HttpResponse class HttpResponse
{ {
public: public:

View File

@ -18,7 +18,6 @@
#include <string> #include <string>
#include <deque> #include <deque>
#include <tr1/memory>
#include <maxscale/server.h> #include <maxscale/server.h>
@ -30,14 +29,12 @@
#include "filter.h" #include "filter.h"
#include "session.h" #include "session.h"
using std::string;
using std::shared_ptr;
using std::deque;
typedef HttpResponse (*ResourceCallback)(HttpRequest& request); typedef HttpResponse (*ResourceCallback)(HttpRequest& request);
class Resource class Resource
{ {
Resource(const Resource&);
Resource& operator = (const Resource&);
public: public:
Resource(ResourceCallback cb, int components, ...); Resource(ResourceCallback cb, int components, ...);
@ -63,10 +60,10 @@ public:
private: private:
bool matching_variable_path(const string& path, const string& target); bool matching_variable_path(const std::string& path, const std::string& target);
ResourceCallback m_cb; /**< Resource handler callback */ ResourceCallback m_cb; /**< Resource handler callback */
deque<string> m_path; /**< Path components */ std::deque<std::string> m_path; /**< Path components */
}; };
/** /**

View File

@ -10,15 +10,14 @@
* of this software will be governed by version 2 or later of the General * of this software will be governed by version 2 or later of the General
* Public License. * Public License.
*/ */
#include "maxscale/resource.hh"
#include <maxscale/spinlock.hh>
#include <list> #include <list>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/jansson.hh> #include <maxscale/jansson.hh>
#include <maxscale/spinlock.hh>
#include "maxscale/resource.hh"
#include "maxscale/httprequest.hh" #include "maxscale/httprequest.hh"
#include "maxscale/httpresponse.hh" #include "maxscale/httpresponse.hh"
#include "maxscale/session.h" #include "maxscale/session.h"
@ -28,7 +27,7 @@
#include "maxscale/config_runtime.h" #include "maxscale/config_runtime.h"
using std::list; using std::list;
using std::string;
using mxs::SpinLock; using mxs::SpinLock;
using mxs::SpinLockGuard; using mxs::SpinLockGuard;
@ -212,11 +211,13 @@ HttpResponse cb_get_session(HttpRequest& request)
HttpResponse cb_maxscale(HttpRequest& request) HttpResponse cb_maxscale(HttpRequest& request)
{ {
// TODO: Show logs
return HttpResponse(MHD_HTTP_OK); return HttpResponse(MHD_HTTP_OK);
} }
HttpResponse cb_logs(HttpRequest& request) HttpResponse cb_logs(HttpRequest& request)
{ {
// TODO: Show logs
return HttpResponse(MHD_HTTP_OK); return HttpResponse(MHD_HTTP_OK);
} }
@ -235,62 +236,68 @@ HttpResponse cb_flush(HttpRequest& request)
HttpResponse cb_threads(HttpRequest& request) HttpResponse cb_threads(HttpRequest& request)
{ {
// Show thread status // TODO: Show thread status
return HttpResponse(MHD_HTTP_OK); return HttpResponse(MHD_HTTP_OK);
} }
HttpResponse cb_tasks(HttpRequest& request) HttpResponse cb_tasks(HttpRequest& request)
{ {
// Show housekeeper tasks // TODO: Show housekeeper tasks
return HttpResponse(MHD_HTTP_OK); return HttpResponse(MHD_HTTP_OK);
} }
HttpResponse cb_modules(HttpRequest& request) HttpResponse cb_modules(HttpRequest& request)
{ {
// Show modules // TODO: Show modules
return HttpResponse(MHD_HTTP_OK); return HttpResponse(MHD_HTTP_OK);
} }
class RootResource class RootResource
{ {
public: public:
typedef list<Resource> ResourceList; typedef std::shared_ptr<Resource> SResource;
typedef list<SResource> ResourceList;
RootResource() RootResource()
{ {
m_get.push_back(Resource(cb_all_servers, 1, "servers")); m_get.push_back(SResource(new Resource(cb_all_servers, 1, "servers")));
m_get.push_back(Resource(cb_get_server, 2, "servers", ":server")); m_get.push_back(SResource(new Resource(cb_get_server, 2, "servers", ":server")));
m_get.push_back(Resource(cb_all_services, 1, "services")); m_get.push_back(SResource(new Resource(cb_all_services, 1, "services")));
m_get.push_back(Resource(cb_get_service, 2, "services", ":service")); m_get.push_back(SResource(new Resource(cb_get_service, 2, "services", ":service")));
m_get.push_back(Resource(cb_all_filters, 1, "filters")); m_get.push_back(SResource(new Resource(cb_all_filters, 1, "filters")));
m_get.push_back(Resource(cb_get_filter, 2, "filters", ":filter")); m_get.push_back(SResource(new Resource(cb_get_filter, 2, "filters", ":filter")));
m_get.push_back(Resource(cb_all_monitors, 1, "monitors")); m_get.push_back(SResource(new Resource(cb_all_monitors, 1, "monitors")));
m_get.push_back(Resource(cb_get_monitor, 2, "monitors", ":monitor")); m_get.push_back(SResource(new Resource(cb_get_monitor, 2, "monitors", ":monitor")));
m_get.push_back(Resource(cb_all_sessions, 1, "sessions")); m_get.push_back(SResource(new Resource(cb_all_sessions, 1, "sessions")));
m_get.push_back(Resource(cb_get_session, 2, "sessions", ":session")); m_get.push_back(SResource(new Resource(cb_get_session, 2, "sessions", ":session")));
m_get.push_back(Resource(cb_maxscale, 1, "maxscale")); m_get.push_back(SResource(new Resource(cb_maxscale, 1, "maxscale")));
m_get.push_back(Resource(cb_threads, 2, "maxscale", "threads")); m_get.push_back(SResource(new Resource(cb_threads, 2, "maxscale", "threads")));
m_get.push_back(Resource(cb_logs, 2, "maxscale", "logs")); m_get.push_back(SResource(new Resource(cb_logs, 2, "maxscale", "logs")));
m_get.push_back(Resource(cb_tasks, 2, "maxscale", "tasks")); m_get.push_back(SResource(new Resource(cb_tasks, 2, "maxscale", "tasks")));
m_get.push_back(Resource(cb_modules, 2, "maxscale", "modules")); m_get.push_back(SResource(new Resource(cb_modules, 2, "maxscale", "modules")));
m_post.push_back(Resource(cb_flush, 3, "maxscale", "logs", "flush")); m_post.push_back(SResource(new Resource(cb_flush, 3, "maxscale", "logs", "flush")));
m_post.push_back(Resource(cb_create_server, 1, "servers")); m_post.push_back(SResource(new Resource(cb_create_server, 1, "servers")));
}
~RootResource()
{
} }
HttpResponse process_request_type(ResourceList& list, HttpRequest& request) HttpResponse process_request_type(ResourceList& list, HttpRequest& request)
{ {
for (ResourceList::iterator it = list.begin(); for (ResourceList::iterator it = list.begin(); it != list.end(); it++)
it != list.end(); it++)
{ {
if (it->match(request)) Resource& r = *(*it);
if (r.match(request))
{ {
return it->call(request); return r.call(request);
} }
} }