correct file formatting; fix issue where RequestDispatcher was not properly handling requests;

pull/19/head
Bryan Biedenkapp 3 years ago
parent 40f1b3be9c
commit 8d151582ce

@ -29,6 +29,7 @@
#include "Defines.h"
#include "network/rest/http/HTTPRequest.h"
#include "network/rest/http/HTTPReply.h"
#include "Log.h"
#include <functional>
#include <map>
@ -36,8 +37,10 @@
#include <regex>
#include <memory>
namespace network {
namespace rest {
namespace network
{
namespace rest
{
// ---------------------------------------------------------------------------
// Structure Declaration
//
@ -119,16 +122,25 @@ namespace network {
typedef RequestMatcher<Reply> MatcherType;
public:
/// <summary>Initializes a new instance of the RequestDispatcher class.</summary>
RequestDispatcher() : m_basePath() { /* stub */ }
RequestDispatcher() : m_basePath(), m_debug(false) { /* stub */ }
/// <summary>Initializes a new instance of the RequestDispatcher class.</summary>
RequestDispatcher(bool debug) : m_basePath(), m_debug(debug) { /* stub */ }
/// <summary>Initializes a new instance of the RequestDispatcher class.</summary>
RequestDispatcher(const std::string& basePath) : m_basePath(basePath) { /* stub */ }
RequestDispatcher(const std::string& basePath, bool debug) : m_basePath(basePath), m_debug(debug) { /* stub */ }
/// <summary></summary>
MatcherType& match(const std::string& expression)
{
MatcherTypePtr& p = m_matchers[expression];
if (!p) {
if (m_debug) {
::LogDebug(LOG_RCON, "creating REST RequestDispatcher, expression = %s", expression.c_str());
}
p = std::make_shared<MatcherType>(expression);
} else {
if (m_debug) {
::LogDebug(LOG_RCON, "fetching REST RequestDispatcher, expression = %s", expression.c_str());
}
}
return *p;
@ -141,19 +153,28 @@ namespace network {
std::smatch what;
if (!matcher.second->regex()) {
if (request.uri.find(matcher.first) != std::string::npos) {
if (m_debug) {
::LogDebug(LOG_RCON, "non-regex endpoint, uri = %s, expression = %s", request.uri.c_str(), matcher.first.c_str());
}
//what = matcher.first;
matcher.second->handleRequest(request, reply, what);
} else {
reply = http::HTTPReply::stockReply(http::HTTPReply::BAD_REQUEST);
return;
}
} else {
if (std::regex_match(request.uri, what, std::regex(matcher.first))) {
if (m_debug) {
::LogDebug(LOG_RCON, "regex endpoint, uri = %s, expression = %s", request.uri.c_str(), matcher.first.c_str());
}
matcher.second->handleRequest(request, reply, what);
} else {
reply = http::HTTPReply::stockReply(http::HTTPReply::BAD_REQUEST);
return;
}
}
}
::LogError(LOG_RCON, "unknown endpoint, uri = %s", request.uri.c_str());
reply = http::HTTPReply::stockReply(http::HTTPReply::BAD_REQUEST, "application/json");
}
private:
@ -161,6 +182,8 @@ namespace network {
std::string m_basePath;
std::map<std::string, MatcherTypePtr> m_matchers;
bool m_debug;
};
typedef RequestDispatcher<http::HTTPRequest, http::HTTPReply> DefaultRequestDispatcher;

@ -48,9 +48,12 @@
#include <iterator>
#include <asio.hpp>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Class Prototypes

@ -42,9 +42,12 @@
#include <set>
#include <mutex>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Class Declaration

@ -41,9 +41,12 @@
#include <string>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Structure Declaration

@ -108,84 +108,141 @@ namespace misc_strings {
namespace stock_replies {
const char ok[] = "";
const char json_ok[] = "{status:200,message:\"ok\"}";
const char created[] =
"<html>"
"<head><title>Created</title></head>"
"<body><h1>201 Created</h1></body>"
"</html>";
const char json_created[] = "{status:201,message:\"created\"}";
const char accepted[] =
"<html>"
"<head><title>Accepted</title></head>"
"<body><h1>202 Accepted</h1></body>"
"</html>";
const char json_accepted[] = "{status:202,message:\"accepted\"}";
const char no_content[] =
"<html>"
"<head><title>No Content</title></head>"
"<body><h1>204 Content</h1></body>"
"</html>";
const char json_no_content[] = "{status:204,message:\"no content\"}";
const char multiple_choices[] =
"<html>"
"<head><title>Multiple Choices</title></head>"
"<body><h1>300 Multiple Choices</h1></body>"
"</html>";
const char json_multiple_choices[] = "{status:300,message:\"multiple choices\"}";
const char moved_permanently[] =
"<html>"
"<head><title>Moved Permanently</title></head>"
"<body><h1>301 Moved Permanently</h1></body>"
"</html>";
const char json_moved_permanently[] = "{status:301,message:\"moved permanently\"}";
const char moved_temporarily[] =
"<html>"
"<head><title>Moved Temporarily</title></head>"
"<body><h1>302 Moved Temporarily</h1></body>"
"</html>";
const char json_moved_temporarily[] = "{status:302,message:\"moved temporarily\"}";
const char not_modified[] =
"<html>"
"<head><title>Not Modified</title></head>"
"<body><h1>304 Not Modified</h1></body>"
"</html>";
const char json_not_modified[] = "{status:304,message:\"not modified\"}";
const char bad_request[] =
"<html>"
"<head><title>Bad Request</title></head>"
"<body><h1>400 Bad Request</h1></body>"
"</html>";
const char json_bad_request[] = "{status:400,message:\"bad request\"}";
const char unauthorized[] =
"<html>"
"<head><title>Unauthorized</title></head>"
"<body><h1>401 Unauthorized</h1></body>"
"</html>";
const char json_unauthorized[] = "{status:401,message:\"unauthorized\"}";
const char forbidden[] =
"<html>"
"<head><title>Forbidden</title></head>"
"<body><h1>403 Forbidden</h1></body>"
"</html>";
const char json_forbidden[] = "{status:403,message:\"forbidden\"}";
const char not_found[] =
"<html>"
"<head><title>Not Found</title></head>"
"<body><h1>404 Not Found</h1></body>"
"</html>";
const char json_not_found[] = "{status:404,message:\"not found\"}";
const char internal_server_error[] =
"<html>"
"<head><title>Internal Server Error</title></head>"
"<body><h1>500 Internal Server Error</h1></body>"
"</html>";
const char json_internal_server_error[] = "{status:500,message:\"internal server error\"}";
const char not_implemented[] =
"<html>"
"<head><title>Not Implemented</title></head>"
"<body><h1>501 Not Implemented</h1></body>"
"</html>";
const char json_not_implemented[] = "{status:501,message:\"not implemented\"}";
const char bad_gateway[] =
"<html>"
"<head><title>Bad Gateway</title></head>"
"<body><h1>502 Bad Gateway</h1></body>"
"</html>";
const char json_bad_gateway[] = "{status:502,message:\"bad gateway\"}";
const char service_unavailable[] =
"<html>"
"<head><title>Service Unavailable</title></head>"
"<body><h1>503 Service Unavailable</h1></body>"
"</html>";
const char json_service_unavailable[] = "{status:503,message:\"service unavailable\"}";
std::string to_string(HTTPReply::StatusType status)
std::string to_string(HTTPReply::StatusType status, std::string contentType)
{
std::transform(contentType.begin(), contentType.end(), contentType.begin(), ::tolower);
if (contentType == "application/json") {
switch (status)
{
case HTTPReply::OK:
return json_ok;
case HTTPReply::CREATED:
return json_created;
case HTTPReply::ACCEPTED:
return json_accepted;
case HTTPReply::NO_CONTENT:
return json_no_content;
case HTTPReply::MULTIPLE_CHOICES:
return json_multiple_choices;
case HTTPReply::MOVED_PERMANENTLY:
return json_moved_permanently;
case HTTPReply::MOVED_TEMPORARILY:
return json_moved_temporarily;
case HTTPReply::NOT_MODIFIED:
return json_not_modified;
case HTTPReply::BAD_REQUEST:
return json_bad_request;
case HTTPReply::UNAUTHORIZED:
return json_unauthorized;
case HTTPReply::FORBIDDEN:
return json_forbidden;
case HTTPReply::NOT_FOUND:
return json_not_found;
case HTTPReply::INTERNAL_SERVER_ERROR:
return json_internal_server_error;
case HTTPReply::NOT_IMPLEMENTED:
return json_not_implemented;
case HTTPReply::BAD_GATEWAY:
return json_bad_gateway;
case HTTPReply::SERVICE_UNAVAILABLE:
return json_service_unavailable;
default:
return json_internal_server_error;
}
}
else {
switch (status)
{
case HTTPReply::OK:
@ -224,6 +281,7 @@ namespace stock_replies {
return internal_server_error;
}
}
}
} // namespace stock_replies
// ---------------------------------------------------------------------------
@ -262,7 +320,7 @@ void HTTPReply::reply(json::object obj, HTTPReply::StatusType s)
{
json::value v = json::value(obj);
std::string json = v.serialize();
reply(json, s, "text/json");
reply(json, s, "application/json");
}
/// <summary>
@ -293,7 +351,7 @@ HTTPReply HTTPReply::stockReply(HTTPReply::StatusType status, const std::string
rep.status = status;
if (status != HTTPReply::NO_CONTENT) {
rep.content = stock_replies::to_string(status);
rep.content = stock_replies::to_string(status, contentType);
rep.ensureDefaultHeaders(contentType);
}

@ -46,9 +46,12 @@
#include <asio.hpp>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Structure Declaration

@ -43,9 +43,12 @@
#include <string>
#include <vector>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Structure Declaration

@ -41,9 +41,12 @@
#include <string>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Class Prototypes

@ -39,9 +39,12 @@
#include <tuple>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Class Prototypes

@ -52,9 +52,12 @@
#include <utility>
#include <memory>
namespace network {
namespace rest {
namespace http {
namespace network
{
namespace rest
{
namespace http
{
// ---------------------------------------------------------------------------
// Class Declaration

Loading…
Cancel
Save

Powered by TurnKey Linux.