From 93d5849fcecf66ad71b0c99a4f63c886631f9c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 2 Sep 2016 20:11:28 +0200 Subject: [PATCH] feature: http is now namespace instead of class --- toREST/include/http.hpp | 16 +++++++---- toREST/src/http.cpp | 56 +++++++++++++------------------------- toREST/tests/http_test.cpp | 2 +- 3 files changed, 30 insertions(+), 44 deletions(-) diff --git a/toREST/include/http.hpp b/toREST/include/http.hpp index a1ad0bb..78ca4ee 100644 --- a/toREST/include/http.hpp +++ b/toREST/include/http.hpp @@ -4,8 +4,7 @@ #include #include -class http { -public: +namespace http { /// http codes enum status { /*! Standard response for successful HTTP requests. */ ok=200, @@ -26,12 +25,17 @@ public: /*! Gateway timed out */ gateway_timeout=504, /*! http-version not supported */ http_version_not_supported=505 }; - typedef std::pair code; typedef std::pair header; typedef std::unordered_map headers; /// creates a http::code object out of a http::status code - static code http_code(status status); + class code { + public: + code(status status); + public: + status first; + std::string second; + }; class basic_response { protected: @@ -46,10 +50,10 @@ public: class response : public basic_response { public: - response():status_code(http::http_code(http::ok)){} + response():status_code(http::ok){} void set_body(const std::string &content) override { body=content; } void add_header(const http::header &header) override { response_headers.emplace(header); } - void set_status(http::status code) override { status_code=http_code(code); } + void set_status(http::status code) override { status_code=code; } protected: std::ostream& do_response(std::ostream &os) const override ; std::string body; diff --git a/toREST/src/http.cpp b/toREST/src/http.cpp index 777faf2..25016a5 100644 --- a/toREST/src/http.cpp +++ b/toREST/src/http.cpp @@ -1,43 +1,25 @@ #include -http::code http::http_code(status status) { +http::code::code(status status):first(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"}; + case accepted: second="Accepted"; break; + case bad_gateway: second="Bad Gateway"; break; + case bad_request: second="Bad Request"; break; + case created: second="Created"; break; + case forbidden: second="Forbidden"; break; + case gateway_timeout: second="Gateway Timeout"; break; + case http_version_not_supported: second="HTTP Version Not Supported"; break; + case internal_server_error: second="Internal Server Error"; break; + case method_not_allowed: second="Method Not Allowed"; break; + case not_acceptable: second="Not Acceptable"; break; + case no_content: second="No Content"; break; + case not_found: second="Not Found"; break; + case not_implemented: second="Not Implemented"; break; + case ok: second="OK"; break; + case payment_required: second="Payment Required"; break; + case service_unavailable: second="Service Unavailable"; break; + case unauthorized: second="Unauthorized"; break; + default: second="UNKNOWN"; } } diff --git a/toREST/tests/http_test.cpp b/toREST/tests/http_test.cpp index a83fb99..ec38eb1 100644 --- a/toREST/tests/http_test.cpp +++ b/toREST/tests/http_test.cpp @@ -4,7 +4,7 @@ using namespace std; TEST_CASE("Check http status"){ - auto out=http::http_code(http::ok); + auto out=http::code(http::ok); REQUIRE(out.first == 200); REQUIRE(out.second == "OK"); // TODO test all