Skip to content

Commit aff4a1a

Browse files
ras0219ras0219-msft
authored andcommitted
Refactor to remove web::http::details::http_network_handler.
1 parent a0a0f8d commit aff4a1a

File tree

7 files changed

+79
-95
lines changed

7 files changed

+79
-95
lines changed

Release/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
160160
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
161161
message("-- Setting msvc options")
162162
set(WARNINGS)
163+
add_compile_options(/bigobj)
163164
else()
164165
message("-- Unknown compiler, success is doubtful.")
165166
message("CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}")

Release/include/cpprest/http_client.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,6 @@ class http_client
720720

721721
private:
722722

723-
void build_pipeline(const uri &base_uri, const http_client_config &client_config);
724-
725723
std::shared_ptr<::web::http::http_pipeline> m_pipeline;
726724
};
727725

Release/src/http/client/http_client.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,32 +282,23 @@ void http_client::add_handler(const std::shared_ptr<http::http_pipeline_stage> &
282282
m_pipeline->append(stage);
283283
}
284284

285-
http_client::http_client(const uri &base_uri)
286-
{
287-
build_pipeline(base_uri, http_client_config());
288-
}
285+
http_client::http_client(const uri &base_uri) : http_client(base_uri, http_client_config())
286+
{}
289287

290288
http_client::http_client(const uri &base_uri, const http_client_config &client_config)
291-
{
292-
build_pipeline(base_uri, client_config);
293-
}
294-
295-
http_client::~http_client() CPPREST_NOEXCEPT {}
296-
297-
void http_client::build_pipeline(const uri &base_uri, const http_client_config &client_config)
298289
{
299290
if (base_uri.scheme().empty())
300291
{
301292
auto uribuilder = uri_builder(base_uri);
302293
uribuilder.set_scheme(_XPLATSTR("http"));
303294
uri uriWithScheme = uribuilder.to_uri();
304295
verify_uri(uriWithScheme);
305-
m_pipeline = ::web::http::http_pipeline::create_pipeline(std::make_shared<details::http_network_handler>(uriWithScheme, client_config));
296+
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(uriWithScheme, client_config));
306297
}
307298
else
308299
{
309300
verify_uri(base_uri);
310-
m_pipeline = ::web::http::http_pipeline::create_pipeline(std::make_shared<details::http_network_handler>(base_uri, client_config));
301+
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(base_uri, client_config));
311302
}
312303

313304
#if !defined(CPPREST_TARGET_XP)
@@ -319,16 +310,18 @@ void http_client::build_pipeline(const uri &base_uri, const http_client_config &
319310
std::make_shared<oauth2::details::oauth2_handler>(client_config.oauth2())));
320311
}
321312

313+
http_client::~http_client() CPPREST_NOEXCEPT {}
314+
322315
const http_client_config & http_client::client_config() const
323316
{
324-
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
325-
return ph->http_client_impl()->client_config();
317+
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
318+
return ph->client_config();
326319
}
327320

328321
const uri & http_client::base_uri() const
329322
{
330-
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
331-
return ph->http_client_impl()->base_uri();
323+
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
324+
return ph->base_uri();
332325
}
333326

334327

Release/src/http/client/http_client_asio.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@
4444
#endif
4545

4646
#include "http_client_impl.h"
47+
#include "cpprest/base_uri.h"
4748
#include "cpprest/details/x509_cert_utilities.h"
4849
#include <unordered_set>
4950

5051
using boost::asio::ip::tcp;
5152

53+
#define CRLF std::string("\r\n")
54+
5255
namespace web { namespace http
5356
{
5457
namespace client
@@ -227,8 +230,6 @@ class asio_connection
227230
m_is_reused = true;
228231
}
229232

230-
void handle_pool_timer(const boost::system::error_code& ec);
231-
232233
// Guards concurrent access to socket/ssl::stream. This is necessary
233234
// because timeouts and cancellation can touch the socket at the same time
234235
// as normal message processing.
@@ -331,7 +332,7 @@ class asio_connection_pool
331332

332333

333334

334-
class asio_client : public _http_client_communicator, public std::enable_shared_from_this<asio_client>
335+
class asio_client : public _http_client_communicator
335336
{
336337
public:
337338
asio_client(http::uri address, http_client_config client_config)
@@ -347,6 +348,8 @@ class asio_client : public _http_client_communicator, public std::enable_shared_
347348

348349
unsigned long open() override { return 0; }
349350

351+
virtual pplx::task<http_response> propagate(http_request request) override;
352+
350353
asio_connection_pool m_pool;
351354
tcp::resolver m_resolver;
352355
};
@@ -656,7 +659,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
656659
extra_headers.append(": no-cache" + CRLF);
657660
}
658661

659-
request_stream << flatten_http_headers(ctx->m_request.headers());
662+
request_stream << ::web::http::details::flatten_http_headers(ctx->m_request.headers());
660663
request_stream << extra_headers;
661664
// Enforce HTTP connection keep alive (even for the old HTTP/1.0 protocol).
662665
request_stream << "Connection: Keep-Alive" << CRLF << CRLF;
@@ -1446,22 +1449,9 @@ class asio_context : public request_context, public std::enable_shared_from_this
14461449
};
14471450

14481451

1449-
1450-
http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
1451-
m_http_client_impl(std::make_shared<asio_client>(base_uri, client_config))
1452-
{}
1453-
1454-
pplx::task<http_response> http_network_handler::propagate(http_request request)
1452+
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
14551453
{
1456-
auto context = details::asio_context::create_request_context(m_http_client_impl, request);
1457-
1458-
// Use a task to externally signal the final result and completion of the task.
1459-
auto result_task = pplx::create_task(context->m_request_completion);
1460-
1461-
// Asynchronously send the response with the HTTP client implementation.
1462-
m_http_client_impl->async_send_request(context);
1463-
1464-
return result_task;
1454+
return std::make_shared<asio_client>(base_uri, client_config);
14651455
}
14661456

14671457
void asio_client::send_request(const std::shared_ptr<request_context> &request_ctx)
@@ -1488,4 +1478,18 @@ void asio_client::send_request(const std::shared_ptr<request_context> &request_c
14881478
ctx->start_request();
14891479
}
14901480

1481+
pplx::task<http_response> asio_client::propagate(http_request request)
1482+
{
1483+
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
1484+
auto context = details::asio_context::create_request_context(self, request);
1485+
1486+
// Use a task to externally signal the final result and completion of the task.
1487+
auto result_task = pplx::create_task(context->m_request_completion);
1488+
1489+
// Asynchronously send the response with the HTTP client implementation.
1490+
this->async_send_request(context);
1491+
1492+
return result_task;
1493+
}
1494+
14911495
}}}} // namespaces

Release/src/http/client/http_client_impl.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class request_context
9696
// Interface used by client implementations. Concrete implementations are responsible for
9797
// sending HTTP requests and receiving the responses.
9898
//
99-
class _http_client_communicator
99+
class _http_client_communicator : public http_pipeline_stage
100100
{
101101
public:
102102

@@ -143,26 +143,9 @@ class _http_client_communicator
143143
int m_scheduled;
144144
};
145145

146-
class http_network_handler : public http_pipeline_stage
147-
{
148-
public:
149-
/// <summary>
150-
/// The constructor is separately defined by each subsystem (winhttp, winrt, asio) to create the platform-specific _http_client_communicator.
151-
/// </summary>
152-
http_network_handler(const uri &base_uri, const http_client_config &client_config);
153-
154-
/// <summary>
155-
/// This method is separately defined by each subsystem (winhttp, winrt, asio) to enable the platform-specific handling behavior.
156-
/// </summary>
157-
virtual pplx::task<http_response> propagate(http_request request) override;
158-
159-
const std::shared_ptr<details::_http_client_communicator>& http_client_impl() const
160-
{
161-
return m_http_client_impl;
162-
}
163-
164-
private:
165-
std::shared_ptr<_http_client_communicator> m_http_client_impl;
166-
};
146+
/// <summary>
147+
/// Factory function implemented by the separate platforms to construct their subclasses of _http_client_communicator
148+
/// </summary>
149+
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config);
167150

168151
}}}}

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ class winhttp_client : public _http_client_communicator
312312
{
313313
public:
314314
winhttp_client(http::uri address, http_client_config client_config)
315-
: _http_client_communicator(std::move(address), std::move(client_config)), m_secure(m_uri.scheme() == _XPLATSTR("https")), m_hSession(nullptr), m_hConnection(nullptr) { }
315+
: _http_client_communicator(std::move(address), std::move(client_config))
316+
, m_secure(m_uri.scheme() == _XPLATSTR("https"))
317+
, m_hSession(nullptr)
318+
, m_hConnection(nullptr) { }
316319

317320
// Closes session.
318321
~winhttp_client()
@@ -335,6 +338,20 @@ class winhttp_client : public _http_client_communicator
335338
}
336339
}
337340

341+
virtual pplx::task<http_response> propagate(http_request request) override
342+
{
343+
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
344+
auto context = details::winhttp_request_context::create_request_context(self, request);
345+
346+
// Use a task to externally signal the final result and completion of the task.
347+
auto result_task = pplx::create_task(context->m_request_completion);
348+
349+
// Asynchronously send the response with the HTTP client implementation.
350+
this->async_send_request(context);
351+
352+
return result_task;
353+
}
354+
338355
protected:
339356

340357
unsigned long report_failure(const utility::string_t& errorMessage)
@@ -1274,26 +1291,13 @@ class winhttp_client : public _http_client_communicator
12741291
bool m_secure;
12751292

12761293
// No copy or assignment.
1277-
winhttp_client(const winhttp_client&);
1278-
winhttp_client &operator=(const winhttp_client&);
1294+
winhttp_client(const winhttp_client&) = delete;
1295+
winhttp_client &operator=(const winhttp_client&) = delete;
12791296
};
12801297

1281-
http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
1282-
m_http_client_impl(std::make_shared<details::winhttp_client>(base_uri, client_config))
1298+
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
12831299
{
1284-
}
1285-
1286-
pplx::task<http_response> http_network_handler::propagate(http_request request)
1287-
{
1288-
auto context = details::winhttp_request_context::create_request_context(m_http_client_impl, request);
1289-
1290-
// Use a task to externally signal the final result and completion of the task.
1291-
auto result_task = pplx::create_task(context->m_request_completion);
1292-
1293-
// Asynchronously send the response with the HTTP client implementation.
1294-
m_http_client_impl->async_send_request(context);
1295-
1296-
return result_task;
1300+
return std::make_shared<details::winhttp_client>(std::move(base_uri), client_config);
12971301
}
12981302

12991303
}}}}

Release/src/http/client/http_client_winrt.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ class winrt_client : public _http_client_communicator
359359
winrt_client(http::uri address, http_client_config client_config)
360360
: _http_client_communicator(std::move(address), std::move(client_config)) { }
361361

362+
virtual pplx::task<http_response> propagate(http_request request) override
363+
{
364+
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
365+
auto context = details::winrt_request_context::create_request_context(self, request);
366+
367+
// Use a task to externally signal the final result and completion of the task.
368+
auto result_task = pplx::create_task(context->m_request_completion);
369+
370+
// Asynchronously send the response with the HTTP client implementation.
371+
this->async_send_request(context);
372+
373+
return result_task;
374+
}
375+
362376
protected:
363377

364378
// Method to open client.
@@ -545,26 +559,13 @@ class winrt_client : public _http_client_communicator
545559
private:
546560

547561
// No copy or assignment.
548-
winrt_client(const winrt_client&);
549-
winrt_client &operator=(const winrt_client&);
562+
winrt_client(const winrt_client&) = delete;
563+
winrt_client &operator=(const winrt_client&) = delete;
550564
};
551565

552-
http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
553-
m_http_client_impl(std::make_shared<details::winrt_client>(base_uri, client_config))
566+
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
554567
{
555-
}
556-
557-
pplx::task<http_response> http_network_handler::propagate(http_request request)
558-
{
559-
auto context = details::winrt_request_context::create_request_context(m_http_client_impl, request);
560-
561-
// Use a task to externally signal the final result and completion of the task.
562-
auto result_task = pplx::create_task(context->m_request_completion);
563-
564-
// Asynchronously send the response with the HTTP client implementation.
565-
m_http_client_impl->async_send_request(context);
566-
567-
return result_task;
568+
return std::make_shared<details::winrt_client>(std::move(base_uri), client_config);
568569
}
569570

570571
}}}}

0 commit comments

Comments
 (0)