From 4461f5153c939ade29f0a5a17ab3d2275ab614ec Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 7 May 2017 16:11:12 -0700 Subject: [PATCH] Tidy up HTTP reason_string (API Change): fix #182 reason_string() is now located in and returns a string view rather than char const*. --- CHANGELOG.md | 1 + include/beast/http.hpp | 2 - include/beast/http/impl/message.ipp | 66 +++++++++++++++++++++++++ include/beast/http/message.hpp | 4 ++ include/beast/http/reason.hpp | 61 +---------------------- include/beast/websocket/impl/stream.ipp | 1 - test/Jamfile | 1 - test/http/CMakeLists.txt | 1 - test/http/message.cpp | 8 +++ test/http/reason.cpp | 29 ----------- 10 files changed, 80 insertions(+), 94 deletions(-) delete mode 100644 test/http/reason.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 8aecdc36..e3cc1b10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ API Changes: * Rename to static_buffer, static_buffer_n * Rename to buffered_read_stream * Harmonize concepts and identifiers with net-ts +* Tidy up HTTP reason_string -------------------------------------------------------------------------------- diff --git a/include/beast/http.hpp b/include/beast/http.hpp index 6d0e41f6..031b752c 100644 --- a/include/beast/http.hpp +++ b/include/beast/http.hpp @@ -19,8 +19,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/include/beast/http/impl/message.ipp b/include/beast/http/impl/message.ipp index aa50cdbf..f04f70b9 100644 --- a/include/beast/http/impl/message.ipp +++ b/include/beast/http/impl/message.ipp @@ -9,6 +9,7 @@ #define BEAST_HTTP_IMPL_MESSAGE_IPP #include +#include #include #include #include @@ -257,6 +258,71 @@ prepare(message& msg, "invalid version for Connection: upgrade", __FILE__, __LINE__); } +namespace detail { + +template +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 ""; + default: + break; + } + return ""; +} + +} // detail + +inline +string_view const +reason_string(int status) +{ + return detail::reason_string(status); +} + } // http } // beast diff --git a/include/beast/http/message.hpp b/include/beast/http/message.hpp index bd04d08c..3353cdd7 100644 --- a/include/beast/http/message.hpp +++ b/include/beast/http/message.hpp @@ -533,6 +533,10 @@ void prepare(message& msg, Options&&... options); +/** Returns the text for a known HTTP status code. */ +string_view const +reason_string(int status); + } // http } // beast diff --git a/include/beast/http/reason.hpp b/include/beast/http/reason.hpp index fce97e9d..fb8d3a15 100644 --- a/include/beast/http/reason.hpp +++ b/include/beast/http/reason.hpp @@ -9,75 +9,16 @@ #define BEAST_HTTP_REASON_HPP #include +#include namespace beast { namespace http { namespace detail { -template -char const* -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 ""; - default: - break; - } - return ""; -} } // detail -/** Returns the text for a known status code integer. */ -inline -char const* -reason_string(int status) -{ - return detail::reason_string(status); -} } // http } // beast diff --git a/include/beast/websocket/impl/stream.ipp b/include/beast/websocket/impl/stream.ipp index e28d7be8..401e914f 100644 --- a/include/beast/websocket/impl/stream.ipp +++ b/include/beast/websocket/impl/stream.ipp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include diff --git a/test/Jamfile b/test/Jamfile index 3badfa9f..bfd83dcd 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -57,7 +57,6 @@ unit-test http-tests : http/message.cpp http/message_parser.cpp http/read.cpp - http/reason.cpp http/rfc7230.cpp http/string_body.cpp http/write.cpp diff --git a/test/http/CMakeLists.txt b/test/http/CMakeLists.txt index b259b905..2893b7da 100644 --- a/test/http/CMakeLists.txt +++ b/test/http/CMakeLists.txt @@ -20,7 +20,6 @@ add_executable (http-tests message.cpp message_parser.cpp read.cpp - reason.cpp rfc7230.cpp string_body.cpp write.cpp diff --git a/test/http/message.cpp b/test/http/message.cpp index 99513368..a650d593 100644 --- a/test/http/message.cpp +++ b/test/http/message.cpp @@ -296,6 +296,13 @@ public: }(); } + void + testReasonString() + { + for(int i = 1; i <= 999; ++i) + BEAST_EXPECT(! reason_string(i).empty()); + } + void run() override { @@ -305,6 +312,7 @@ public: testPrepare(); testSwap(); testSpecialMembers(); + testReasonString(); } }; diff --git a/test/http/reason.cpp b/test/http/reason.cpp deleted file mode 100644 index c5987d35..00000000 --- a/test/http/reason.cpp +++ /dev/null @@ -1,29 +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) -// - -// Test that header file is self-contained. -#include - -#include - -namespace beast { -namespace http { - -class reason_test : public unit_test::suite -{ -public: - void run() override - { - for(int i = 1; i <= 999; ++i) - BEAST_EXPECT(reason_string(i) != nullptr); - } -}; - -BEAST_DEFINE_TESTSUITE(reason,http,beast); - -} // http -} // beast