diff --git a/HttpStatusCodes_C++.h b/HttpStatusCodes_C++.h index 4d58151..2b40a0e 100644 --- a/HttpStatusCodes_C++.h +++ b/HttpStatusCodes_C++.h @@ -13,6 +13,7 @@ #define HTTPSTATUSCODES_CPP_H_ #include +#include /*! Namespace for HTTP status codes and reason phrases. */ @@ -31,6 +32,7 @@ enum Code Continue = 100, //!< Indicates that the initial part of a request has been received and has not yet been rejected by the server. SwitchingProtocols = 101, //!< Indicates that the server understands and is willing to comply with the client's request, via the Upgrade header field, for a change in the application protocol being used on this connection. Processing = 102, //!< Is an interim response used to inform the client that the server has accepted the complete request, but has not yet completed it. + EarlyHints = 103, //!< Used to return some response headers before final HTTP message. /*####### 2xx - Successful #######*/ /* Indicates that the client's request was successfully received, @@ -55,7 +57,7 @@ enum Code Found = 302, //!< Indicates that the target resource resides temporarily under a different URI. SeeOther = 303, //!< Indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, that is intended to provide an indirect response to the original request. NotModified = 304, //!< Indicates that a conditional GET request has been received and would have resulted in a 200 (OK) response if it were not for the fact that the condition has evaluated to false. - UseProxy = 305, //!< \deprecated + UseProxy = 305, //!< The requested resource is available only through a proxy, the address for which is provided in the response. Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons. TemporaryRedirect = 307, //!< Indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. PermanentRedirect = 308, //!< The target resource has been assigned a new permanent URI and any future references to this resource outght to use one of the enclosed URIs. [...] This status code is similar to 301 Moved Permanently (Section 7.3.2 of rfc7231), except that it does not allow rewriting the request method from POST to GET. @@ -102,6 +104,8 @@ enum Code HTTPVersionNotSupported = 505, //!< Indicates that the server does not support, or refuses to support, the protocol version that was used in the request message. VariantAlsoNegotiates = 506, //!< Indicates that the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. InsufficientStorage = 507, //!< Means the method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request. + LoopDetected = 508, //!< The server detected an infinite loop while processing the request (sent in lieu of 208 Already Reported). [WebDAV; RFC 5842] + NotExtended = 510, //!< Further extensions to the request are required for the server to fulfill it. [RFC 2774] NetworkAuthenticationRequired = 511 //!< Indicates that the client needs to authenticate to gain network access. }; @@ -126,6 +130,7 @@ inline std::string reasonPhrase(int code) case 100: return "Continue"; case 101: return "Switching Protocols"; case 102: return "Processing"; + case 103: return "Early Hints"; //####### 2xx - Successful ####### case 200: return "OK"; @@ -145,6 +150,7 @@ inline std::string reasonPhrase(int code) case 303: return "See Other"; case 304: return "Not Modified"; case 305: return "Use Proxy"; + case 306: return "Switch Proxy"; case 307: return "Temporary Redirect"; case 308: return "Permanent Redirect"; @@ -186,12 +192,32 @@ inline std::string reasonPhrase(int code) case 505: return "HTTP Version Not Supported"; case 506: return "Variant Also Negotiates"; case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; case 511: return "Network Authentication Required"; default: return std::string(); } } +/*! Returns the code + standard HTTP reason phrase for a HTTP status code in the format "HTTP : " + * \param code An HTTP status code. + * \return The code plus standard HTTP reason phrase for the given \p code in the format "HTTP : " or "HTTP " + * if no standard phrase for the given \p code is known. + */ +inline std::string reasonPhraseEx(int code) +{ + std::stringstream ss; + + const std::string phrase = reasonPhrase(code); + if (phrase.empty()) + ss << "HTTP " << code; + else + ss << "HTTP " << code << ": " << phrase; + + return ss.str(); +} + } // namespace HttpStatus diff --git a/HttpStatusCodes_C++11.h b/HttpStatusCodes_C++11.h index c6074d0..1ba0e50 100644 --- a/HttpStatusCodes_C++11.h +++ b/HttpStatusCodes_C++11.h @@ -32,6 +32,7 @@ enum class Code Continue = 100, //!< Indicates that the initial part of a request has been received and has not yet been rejected by the server. SwitchingProtocols = 101, //!< Indicates that the server understands and is willing to comply with the client's request, via the Upgrade header field, for a change in the application protocol being used on this connection. Processing = 102, //!< Is an interim response used to inform the client that the server has accepted the complete request, but has not yet completed it. + EarlyHints = 103, //!< Used to return some response headers before final HTTP message. /*####### 2xx - Successful #######*/ /* Indicates that the client's request was successfully received, @@ -56,7 +57,7 @@ enum class Code Found = 302, //!< Indicates that the target resource resides temporarily under a different URI. SeeOther = 303, //!< Indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, that is intended to provide an indirect response to the original request. NotModified = 304, //!< Indicates that a conditional GET request has been received and would have resulted in a 200 (OK) response if it were not for the fact that the condition has evaluated to false. - UseProxy = 305, //!< \deprecated + UseProxy = 305, //!< The requested resource is available only through a proxy, the address for which is provided in the response. Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons. TemporaryRedirect = 307, //!< Indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. PermanentRedirect = 308, //!< The target resource has been assigned a new permanent URI and any future references to this resource outght to use one of the enclosed URIs. [...] This status code is similar to 301 Moved Permanently (Section 7.3.2 of rfc7231), except that it does not allow rewriting the request method from POST to GET. @@ -103,6 +104,8 @@ enum class Code HTTPVersionNotSupported = 505, //!< Indicates that the server does not support, or refuses to support, the protocol version that was used in the request message. VariantAlsoNegotiates = 506, //!< Indicates that the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. InsufficientStorage = 507, //!< Means the method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request. + LoopDetected = 508, //!< The server detected an infinite loop while processing the request (sent in lieu of 208 Already Reported). [WebDAV; RFC 5842] + NotExtended = 510, //!< Further extensions to the request are required for the server to fulfill it. [RFC 2774] NetworkAuthenticationRequired = 511 //!< Indicates that the client needs to authenticate to gain network access. }; @@ -134,6 +137,7 @@ inline std::string reasonPhrase(int code) case 100: return "Continue"; case 101: return "Switching Protocols"; case 102: return "Processing"; + case 103: return "Early Hints"; //####### 2xx - Successful ####### case 200: return "OK"; @@ -153,6 +157,7 @@ inline std::string reasonPhrase(int code) case 303: return "See Other"; case 304: return "Not Modified"; case 305: return "Use Proxy"; + case 306: return "Switch Proxy"; case 307: return "Temporary Redirect"; case 308: return "Permanent Redirect"; @@ -194,6 +199,8 @@ inline std::string reasonPhrase(int code) case 505: return "HTTP Version Not Supported"; case 506: return "Variant Also Negotiates"; case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; case 511: return "Network Authentication Required"; default: return std::string(); @@ -211,6 +218,33 @@ inline std::string reasonPhrase(Code code) return reasonPhrase(static_cast(code)); } +/*! Returns the code + standard HTTP reason phrase for a HTTP status code in the format "HTTP : " + * \param code An HTTP status code. + * \return The code plus standard HTTP reason phrase for the given \p code in the format "HTTP : " or "HTTP " + * if no standard phrase for the given \p code is known. + */ +inline std::string reasonPhraseEx(int code) +{ + const auto phrase = reasonPhrase(code); + if (phrase.empty()) + return "HTTP " + std::to_string(code); + else + return "HTTP " + std::to_string(code) + ": " + phrase; +} + +/*! \overload + * + * Returns the code + standard HTTP reason phrase for a HTTP status code in the format "HTTP : " + * \param code An HttpStatus::Code. + * \return The code plus standard HTTP reason phrase for the given \p code in the format "HTTP : " or "HTTP " + * if no standard phrase for the given \p code is known. + */ +inline std::string reasonPhraseEx(Code code) +{ + return reasonPhraseEx(static_cast(code)); +} + + } // namespace HttpStatus #endif /* HTTPSTATUSCODES_CPP11_H_ */ diff --git a/HttpStatusCodes_C.h b/HttpStatusCodes_C.h index e9b1c1c..58975f8 100644 --- a/HttpStatusCodes_C.h +++ b/HttpStatusCodes_C.h @@ -25,6 +25,7 @@ enum HttpStatus_Code HttpStatus_Continue = 100, /*!< Indicates that the initial part of a request has been received and has not yet been rejected by the server. */ //!< HttpStatus_Continue HttpStatus_SwitchingProtocols = 101, /*!< Indicates that the server understands and is willing to comply with the client's request, via the Upgrade header field, for a change in the application protocol being used on this connection. */ //!< HttpStatus_SwitchingProtocols HttpStatus_Processing = 102, /*!< Is an interim response used to inform the client that the server has accepted the complete request, but has not yet completed it. */ //!< HttpStatus_Processing + HttpStatus_EarlyHints = 103, /*!< Used to return some response headers before final HTTP message.*/ /*####### 2xx - Successful #######*/ /* Indicates that the client's request was successfully received, @@ -49,7 +50,7 @@ enum HttpStatus_Code HttpStatus_Found = 302, /*!< Indicates that the target resource resides temporarily under a different URI. */ //!< HttpStatus_Found HttpStatus_SeeOther = 303, /*!< Indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, that is intended to provide an indirect response to the original request. */ //!< HttpStatus_SeeOther HttpStatus_NotModified = 304, /*!< Indicates that a conditional GET request has been received and would have resulted in a 200 (OK) response if it were not for the fact that the condition has evaluated to false. */ //!< HttpStatus_NotModified - HttpStatus_UseProxy = 305, /*!< \deprecated */ //!< HttpStatus_UseProxy + HttpStatus_UseProxy = 305, /*!< The requested resource is available only through a proxy, the address for which is provided in the response. Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons. */ //!< HttpStatus_UseProxy HttpStatus_TemporaryRedirect = 307, /*!< Indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. */ //!< HttpStatus_TemporaryRedirect HttpStatus_PermanentRedirect = 308, /*!< The target resource has been assigned a new permanent URI and any future references to this resource outght to use one of the enclosed URIs. [...] This status code is similar to 301 Moved Permanently (Section 7.3.2 of rfc7231), except that it does not allow rewriting the request method from POST to GET. *///!< HttpStatus_PermanentRedirect @@ -96,6 +97,8 @@ enum HttpStatus_Code HttpStatus_HTTPVersionNotSupported = 505, /*!< Indicates that the server does not support, or refuses to support, the protocol version that was used in the request message. */ //!< HttpStatus_HTTPVersionNotSupported HttpStatus_VariantAlsoNegotiates = 506, /*!< Indicates that the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. */ //!< HttpStatus_VariantAlsoNegotiates HttpStatus_InsufficientStorage = 507, /*!< Means the method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request. */ //!< HttpStatus_InsufficientStorage + HttpStatus_LoopDetected = 508, /*!< The server detected an infinite loop while processing the request (sent in lieu of 208 Already Reported). [WebDAV; RFC 5842] */ + HttpStatus_NotExtended = 510, /*!< Further extensions to the request are required for the server to fulfill it. [RFC 2774] */ HttpStatus_NetworkAuthenticationRequired = 511 /*!< Indicates that the client needs to authenticate to gain network access. */ //!< HttpStatus_NetworkAuthenticationRequired }; @@ -120,6 +123,7 @@ static const char* HttpStatus_reasonPhrase(int code) case 100: return "Continue"; case 101: return "Switching Protocols"; case 102: return "Processing"; + case 103: return "Early Hints"; /*####### 2xx - Successful #######*/ case 200: return "OK"; @@ -139,6 +143,7 @@ static const char* HttpStatus_reasonPhrase(int code) case 303: return "See Other"; case 304: return "Not Modified"; case 305: return "Use Proxy"; + case 306: return "Switch Proxy"; case 307: return "Temporary Redirect"; case 308: return "Permanent Redirect"; @@ -180,11 +185,12 @@ static const char* HttpStatus_reasonPhrase(int code) case 505: return "HTTP Version Not Supported"; case 506: return "Variant Also Negotiates"; case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; case 511: return "Network Authentication Required"; default: return 0; } - } diff --git a/HttpStatusCodes_Qt.h b/HttpStatusCodes_Qt.h index 42f220b..1e7e9bc 100644 --- a/HttpStatusCodes_Qt.h +++ b/HttpStatusCodes_Qt.h @@ -41,6 +41,7 @@ enum Code Continue = 100, //!< Indicates that the initial part of a request has been received and has not yet been rejected by the server. SwitchingProtocols = 101, //!< Indicates that the server understands and is willing to comply with the client's request, via the Upgrade header field, for a change in the application protocol being used on this connection. Processing = 102, //!< Is an interim response used to inform the client that the server has accepted the complete request, but has not yet completed it. + EarlyHints = 103, //!< Used to return some response headers before final HTTP message. /*####### 2xx - Successful #######*/ /* Indicates that the client's request was successfully received, @@ -65,7 +66,7 @@ enum Code Found = 302, //!< Indicates that the target resource resides temporarily under a different URI. SeeOther = 303, //!< Indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, that is intended to provide an indirect response to the original request. NotModified = 304, //!< Indicates that a conditional GET request has been received and would have resulted in a 200 (OK) response if it were not for the fact that the condition has evaluated to false. - UseProxy = 305, //!< \deprecated + UseProxy = 305, //!< The requested resource is available only through a proxy, the address for which is provided in the response. Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons. TemporaryRedirect = 307, //!< Indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. PermanentRedirect = 308, //!< The target resource has been assigned a new permanent URI and any future references to this resource outght to use one of the enclosed URIs. [...] This status code is similar to 301 Moved Permanently (Section 7.3.2 of rfc7231), except that it does not allow rewriting the request method from POST to GET. @@ -112,6 +113,8 @@ enum Code HTTPVersionNotSupported = 505, //!< Indicates that the server does not support, or refuses to support, the protocol version that was used in the request message. VariantAlsoNegotiates = 506, //!< Indicates that the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. InsufficientStorage = 507, //!< Means the method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request. + LoopDetected = 508, //!< The server detected an infinite loop while processing the request (sent in lieu of 208 Already Reported). [WebDAV; RFC 5842] + NotExtended = 510, //!< Further extensions to the request are required for the server to fulfill it. [RFC 2774] NetworkAuthenticationRequired = 511 //!< Indicates that the client needs to authenticate to gain network access. }; #if (QT_VERSION >= 0x050800) @@ -140,6 +143,7 @@ inline QString reasonPhrase(int code) case 100: return QStringLiteral("Continue"); case 101: return QStringLiteral("Switching Protocols"); case 102: return QStringLiteral("Processing"); + case 103: return QStringLiteral("Early Hints"); //####### 2xx - Successful ####### case 200: return QStringLiteral("OK"); @@ -159,6 +163,7 @@ inline QString reasonPhrase(int code) case 303: return QStringLiteral("See Other"); case 304: return QStringLiteral("Not Modified"); case 305: return QStringLiteral("Use Proxy"); + case 306: return QStringLiteral("Switch Proxy"); case 307: return QStringLiteral("Temporary Redirect"); case 308: return QStringLiteral("Permanent Redirect"); @@ -200,6 +205,8 @@ inline QString reasonPhrase(int code) case 505: return QStringLiteral("HTTP Version Not Supported"); case 506: return QStringLiteral("Variant Also Negotiates"); case 507: return QStringLiteral("Insufficient Storage"); + case 508: return QStringLiteral("Loop Detected"); + case 510: return QStringLiteral("Not Extended"); case 511: return QStringLiteral("Network Authentication Required"); default: return QString(); diff --git a/tests/C++11VariantTest.cpp b/tests/C++11VariantTest.cpp index 454d39c..20b6570 100644 --- a/tests/C++11VariantTest.cpp +++ b/tests/C++11VariantTest.cpp @@ -30,6 +30,19 @@ TEST(ReasonPhraseTest, testIntegerOverload) ASSERT_EQ(HttpStatus::reasonPhrase(static_cast(HttpStatus::Code::ServiceUnavailable)), std::string("Service Unavailable")); } +TEST(ReasonPhraseTest, testEnumOverloadEx) +{ + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::Code::OK), std::string("HTTP 200: OK")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::Code::NotFound), std::string("HTTP 404: Not Found")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::Code::InternalServerError), std::string("HTTP 500: Internal Server Error")); +} + +TEST(ReasonPhraseTest, testIntegerOverloadEx) +{ + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::Code::Accepted)), std::string("HTTP 202: Accepted")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::Code::MethodNotAllowed)), std::string("HTTP 405: Method Not Allowed")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::Code::ServiceUnavailable)), std::string("HTTP 503: Service Unavailable")); +} //####### Category Tester Test ####### diff --git a/tests/C++VariantTest.cpp b/tests/C++VariantTest.cpp index d4e1d9d..cd1419f 100644 --- a/tests/C++VariantTest.cpp +++ b/tests/C++VariantTest.cpp @@ -30,6 +30,20 @@ TEST(ReasonPhraseTest, testIntegerParameter) ASSERT_EQ(HttpStatus::reasonPhrase(static_cast(HttpStatus::GatewayTimeout)), std::string("Gateway Time-out")); } +TEST(ReasonPhraseTest, testEnumParameterEx) +{ + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::OK), std::string("HTTP 200: OK")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::NotFound), std::string("HTTP 404: Not Found")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(HttpStatus::InternalServerError), std::string("HTTP 500: Internal Server Error")); +} + +TEST(ReasonPhraseTest, testIntegerParameterEx) +{ + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::Created)), std::string("HTTP 201: Created")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::Unauthorized)), std::string("HTTP 401: Unauthorized")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(static_cast(HttpStatus::GatewayTimeout)), std::string("HTTP 504: Gateway Time-out")); + ASSERT_EQ(HttpStatus::reasonPhraseEx(999), std::string("HTTP 999")); +} //####### Category Tester Test ####### INSTANTIATE_TEST_CASE_P(DefaultInstance, CategoryTesterTest, ::testing::Values(