forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client_impl.h
More file actions
151 lines (110 loc) · 4.64 KB
/
http_client_impl.h
File metadata and controls
151 lines (110 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***
* ==++==
*
* 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.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* HTTP Library: Client-side APIs.
*
* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#pragma once
#include "cpprest/details/basic_types.h"
#include "cpprest/astreambuf.h"
#include "cpprest/http_client.h"
#include "cpprest/http_msg.h"
#include <stdexcept>
#include <string>
#include <memory>
namespace web { namespace http { namespace client { namespace details {
class _http_client_communicator;
// Request context encapsulating everything necessary for creating and responding to a request.
class request_context
{
public:
// Destructor to clean up any held resources.
virtual ~request_context() {}
virtual void report_exception(std::exception_ptr exceptionPtr);
virtual concurrency::streams::streambuf<uint8_t> _get_readbuffer();
void complete_headers();
/// <summary>
/// Completes this request, setting the underlying task completion event, and cleaning up the handles
/// </summary>
void complete_request(utility::size64_t body_size);
void report_error(unsigned long error_code, const std::string &errorMessage);
#ifdef _WIN32
void report_error(unsigned long error_code, const std::wstring &errorMessage);
#endif
template<typename _ExceptionType>
void report_exception(const _ExceptionType &e)
{
report_exception(std::make_exception_ptr(e));
}
concurrency::streams::streambuf<uint8_t> _get_writebuffer();
// Reference to the http_client implementation.
std::shared_ptr<_http_client_communicator> m_http_client;
// request/response pair.
http_request m_request;
http_response m_response;
utility::size64_t m_uploaded;
utility::size64_t m_downloaded;
// task completion event to signal request is completed.
pplx::task_completion_event<http_response> m_request_completion;
// Registration for cancellation notification if enabled.
pplx::cancellation_token_registration m_cancellationRegistration;
protected:
request_context(const std::shared_ptr<_http_client_communicator> &client, const http_request &request);
virtual void finish();
};
//
// Interface used by client implementations. Concrete implementations are responsible for
// sending HTTP requests and receiving the responses.
//
class _http_client_communicator : public http_pipeline_stage
{
public:
virtual ~_http_client_communicator() override = default;
// Asynchronously send a HTTP request and process the response.
void async_send_request(const std::shared_ptr<request_context> &request);
void finish_request();
const http_client_config& client_config() const;
const uri & base_uri() const;
protected:
_http_client_communicator(http::uri&& address, http_client_config&& client_config);
// Method to open client.
virtual unsigned long open() = 0;
// HTTP client implementations must implement send_request.
virtual void send_request(_In_ const std::shared_ptr<request_context> &request) = 0;
// URI to connect to.
const http::uri m_uri;
private:
http_client_config m_client_config;
bool m_opened;
pplx::extensibility::critical_section_t m_open_lock;
// Wraps opening the client around sending a request.
void open_and_send_request(const std::shared_ptr<request_context> &request);
unsigned long open_if_required();
void push_request(const std::shared_ptr<request_context> &request);
// Queue used to guarantee ordering of requests, when applicable.
std::queue<std::shared_ptr<request_context>> m_requests_queue;
int m_scheduled;
};
/// <summary>
/// Factory function implemented by the separate platforms to construct their subclasses of _http_client_communicator
/// </summary>
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri&& base_uri, http_client_config&& client_config);
}}}}