From 0e2817da8877723ff8578008c73766135432782e Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 12 Sep 2017 20:38:51 +0800 Subject: [PATCH] [Master] Move response stuff to ResponseUtils.hpp --- apps/master/RestServer.cpp | 23 ++++++----------------- apps/master/RestUtils.hpp | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 apps/master/RestUtils.hpp diff --git a/apps/master/RestServer.cpp b/apps/master/RestServer.cpp index b266db48d..58cb31ed9 100644 --- a/apps/master/RestServer.cpp +++ b/apps/master/RestServer.cpp @@ -7,22 +7,12 @@ #include #include +#include "RestUtils.hpp" + using namespace std; using namespace chrono; using namespace boost::property_tree; -static string response201 = "HTTP/1.1 201 Created\r\nContent-Length: 7\r\n\r\nCreated"; -static string response202 = "HTTP/1.1 202 Accepted\r\nContent-Length: 8\r\n\r\nAccepted"; -static string response400 = "HTTP/1.1 400 Bad Request\r\nContent-Length: 11\r\n\r\nbad request"; - -inline void ResponseStr(HttpServer::Response &response, string content, string type = "", string code = "200 OK") -{ - response << "HTTP/1.1 " << code << "\r\n"; - if (!type.empty()) - response << "Content-Type: " << type <<"\r\n"; - response << "Content-Length: " << content.length() << "\r\n\r\n" << content; -} - inline void ptreeToServer(boost::property_tree::ptree &pt, MasterServer::SServer &server) { server.SetName(pt.get("hostname").c_str()); @@ -70,7 +60,7 @@ void RestServer::start() auto port = (unsigned short)stoi(&(addr[addr.find(':')+1])); queryToStringStream(ss, "server", serverMap->at(RakNet::SystemAddress(addr.c_str(), port))); ss << "}"; - ResponseStr(*response, ss.str(), "application/json"); + ResponseStr(response, ss.str(), "application/json"); } catch(out_of_range e) { @@ -93,7 +83,7 @@ void RestServer::start() ss << ", "; } ss << "}}"; - ResponseStr(*response, ss.str(), "application/json"); + ResponseStr(response, ss.str(), "application/json"); updatedCache = false; } *response << str; @@ -110,7 +100,7 @@ void RestServer::start() MasterServer::SServer server; ptreeToServer(pt, server); - unsigned short port = pt.get("port"); + auto port = pt.get("port"); server.lastUpdate = steady_clock::now(); serverMap->insert({RakNet::SystemAddress(request->remote_endpoint_address.c_str(), port), server}); updatedCache = true; @@ -137,7 +127,6 @@ void RestServer::start() *response << response400; return; } - if (request->content.size() != 0) { try @@ -171,7 +160,7 @@ void RestServer::start() ss << ", \"players\": " << players; ss << "}"; - ResponseStr(*response, ss.str(), "application/json"); + ResponseStr(response, ss.str(), "application/json"); }; httpServer.default_resource["GET"]=[](auto response, auto /*request*/) { diff --git a/apps/master/RestUtils.hpp b/apps/master/RestUtils.hpp new file mode 100644 index 000000000..f9127403c --- /dev/null +++ b/apps/master/RestUtils.hpp @@ -0,0 +1,25 @@ +// +// Created by koncord on 04.09.17. +// + +#pragma once + +#include +#include "SimpleWeb/base_server.hpp" + +static std::string response201 = "HTTP/1.1 201 Created\r\nContent-Length: 7\r\n\r\nCreated"; +static std::string response202 = "HTTP/1.1 202 Accepted\r\nContent-Length: 8\r\n\r\nAccepted"; +static std::string response400 = "HTTP/1.1 400 Bad Request\r\nContent-Length: 11\r\n\r\nbad request"; + +static std::string response403 = "HTTP/1.1 403 Forbidden\r\nContent-Length: 9\r\n\r\nForbidden"; +static std::string response500 = "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 21\r\n\r\nInternal Server Error"; + +template +inline void ResponseStr(std::shared_ptr::Response> response, + std::string content, std::string type = "", std::string code = "200 OK") +{ + *response << "HTTP/1.1 " << code << "\r\n"; + if (!type.empty()) + *response << "Content-Type: " << type <<"\r\n"; + *response << "Content-Length: " << content.length() << "\r\n\r\n" << content; +}