Skip to content

Commit

Permalink
Turn on HTTP/2 libcurl, WinInet and WinHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomagdy committed Apr 17, 2019
1 parent d0fe455 commit f024f1d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -26,6 +26,10 @@ endif()
if(POLICY CMP0056)
cmake_policy(SET CMP0056 NEW)
endif()
if(POLICY CMP0057)
cmake_policy(SET CMP0057 NEW) # support IN_LIST
endif()


# 3.0 or higher is strongly suggested; build settings (target_compile_options/etc...) sometimes do not get propagated properly under certain conditions prior to this version
# Making this a hard requirement is potentially disruptive to existing customers who aren't affected by the bad behavior though, so just warn for now
Expand Down
43 changes: 43 additions & 0 deletions aws-cpp-sdk-core/CMakeLists.txt
Expand Up @@ -84,10 +84,20 @@ file(GLOB UTILS_MEMORY_STL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/memo
file(GLOB UTILS_STREAM_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/stream/*.cpp")
file(GLOB UTILS_CRYPTO_FACTORY_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/crypto/factory/*.cpp")

include(CheckCXXSourceCompiles)
include(CheckCSourceRuns)

# http client implementations
if(ENABLE_CURL_CLIENT)
file(GLOB HTTP_CURL_CLIENT_HEADERS "include/aws/core/http/curl/*.h")
file(GLOB HTTP_CURL_CLIENT_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/http/curl/*.cpp")
set(CMAKE_REQUIRED_LIBRARIES "curl")
check_c_source_runs("
#include <curl/curl.h>
int main() {
CURL* handle = curl_easy_init();
return curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); }" CURL_HAS_H2)
unset(CMAKE_REQUIRED_LIBRARIES)
elseif(ENABLE_WINDOWS_CLIENT)
if(USE_IXML_HTTP_REQUEST_2)
set(HTTP_WINDOWS_CLIENT_HEADERS "include/aws/core/http/windows/IXmlHttpRequest2HttpClient.h")
Expand All @@ -109,6 +119,27 @@ elseif(ENABLE_WINDOWS_CLIENT)
else()
file(GLOB HTTP_WINDOWS_CLIENT_HEADERS "include/aws/core/http/windows/Win*.h")
file(GLOB HTTP_WINDOWS_CLIENT_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/http/windows/Win*.cpp")

# Strating with Windows 10, version 1507, WinINet supports HTTP2.
# https://docs.microsoft.com/en-us/windows/desktop/WinInet/option-flags#INTERNET_OPTION_ENABLE_HTTP_PROTOCOL
# Starting with Windows 10, version 1607, WinHttp supports HTTP2.
# https://docs.microsoft.com/en-us/windows/desktop/WinHttp/option-flags#WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL
check_cxx_source_compiles("
#include <Windows.h>
#include <winhttp.h>
int main() {
int x = WINHTTP_PROTOCOL_FLAG_HTTP2;
return x;
}" WINHTTP_HAS_H2)

check_cxx_source_compiles("
#include <Windows.h>
#include <WinInet.h>
int main() {
int x = HTTP_PROTOCOL_FLAG_HTTP2;
return x;
}" WININET_HAS_H2)

endif()
endif()

Expand Down Expand Up @@ -356,6 +387,18 @@ target_compile_definitions(${PROJECT_NAME} PUBLIC "AWS_SDK_VERSION_MAJOR=${AWSSD
target_compile_definitions(${PROJECT_NAME} PUBLIC "AWS_SDK_VERSION_MINOR=${AWSSDK_VERSION_MINOR}")
target_compile_definitions(${PROJECT_NAME} PUBLIC "AWS_SDK_VERSION_PATCH=${AWSSDK_VERSION_PATCH}")

if (WININET_HAS_H2)
target_compile_definitions(${PROJECT_NAME} PRIVATE "WININET_HAS_H2")
endif()

if (WINHTTP_HAS_H2)
target_compile_definitions(${PROJECT_NAME} PRIVATE "WINHTTP_HAS_H2")
endif()

if (CURL_HAS_H2)
target_compile_definitions(${PROJECT_NAME} PRIVATE "CURL_HAS_H2")
endif()

set(Core_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/include/")

if(PLATFORM_CUSTOM)
Expand Down
3 changes: 3 additions & 0 deletions aws-cpp-sdk-core/source/http/curl/CurlHandleContainer.cpp
Expand Up @@ -122,4 +122,7 @@ void CurlHandleContainer::SetDefaultOptionsOnHandle(CURL* handle)
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, m_enableTcpKeepAlive ? 1L : 0L);
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, m_tcpKeepAliveIntervalMs);
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, m_tcpKeepAliveIntervalMs);
#ifdef CURL_HAS_H2
curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
#endif
}
22 changes: 21 additions & 1 deletion aws-cpp-sdk-core/source/http/windows/WinHttpSyncHttpClient.cpp
Expand Up @@ -25,6 +25,7 @@
#include <aws/core/utils/Array.h>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
#include <aws/core/utils/ratelimiter/RateLimiterInterface.h>
#include <aws/core/utils/UnreferencedParam.h>

#include <Windows.h>
#include <winhttp.h>
Expand All @@ -39,6 +40,23 @@ using namespace Aws::Utils::Logging;

static const uint32_t HTTP_REQUEST_WRITE_BUFFER_LENGTH = 8192;

static void WinHttpEnableHttp2(void* handle)
{
#ifdef WINHTTP_HAS_H2
DWORD http2 = WINHTTP_PROTOCOL_FLAG_HTTP2;
if (!WinHttpSetOption(handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, &http2, sizeof(http2)))
{
AWS_LOGSTREAM_ERROR("WinHttpHttp2", "Failed to enable HTTP/2 on WinHttp handle: " << handle << ". Falling back to HTTP/1.1.");
}
else
{
AWS_LOGSTREAM_DEBUG("WinHttpHttp2", "HTTP/2 enabled on WinHttp handle: " << handle << ".");
}
#else
AWS_UNREFERENCED_PARAM(handle);
#endif
}

WinHttpSyncHttpClient::WinHttpSyncHttpClient(const ClientConfiguration& config) :
Base()
{
Expand Down Expand Up @@ -84,7 +102,7 @@ WinHttpSyncHttpClient::WinHttpSyncHttpClient(const ClientConfiguration& config)
{
AWS_LOGSTREAM_WARN(GetLogTag(), "Error setting timeouts " << GetLastError());
}

WinHttpEnableHttp2(GetOpenHandle());
m_verifySSL = config.verifySSL;
if (m_verifySSL)
{
Expand Down Expand Up @@ -160,6 +178,8 @@ void* WinHttpSyncHttpClient::OpenRequest(const Aws::Http::HttpRequest& request,
if (!WinHttpSetOption(hHttpRequest, WINHTTP_OPTION_DISABLE_FEATURE, &requestFlags, sizeof(requestFlags)))
AWS_LOGSTREAM_FATAL(GetLogTag(), "Failed to turn off redirects!");
}

WinHttpEnableHttp2(hHttpRequest);
return hHttpRequest;
}

Expand Down
21 changes: 20 additions & 1 deletion aws-cpp-sdk-core/source/http/windows/WinINetSyncHttpClient.cpp
Expand Up @@ -25,6 +25,7 @@
#include <aws/core/utils/memory/AWSMemory.h>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
#include <aws/core/utils/ratelimiter/RateLimiterInterface.h>
#include <aws/core/utils/UnreferencedParam.h>

#include <Windows.h>
#include <WinInet.h>
Expand All @@ -39,6 +40,23 @@ using namespace Aws::Utils::Logging;

static const uint32_t HTTP_REQUEST_WRITE_BUFFER_LENGTH = 8192;

static void WinINetEnableHttp2(void* handle)
{
#ifdef WININET_HAS_H2
DWORD http2 = HTTP_PROTOCOL_FLAG_HTTP2;
if (!InternetSetOptionA(handle, INTERNET_OPTION_ENABLE_HTTP_PROTOCOL, &http2, sizeof(http2)))
{
AWS_LOGSTREAM_ERROR("WinINetHttp2", "Failed to enable HTTP/2 on WinInet handle: " << handle << ". Falling back to HTTP/1.1.");
}
else
{
AWS_LOGSTREAM_DEBUG("WinINetHttp2", "HTTP/2 enabled on WinInet handle: " << handle << ".");
}
#else
AWS_UNREFERENCED_PARAM(handle);
#endif
}

WinINetSyncHttpClient::WinINetSyncHttpClient(const ClientConfiguration& config) :
Base()
{
Expand Down Expand Up @@ -76,7 +94,7 @@ WinINetSyncHttpClient::WinINetSyncHttpClient(const ClientConfiguration& config)

//override offline mode.
InternetSetOptionA(GetOpenHandle(), INTERNET_OPTION_IGNORE_OFFLINE, nullptr, 0);

WinINetEnableHttp2(GetOpenHandle());
if (!config.verifySSL)
{
AWS_LOGSTREAM_WARN(GetLogTag(), "Turning ssl unknown ca verification off.");
Expand Down Expand Up @@ -131,6 +149,7 @@ void* WinINetSyncHttpClient::OpenRequest(const Aws::Http::HttpRequest& request,
if (!m_proxyPassword.empty() && !InternetSetOptionA(hHttpRequest, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)m_proxyPassword.c_str(), (DWORD)m_proxyPassword.length()))
AWS_LOGSTREAM_FATAL(GetLogTag(), "Failed setting password for proxy with error code: " << GetLastError());
}
WinINetEnableHttp2(hHttpRequest);

return hHttpRequest;
}
Expand Down
3 changes: 1 addition & 2 deletions cmake/external_dependencies.cmake
@@ -1,5 +1,3 @@


# Zlib
if(PLATFORM_ANDROID AND ANDROID_BUILD_ZLIB)
set(BUILD_ZLIB 1)
Expand Down Expand Up @@ -117,6 +115,7 @@ if(NOT NO_HTTP_CLIENT)
set(CLIENT_LIBS_ABSTRACT_NAME Wininet winhttp)
message(STATUS "Http client: WinHttp")
endif()

else()
message(FATAL_ERROR "No http client available for target platform and client injection not enabled (-DNO_HTTP_CLIENT=ON)")
endif()
Expand Down

0 comments on commit f024f1d

Please sign in to comment.