Browse Source

feature: http is now namespace instead of class

master
Jørgen Lien Sellæg 10 years ago
parent
commit
93d5849fce
  1. 16
      toREST/include/http.hpp
  2. 56
      toREST/src/http.cpp
  3. 2
      toREST/tests/http_test.cpp

16
toREST/include/http.hpp

@ -4,8 +4,7 @@
#include <json.hpp> #include <json.hpp>
#include <unordered_map> #include <unordered_map>
class http { namespace http {
public:
/// http codes /// http codes
enum status { enum status {
/*! Standard response for successful HTTP requests. */ ok=200, /*! Standard response for successful HTTP requests. */ ok=200,
@ -26,12 +25,17 @@ public:
/*! Gateway timed out */ gateway_timeout=504, /*! Gateway timed out */ gateway_timeout=504,
/*! http-version not supported */ http_version_not_supported=505 /*! http-version not supported */ http_version_not_supported=505
}; };
typedef std::pair<status,std::string> code;
typedef std::pair<std::string,std::string> header; typedef std::pair<std::string,std::string> header;
typedef std::unordered_map<std::string,std::string> headers; typedef std::unordered_map<std::string,std::string> headers;
/// creates a http::code object out of a http::status code /// 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 { class basic_response {
protected: protected:
@ -46,10 +50,10 @@ public:
class response : public basic_response { class response : public basic_response {
public: 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 set_body(const std::string &content) override { body=content; }
void add_header(const http::header &header) override { response_headers.emplace(header); } 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: protected:
std::ostream& do_response(std::ostream &os) const override ; std::ostream& do_response(std::ostream &os) const override ;
std::string body; std::string body;

56
toREST/src/http.cpp

@ -1,43 +1,25 @@
#include <http.hpp> #include <http.hpp>
http::code http::http_code(status status) { http::code::code(status status):first(status){
switch(status) { switch(status) {
case accepted: case accepted: second="Accepted"; break;
return {status, "Accepted"}; case bad_gateway: second="Bad Gateway"; break;
case bad_gateway: case bad_request: second="Bad Request"; break;
return {status, "Bad gateway"}; case created: second="Created"; break;
case bad_request: case forbidden: second="Forbidden"; break;
return {status, "Bad Request"}; case gateway_timeout: second="Gateway Timeout"; break;
case created: case http_version_not_supported: second="HTTP Version Not Supported"; break;
return {status, "Created"}; case internal_server_error: second="Internal Server Error"; break;
case forbidden: case method_not_allowed: second="Method Not Allowed"; break;
return {status, "Forbidden"}; case not_acceptable: second="Not Acceptable"; break;
case gateway_timeout: case no_content: second="No Content"; break;
return {status, "Gateway Timeout"}; case not_found: second="Not Found"; break;
case http_version_not_supported: case not_implemented: second="Not Implemented"; break;
return {status, "HTTP Version Not Supported"}; case ok: second="OK"; break;
case internal_server_error: case payment_required: second="Payment Required"; break;
return {status, "Internal Server Error"}; case service_unavailable: second="Service Unavailable"; break;
case method_not_allowed: case unauthorized: second="Unauthorized"; break;
return {status, "Method Not Allowed"}; default: second="UNKNOWN";
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"};
} }
} }

2
toREST/tests/http_test.cpp

@ -4,7 +4,7 @@
using namespace std; using namespace std;
TEST_CASE("Check http status"){ TEST_CASE("Check http status"){
auto out=http::http_code(http::ok); auto out=http::code(http::ok);
REQUIRE(out.first == 200); REQUIRE(out.first == 200);
REQUIRE(out.second == "OK"); REQUIRE(out.second == "OK");
// TODO test all // TODO test all

Loading…
Cancel
Save