2 changed files with 80 additions and 27 deletions
@ -0,0 +1,73 @@ |
|||||||
|
#include <http.hpp> |
||||||
|
|
||||||
|
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}}); |
||||||
|
} |
||||||
Loading…
Reference in new issue