From 38d3535a1e1cb4cc4ad4e4a25d108d27d8a0b19a Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Mon, 5 Apr 2021 20:53:05 -0700 Subject: [PATCH] Changed the `Azure::Core::Http::HttpMethod` regular enum into an extensible enum class. (#2048) * Changed the `Azure::Core::Http::HttpMethod` regular enum into an extensible enum class. * Update CL to mention removal of HttpMethodToString. * Delete the default http method ctor. --- sdk/core/azure-core/CHANGELOG.md | 1 + .../azure-core/inc/azure/core/http/http.hpp | 53 ++++++------------- sdk/core/azure-core/src/http/curl/curl.cpp | 2 +- sdk/core/azure-core/src/http/http.cpp | 7 +++ sdk/core/azure-core/src/http/log_policy.cpp | 2 +- .../src/http/winhttp/win_http_transport.cpp | 24 +++------ .../src/shared_key_policy.cpp | 2 +- 7 files changed, 35 insertions(+), 56 deletions(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index fb5789cf3..f70e1d009 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -21,6 +21,7 @@ - Removed `Azure::Core::PackageVersion`. - Removed from `Azure::Core::Http::Policies` namespace: `HttpPolicyOrder`, `TransportPolicy`, `RetryPolicy`, `RequestIdPolicy`, `TelemetryPolicy`, `BearerTokenAuthenticationPolicy`, `LogPolicy`. - Renamed `Azure::Core::Http::RawResponse::GetBodyStream()` to `ExtractBodyStream()`. +- Changed the `Azure::Core::Http::HttpMethod` regular enum into an extensible enum class and removed the `HttpMethodToString` helper method. - Introduced `Azure::Core::Context::Key` class which takes place of `std::string` used for `Azure::Core::Context` keys previously. ## 1.0.0-beta.7 (2021-03-11) diff --git a/sdk/core/azure-core/inc/azure/core/http/http.hpp b/sdk/core/azure-core/inc/azure/core/http/http.hpp index 4ae32163a..faa923b24 100644 --- a/sdk/core/azure-core/inc/azure/core/http/http.hpp +++ b/sdk/core/azure-core/inc/azure/core/http/http.hpp @@ -84,43 +84,24 @@ namespace Azure { namespace Core { namespace Http { /** * HTTP request method. */ - enum class HttpMethod - { - Get, ///< GET - Head, ///< HEAD - Post, ///< POST - Put, ///< PUT - Delete, ///< DELETE - Patch, ///< PATCH - }; + class HttpMethod { + public: + HttpMethod() = delete; + explicit HttpMethod(std::string value) : m_value(std::move(value)) {} + bool operator==(const HttpMethod& other) const { return m_value == other.m_value; } + bool operator!=(const HttpMethod& other) const { return !(*this == other); } + const std::string& ToString() const { return m_value; } - /** - * @brief Get a string representation for a value of #HttpMethod. - * - * @param method A value of #Azure::Core::Http::HttpMethod value. - * - * @return String name that corresponds to a value of #Azure::Core::Http::HttpMethod type. - */ - inline std::string HttpMethodToString(const HttpMethod& method) - { - switch (method) - { - case HttpMethod::Get: - return "GET"; - case HttpMethod::Head: - return "HEAD"; - case HttpMethod::Post: - return "POST"; - case HttpMethod::Put: - return "PUT"; - case HttpMethod::Delete: - return "DELETE"; - case HttpMethod::Patch: - return "PATCH"; - default: - return ""; - } - } + AZ_CORE_DLLEXPORT const static HttpMethod Get; + AZ_CORE_DLLEXPORT const static HttpMethod Head; + AZ_CORE_DLLEXPORT const static HttpMethod Post; + AZ_CORE_DLLEXPORT const static HttpMethod Put; + AZ_CORE_DLLEXPORT const static HttpMethod Delete; + AZ_CORE_DLLEXPORT const static HttpMethod Patch; + + private: + std::string m_value; + }; // extensible enum HttpMethod namespace Policies { namespace _internal { class RetryPolicy; diff --git a/sdk/core/azure-core/src/http/curl/curl.cpp b/sdk/core/azure-core/src/http/curl/curl.cpp index 4714e8535..14dceb182 100644 --- a/sdk/core/azure-core/src/http/curl/curl.cpp +++ b/sdk/core/azure-core/src/http/curl/curl.cpp @@ -154,7 +154,7 @@ void static inline SetHeader(Azure::Core::Http::RawResponse& response, std::stri // https://tools.ietf.org/html/rfc7230#section-3.1.1 static inline std::string GetHTTPMessagePreBody(Azure::Core::Http::Request const& request) { - std::string httpRequest(HttpMethodToString(request.GetMethod())); + std::string httpRequest(request.GetMethod().ToString()); // HTTP version hardcoded to 1.1 auto const url = request.GetUrl().GetRelativeUrl(); httpRequest += " /" + url + " HTTP/1.1\r\n"; diff --git a/sdk/core/azure-core/src/http/http.cpp b/sdk/core/azure-core/src/http/http.cpp index 4e8b3a177..6b9922f12 100644 --- a/sdk/core/azure-core/src/http/http.cpp +++ b/sdk/core/azure-core/src/http/http.cpp @@ -18,6 +18,13 @@ char const Azure::Core::Http::_internal::HttpShared::Accept[] = "accept"; char const Azure::Core::Http::_internal::HttpShared::MsRequestId[] = "x-ms-request-id"; char const Azure::Core::Http::_internal::HttpShared::MsClientRequestId[] = "x-ms-client-request-id"; +const HttpMethod HttpMethod::Get("GET"); +const HttpMethod HttpMethod::Head("HEAD"); +const HttpMethod HttpMethod::Post("POST"); +const HttpMethod HttpMethod::Put("PUT"); +const HttpMethod HttpMethod::Delete("DELETE"); +const HttpMethod HttpMethod::Patch("PATCH"); + void Azure::Core::Http::_detail::RawResponseHelpers::InsertHeaderWithValidation( Azure::Core::CaseInsensitiveMap& headers, std::string const& headerName, diff --git a/sdk/core/azure-core/src/http/log_policy.cpp b/sdk/core/azure-core/src/http/log_policy.cpp index 9c3f48c7a..7c58d3911 100644 --- a/sdk/core/azure-core/src/http/log_policy.cpp +++ b/sdk/core/azure-core/src/http/log_policy.cpp @@ -42,7 +42,7 @@ inline std::string GetRequestLogMessage(LogOptions const& options, Request const auto const& requestUrl = request.GetUrl(); std::ostringstream log; - log << "HTTP Request : " << HttpMethodToString(request.GetMethod()) << " " + log << "HTTP Request : " << request.GetMethod().ToString() << " " << requestUrl.GetUrlWithoutQuery(); { auto encodedRequestQueryParams = requestUrl.GetQueryParameters(); diff --git a/sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp b/sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp index 2dcd776da..a793dc58e 100644 --- a/sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp +++ b/sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp @@ -22,23 +22,13 @@ inline std::wstring HttpMethodToWideString(HttpMethod method) // This string should be all uppercase. // Many servers treat HTTP verbs as case-sensitive, and the Internet Engineering Task Force (IETF) // Requests for Comments (RFCs) spell these verbs using uppercase characters only. - switch (method) - { - case HttpMethod::Get: - return L"GET"; - case HttpMethod::Head: - return L"HEAD"; - case HttpMethod::Post: - return L"POST"; - case HttpMethod::Put: - return L"PUT"; - case HttpMethod::Delete: - return L"DELETE"; - case HttpMethod::Patch: - return L"PATCH"; - default: - throw Azure::Core::Http::TransportException("Invalid or unsupported HTTP method."); - } + + std::string httpMethodString = method.ToString(); + + // Assuming ASCII here is OK since the input is expected to be an HTTP method string. + // Converting this way is only safe when the text is ASCII. + std::wstring wideStr(httpMethodString.begin(), httpMethodString.end()); + return wideStr; } // Convert a UTF-8 string to a wide Unicode string. diff --git a/sdk/storage/azure-storage-common/src/shared_key_policy.cpp b/sdk/storage/azure-storage-common/src/shared_key_policy.cpp index 382e8c929..31a866394 100644 --- a/sdk/storage/azure-storage-common/src/shared_key_policy.cpp +++ b/sdk/storage/azure-storage-common/src/shared_key_policy.cpp @@ -16,7 +16,7 @@ namespace Azure { namespace Storage { namespace _internal { std::string SharedKeyPolicy::GetSignature(const Core::Http::Request& request) const { std::string string_to_sign; - string_to_sign += Azure::Core::Http::HttpMethodToString(request.GetMethod()) + "\n"; + string_to_sign += request.GetMethod().ToString() + "\n"; const auto& headers = request.GetHeaders(); for (std::string headerName :