forked from boostorg/beast
Refactor treatment of status code and obsolete reason (API Change):
fix #398 A new enum status is added for the status code. The function obsolete_reason returns default reason phrasing. If a response has an empty reason, the serializer will automatically insert the default reason phrase for the status code.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include <beast/http/read.hpp>
|
||||
#include <beast/http/rfc7230.hpp>
|
||||
#include <beast/http/serializer.hpp>
|
||||
#include <beast/http/status.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/verb.hpp>
|
||||
#include <beast/http/write.hpp>
|
||||
|
||||
@@ -254,71 +254,6 @@ prepare(message<isRequest, Body, Fields>& msg,
|
||||
"invalid version for Connection: upgrade"});
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class = void>
|
||||
string_view
|
||||
reason_string(int status)
|
||||
{
|
||||
switch(status)
|
||||
{
|
||||
case 100: return "Continue";
|
||||
case 101: return "Switching Protocols";
|
||||
case 200: return "OK";
|
||||
case 201: return "Created";
|
||||
case 202: return "Accepted";
|
||||
case 203: return "Non-Authoritative Information";
|
||||
case 204: return "No Content";
|
||||
case 205: return "Reset Content";
|
||||
case 206: return "Partial Content";
|
||||
case 300: return "Multiple Choices";
|
||||
case 301: return "Moved Permanently";
|
||||
case 302: return "Found";
|
||||
case 303: return "See Other";
|
||||
case 304: return "Not Modified";
|
||||
case 305: return "Use Proxy";
|
||||
case 307: return "Temporary Redirect";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 402: return "Payment Required";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 405: return "Method Not Allowed";
|
||||
case 406: return "Not Acceptable";
|
||||
case 407: return "Proxy Authentication Required";
|
||||
case 408: return "Request Timeout";
|
||||
case 409: return "Conflict";
|
||||
case 410: return "Gone";
|
||||
case 411: return "Length Required";
|
||||
case 412: return "Precondition Failed";
|
||||
case 413: return "Request Entity Too Large";
|
||||
case 414: return "Request-URI Too Long";
|
||||
case 415: return "Unsupported Media Type";
|
||||
case 416: return "Requested Range Not Satisfiable";
|
||||
case 417: return "Expectation Failed";
|
||||
case 500: return "Internal Server Error";
|
||||
case 501: return "Not Implemented";
|
||||
case 502: return "Bad Gateway";
|
||||
case 503: return "Service Unavailable";
|
||||
case 504: return "Gateway Timeout";
|
||||
case 505: return "HTTP Version Not Supported";
|
||||
|
||||
case 306: return "<reserved>";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "<unknown-status>";
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
inline
|
||||
string_view const
|
||||
reason_string(int status)
|
||||
{
|
||||
return detail::reason_string(status);
|
||||
}
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
|
||||
@@ -9,9 +9,11 @@
|
||||
#define BEAST_HTTP_IMPL_SERIALIZER_IPP
|
||||
|
||||
#include <beast/http/error.hpp>
|
||||
#include <beast/http/status.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <ostream>
|
||||
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
namespace detail {
|
||||
@@ -41,7 +43,12 @@ write_start_line(std::ostream& os,
|
||||
case 10: os << "HTTP/1.0 "; break;
|
||||
case 11: os << "HTTP/1.1 "; break;
|
||||
}
|
||||
os << msg.status << " " << msg.reason() << "\r\n";
|
||||
auto const reason = msg.reason();
|
||||
if(reason.empty())
|
||||
os << msg.status << " " << msg.reason() << "\r\n";
|
||||
else
|
||||
os << msg.status << " " <<
|
||||
obsolete_reason(static_cast<status>(msg.status)) << "\r\n";
|
||||
}
|
||||
|
||||
template<class FieldSequence>
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_IMPL_STATUS_IPP
|
||||
#define BEAST_HTTP_IMPL_STATUS_IPP
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
namespace detail {
|
||||
|
||||
template<class = void>
|
||||
string_view
|
||||
status_to_string(int v)
|
||||
{
|
||||
switch(static_cast<status>(v))
|
||||
{
|
||||
// 1xx
|
||||
case status::continue_: return "Continue";
|
||||
case status::switching_protocols: return "Switching Protocols";
|
||||
case status::processing: return "Processing";
|
||||
|
||||
// 2xx
|
||||
case status::ok: return "OK";
|
||||
case status::created: return "Created";
|
||||
case status::accepted: return "Accepted";
|
||||
case status::non_authoritative_information: return "Non-Authoritative Information";
|
||||
case status::no_content: return "No Content";
|
||||
case status::reset_content: return "Reset Content";
|
||||
case status::partial_content: return "Partial Content";
|
||||
case status::multi_status: return "Multi-Status";
|
||||
case status::already_reported: return "Already Reported";
|
||||
case status::im_used: return "IM Used";
|
||||
|
||||
// 3xx
|
||||
case status::multiple_choices: return "Multiple Choices";
|
||||
case status::moved_permanently: return "Moved Permanently";
|
||||
case status::found: return "Found";
|
||||
case status::see_other: return "See Other";
|
||||
case status::not_modified: return "Not Modified";
|
||||
case status::use_proxy: return "Use Proxy";
|
||||
case status::temporary_redirect: return "Temporary Redirect";
|
||||
case status::permanent_redirect: return "Permanent Redirect";
|
||||
|
||||
// 4xx
|
||||
case status::bad_request: return "Bad Request";
|
||||
case status::unauthorized: return "Unauthorized";
|
||||
case status::payment_required: return "Payment Required";
|
||||
case status::forbidden: return "Forbidden";
|
||||
case status::not_found: return "Not Found";
|
||||
case status::method_not_allowed: return "Method Not Allowed";
|
||||
case status::not_acceptable: return "Not Acceptable";
|
||||
case status::proxy_authentication_required: return "Proxy Authentication Required";
|
||||
case status::request_timeout: return "Request Timeout";
|
||||
case status::conflict: return "Conflict";
|
||||
case status::gone: return "Gone";
|
||||
case status::length_required: return "Length Required";
|
||||
case status::precondition_failed: return "Precondition Failed";
|
||||
case status::payload_too_large: return "Payload Too Large";
|
||||
case status::uri_too_long: return "URI Too Long";
|
||||
case status::unsupported_media_type: return "Unsupported Media Type";
|
||||
case status::range_not_satisfiable: return "Range Not Satisfiable";
|
||||
case status::expectation_failed: return "Expectation Failed";
|
||||
case status::misdirected_request: return "Misdirected Request";
|
||||
case status::unprocessable_entity: return "Unprocessable Entity";
|
||||
case status::locked: return "Locked";
|
||||
case status::failed_dependency: return "Failed Dependency";
|
||||
case status::upgrade_required: return "Upgrade Required";
|
||||
case status::precondition_required: return "Precondition Required";
|
||||
case status::too_many_requests: return "Too Many Requests";
|
||||
case status::request_header_fields_too_large: return "Request Header Fields Too Large";
|
||||
case status::connection_closed_without_response: return "Connection Closed Without Response";
|
||||
case status::unavailable_for_legal_reasons: return "Unavailable For Legal Reasons";
|
||||
case status::client_closed_request: return "Client Closed Request";
|
||||
// 5xx
|
||||
case status::internal_server_error: return "Internal Server Error";
|
||||
case status::not_implemented: return "Not Implemented";
|
||||
case status::bad_gateway: return "Bad Gateway";
|
||||
case status::service_unavailable: return "Service Unavailable";
|
||||
case status::gateway_timeout: return "Gateway Timeout";
|
||||
case status::http_version_not_supported: return "HTTP Version Not Supported";
|
||||
case status::variant_also_negotiates: return "Variant Also Negotiates";
|
||||
case status::insufficient_storage: return "Insufficient Storage";
|
||||
case status::loop_detected: return "Loop Detected";
|
||||
case status::not_extended: return "Not Extended";
|
||||
case status::network_authentication_required: return "Network Authentication Required";
|
||||
case status::network_connect_timeout_error: return "Network Connect Timeout Error";
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "Unknown Status";
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
inline
|
||||
string_view
|
||||
obsolete_reason(status v)
|
||||
{
|
||||
return detail::status_to_string(
|
||||
static_cast<int>(v));
|
||||
}
|
||||
|
||||
inline
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, status v)
|
||||
{
|
||||
return os << obsolete_reason(v);
|
||||
}
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
@@ -527,10 +527,6 @@ void
|
||||
prepare(message<isRequest, Body, Fields>& msg,
|
||||
Options&&... options);
|
||||
|
||||
/** Returns the text for a known HTTP status code. */
|
||||
string_view const
|
||||
reason_string(int status);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_REASON_HPP
|
||||
#define BEAST_HTTP_REASON_HPP
|
||||
|
||||
#include <beast/config.hpp>
|
||||
#include <beast/core/string_view.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
namespace detail {
|
||||
|
||||
|
||||
} // detail
|
||||
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_STATUS_HPP
|
||||
#define BEAST_HTTP_STATUS_HPP
|
||||
|
||||
#include <beast/config.hpp>
|
||||
#include <beast/core/string_view.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
enum class status : int
|
||||
{
|
||||
continue_ = 100,
|
||||
switching_protocols = 101,
|
||||
processing = 102,
|
||||
|
||||
ok = 200,
|
||||
created = 201,
|
||||
accepted = 202,
|
||||
non_authoritative_information = 203,
|
||||
no_content = 204,
|
||||
reset_content = 205,
|
||||
partial_content = 206,
|
||||
multi_status = 207,
|
||||
already_reported = 208,
|
||||
im_used = 226,
|
||||
|
||||
multiple_choices = 300,
|
||||
moved_permanently = 301,
|
||||
found = 302,
|
||||
see_other = 303,
|
||||
not_modified = 304,
|
||||
use_proxy = 305,
|
||||
temporary_redirect = 307,
|
||||
permanent_redirect = 308,
|
||||
|
||||
bad_request = 400,
|
||||
unauthorized = 401,
|
||||
payment_required = 402,
|
||||
forbidden = 403,
|
||||
not_found = 404,
|
||||
method_not_allowed = 405,
|
||||
not_acceptable = 406,
|
||||
proxy_authentication_required = 407,
|
||||
request_timeout = 408,
|
||||
conflict = 409,
|
||||
gone = 410,
|
||||
length_required = 411,
|
||||
precondition_failed = 412,
|
||||
payload_too_large = 413,
|
||||
uri_too_long = 414,
|
||||
unsupported_media_type = 415,
|
||||
range_not_satisfiable = 416,
|
||||
expectation_failed = 417,
|
||||
misdirected_request = 421,
|
||||
unprocessable_entity = 422,
|
||||
locked = 423,
|
||||
failed_dependency = 424,
|
||||
upgrade_required = 426,
|
||||
precondition_required = 428,
|
||||
too_many_requests = 429,
|
||||
request_header_fields_too_large = 431,
|
||||
connection_closed_without_response = 444,
|
||||
unavailable_for_legal_reasons = 451,
|
||||
client_closed_request = 499,
|
||||
|
||||
internal_server_error = 500,
|
||||
not_implemented = 501,
|
||||
bad_gateway = 502,
|
||||
service_unavailable = 503,
|
||||
gateway_timeout = 504,
|
||||
http_version_not_supported = 505,
|
||||
variant_also_negotiates = 506,
|
||||
insufficient_storage = 507,
|
||||
loop_detected = 508,
|
||||
not_extended = 510,
|
||||
network_authentication_required = 511,
|
||||
network_connect_timeout_error = 599
|
||||
};
|
||||
|
||||
/// Returns the obsolete reason-phrase text for a status code.
|
||||
string_view
|
||||
obsolete_reason(status v);
|
||||
|
||||
/// Outputs the reason phrase of a status code to a stream.
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, status v);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#include <beast/http/impl/status.ipp>
|
||||
|
||||
#endif
|
||||
@@ -217,7 +217,6 @@ build_response(http::header<true, Fields> const& req,
|
||||
{
|
||||
response_type res;
|
||||
res.status = 400;
|
||||
res.reason(http::reason_string(res.status));
|
||||
res.version = req.version;
|
||||
res.body = text;
|
||||
prepare(res);
|
||||
@@ -248,7 +247,6 @@ build_response(http::header<true, Fields> const& req,
|
||||
{
|
||||
response_type res;
|
||||
res.status = 426;
|
||||
res.reason(http::reason_string(res.status));
|
||||
res.version = req.version;
|
||||
res.fields.insert("Sec-WebSocket-Version", "13");
|
||||
prepare(res);
|
||||
@@ -266,7 +264,6 @@ build_response(http::header<true, Fields> const& req,
|
||||
res.fields, unused, offer, pmd_opts_);
|
||||
}
|
||||
res.status = 101;
|
||||
res.reason(http::reason_string(res.status));
|
||||
res.version = req.version;
|
||||
res.fields.insert("Upgrade", "websocket");
|
||||
res.fields.insert("Connection", "upgrade");
|
||||
|
||||
Reference in New Issue
Block a user