From 32cb396083f966364bc9c3fd224bff5c8ca82ec7 Mon Sep 17 00:00:00 2001 From: Atul Bagga Date: Wed, 27 Oct 2021 16:42:03 +0530 Subject: [PATCH] Remove unnecessary formatting changes to reduce diff - introduced during merge due to auto format extension --- CONTRIBUTORS.txt | 1 + Release/include/cpprest/certificate_info.h | 75 ++- Release/include/cpprest/oauth2.h | 8 +- Release/include/cpprest/ws_client.h | 37 +- Release/src/http/client/http_client_impl.h | 2 +- .../src/http/client/http_client_winhttp.cpp | 558 +++++++++--------- 6 files changed, 332 insertions(+), 349 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 2db1003aa7..193e7b3511 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -23,6 +23,7 @@ LeonidCSIT kreuzerkrieg evanc Jesse Towner (jwtowner) +Atul Bagga (atbagga) Abinsula s.r.l. Gianfranco Costamagna (LocutusOfBorg) diff --git a/Release/include/cpprest/certificate_info.h b/Release/include/cpprest/certificate_info.h index 6939386542..09dd21553e 100644 --- a/Release/include/cpprest/certificate_info.h +++ b/Release/include/cpprest/certificate_info.h @@ -1,48 +1,45 @@ /*** -* ==++== -* -* Copyright (c) Microsoft Corporation. All rights reserved. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -* -* ==--== -* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ -* -* Certificate info -* -* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk -* -* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -****/ + * Copyright (C) Microsoft. All rights reserved. + * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. + * + * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ + * + * Certificate info + * + * For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk + * + * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + ****/ #pragma once -#include #include +#include -namespace web { namespace http { namespace client { - - using CertificateChain = std::vector>; - - struct certificate_info +namespace web +{ +namespace http +{ +namespace client +{ +using CertificateChain = std::vector>; + +struct certificate_info +{ + std::string host_name; + CertificateChain certificate_chain; + long certificate_error {0}; + bool verified {false}; + + certificate_info(const std::string host) : host_name(host) { } + certificate_info(const std::string host, CertificateChain chain, long error = 0) + : host_name(host), certificate_chain(chain), certificate_error(error) { - std::string host_name; - CertificateChain certificate_chain; - long certificate_error{ 0 }; - bool verified{ false }; - - certificate_info(const std::string host) : host_name(host) {} - certificate_info(const std::string host, CertificateChain chain, long error = 0) : host_name(host), certificate_chain(chain), certificate_error(error) {} - }; + } +}; - using CertificateChainFunction = std::function certificate_Info)>; +using CertificateChainFunction = std::function certificate_Info)>; -}}} +} // namespace client +} // namespace http +} // namespace web diff --git a/Release/include/cpprest/oauth2.h b/Release/include/cpprest/oauth2.h index 8ef685183c..733adc7a2f 100644 --- a/Release/include/cpprest/oauth2.h +++ b/Release/include/cpprest/oauth2.h @@ -59,8 +59,8 @@ namespace experimental class oauth2_exception : public std::exception { public: - oauth2_exception(utility::string_t msg) : m_msg(utility::conversions::to_utf8string(std::move(msg))) { } - ~oauth2_exception() CPPREST_NOEXCEPT { } + oauth2_exception(utility::string_t msg) : m_msg(utility::conversions::to_utf8string(std::move(msg))) {} + ~oauth2_exception() CPPREST_NOEXCEPT {} const char* what() const CPPREST_NOEXCEPT { return m_msg.c_str(); } private: @@ -503,7 +503,7 @@ class oauth2_config friend class web::http::client::http_client_config; friend class web::http::oauth2::details::oauth2_handler; - oauth2_config() : m_implicit_grant(false), m_bearer_auth(true), m_http_basic_auth(true) { } + oauth2_config() : m_implicit_grant(false), m_bearer_auth(true), m_http_basic_auth(true) {} _ASYNCRTIMP pplx::task _request_token(uri_builder& request_body); @@ -553,7 +553,7 @@ namespace details class oauth2_handler : public http_pipeline_stage { public: - oauth2_handler(std::shared_ptr cfg) : m_config(std::move(cfg)) { } + oauth2_handler(std::shared_ptr cfg) : m_config(std::move(cfg)) {} virtual pplx::task propagate(http_request request) override { diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index e90d19bced..bd52cbc8ba 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -228,7 +228,6 @@ class websocket_client_config } private: - http::client::CertificateChainFunction m_certificate_chain_callback; web::web_proxy m_proxy; web::credentials m_credentials; web::http::http_headers m_headers; @@ -237,8 +236,8 @@ class websocket_client_config bool m_validate_certificates; http::client::CertificateChainFunction m_certificate_chain_callback; -#if !defined(_WIN32) || - !defined(__cplusplus_winrt) std::function m_ssl_context_callback; +#if !defined(_WIN32) || !defined(__cplusplus_winrt) + std::function m_ssl_context_callback; #endif }; @@ -252,14 +251,14 @@ class websocket_exception : public std::exception /// Creates an websocket_exception with just a string message and no error code. /// /// Error message string. - websocket_exception(const utility::string_t& whatArg) : m_msg(utility::conversions::to_utf8string(whatArg)) { } + websocket_exception(const utility::string_t& whatArg) : m_msg(utility::conversions::to_utf8string(whatArg)) {} #ifdef _WIN32 /// /// Creates an websocket_exception with just a string message and no error code. /// /// Error message string. - websocket_exception(std::string whatArg) : m_msg(std::move(whatArg)) { } + websocket_exception(std::string whatArg) : m_msg(std::move(whatArg)) {} #endif /// @@ -349,9 +348,9 @@ namespace details class websocket_client_callback_impl { public: - websocket_client_callback_impl(websocket_client_config config) : m_config(std::move(config)) { } + websocket_client_callback_impl(websocket_client_config config) : m_config(std::move(config)) {} - virtual ~websocket_client_callback_impl() CPPREST_NOEXCEPT { } + virtual ~websocket_client_callback_impl() CPPREST_NOEXCEPT {} virtual pplx::task connect() = 0; @@ -443,7 +442,7 @@ class websocket_client /// /// Creates a new websocket_client. /// - websocket_client() : m_client(std::make_shared(websocket_client_config())) { } + websocket_client() : m_client(std::make_shared(websocket_client_config())) {} /// /// Creates a new websocket_client. @@ -467,19 +466,17 @@ class websocket_client m_client->callback_client()->verify_uri(uri); m_client->callback_client()->set_uri(uri); auto client = m_client; - return m_client->callback_client()->connect().then( - [client](pplx::task result) + return m_client->callback_client()->connect().then([client](pplx::task result) { + try + { + result.get(); + } + catch (const websocket_exception& ex) { - try - { - result.get(); - } - catch (const websocket_exception& ex) - { - client->close_pending_tasks_with_error(ex); - throw; - } - }); + client->close_pending_tasks_with_error(ex); + throw; + } + }); } /// diff --git a/Release/src/http/client/http_client_impl.h b/Release/src/http/client/http_client_impl.h index 853fb305a9..074e1f301f 100644 --- a/Release/src/http/client/http_client_impl.h +++ b/Release/src/http/client/http_client_impl.h @@ -53,7 +53,7 @@ class request_context { public: // Destructor to clean up any held resources. - virtual ~request_context() { } + virtual ~request_context() {} virtual void report_exception(std::exception_ptr exceptionPtr); diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index ae1ee90c37..8bdd252f25 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -14,8 +14,8 @@ ****/ #include "stdafx.h" -#include "../common/internal_http_helpers.h" #include "../common/x509_cert_utilities.h" +#include "../common/internal_http_helpers.h" #include "cpprest/http_headers.h" #include "http_client_impl.h" #ifdef WIN32 @@ -174,7 +174,7 @@ class memory_holder size_t m_size; public: - memory_holder() : m_externalData(nullptr), m_size(0) { } + memory_holder() : m_externalData(nullptr), m_size(0) {} void allocate_space(size_t length) { @@ -924,7 +924,7 @@ class winhttp_client final : public _http_client_communicator DWORD secure_protocols(WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2); if (!WinHttpSetOption( - m_hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, &secure_protocols, sizeof(secure_protocols))) + m_hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, &secure_protocols, sizeof(secure_protocols))) { return GetLastError(); } @@ -1161,8 +1161,8 @@ class winhttp_client final : public _http_client_communicator // And another 1 to enable the response (headers) of the rejected automatic redirect to be returned // rather than reporting an error "WinHttpReceiveResponse: 12156: The HTTP redirect request failed". DWORD maxRedirects = client_config().max_redirects() < MAXDWORD - 2 - ? static_cast(client_config().max_redirects() + 2) - : MAXDWORD; + ? static_cast(client_config().max_redirects() + 2) + : MAXDWORD; // Therefore, effective max redirects winhttp_context->m_remaining_redirects = maxRedirects - 2; @@ -1178,8 +1178,8 @@ class winhttp_client final : public _http_client_communicator // (Dis)allow HTTPS to HTTP redirects. DWORD redirectPolicy = client_config().https_to_http_redirects() - ? WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS - : WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP; + ? WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS + : WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP; if (!WinHttpSetOption(winhttp_context->m_request_handle, WINHTTP_OPTION_REDIRECT_POLICY, &redirectPolicy, @@ -1252,9 +1252,8 @@ class winhttp_client final : public _http_client_communicator if (msg._cancellation_token() != pplx::cancellation_token::none()) { // cancellation callback is unregistered when request is completed. - winhttp_context->m_cancellationRegistration = msg._cancellation_token().register_callback( - [weak_winhttp_context]() - { + winhttp_context->m_cancellationRegistration = + msg._cancellation_token().register_callback([weak_winhttp_context]() { // Call the WinHttpSendRequest API after WinHttpCloseHandle will give invalid handle error and we // throw this exception. Call the cleanup to make the m_request_handle as nullptr, otherwise, // Application Verifier will give AV exception on m_request_handle. @@ -1415,8 +1414,7 @@ class winhttp_client final : public _http_client_communicator p_request_context->allocate_request_space( nullptr, chunk_size + http::details::chunked_encoding::additional_encoding_space); - auto after_read = [p_request_context, chunk_size, &compressor](pplx::task op) - { + auto after_read = [p_request_context, chunk_size, &compressor](pplx::task op) { size_t bytes_read; try { @@ -1492,132 +1490,133 @@ class winhttp_client final : public _http_client_communicator if (compressor) { - auto do_compress = [p_request_context, chunk_size, &compressor](pplx::task op) -> pplx::task - { - size_t bytes_read; - - try - { - bytes_read = op.get(); - } - catch (...) + auto do_compress = + [p_request_context, chunk_size, &compressor](pplx::task op) -> pplx::task { - return pplx::task_from_exception(std::current_exception()); - } + size_t bytes_read; - uint8_t* buffer = p_request_context->m_compression_state.m_acquired; - if (buffer == nullptr) - { - buffer = p_request_context->m_compression_state.m_buffer.data(); - } + try + { + bytes_read = op.get(); + } + catch (...) + { + return pplx::task_from_exception(std::current_exception()); + } - web::http::compression::operation_hint hint = web::http::compression::operation_hint::has_more; + uint8_t* buffer = p_request_context->m_compression_state.m_acquired; + if (buffer == nullptr) + { + buffer = p_request_context->m_compression_state.m_buffer.data(); + } - if (bytes_read) - { - // An actual read always resets compression state for the next chunk - _ASSERTE(p_request_context->m_compression_state.m_bytes_processed == - p_request_context->m_compression_state.m_bytes_read); - _ASSERTE(!p_request_context->m_compression_state.m_needs_flush); - p_request_context->m_compression_state.m_bytes_read = bytes_read; - p_request_context->m_compression_state.m_bytes_processed = 0; - if (p_request_context->m_readBufferCopy) + web::http::compression::operation_hint hint = web::http::compression::operation_hint::has_more; + + if (bytes_read) { - // If we've been asked to keep a copy of the raw data for restarts, do so here, pre-compression - p_request_context->m_readBufferCopy->putn_nocopy(buffer, bytes_read).wait(); + // An actual read always resets compression state for the next chunk + _ASSERTE(p_request_context->m_compression_state.m_bytes_processed == + p_request_context->m_compression_state.m_bytes_read); + _ASSERTE(!p_request_context->m_compression_state.m_needs_flush); + p_request_context->m_compression_state.m_bytes_read = bytes_read; + p_request_context->m_compression_state.m_bytes_processed = 0; + if (p_request_context->m_readBufferCopy) + { + // If we've been asked to keep a copy of the raw data for restarts, do so here, pre-compression + p_request_context->m_readBufferCopy->putn_nocopy(buffer, bytes_read).wait(); + } + if (p_request_context->m_remaining_to_write == bytes_read) + { + // We've read to the end of the stream; finalize here if possible. We'll + // decrement the remaining count as we actually process the read buffer. + hint = web::http::compression::operation_hint::is_last; + } } - if (p_request_context->m_remaining_to_write == bytes_read) + else if (p_request_context->m_compression_state.m_needs_flush) { - // We've read to the end of the stream; finalize here if possible. We'll - // decrement the remaining count as we actually process the read buffer. + // All input has been consumed, but we still need to collect additional compressed output; + // this is done (in theory it can be multiple times) as a finalizing operation hint = web::http::compression::operation_hint::is_last; } - } - else if (p_request_context->m_compression_state.m_needs_flush) - { - // All input has been consumed, but we still need to collect additional compressed output; - // this is done (in theory it can be multiple times) as a finalizing operation - hint = web::http::compression::operation_hint::is_last; - } - else if (p_request_context->m_compression_state.m_bytes_processed == - p_request_context->m_compression_state.m_bytes_read) - { - if (p_request_context->m_remaining_to_write && - p_request_context->m_remaining_to_write != (std::numeric_limits::max)()) + else if (p_request_context->m_compression_state.m_bytes_processed == + p_request_context->m_compression_state.m_bytes_read) { - // The stream ended earlier than we detected it should - return pplx::task_from_exception(http_exception( - U("Unexpected end of request body stream encountered before expected length met."))); + if (p_request_context->m_remaining_to_write && + p_request_context->m_remaining_to_write != (std::numeric_limits::max)()) + { + // The stream ended earlier than we detected it should + return pplx::task_from_exception(http_exception( + U("Unexpected end of request body stream encountered before expected length met."))); + } + + // We think we're done; inform the compression library so it can finalize and/or give us any pending + // compressed bytes. Note that we may end up here multiple times if m_needs_flush is set, until all + // compressed bytes are drained. + hint = web::http::compression::operation_hint::is_last; } + // else we're still compressing bytes from the previous read - // We think we're done; inform the compression library so it can finalize and/or give us any pending - // compressed bytes. Note that we may end up here multiple times if m_needs_flush is set, until all - // compressed bytes are drained. - hint = web::http::compression::operation_hint::is_last; - } - // else we're still compressing bytes from the previous read - - _ASSERTE(p_request_context->m_compression_state.m_bytes_processed <= - p_request_context->m_compression_state.m_bytes_read); - - uint8_t* in = buffer + p_request_context->m_compression_state.m_bytes_processed; - size_t inbytes = p_request_context->m_compression_state.m_bytes_read - - p_request_context->m_compression_state.m_bytes_processed; - return compressor - ->compress(in, - inbytes, - &p_request_context->m_body_data.get()[http::details::chunked_encoding::data_offset], - chunk_size, - hint) - .then( - [p_request_context, bytes_read, hint, chunk_size]( - pplx::task op) -> pplx::task - { - http::compression::operation_result r; + _ASSERTE(p_request_context->m_compression_state.m_bytes_processed <= + p_request_context->m_compression_state.m_bytes_read); - try - { - r = op.get(); - } - catch (...) + uint8_t* in = buffer + p_request_context->m_compression_state.m_bytes_processed; + size_t inbytes = p_request_context->m_compression_state.m_bytes_read - + p_request_context->m_compression_state.m_bytes_processed; + return compressor + ->compress(in, + inbytes, + &p_request_context->m_body_data.get()[http::details::chunked_encoding::data_offset], + chunk_size, + hint) + .then( + [p_request_context, bytes_read, hint, chunk_size]( + pplx::task op) -> pplx::task { - return pplx::task_from_exception(std::current_exception()); - } + http::compression::operation_result r; - if (hint == web::http::compression::operation_hint::is_last) - { - // We're done reading all chunks, but the compressor may still have compressed bytes to - // drain from previous reads - _ASSERTE(r.done || r.output_bytes_produced == chunk_size); - p_request_context->m_compression_state.m_needs_flush = !r.done; - p_request_context->m_compression_state.m_done = r.done; - } + try + { + r = op.get(); + } + catch (...) + { + return pplx::task_from_exception(std::current_exception()); + } - // Update the number of bytes compressed in this read chunk; if it's been fully compressed, - // we'll reset m_bytes_processed and m_bytes_read after reading the next chunk - p_request_context->m_compression_state.m_bytes_processed += r.input_bytes_processed; - _ASSERTE(p_request_context->m_compression_state.m_bytes_processed <= - p_request_context->m_compression_state.m_bytes_read); - if (p_request_context->m_remaining_to_write != (std::numeric_limits::max)()) - { - _ASSERTE(p_request_context->m_remaining_to_write >= r.input_bytes_processed); - p_request_context->m_remaining_to_write -= r.input_bytes_processed; - } + if (hint == web::http::compression::operation_hint::is_last) + { + // We're done reading all chunks, but the compressor may still have compressed bytes to + // drain from previous reads + _ASSERTE(r.done || r.output_bytes_produced == chunk_size); + p_request_context->m_compression_state.m_needs_flush = !r.done; + p_request_context->m_compression_state.m_done = r.done; + } - if (p_request_context->m_compression_state.m_acquired != nullptr && - p_request_context->m_compression_state.m_bytes_processed == - p_request_context->m_compression_state.m_bytes_read) - { - // Release the acquired buffer back to the streambuf at the earliest possible point - p_request_context->_get_readbuffer().release( - p_request_context->m_compression_state.m_acquired, - p_request_context->m_compression_state.m_bytes_processed); - p_request_context->m_compression_state.m_acquired = nullptr; - } + // Update the number of bytes compressed in this read chunk; if it's been fully compressed, + // we'll reset m_bytes_processed and m_bytes_read after reading the next chunk + p_request_context->m_compression_state.m_bytes_processed += r.input_bytes_processed; + _ASSERTE(p_request_context->m_compression_state.m_bytes_processed <= + p_request_context->m_compression_state.m_bytes_read); + if (p_request_context->m_remaining_to_write != (std::numeric_limits::max)()) + { + _ASSERTE(p_request_context->m_remaining_to_write >= r.input_bytes_processed); + p_request_context->m_remaining_to_write -= r.input_bytes_processed; + } - return pplx::task_from_result(r.output_bytes_produced); - }); - }; + if (p_request_context->m_compression_state.m_acquired != nullptr && + p_request_context->m_compression_state.m_bytes_processed == + p_request_context->m_compression_state.m_bytes_read) + { + // Release the acquired buffer back to the streambuf at the earliest possible point + p_request_context->_get_readbuffer().release( + p_request_context->m_compression_state.m_acquired, + p_request_context->m_compression_state.m_bytes_processed); + p_request_context->m_compression_state.m_acquired = nullptr; + } + + return pplx::task_from_result(r.output_bytes_produced); + }); + }; if (p_request_context->m_compression_state.m_bytes_processed < p_request_context->m_compression_state.m_bytes_read || @@ -1738,45 +1737,43 @@ class winhttp_client final : public _http_client_communicator p_request_context->allocate_request_space(nullptr, safeCount); rbuf.getn(p_request_context->m_body_data.get(), safeCount) - .then( - [p_request_context, rbuf](pplx::task op) + .then([p_request_context, rbuf](pplx::task op) { + size_t read; + try { - size_t read; - try - { - read = op.get(); - } - catch (...) - { - p_request_context->report_exception(std::current_exception()); - return; - } - _ASSERTE(read != static_cast(-1)); + read = op.get(); + } + catch (...) + { + p_request_context->report_exception(std::current_exception()); + return; + } + _ASSERTE(read != static_cast(-1)); - if (read == 0) - { - p_request_context->report_exception(http_exception( - U("Unexpected end of request body stream encountered before Content-Length met."))); - return; - } + if (read == 0) + { + p_request_context->report_exception(http_exception( + U("Unexpected end of request body stream encountered before Content-Length met."))); + return; + } - p_request_context->m_remaining_to_write -= read; + p_request_context->m_remaining_to_write -= read; - // Stop writing chunks after this one if no more data. - if (p_request_context->m_remaining_to_write == 0) - { - p_request_context->m_bodyType = no_body; - } + // Stop writing chunks after this one if no more data. + if (p_request_context->m_remaining_to_write == 0) + { + p_request_context->m_bodyType = no_body; + } - if (!WinHttpWriteData(p_request_context->m_request_handle, - p_request_context->m_body_data.get(), - static_cast(read), - nullptr)) - { - auto errorCode = GetLastError(); - p_request_context->report_error(errorCode, build_error_msg(errorCode, "WinHttpWriteData")); - } - }); + if (!WinHttpWriteData(p_request_context->m_request_handle, + p_request_context->m_body_data.get(), + static_cast(read), + nullptr)) + { + auto errorCode = GetLastError(); + p_request_context->report_error(errorCode, build_error_msg(errorCode, "WinHttpWriteData")); + } + }); } } @@ -1983,7 +1980,6 @@ class winhttp_client final : public _http_client_communicator switch (statusCode) { - case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR: case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR: { WINHTTP_ASYNC_RESULT* error_result = reinterpret_cast(statusInfo); @@ -2485,8 +2481,7 @@ class winhttp_client final : public _http_client_communicator // Oddly enough, WinHttp doesn't de-chunk for us if "chunked" isn't the only // encoding, so we need to do so on the fly as we process the received data auto process_buffer = - [chunk_size](winhttp_request_context* c, size_t bytes_produced, bool outer) -> bool - { + [chunk_size](winhttp_request_context* c, size_t bytes_produced, bool outer) -> bool { if (!c->m_compression_state.m_chunk_bytes) { if (c->m_compression_state.m_chunked) @@ -2570,126 +2565,121 @@ class winhttp_client final : public _http_client_communicator return true; }; - pplx::details::_do_while( - [p_request_context, chunk_size, process_buffer]() -> pplx::task - { - uint8_t* buffer; + pplx::details::_do_while([p_request_context, chunk_size, process_buffer]() -> pplx::task { + uint8_t* buffer; - try - { - if (!process_buffer(p_request_context.get(), 0, true)) - { - // The chunked request has been completely processed (or contains no data in the - // first place) - return pplx::task_from_result(false); - } - } - catch (...) + try + { + if (!process_buffer(p_request_context.get(), 0, true)) { - // The outer do-while requires an explicit task return to activate the then() clause - return pplx::task_from_exception(std::current_exception()); + // The chunked request has been completely processed (or contains no data in the + // first place) + return pplx::task_from_result(false); } + } + catch (...) + { + // The outer do-while requires an explicit task return to activate the then() clause + return pplx::task_from_exception(std::current_exception()); + } - // If it's possible to know how much post-compression data we're expecting (for instance if - // we can discern how much total data the ostream can support, we could allocate (or at - // least attempt to acquire) based on that - p_request_context->m_compression_state.m_acquired = - p_request_context->_get_writebuffer().alloc(chunk_size); - if (p_request_context->m_compression_state.m_acquired) - { - buffer = p_request_context->m_compression_state.m_acquired; - } - else - { - // The streambuf couldn't accommodate our request; we'll use m_body_data's - // internal vector as temporary storage, then putn() to the caller's stream - p_request_context->allocate_reply_space(nullptr, chunk_size); - buffer = p_request_context->m_body_data.get(); - } + // If it's possible to know how much post-compression data we're expecting (for instance if + // we can discern how much total data the ostream can support, we could allocate (or at + // least attempt to acquire) based on that + p_request_context->m_compression_state.m_acquired = + p_request_context->_get_writebuffer().alloc(chunk_size); + if (p_request_context->m_compression_state.m_acquired) + { + buffer = p_request_context->m_compression_state.m_acquired; + } + else + { + // The streambuf couldn't accommodate our request; we'll use m_body_data's + // internal vector as temporary storage, then putn() to the caller's stream + p_request_context->allocate_reply_space(nullptr, chunk_size); + buffer = p_request_context->m_body_data.get(); + } - uint8_t* in = p_request_context->m_compression_state.m_buffer.data() + - p_request_context->m_compression_state.m_bytes_processed; - size_t inbytes = p_request_context->m_compression_state.m_chunk_bytes; - if (inbytes) - { - p_request_context->m_compression_state.m_started = true; - } - return p_request_context->m_decompressor - ->decompress( - in, inbytes, buffer, chunk_size, web::http::compression::operation_hint::has_more) - .then( - [p_request_context, buffer, chunk_size, process_buffer]( - pplx::task op) + uint8_t* in = p_request_context->m_compression_state.m_buffer.data() + + p_request_context->m_compression_state.m_bytes_processed; + size_t inbytes = p_request_context->m_compression_state.m_chunk_bytes; + if (inbytes) + { + p_request_context->m_compression_state.m_started = true; + } + return p_request_context->m_decompressor + ->decompress( + in, inbytes, buffer, chunk_size, web::http::compression::operation_hint::has_more) + .then( + [p_request_context, buffer, chunk_size, process_buffer]( + pplx::task op) + { + auto r = op.get(); + auto keep_going = + [&r, process_buffer](winhttp_request_context* c) -> pplx::task { - auto r = op.get(); - auto keep_going = - [&r, process_buffer](winhttp_request_context* c) -> pplx::task - { - _ASSERTE(r.input_bytes_processed <= c->m_compression_state.m_chunk_bytes); - c->m_compression_state.m_chunk_bytes -= r.input_bytes_processed; - c->m_compression_state.m_bytes_processed += r.input_bytes_processed; - c->m_compression_state.m_done = r.done; - - try - { - // See if we still have more work to do for this section and/or for the - // response in general - return pplx::task_from_result( - process_buffer(c, r.output_bytes_produced, false)); - } - catch (...) - { - return pplx::task_from_exception(std::current_exception()); - } - }; - - _ASSERTE(p_request_context->m_compression_state.m_bytes_processed + - r.input_bytes_processed <= - p_request_context->m_compression_state.m_bytes_read); + _ASSERTE(r.input_bytes_processed <= c->m_compression_state.m_chunk_bytes); + c->m_compression_state.m_chunk_bytes -= r.input_bytes_processed; + c->m_compression_state.m_bytes_processed += r.input_bytes_processed; + c->m_compression_state.m_done = r.done; - if (p_request_context->m_compression_state.m_acquired != nullptr) + try { - // We decompressed directly into the output stream - p_request_context->m_compression_state.m_acquired = nullptr; - p_request_context->_get_writebuffer().commit(r.output_bytes_produced); - return keep_going(p_request_context.get()); + // See if we still have more work to do for this section and/or for the + // response in general + return pplx::task_from_result( + process_buffer(c, r.output_bytes_produced, false)); } + catch (...) + { + return pplx::task_from_exception(std::current_exception()); + } + }; - // We decompressed into our own buffer; let the stream copy the data - return p_request_context->_get_writebuffer() - .putn_nocopy(buffer, r.output_bytes_produced) - .then( - [p_request_context, r, keep_going](pplx::task op) - { - if (op.get() != r.output_bytes_produced) - { - return pplx::task_from_exception(std::runtime_error( - "Response stream unexpectedly failed to write the " - "requested number of bytes")); - } - return keep_going(p_request_context.get()); - }); - }); - }) - .then( - [p_request_context](pplx::task op) - { - try - { - op.get(); - } - catch (...) - { - // We're only here to pick up any exception that may have been thrown, and to clean - // up if needed - if (p_request_context->m_compression_state.m_acquired) + _ASSERTE(p_request_context->m_compression_state.m_bytes_processed + + r.input_bytes_processed <= + p_request_context->m_compression_state.m_bytes_read); + + if (p_request_context->m_compression_state.m_acquired != nullptr) { - p_request_context->_get_writebuffer().commit(0); + // We decompressed directly into the output stream p_request_context->m_compression_state.m_acquired = nullptr; + p_request_context->_get_writebuffer().commit(r.output_bytes_produced); + return keep_going(p_request_context.get()); } - p_request_context->report_exception(std::current_exception()); + + // We decompressed into our own buffer; let the stream copy the data + return p_request_context->_get_writebuffer() + .putn_nocopy(buffer, r.output_bytes_produced) + .then( + [p_request_context, r, keep_going](pplx::task op) + { + if (op.get() != r.output_bytes_produced) + { + return pplx::task_from_exception(std::runtime_error( + "Response stream unexpectedly failed to write the " + "requested number of bytes")); + } + return keep_going(p_request_context.get()); + }); + }); + }).then([p_request_context](pplx::task op) { + try + { + op.get(); + } + catch (...) + { + // We're only here to pick up any exception that may have been thrown, and to clean + // up if needed + if (p_request_context->m_compression_state.m_acquired) + { + p_request_context->_get_writebuffer().commit(0); + p_request_context->m_compression_state.m_acquired = nullptr; } - }); + p_request_context->report_exception(std::current_exception()); + } + }); } else { @@ -2703,31 +2693,29 @@ class winhttp_client final : public _http_client_communicator else { writebuf.putn_nocopy(p_request_context->m_body_data.get(), bytesRead) - .then( - [hRequestHandle, p_request_context, bytesRead](pplx::task op) + .then([hRequestHandle, p_request_context, bytesRead](pplx::task op) { + size_t written = 0; + try { - size_t written = 0; - try - { - written = op.get(); - } - catch (...) - { - p_request_context->report_exception(std::current_exception()); - return; - } + written = op.get(); + } + catch (...) + { + p_request_context->report_exception(std::current_exception()); + return; + } - // If we couldn't write everything, it's time to exit. - if (written != bytesRead) - { - p_request_context->report_exception( - std::runtime_error("response stream unexpectedly failed to write the " - "requested number of bytes")); - return; - } + // If we couldn't write everything, it's time to exit. + if (written != bytesRead) + { + p_request_context->report_exception( + std::runtime_error("response stream unexpectedly failed to write the " + "requested number of bytes")); + return; + } - read_next_response_chunk(p_request_context.get(), bytesRead); - }); + read_next_response_chunk(p_request_context.get(), bytesRead); + }); } } return;