21 return "Unauthorized";
23 return "Payment Required";
29 return "Method Not Allowed";
31 return "Not Acceptable";
33 return "Proxy Authentication Required";
35 return "Request Timeout";
41 return "Length Required";
43 return "Precondition Failed";
45 return "Payload Too Large";
47 return "URI Too Long";
49 return "Unsupported Media Type";
51 return "Range Not Satisfiable";
53 return "Expectation Failed";
55 return "I'm a teapot";
57 return "Misdirected Request";
59 return "Unprocessable Entity";
63 return "Failed Dependency";
67 return "Upgrade Required";
69 return "Precondition Required";
71 return "Too Many Requests";
73 return "Request Header Fields Too Large";
75 return "Unavailable For Legal Reasons";
79 return "Internal Server Error";
81 return "Not Implemented";
85 return "Service Unavailable";
87 return "Gateway Timeout";
89 return "HTTP Version Not Supported";
91 return "Variant Also Negotiates";
93 return "Insufficient Storage";
95 return "Loop Detected";
97 return "Not Extended";
99 return "Network Authentication Required";
101 return "Unknown Error";
108 std::ostringstream oss;
135 if (
static_cast<unsigned char>(c) < 0x20)
137 oss <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
138 <<
static_cast<int>(c);
153 std::ostringstream oss;
188 std::ostringstream json;
190 json <<
" \"type\": \"about:blank\",\n";
192 json <<
" \"status\": " << response.
status_code <<
",\n";
196 if (!
error.request_id.empty())
202 auto time_t_val = std::chrono::system_clock::to_time_t(
error.timestamp);
205 gmtime_s(&tm_val, &time_t_val);
207 gmtime_r(&time_t_val, &tm_val);
210 std::strftime(time_buf,
sizeof(time_buf),
"%Y-%m-%dT%H:%M:%SZ", &tm_val);
211 json <<
",\n \"timestamp\": \"" << time_buf <<
"\"";
216 response.
set_header(
"Content-Type",
"application/problem+json; charset=utf-8");
227 std::ostringstream html;
228 html <<
"<!DOCTYPE html>\n";
229 html <<
"<html lang=\"en\">\n";
231 html <<
" <meta charset=\"utf-8\">\n";
232 html <<
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";
235 html <<
" <style>\n";
236 html <<
" body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; "
237 <<
"margin: 40px auto; max-width: 650px; line-height: 1.6; color: #333; padding: 0 10px; }\n";
238 html <<
" h1 { color: #c00; border-bottom: 2px solid #eee; padding-bottom: 10px; }\n";
239 html <<
" .detail { background: #f9f9f9; padding: 15px; border-radius: 5px; }\n";
240 html <<
" .meta { color: #666; font-size: 0.9em; margin-top: 20px; }\n";
241 html <<
" </style>\n";
247 if (!
error.message.empty())
252 if (!
error.detail.empty())
254 html <<
" <div class=\"detail\">\n";
259 html <<
" <div class=\"meta\">\n";
260 if (!
error.request_id.empty())
264 html <<
" <p><em>NetworkSystem HTTP Server</em></p>\n";
270 response.
set_header(
"Content-Type",
"text/html; charset=utf-8");
281 error.detail = detail;
282 error.request_id = request_id;
283 error.timestamp = std::chrono::system_clock::now();
static auto build_json_error(const http_error &error) -> http_response
Build JSON format error response (RFC 7807)
static auto build_html_error(const http_error &error) -> http_response
Build HTML format error response.
static auto make_error(http_error_code code, const std::string &detail="", const std::string &request_id="") -> http_error
Create http_error from error code.
auto get_error_status_text(http_error_code code) -> std::string_view
Get status text for HTTP error code.
http_error_code
Standard HTTP error codes (RFC 7231)
@ unavailable_for_legal_reasons
@ proxy_authentication_required
@ request_header_fields_too_large
@ variant_also_negotiates
@ network_authentication_required
@ http_version_not_supported
static auto escape_html_string(const std::string &input) -> std::string
static auto escape_json_string(const std::string &input) -> std::string
@ error
Black hole detected, reset to base.
Structured HTTP error information.
Represents an HTTP response message.
auto set_body_string(const std::string &content) -> void
Set body from string.
auto set_header(const std::string &name, const std::string &value) -> void
Set a header value.
std::string status_message