Skip to content

Commit 98320fe

Browse files
committed
Hide definition of http_pipeline.
1 parent 7c9189e commit 98320fe

File tree

5 files changed

+113
-140
lines changed

5 files changed

+113
-140
lines changed

‎Release/include/cpprest/http_client.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ class http_client_config
397397
#endif
398398
};
399399

400+
class http_pipeline;
401+
400402
/// <summary>
401403
/// HTTP client class, used to maintain a connection to an HTTP service for an extended session.
402404
/// </summary>

‎Release/include/cpprest/http_msg.h‎

Lines changed: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,10 @@ class http_request
13181318
std::shared_ptr<http::details::_http_request> _m_impl;
13191319
};
13201320

1321+
namespace client {
1322+
class http_pipeline;
1323+
}
1324+
13211325
/// <summary>
13221326
/// HTTP client handler class, used to represent an HTTP pipeline stage.
13231327
/// </summary>
@@ -1333,6 +1337,11 @@ class http_pipeline_stage : public std::enable_shared_from_this<http_pipeline_st
13331337
{
13341338
public:
13351339

1340+
http_pipeline_stage() {}
1341+
1342+
http_pipeline_stage & operator=(const http_pipeline_stage &) = delete;
1343+
http_pipeline_stage(const http_pipeline_stage &) = delete;
1344+
13361345
virtual ~http_pipeline_stage()
13371346
{
13381347
}
@@ -1346,10 +1355,6 @@ class http_pipeline_stage : public std::enable_shared_from_this<http_pipeline_st
13461355

13471356
protected:
13481357

1349-
http_pipeline_stage()
1350-
{
1351-
}
1352-
13531358
/// <summary>
13541359
/// Gets the next stage in the pipeline.
13551360
/// </summary>
@@ -1363,13 +1368,14 @@ class http_pipeline_stage : public std::enable_shared_from_this<http_pipeline_st
13631368
/// Gets a shared pointer to this pipeline stage.
13641369
/// </summary>
13651370
/// <returns>A shared pointer to a pipeline stage.</returns>
1371+
CASABLANCA_DEPRECATED("This api is redundant. Use 'shared_from_this()' directly instead.")
13661372
std::shared_ptr<http_pipeline_stage> current_stage()
13671373
{
13681374
return this->shared_from_this();
13691375
}
13701376

13711377
private:
1372-
friend class http_pipeline;
1378+
friend class ::web::http::client::http_pipeline;
13731379

13741380
void set_next_stage(const std::shared_ptr<http_pipeline_stage> &next)
13751381
{
@@ -1378,116 +1384,6 @@ class http_pipeline_stage : public std::enable_shared_from_this<http_pipeline_st
13781384

13791385
std::shared_ptr<http_pipeline_stage> m_next_stage;
13801386

1381-
// No copy or assignment.
1382-
http_pipeline_stage & operator=(const http_pipeline_stage &);
1383-
http_pipeline_stage(const http_pipeline_stage &);
1384-
};
1385-
1386-
namespace details {
1387-
1388-
class function_pipeline_wrapper : public http::http_pipeline_stage
1389-
{
1390-
public:
1391-
function_pipeline_wrapper(std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> handler) : m_handler(handler)
1392-
{
1393-
}
1394-
1395-
virtual pplx::task<http_response> propagate(http_request request) override
1396-
{
1397-
return m_handler(request, next_stage());
1398-
}
1399-
private:
1400-
1401-
std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> m_handler;
1402-
};
1403-
1404-
} // namespace details
1405-
1406-
/// <summary>
1407-
///
1408-
/// </summary>
1409-
class http_pipeline
1410-
{
1411-
public:
1412-
1413-
/// <summary>
1414-
/// Create an http pipeline that consists of a linear chain of stages
1415-
/// </summary>
1416-
/// <param name="last">The final stage</param>
1417-
static std::shared_ptr<http_pipeline> create_pipeline(const std::shared_ptr<http_pipeline_stage> &last)
1418-
{
1419-
return std::shared_ptr<http_pipeline>(new http_pipeline(last));
1420-
}
1421-
1422-
/// <summary>
1423-
/// Initiate an http request into the pipeline
1424-
/// </summary>
1425-
/// <param name="request">Http request</param>
1426-
pplx::task<http_response> propagate(http_request request)
1427-
{
1428-
std::shared_ptr<http_pipeline_stage> first;
1429-
{
1430-
pplx::extensibility::scoped_recursive_lock_t l(m_lock);
1431-
first = (m_stages.size() > 0) ? m_stages[0] : m_last_stage;
1432-
}
1433-
return first->propagate(request);
1434-
}
1435-
1436-
/// <summary>
1437-
/// Adds an HTTP pipeline stage to the pipeline.
1438-
/// </summary>
1439-
/// <param name="stage">A pipeline stage.</param>
1440-
void append(const std::shared_ptr<http_pipeline_stage> &stage)
1441-
{
1442-
pplx::extensibility::scoped_recursive_lock_t l(m_lock);
1443-
1444-
if (m_stages.size() > 0)
1445-
{
1446-
std::shared_ptr<http_pipeline_stage> penultimate = m_stages[m_stages.size()-1];
1447-
penultimate->set_next_stage(stage);
1448-
}
1449-
stage->set_next_stage(m_last_stage);
1450-
1451-
m_stages.push_back(stage);
1452-
}
1453-
1454-
/// <summary>
1455-
/// Sets the last stage of the pipeline.
1456-
/// </summary>
1457-
/// <param name="last">Shared pointer to pipeline stage to set as the last.</param>
1458-
void set_last_stage(const std::shared_ptr<http_pipeline_stage> &last)
1459-
{
1460-
m_last_stage = last;
1461-
}
1462-
1463-
/// <summary>
1464-
/// Retrieves the last stage in this pipeline.
1465-
/// </summary>
1466-
/// <returns>A shared pointer to last stage.</returns>
1467-
const std::shared_ptr<http_pipeline_stage>& last_stage() const
1468-
{
1469-
return m_last_stage;
1470-
}
1471-
1472-
private:
1473-
1474-
http_pipeline(const std::shared_ptr<http_pipeline_stage> &last) : m_last_stage(last)
1475-
{
1476-
}
1477-
1478-
// The vector of pipeline stages.
1479-
std::vector<std::shared_ptr<http_pipeline_stage>> m_stages;
1480-
1481-
// The last stage is always set up by the client or listener and cannot
1482-
// be changed. All application-defined stages are executed before the
1483-
// last stage, which is typically a send or dispatch.
1484-
std::shared_ptr<http_pipeline_stage> m_last_stage;
1485-
1486-
pplx::extensibility::recursive_lock_t m_lock;
1487-
1488-
// No copy or assignment.
1489-
http_pipeline & operator=(const http_pipeline &);
1490-
http_pipeline(const http_pipeline &);
14911387
};
14921388

14931389
}}

‎Release/src/http/client/http_client.cpp‎

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,83 @@ inline void request_context::finish()
271271

272272
} // namespace details
273273

274+
/// <summary>
275+
/// Private implementation of http_client. Manages the http request processing pipeline.
276+
/// </summary>
277+
class http_pipeline
278+
{
279+
public:
280+
http_pipeline(std::shared_ptr<details::_http_client_communicator> last) : m_last_stage(std::move(last))
281+
{}
282+
283+
// No copy or assignment.
284+
http_pipeline & operator=(const http_pipeline &) = delete;
285+
http_pipeline(const http_pipeline &) = delete;
286+
287+
/// <summary>
288+
/// Initiate an http request into the pipeline
289+
/// </summary>
290+
/// <param name="request">Http request</param>
291+
pplx::task<http_response> propagate(http_request request)
292+
{
293+
std::shared_ptr<http_pipeline_stage> first;
294+
{
295+
pplx::extensibility::scoped_recursive_lock_t l(m_lock);
296+
first = (m_stages.size() > 0) ? m_stages[0] : m_last_stage;
297+
}
298+
return first->propagate(request);
299+
}
300+
301+
/// <summary>
302+
/// Adds an HTTP pipeline stage to the pipeline.
303+
/// </summary>
304+
/// <param name="stage">A pipeline stage.</param>
305+
void append(const std::shared_ptr<http_pipeline_stage> &stage)
306+
{
307+
pplx::extensibility::scoped_recursive_lock_t l(m_lock);
308+
309+
if (m_stages.size() > 0)
310+
{
311+
std::shared_ptr<http_pipeline_stage> penultimate = m_stages[m_stages.size() - 1];
312+
penultimate->set_next_stage(stage);
313+
}
314+
stage->set_next_stage(m_last_stage);
315+
316+
m_stages.push_back(stage);
317+
}
318+
319+
// The last stage is always set up by the client or listener and cannot
320+
// be changed. All application-defined stages are executed before the
321+
// last stage, which is typically a send or dispatch.
322+
const std::shared_ptr<details::_http_client_communicator> m_last_stage;
323+
324+
private:
325+
326+
// The vector of pipeline stages.
327+
std::vector<std::shared_ptr<http_pipeline_stage>> m_stages;
328+
329+
pplx::extensibility::recursive_lock_t m_lock;
330+
};
274331

275332
void http_client::add_handler(const std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> &handler)
276333
{
277-
m_pipeline->append(std::make_shared<::web::http::details::function_pipeline_wrapper>(handler));
334+
class function_pipeline_wrapper : public http::http_pipeline_stage
335+
{
336+
public:
337+
function_pipeline_wrapper(const std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> &handler) : m_handler(handler)
338+
{
339+
}
340+
341+
virtual pplx::task<http_response> propagate(http_request request) override
342+
{
343+
return m_handler(std::move(request), next_stage());
344+
}
345+
private:
346+
347+
std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> m_handler;
348+
};
349+
350+
m_pipeline->append(std::make_shared<function_pipeline_wrapper>(handler));
278351
}
279352

280353
void http_client::add_handler(const std::shared_ptr<http::http_pipeline_stage> &stage)
@@ -287,20 +360,24 @@ http_client::http_client(const uri &base_uri) : http_client(base_uri, http_clien
287360

288361
http_client::http_client(const uri &base_uri, const http_client_config &client_config)
289362
{
363+
std::shared_ptr<details::_http_client_communicator> final_pipeline_stage;
364+
290365
if (base_uri.scheme().empty())
291366
{
292367
auto uribuilder = uri_builder(base_uri);
293368
uribuilder.set_scheme(_XPLATSTR("http"));
294369
uri uriWithScheme = uribuilder.to_uri();
295370
verify_uri(uriWithScheme);
296-
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(uriWithScheme, client_config));
371+
final_pipeline_stage = details::create_platform_final_pipeline_stage(uriWithScheme, client_config);
297372
}
298373
else
299374
{
300375
verify_uri(base_uri);
301-
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(base_uri, client_config));
376+
final_pipeline_stage = details::create_platform_final_pipeline_stage(base_uri, client_config);
302377
}
303378

379+
m_pipeline = std::make_shared<http_pipeline>(std::move(final_pipeline_stage));
380+
304381
#if !defined(CPPREST_TARGET_XP)
305382
add_handler(std::static_pointer_cast<http::http_pipeline_stage>(
306383
std::make_shared<oauth1::details::oauth1_handler>(client_config.oauth1())));
@@ -314,14 +391,29 @@ http_client::~http_client() CPPREST_NOEXCEPT {}
314391

315392
const http_client_config & http_client::client_config() const
316393
{
317-
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
318-
return ph->client_config();
394+
return m_pipeline->m_last_stage->client_config();
319395
}
320396

321397
const uri & http_client::base_uri() const
322398
{
323-
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
324-
return ph->base_uri();
399+
return m_pipeline->m_last_stage->base_uri();
400+
}
401+
402+
// Macros to help build string at compile time and avoid overhead.
403+
#define STRINGIFY(x) _XPLATSTR(#x)
404+
#define TOSTRING(x) STRINGIFY(x)
405+
#define USERAGENT _XPLATSTR("cpprestsdk/") TOSTRING(CPPREST_VERSION_MAJOR) _XPLATSTR(".") TOSTRING(CPPREST_VERSION_MINOR) _XPLATSTR(".") TOSTRING(CPPREST_VERSION_REVISION)
406+
407+
pplx::task<http_response> http_client::request(http_request request, const pplx::cancellation_token &token)
408+
{
409+
if (!request.headers().has(header_names::user_agent))
410+
{
411+
request.headers().add(header_names::user_agent, USERAGENT);
412+
}
413+
414+
request._set_base_uri(base_uri());
415+
request._set_cancellation_token(token);
416+
return m_pipeline->propagate(request);
325417
}
326418

327419

‎Release/src/http/client/http_client_msg.cpp‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,4 @@ utility::string_t details::_http_response::to_string() const
9797
return buffer.str();
9898
}
9999

100-
// Macros to help build string at compile time and avoid overhead.
101-
#define STRINGIFY(x) _XPLATSTR(#x)
102-
#define TOSTRING(x) STRINGIFY(x)
103-
#define USERAGENT _XPLATSTR("cpprestsdk/") TOSTRING(CPPREST_VERSION_MAJOR) _XPLATSTR(".") TOSTRING(CPPREST_VERSION_MINOR) _XPLATSTR(".") TOSTRING(CPPREST_VERSION_REVISION)
104-
105-
pplx::task<http_response> client::http_client::request(http_request request, const pplx::cancellation_token &token)
106-
{
107-
if(!request.headers().has(header_names::user_agent))
108-
{
109-
request.headers().add(header_names::user_agent, USERAGENT);
110-
}
111-
112-
request._set_base_uri(base_uri());
113-
request._set_cancellation_token(token);
114-
return m_pipeline->propagate(request);
115-
}
116-
117100
}} // namespace web::http

‎Release/tests/functional/http/client/pipeline_stage_tests.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class modify_count_responses_stage : public http_pipeline_stage
244244
{
245245
request.headers().set_content_type(U("modified content type"));
246246

247-
auto currentStage = current_stage();
247+
auto currentStage = this->shared_from_this();
248248
return next_stage()->propagate(request).then([currentStage](http_response response) -> http_response
249249
{
250250

0 commit comments

Comments
 (0)