From 614fa143ba6fe7229c61983bea1744b289c22bb6 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Mon, 22 Jun 2020 18:23:30 -0700 Subject: [PATCH] adding host header automatically if missing for libcurl transport (#200) --- sdk/core/azure-core/inc/http/http.hpp | 2 ++ sdk/core/azure-core/src/http/curl/curl.cpp | 11 +++++++++++ sdk/core/azure-core/src/http/request.cpp | 2 ++ .../curl/src/azure_core_with_curl_bodyStream.cpp | 4 +--- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/sdk/core/azure-core/inc/http/http.hpp b/sdk/core/azure-core/inc/http/http.hpp index 160ac7cd1..719f6bc80 100644 --- a/sdk/core/azure-core/inc/http/http.hpp +++ b/sdk/core/azure-core/inc/http/http.hpp @@ -193,6 +193,7 @@ namespace Azure { namespace Core { namespace Http { return this->m_scheme + "://" + this->m_host + port + this->m_path; // TODO: add query params } std::string GetPath() const { return this->m_path; } + std::string GetHost() const { return this->m_host; } std::map GetQueryParameters() const { return this->m_queryParameters; @@ -259,6 +260,7 @@ namespace Azure { namespace Core { namespace Http { // Methods used by transport layer (and logger) to send request HttpMethod GetMethod() const; std::string GetEncodedUrl() const; // should call URL encode + std::string GetHost() const; std::map GetHeaders() const; BodyStream* GetBodyStream(); std::string GetHTTPMessagePreBody() const; diff --git a/sdk/core/azure-core/src/http/curl/curl.cpp b/sdk/core/azure-core/src/http/curl/curl.cpp index c5ca9a1d6..90a39aea5 100644 --- a/sdk/core/azure-core/src/http/curl/curl.cpp +++ b/sdk/core/azure-core/src/http/curl/curl.cpp @@ -45,6 +45,17 @@ CURLcode CurlSession::Perform(Context& context) return settingUp; } + // Make sure host is set + // TODO-> use isEqualNoCase here once it is merged + { + auto headers = this->m_request.GetHeaders(); + auto hostHeader = headers.find("Host"); + if (hostHeader == headers.end()) + { + this->m_request.AddHeader("Host", this->m_request.GetHost()); + } + } + settingUp = SetConnectOnly(); if (settingUp != CURLE_OK) { diff --git a/sdk/core/azure-core/src/http/request.cpp b/sdk/core/azure-core/src/http/request.cpp index a9fde7f00..c4b4629f7 100644 --- a/sdk/core/azure-core/src/http/request.cpp +++ b/sdk/core/azure-core/src/http/request.cpp @@ -69,6 +69,8 @@ std::string Request::GetEncodedUrl() const return m_url.ToString() + GetQueryString(); } +std::string Request::GetHost() const { return m_url.GetHost(); } + std::map Request::GetHeaders() const { // create map with retry headers witch are the most important and we don't want diff --git a/sdk/samples/http_client/curl/src/azure_core_with_curl_bodyStream.cpp b/sdk/samples/http_client/curl/src/azure_core_with_curl_bodyStream.cpp index c04ed0317..c6a4a3bfb 100644 --- a/sdk/samples/http_client/curl/src/azure_core_with_curl_bodyStream.cpp +++ b/sdk/samples/http_client/curl/src/azure_core_with_curl_bodyStream.cpp @@ -9,12 +9,12 @@ #include "http/pipeline.hpp" +#include #include #include #include #include #include -#include using namespace Azure::Core; using namespace Azure::Core::Http; @@ -121,7 +121,6 @@ Http::Request createPutRequest() request.AddHeader("other", "header2"); request.AddHeader("header", "value"); - request.AddHeader("Host", "httpbin.org"); request.AddHeader("Content-Length", std::to_string(BufferSize)); return request; @@ -148,7 +147,6 @@ Http::Request createPutStreamRequest() request.AddHeader("other", "header2"); request.AddHeader("header", "value"); - request.AddHeader("Host", "httpbin.org"); request.AddHeader("Content-Length", std::to_string(StreamSize)); request.AddQueryParameter("dinamicArg", "1");