27 #ifndef _CASA_HTTP_CLIENT_H
28 #define _CASA_HTTP_CLIENT_H
30 #if defined (__cplusplus_winrt)
31 #if !defined(__WRL_NO_DEFAULT_LIB__)
32 #define __WRL_NO_DEFAULT_LIB__
36 namespace web {
namespace http{
namespace client{
37 typedef IXMLHTTPRequest2* native_handle;}}}
39 namespace web {
namespace http{
namespace client{
40 typedef void* native_handle;}}}
41 #endif // __cplusplus_winrt
45 #if !defined(_WIN32) && !defined(__cplusplus_winrt)
46 #if defined(__clang__)
47 #pragma clang diagnostic push
48 #pragma clang diagnostic ignored "-Wconversion"
50 #include "boost/asio/ssl.hpp"
51 #if defined(__clang__)
52 #pragma clang diagnostic pop
56 #include "pplx/pplxtasks.h"
57 #include "cpprest/http_msg.h"
58 #include "cpprest/json.h"
59 #include "cpprest/uri.h"
60 #include "cpprest/details/web_utilities.h"
61 #include "cpprest/details/basic_types.h"
62 #include "cpprest/asyncrt_utils.h"
64 #if !defined(CPPREST_TARGET_XP)
65 #include "cpprest/oauth1.h"
68 #include "cpprest/oauth2.h"
94 m_guarantee_order(
false),
95 m_timeout(std::chrono::seconds(30)),
97 #if !defined(__cplusplus_winrt)
98 , m_validate_certificates(
true)
100 , m_set_user_nativehandle_options([](native_handle)->
void{})
101 #
if !defined(_WIN32) && !defined(__cplusplus_winrt)
102 , m_ssl_context_callback([](boost::asio::ssl::context&)->
void{})
104 #
if defined(_WIN32) && !defined(__cplusplus_winrt)
105 , m_buffer_request(
false)
110 #if !defined(CPPREST_TARGET_XP)
111 const std::shared_ptr<oauth1::experimental::oauth1_config>
oauth1()
const
126 m_oauth1 = std::make_shared<oauth1::experimental::oauth1_config>(std::move(config));
134 const std::shared_ptr<oauth2::experimental::oauth2_config>
oauth2()
const
145 m_oauth2 = std::make_shared<oauth2::experimental::oauth2_config>(std::move(config));
163 m_proxy = std::move(proxy);
172 return m_credentials;
181 m_credentials = cred;
190 return m_guarantee_order;
197 CASABLANCA_DEPRECATED(
"Confusing API will be removed in future releases. If you need to order HTTP requests use task continuations.")
209 return std::chrono::duration_cast<utility::seconds>(m_timeout);
219 return std::chrono::duration_cast<T>(m_timeout);
228 m_timeout = std::chrono::duration_cast<std::chrono::microseconds>(
timeout);
237 return m_chunksize == 0 ? 64 * 1024 : m_chunksize;
257 return m_chunksize == 0;
260 #if !defined(__cplusplus_winrt)
267 return m_validate_certificates;
277 m_validate_certificates = validate_certs;
282 #if !defined(__cplusplus_winrt)
283 bool buffer_request()
const
289 return m_buffer_request;
299 void set_buffer_request(
bool buffer_request)
301 m_buffer_request = buffer_request;
320 m_set_user_nativehandle_options = callback;
329 m_set_user_nativehandle_options(handle);
332 #if !defined(_WIN32) && !defined(__cplusplus_winrt)
339 m_ssl_context_callback = callback;
347 return m_ssl_context_callback;
352 #if !defined(CPPREST_TARGET_XP)
353 std::shared_ptr<oauth1::experimental::oauth1_config> m_oauth1;
356 std::shared_ptr<oauth2::experimental::oauth2_config> m_oauth2;
360 bool m_guarantee_order;
362 std::chrono::microseconds m_timeout;
365 #if !defined(__cplusplus_winrt)
367 bool m_validate_certificates;
370 std::function<void(native_handle)> m_set_user_nativehandle_options;
372 #if !defined(_WIN32) && !defined(__cplusplus_winrt)
373 std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
375 #if defined(_WIN32) && !defined(__cplusplus_winrt)
376 bool m_buffer_request;
425 m_pipeline->append(std::make_shared<::web::http::details::function_pipeline_wrapper>(handler));
432 void add_handler(
const std::shared_ptr<http::http_pipeline_stage> &stage)
434 m_pipeline->append(stage);
466 const utility::string_t &path_query_fragment,
484 const utility::string_t &path_query_fragment,
506 const utf8string &path_query_fragment,
507 const utf8string &body_data,
508 const utf8string &content_type =
"text/plain; charset=utf-8",
513 msg.
set_body(body_data, content_type);
529 const utf8string &path_query_fragment,
530 utf8string &&body_data,
531 const utf8string &content_type =
"text/plain; charset=utf-8",
536 msg.
set_body(std::move(body_data), content_type);
552 const utf16string &path_query_fragment,
553 const utf16string &body_data,
559 msg.
set_body(body_data, content_type);
574 const utf8string &path_query_fragment,
575 const utf8string &body_data,
578 return request(mtd, path_query_fragment, body_data,
"text/plain; charset=utf-8", token);
592 const utf8string &path_query_fragment,
593 utf8string &&body_data,
598 msg.
set_body(std::move(body_data),
"text/plain; charset=utf-8");
613 const utf16string &path_query_fragment,
614 const utf16string &body_data,
620 #if !defined (__cplusplus_winrt)
632 const utility::string_t &path_query_fragment,
633 const concurrency::streams::istream &body,
634 const utility::string_t &content_type = _XPLATSTR(
"application/octet-stream"),
638 msg.set_request_uri(path_query_fragment);
639 msg.set_body(body, content_type);
653 const utility::string_t &path_query_fragment,
654 const concurrency::streams::istream &body,
657 return request(mtd, path_query_fragment, body, _XPLATSTR(
"application/octet-stream"), token);
659 #endif // __cplusplus_winrt
674 const utility::string_t &path_query_fragment,
675 const concurrency::streams::istream &body,
676 size_t content_length,
677 const utility::string_t &content_type = _XPLATSTR(
"application/octet-stream"),
682 msg.
set_body(body, content_length, content_type);
698 const utility::string_t &path_query_fragment,
699 const concurrency::streams::istream &body,
700 size_t content_length,
703 return request(mtd, path_query_fragment, body, content_length, _XPLATSTR(
"application/octet-stream"), token);
710 std::shared_ptr<::web::http::http_pipeline> m_pipeline;
Represents a set of user credentials (user name and password) to be used for authentication.
Definition: web_utilities.h:80
void set_request_uri(const uri &uri)
Set the underling URI of the request message.
Definition: http_msg.h:814
OAuth 2.0 configuration.
Definition: oauth2.h:212
const std::shared_ptr< oauth2::experimental::oauth2_config > oauth2() const
Get OAuth 2.0 configuration.
Definition: http_client.h:134
const http::client::credentials & credentials() const
Get the client credentials
Definition: http_client.h:170
pplx::task< http_response > request(const method &mtd, const utf16string &path_query_fragment, const utf16string &body_data, const utf16string &content_type=::utility::conversions::to_utf16string("text/plain"), const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:550
void add_handler(const std::shared_ptr< http::http_pipeline_stage > &stage)
Adds an HTTP pipeline stage to the client.
Definition: http_client.h:432
_ASYNCRTIMP http_client(const uri &base_uri)
Creates a new http_client connected to specified uri.
Definition: http_client_impl.h:419
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
pplx::task< http_response > request(const method &mtd, const utf16string &path_query_fragment, const utf16string &body_data, const pplx::cancellation_token &token)
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:611
void set_nativehandle_options(const std::function< void(native_handle)> &callback)
Sets a callback to enable custom setting of platform specific options.
Definition: http_client.h:318
size_t chunksize() const
Get the client chunk size.
Definition: http_client.h:235
pplx::task< http_response > request(const method &mtd, const utf8string &path_query_fragment, utf8string &&body_data, const utf8string &content_type="text/plain; charset=utf-8", const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:527
pplx::task< http_response > request(const method &mtd, const utf8string &path_query_fragment, const utf8string &body_data, const utf8string &content_type="text/plain; charset=utf-8", const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:504
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
void set_guarantee_order(bool guarantee_order)
Set the 'guarantee order' property
Definition: http_client.h:198
const std::function< void(boost::asio::ssl::context &)> & get_ssl_context_callback() const
Gets the user's callback to allow for customization of the ssl context.
Definition: http_client.h:345
pplx::task< http_response > request(const method &mtd, const utility::string_t &path_query_fragment, const json::value &body_data, const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request.
Definition: http_client.h:482
void set_chunksize(size_t size)
Sets the client chunk size.
Definition: http_client.h:245
HTTP client class, used to maintain a connection to an HTTP service for an extended session...
Definition: http_client.h:383
void set_validate_certificates(bool validate_certs)
Sets the server certificate validation property.
Definition: http_client.h:275
HTTP client configuration class, used to set the possible configuration options used to create an htt...
Definition: http_client.h:90
pplx::task< http_response > request(const method &mtd, const utf8string &path_query_fragment, const utf8string &body_data, const pplx::cancellation_token &token)
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:572
utility::seconds timeout() const
Get the timeout
Definition: http_client.h:207
_ASYNCRTIMP pplx::task< http_response > request(http_request request, const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request.
bool is_default_chunksize() const
Returns true if the default chunk size is in use.
Definition: http_client.h:255
_ASYNCRTIMP utility::string_t __cdecl to_string_t(std::string &&s)
Converts to a platform dependent Unicode string type.
void set_body(utf8string &&body_text, const utf8string &content_type=utf8string("text/plain; charset=utf-8"))
Sets the body of the message to a textual string and set the "Content-Type" header. Assumes the character encoding of the string is UTF-8.
Definition: http_msg.h:921
utility::string_t method
Predefined method strings for the standard HTTP methods mentioned in the HTTP 1.1 specification...
Definition: http_msg.h:62
web_proxy represents the concept of the web proxy, which can be auto-discovered, disabled, or specified explicitly by the user.
Definition: web_utilities.h:157
OAuth 1.0 configuration class.
Definition: oauth1.h:238
_ASYNCRTIMP const http_client_config & client_config() const
Get client configuration object
Definition: http_client_impl.h:454
_ASYNCRTIMP utf16string __cdecl to_utf16string(const std::string &value)
Converts to a UTF-16 from string.
~http_client() CPPREST_NOEXCEPT
Note the destructor doesn't necessarily close the connection and release resources. The connection is reference counted with the http_responses.
Definition: http_client.h:403
T timeout() const
Get the timeout
Definition: http_client.h:217
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
pplx::task< http_response > request(const method &mtd, const utf8string &path_query_fragment, utf8string &&body_data, const pplx::cancellation_token &token)
Asynchronously sends an HTTP request with a string body. Assumes the character encoding of the string...
Definition: http_client.h:590
The cancellation_token class represents the ability to determine whether some operation has been requ...
Definition: pplxcancellation_token.h:712
void invoke_nativehandle_options(native_handle handle) const
Invokes a user's callback to allow for customization of the request.
Definition: http_client.h:327
A JSON value represented as a C++ class.
Definition: json.h:83
_ASYNCRTIMP const uri & base_uri() const
Gets the base URI.
Definition: http_client_impl.h:460
const web_proxy & proxy() const
Get the web proxy object
Definition: http_client.h:152
void set_proxy(web_proxy proxy)
Set the web proxy object
Definition: http_client.h:161
pplx::task< http_response > request(const method &mtd, const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request.
Definition: http_client.h:451
pplx::task< http_response > request(const method &mtd, const utility::string_t &path_query_fragment, const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request.
Definition: http_client.h:464
static cancellation_token none()
Returns a cancellation token which can never be subject to cancellation.
Definition: pplxcancellation_token.h:724
Represents an HTTP request.
Definition: http_msg.h:771
bool guarantee_order() const
Get the 'guarantee order' property
Definition: http_client.h:188
pplx::task< http_response > request(const method &mtd, const utility::string_t &path_query_fragment, const concurrency::streams::istream &body, size_t content_length, const utility::string_t &content_type=_XPLATSTR("application/octet-stream"), const pplx::cancellation_token &token=pplx::cancellation_token::none())
Asynchronously sends an HTTP request.
Definition: http_client.h:672
const std::shared_ptr< oauth1::experimental::oauth1_config > oauth1() const
Get OAuth 1.0 configuration.
Definition: http_client.h:115
pplx::task< http_response > request(const method &mtd, const utility::string_t &path_query_fragment, const concurrency::streams::istream &body, size_t content_length, const pplx::cancellation_token &token)
Asynchronously sends an HTTP request.
Definition: http_client.h:696
void set_oauth1(oauth1::experimental::oauth1_config config)
Set OAuth 1.0 configuration.
Definition: http_client.h:124
void set_oauth2(oauth2::experimental::oauth2_config config)
Set OAuth 2.0 configuration.
Definition: http_client.h:143
void add_handler(const std::function< pplx::task< http_response >(http_request, std::shared_ptr< http::http_pipeline_stage >)> &handler)
Adds an HTTP pipeline stage to the client.
Definition: http_client.h:423
void set_ssl_context_callback(const std::function< void(boost::asio::ssl::context &)> &callback)
Sets a callback to enable custom setting of the ssl context, at construction time.
Definition: http_client.h:337
void set_credentials(const http::client::credentials &cred)
Set the client credentials
Definition: http_client.h:179
bool validate_certificates() const
Gets the server certificate validation property.
Definition: http_client.h:265
pplx::task< http_response > request(const method &mtd, const utility::string_t &path_query_fragment, const concurrency::streams::istream &body, const pplx::cancellation_token &token)
Asynchronously sends an HTTP request.
Definition: http_client.h:651
void set_timeout(const T &timeout)
Set the timeout
Definition: http_client.h:226