From 0ac8114db38bc145564fd45427ae9a0803c7cea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 2 Sep 2016 01:32:22 +0200 Subject: [PATCH] cleanup: split imp and decl --- toREST/include/http.hpp | 34 ++++--------------- toREST/src/http.cpp | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 toREST/src/http.cpp diff --git a/toREST/include/http.hpp b/toREST/include/http.hpp index 9e17eff..a1ad0bb 100644 --- a/toREST/include/http.hpp +++ b/toREST/include/http.hpp @@ -31,7 +31,7 @@ public: typedef std::unordered_map headers; /// creates a http::code object out of a http::status code - static code http_code(status status){switch(status){case accepted:return{status,"Accepted"};case bad_gateway:return{status,"Bad gateway"};case bad_request:return{status,"Bad Request"};case created:return{status,"Created"};case forbidden:return{status,"Forbidden"};case gateway_timeout:return{status,"Gateway Timeout"};case http_version_not_supported:return{status,"HTTP Version Not Supported"};case internal_server_error:return{status,"Internal Server Error"};case method_not_allowed:return{status,"Method Not Allowed"};case not_acceptable:return{status,"Not Acceptable"};case no_content:return{status,"No Content"};case not_found:return{status,"Not Found"};case not_implemented:return{status,"Not Implemented"};case ok:return{status,"OK"};case payment_required:return{status,"Payment Required"};case service_unavailable:return{status,"Service Unavailable"};case unauthorized:return{status,"Unauthorized"};default: return {status,"UNKNOWN"};}} + static code http_code(status status); class basic_response { protected: @@ -51,24 +51,9 @@ public: void add_header(const http::header &header) override { response_headers.emplace(header); } void set_status(http::status code) override { status_code=http_code(code); } protected: - std::ostream& do_response(std::ostream &os) const override { - os << "HTTP/1.1" << " " << status_code.first << " " << status_code.second << "\r\n"; - for(auto &header:response_headers) - os << header.first << ": " << header.second << "\r\n"; - if(status_code.first!=http::no_content){ - os << "Content-Length: "; - if(!body.empty()){ - return os << body.size() << "\r\n\r\n" << body; - } else { - auto body_replace=std::to_string(status_code.first) + " " + status_code.second; - return os << body_replace.size() << "\r\n\r\n" << body_replace; - } - } - return os << "\r\n"; - } - protected: - headers response_headers; + std::ostream& do_response(std::ostream &os) const override ; std::string body; + headers response_headers; code status_code; }; @@ -76,17 +61,12 @@ public: /// application/json, the response defaults to 200 OK. class json_response : public response { public: - json_response(){ - add_header({"Content-Type","application/json"}); - set_body({{"code",status_code.first},{"status",status_code.second}}); - } + json_response(); /// Method updates the http status code. set_statups also updates the body to correspond to the new status. /// Notice: if you previously set a body, it will be overwritten by the status code JSON representation. - void set_status(http::status code) override { - response::set_status(code); - set_body({{"code",status_code.first},{"status",status_code.second}}); - } - void set_body(nlohmann::json json){ response::set_body(json.dump()); } + void set_status(http::status code) override ; + void set_body(const nlohmann::json &json) ; + using response::set_body; }; }; diff --git a/toREST/src/http.cpp b/toREST/src/http.cpp new file mode 100644 index 0000000..777faf2 --- /dev/null +++ b/toREST/src/http.cpp @@ -0,0 +1,73 @@ +#include + +http::code http::http_code(status status) { + switch(status) { + case accepted: + return {status, "Accepted"}; + case bad_gateway: + return {status, "Bad gateway"}; + case bad_request: + return {status, "Bad Request"}; + case created: + return {status, "Created"}; + case forbidden: + return {status, "Forbidden"}; + case gateway_timeout: + return {status, "Gateway Timeout"}; + case http_version_not_supported: + return {status, "HTTP Version Not Supported"}; + case internal_server_error: + return {status, "Internal Server Error"}; + case method_not_allowed: + return {status, "Method Not Allowed"}; + case not_acceptable: + return {status, "Not Acceptable"}; + case no_content: + return {status, "No Content"}; + case not_found: + return {status, "Not Found"}; + case not_implemented: + return {status, "Not Implemented"}; + case ok: + return {status, "OK"}; + case payment_required: + return {status, "Payment Required"}; + case service_unavailable: + return {status, "Service Unavailable"}; + case unauthorized: + return {status, "Unauthorized"}; + default: + return {status, "UNKNOWN"}; + } +} + +std::ostream &http::response::do_response(std::ostream &os) const { + os << "HTTP/1.1" + << " " << status_code.first << " " << status_code.second << "\r\n"; + for(auto &header : response_headers) + os << header.first << ": " << header.second << "\r\n"; + if(status_code.first != http::no_content) { + os << "Content-Length: "; + if(!body.empty()) { + return os << body.size() << "\r\n\r\n" + << body; + } else { + auto body_replace= std::to_string(status_code.first) + " " + status_code.second; + return os << body_replace.size() << "\r\n\r\n" + << body_replace; + } + } + return os << "\r\n"; +} + +void http::json_response::set_status(http::status code){ + response::set_status(code); + set_body({{"code",status_code.first},{"status",status_code.second}}); +} + +void http::json_response::set_body(const nlohmann::json &json){ response::set_body(json.dump()); } + +http::json_response::json_response(){ + add_header({"Content-Type","application/json"}); + set_body({{"code",status_code.first},{"status",status_code.second}}); +}