Skip to content

Commit b1194f3

Browse files
committed
Merge pull request microsoft#376 from xqp/master
2 parents 039b5b9 + d72c5f9 commit b1194f3

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

Release/include/cpprest/http_client.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ class http_client_config
312312
}
313313
#endif
314314

315+
/// <summary>
316+
/// Sets a callback to enable custom setting of platform specific options.
317+
/// </summary>
318+
/// <remarks>
319+
/// The native_handle is the following type depending on the underlying platform:
320+
/// Windows Desktop, WinHTTP - HINTERNET (session)
321+
/// </remarks>
322+
/// <param name="callback">A user callback allowing for customization of the session</param>
323+
void set_nativesessionhandle_options(const std::function<void(native_handle)> &callback)
324+
{
325+
m_set_user_nativesessionhandle_options = callback;
326+
}
327+
328+
/// <summary>
329+
/// Invokes a user's callback to allow for customization of the session.
330+
/// </summary>
331+
/// <remarks>Internal Use Only</remarks>
332+
/// <param name="handle">A internal implementation handle.</param>
333+
void _invoke_nativesessionhandle_options(native_handle handle) const
334+
{
335+
m_set_user_nativesessionhandle_options(handle);
336+
}
337+
315338
/// <summary>
316339
/// Sets a callback to enable custom setting of platform specific options.
317340
/// </summary>
@@ -397,6 +420,7 @@ class http_client_config
397420
#endif
398421

399422
std::function<void(native_handle)> m_set_user_nativehandle_options;
423+
std::function<void(native_handle)> m_set_user_nativesessionhandle_options;
400424

401425
#if !defined(_WIN32) && !defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_ASIO)
402426
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;

Release/src/http/client/http_client.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,6 @@ _http_client_communicator::_http_client_communicator(http::uri&& address, http_c
205205
void _http_client_communicator::open_and_send_request(const std::shared_ptr<request_context> &request)
206206
{
207207
// First see if client needs to be opened.
208-
auto error = open_if_required();
209-
210-
if (error != 0)
211-
{
212-
// Failed to open
213-
request->report_error(error, _XPLATSTR("Open failed"));
214-
215-
// DO NOT TOUCH the this pointer after completing the request
216-
// This object could be freed along with the request as it could
217-
// be the last reference to this object
218-
return;
219-
}
220-
221-
send_request(request);
222-
}
223-
224-
unsigned long _http_client_communicator::open_if_required()
225-
{
226208
unsigned long error = 0;
227209

228210
if (!m_opened)
@@ -241,7 +223,18 @@ unsigned long _http_client_communicator::open_if_required()
241223
}
242224
}
243225

244-
return error;
226+
if (error != 0)
227+
{
228+
// Failed to open
229+
request->report_error(error, _XPLATSTR("Open failed"));
230+
231+
// DO NOT TOUCH the this pointer after completing the request
232+
// This object could be freed along with the request as it could
233+
// be the last reference to this object
234+
return;
235+
}
236+
237+
send_request(request);
245238
}
246239

247240
inline void request_context::finish()

Release/src/http/client/http_client_impl.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@ class _http_client_communicator : public http_pipeline_stage
130130

131131
http_client_config m_client_config;
132132

133-
bool m_opened;
133+
std::atomic<bool> m_opened;
134134

135135
pplx::extensibility::critical_section_t m_open_lock;
136136

137137
// Wraps opening the client around sending a request.
138138
void open_and_send_request_async(const std::shared_ptr<request_context> &request);
139139
void open_and_send_request(const std::shared_ptr<request_context> &request);
140140

141-
unsigned long open_if_required();
142-
143141
// Queue used to guarantee ordering of requests, when applicable.
144142
std::queue<std::shared_ptr<request_context>> m_requests_queue;
145143
int m_scheduled;

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class winhttp_client : public _http_client_communicator
356356
}
357357

358358
// Open session and connection with the server.
359-
unsigned long open()
359+
virtual unsigned long open() override
360360
{
361361
DWORD access_type;
362362
LPCWSTR proxy_name;
@@ -448,13 +448,17 @@ class winhttp_client : public _http_client_communicator
448448
}
449449
}
450450
#endif
451-
//Enable TLS 1.1 and 1.2
452-
HRESULT result(S_OK);
451+
//Enable TLS 1.1 and 1.2
453452
BOOL win32_result(FALSE);
454-
453+
455454
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);
456455
win32_result = ::WinHttpSetOption(m_hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, &secure_protocols, sizeof(secure_protocols));
457-
if(FALSE == win32_result){ result = HRESULT_FROM_WIN32(::GetLastError()); }
456+
if(FALSE == win32_result)
457+
{
458+
return report_failure(_XPLATSTR("Error setting session options"));
459+
}
460+
461+
config._invoke_nativesessionhandle_options(m_hSession);
458462

459463
// Register asynchronous callback.
460464
if(WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(

0 commit comments

Comments
 (0)