You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.9 KiB
58 lines
2.9 KiB
#ifndef _TR_HTTP_HPP_ |
|
#define _TR_HTTP_HPP_ |
|
|
|
#include <json.hpp> |
|
#include <unordered_map> |
|
|
|
namespace http { |
|
/// 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<std::string, std::string> header; |
|
typedef std::unordered_map<std::string, std::string> headers; |
|
|
|
/// creates a http::code object out of a http::status code |
|
class code { |
|
public: |
|
code(status status); |
|
|
|
public: |
|
status first; |
|
std::string second; |
|
}; |
|
|
|
class response { |
|
public: |
|
friend std::ostream &operator<<(std::ostream &os, const response &rh) { return rh.do_response(os); } |
|
response() : status_code(http::ok) {} |
|
void set_body(const std::string &content) { body = content; } |
|
void set_body(const nlohmann::json &json); |
|
void add_header(const http::header &header) { response_headers.emplace(header); } |
|
void set_status(http::status code) { status_code = code; } |
|
std::ostream &do_response(std::ostream &os) const; |
|
|
|
protected: |
|
std::string body; |
|
headers response_headers; |
|
code status_code; |
|
}; |
|
}; |
|
|
|
#endif
|
|
|