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:
Vinnie Falco committed 2017-07-20 08:12:17 -07:00
1 parent f7211da154
commit e5cfe9c11f
14 files changed
+333 -108

No files matched your search

-65
View File
@@ -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
+8 -1
View File
@@ -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>
+118
View File
@@ -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