#ifndef _TOREST_HTTP_HPP_ #define _TOREST_HTTP_HPP_ #include #include class http { public: /// http codes enum status { /*! Standard response for successful HTTP requests. */ ok=200, /*! The request has been fulfilled, resulting in the creation of a new resource. */ created=201, /*! The request has been accepted for processing, but the processing has not been completed. */ accepted=202, /*! The server successfully processed the request and is not returning any content. */ no_content=204, /*! The server cannot or will not process the request due to an apparent client error */ bad_request=400, /*! Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. */ unauthorized=401, /*! Reserved for future use. The */ payment_required=402, /*! the request was a valid request, but the server is refusing to respond to it. */ forbidden=403, /*! The requested resource could not be found but may be available in the future. */ not_found=404, /*! Allowed A request method is not supported for the requested resource */ method_not_allowed=405, /*! The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. */ not_acceptable=406, /*! A generic error message, given when an unexpected condition was encountered and no more specific message is suitable. */ internal_server_error=500, /*! The server either does not recognize the request method, or it lacks the ability to fulfill the request. */ not_implemented=501, /*! The server was acting as a gateway or proxy and received an invalid response from the upstream server. */ bad_gateway=502, /*! The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state. */ service_unavailable=503, /*! 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 basic_response { protected: virtual std::ostream& do_response(std::ostream& os)const=0; public: virtual ~basic_response(){} virtual void add_header(const http::header &header)=0; virtual void set_status(http::status code)=0; virtual void set_body(const std::string &content)=0; friend std::ostream& operator<<(std::ostream& os,const http::basic_response &rh){ return rh.do_response(os); } }; class response : public basic_response { public: response():status_code(http::http_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); } protected: std::ostream& do_response(std::ostream &os) const override ; std::string body; headers response_headers; code status_code; }; /// A JSON response. The default constructor sets the Content-Type header to /// application/json, the response defaults to 200 OK. class json_response : public response { public: 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 ; void set_body(const nlohmann::json &json) ; using response::set_body; }; }; #endif // _TOREST_HTTP_HPP_