Skip to content

Commit 22b5ef3

Browse files
committed
Misc changes in the http portions:
1. Fixed a bunch of parameters that should be passing by const reference. Some of these were trying to pass by value and 'move' when it isn't going to be more efficient and isn't worth it. 2. Updated a bunch of places passing shared_ptr by value. 3. Removed some unnecessary constructors/assignment operators. 4. Removed pointless streambuf check from desktop client implementation. 5. Updated some comments to be more clear.
1 parent ae63e64 commit 22b5ef3

File tree

9 files changed

+228
-278
lines changed

9 files changed

+228
-278
lines changed

Release/include/cpprest/http_client.h

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/***
22
* ==++==
33
*
4-
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -49,7 +49,7 @@ typedef void* native_handle;}}}
4949
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
5050
#include <ppltasks.h>
5151
namespace pplx = Concurrency;
52-
#else
52+
#else
5353
#include "pplx/pplxtasks.h"
5454
#endif
5555

@@ -66,7 +66,7 @@ namespace pplx = Concurrency;
6666
#include "cpprest/oauth2.h"
6767

6868
/// The web namespace contains functionality common to multiple protocols like HTTP and WebSockets.
69-
namespace web
69+
namespace web
7070
{
7171
/// Declarations and functionality for the HTTP protocol.
7272
namespace http
@@ -88,7 +88,7 @@ namespace details {
8888
#else
8989
class winhttp_client;
9090
#endif // __cplusplus_winrt
91-
}
91+
}
9292
#endif // _MS_WINDOWS
9393

9494
/// <summary>
@@ -98,7 +98,7 @@ namespace details {
9898
class http_client_config
9999
{
100100
public:
101-
http_client_config() :
101+
http_client_config() :
102102
m_guarantee_order(false),
103103
m_timeout(utility::seconds(30)),
104104
m_chunksize(0)
@@ -217,7 +217,7 @@ class http_client_config
217217
/// Set the timeout
218218
/// </summary>
219219
/// <param name="timeout">The timeout (in seconds) used for each send and receive operation on the client.</param>
220-
void set_timeout(utility::seconds timeout)
220+
void set_timeout(const utility::seconds &timeout)
221221
{
222222
m_timeout = timeout;
223223
}
@@ -284,7 +284,7 @@ class http_client_config
284284
}
285285

286286
/// <summary>
287-
/// Sets the request buffering property.
287+
/// Sets the request buffering property.
288288
/// If true, in cases where the request body/stream doesn't support seeking the request data will be buffered.
289289
/// This can help in situations where an authentication challenge might be expected.
290290
/// </summary>
@@ -296,12 +296,12 @@ class http_client_config
296296
}
297297
#endif
298298
#endif
299-
299+
300300
/// <summary>
301301
/// Sets a callback to enable custom setting of winhttp options
302302
/// </summary>
303303
/// <param name="callback">A user callback allowing for customization of the request</param>
304-
void set_nativehandle_options(std::function<void(native_handle)> callback)
304+
void set_nativehandle_options(const std::function<void(native_handle)> &callback)
305305
{
306306
m_set_user_nativehandle_options = callback;
307307
}
@@ -369,7 +369,7 @@ class http_client
369369
/// Note the destructor doesn't necessarily close the connection and release resources.
370370
/// The connection is reference counted with the http_responses.
371371
/// </summary>
372-
~http_client() {}
372+
~http_client() _noexcept {}
373373

374374
/// <summary>
375375
/// Gets the base uri
@@ -398,7 +398,7 @@ class http_client
398398
/// Adds an HTTP pipeline stage to the client.
399399
/// </summary>
400400
/// <param name="stage">A shared pointer to a pipeline stage.</param>
401-
void add_handler(std::shared_ptr<http::http_pipeline_stage> stage)
401+
void add_handler(const std::shared_ptr<http::http_pipeline_stage> &stage)
402402
{
403403
m_pipeline->append(stage);
404404
}
@@ -409,17 +409,17 @@ class http_client
409409
/// <param name="request">Request to send.</param>
410410
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
411411
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
412-
_ASYNCRTIMP pplx::task<http_response> request(http_request request, pplx::cancellation_token token = pplx::cancellation_token::none());
412+
_ASYNCRTIMP pplx::task<http_response> request(http_request request, const pplx::cancellation_token &token = pplx::cancellation_token::none());
413413

414414
/// <summary>
415415
/// Asynchronously sends an HTTP request.
416416
/// </summary>
417417
/// <param name="mtd">HTTP request method.</param>
418418
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
419419
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
420-
pplx::task<http_response> request(method mtd, pplx::cancellation_token token = pplx::cancellation_token::none())
420+
pplx::task<http_response> request(const method &mtd, const pplx::cancellation_token &token = pplx::cancellation_token::none())
421421
{
422-
http_request msg(std::move(mtd));
422+
http_request msg(mtd);
423423
return request(msg, token);
424424
}
425425

@@ -431,11 +431,11 @@ class http_client
431431
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
432432
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
433433
pplx::task<http_response> request(
434-
method mtd,
434+
const method &mtd,
435435
const utility::string_t &path_query_fragment,
436-
pplx::cancellation_token token = pplx::cancellation_token::none())
436+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
437437
{
438-
http_request msg(std::move(mtd));
438+
http_request msg(mtd);
439439
msg.set_request_uri(path_query_fragment);
440440
return request(msg, token);
441441
}
@@ -449,12 +449,12 @@ class http_client
449449
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
450450
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
451451
pplx::task<http_response> request(
452-
method mtd,
453-
const utility::string_t &path_query_fragment,
452+
const method &mtd,
453+
const utility::string_t &path_query_fragment,
454454
const json::value &body_data,
455-
pplx::cancellation_token token = pplx::cancellation_token::none())
455+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
456456
{
457-
http_request msg(std::move(mtd));
457+
http_request msg(mtd);
458458
msg.set_request_uri(path_query_fragment);
459459
msg.set_body(body_data);
460460
return request(msg, token);
@@ -469,16 +469,17 @@ class http_client
469469
/// <param name="body_data">String containing the text to use in the message body.</param>
470470
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
471471
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
472+
// TODO overload???Not sure...
472473
pplx::task<http_response> request(
473-
method mtd,
474+
const method &mtd,
474475
const utility::string_t &path_query_fragment,
475476
const utility::string_t &body_data,
476-
utility::string_t content_type = _XPLATSTR("text/plain"),
477-
pplx::cancellation_token token = pplx::cancellation_token::none())
477+
const utility::string_t &content_type = _XPLATSTR("text/plain"),
478+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
478479
{
479-
http_request msg(std::move(mtd));
480+
http_request msg(mtd);
480481
msg.set_request_uri(path_query_fragment);
481-
msg.set_body(body_data, std::move(content_type));
482+
msg.set_body(body_data, content_type);
482483
return request(msg, token);
483484
}
484485

@@ -490,11 +491,12 @@ class http_client
490491
/// <param name="body_data">String containing the text to use in the message body.</param>
491492
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
492493
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
494+
// TODO overload not usre???
493495
pplx::task<http_response> request(
494-
method mtd,
496+
const method &mtd,
495497
const utility::string_t &path_query_fragment,
496498
const utility::string_t &body_data,
497-
pplx::cancellation_token token)
499+
const pplx::cancellation_token &token)
498500
{
499501
return request(mtd, path_query_fragment, body_data, _XPLATSTR("text/plain"), token);
500502
}
@@ -510,15 +512,15 @@ class http_client
510512
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
511513
/// <returns>A task that is completed once a response from the request is received.</returns>
512514
pplx::task<http_response> request(
513-
method mtd,
515+
const method &mtd,
514516
const utility::string_t &path_query_fragment,
515-
concurrency::streams::istream body,
516-
utility::string_t content_type = _XPLATSTR("application/octet-stream"),
517-
pplx::cancellation_token token = pplx::cancellation_token::none())
517+
const concurrency::streams::istream &body,
518+
const utility::string_t &content_type = _XPLATSTR("application/octet-stream"),
519+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
518520
{
519-
http_request msg(std::move(mtd));
521+
http_request msg(mtd);
520522
msg.set_request_uri(path_query_fragment);
521-
msg.set_body(body, std::move(content_type));
523+
msg.set_body(body, content_type);
522524
return request(msg, token);
523525
}
524526

@@ -531,10 +533,10 @@ class http_client
531533
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
532534
/// <returns>A task that is completed once a response from the request is received.</returns>
533535
pplx::task<http_response> request(
534-
method mtd,
536+
const method &mtd,
535537
const utility::string_t &path_query_fragment,
536-
concurrency::streams::istream body,
537-
pplx::cancellation_token token)
538+
const concurrency::streams::istream &body,
539+
const pplx::cancellation_token &token)
538540
{
539541
return request(mtd, path_query_fragment, body, _XPLATSTR("application/octet-stream"), token);
540542
}
@@ -552,16 +554,16 @@ class http_client
552554
/// <returns>A task that is completed once a response from the request is received.</returns>
553555
/// <remarks>Winrt requires to provide content_length.</remarks>
554556
pplx::task<http_response> request(
555-
method mtd,
557+
const method &mtd,
556558
const utility::string_t &path_query_fragment,
557-
concurrency::streams::istream body,
559+
const concurrency::streams::istream &body,
558560
size_t content_length,
559-
utility::string_t content_type= _XPLATSTR("application/octet-stream"),
560-
pplx::cancellation_token token = pplx::cancellation_token::none())
561+
const utility::string_t &content_type = _XPLATSTR("application/octet-stream"),
562+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
561563
{
562-
http_request msg(std::move(mtd));
564+
http_request msg(mtd);
563565
msg.set_request_uri(path_query_fragment);
564-
msg.set_body(body, content_length, std::move(content_type));
566+
msg.set_body(body, content_length, content_type);
565567
return request(msg, token);
566568
}
567569

@@ -576,22 +578,22 @@ class http_client
576578
/// <returns>A task that is completed once a response from the request is received.</returns>
577579
/// <remarks>Winrt requires to provide content_length.</remarks>
578580
pplx::task<http_response> request(
579-
method mtd,
581+
const method &mtd,
580582
const utility::string_t &path_query_fragment,
581-
concurrency::streams::istream body,
583+
const concurrency::streams::istream &body,
582584
size_t content_length,
583-
pplx::cancellation_token token)
585+
const pplx::cancellation_token &token)
584586
{
585587
return request(mtd, path_query_fragment, body, content_length, _XPLATSTR("application/octet-stream"), token);
586588
}
587589

588590
private:
589591

590592
void build_pipeline(uri base_uri, http_client_config client_config);
591-
593+
592594
std::shared_ptr<::web::http::http_pipeline> m_pipeline;
593595
};
594596

595-
}}} // namespaces
597+
}}}
596598

597599
#endif /* _CASA_HTTP_CLIENT_H */

Release/include/cpprest/http_client_impl.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/***
22
* ==++==
33
*
4-
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
@@ -40,7 +40,7 @@ using namespace Platform;
4040
using namespace Microsoft::WRL;
4141
#endif
4242

43-
using namespace web;
43+
using namespace web;
4444
using namespace concurrency;
4545
#else
4646
#include <boost/asio.hpp>
@@ -162,7 +162,7 @@ class request_context
162162
void report_exception(std::exception_ptr exceptionPtr)
163163
{
164164
auto response_impl = m_response._get_impl();
165-
165+
166166
// If cancellation has been triggered then ignore any errors.
167167
if(m_request._cancellation_token().is_canceled())
168168
{
@@ -221,10 +221,10 @@ class request_context
221221

222222
protected:
223223

224-
request_context(std::shared_ptr<_http_client_communicator> client, http_request &request)
225-
: m_http_client(client),
224+
request_context(const std::shared_ptr<_http_client_communicator> &client, const http_request &request)
225+
: m_http_client(client),
226226
m_request(request),
227-
m_exceptionPtr(),
227+
m_exceptionPtr(),
228228
m_uploaded(0),
229229
m_downloaded(0)
230230
{
@@ -254,7 +254,7 @@ class _http_client_communicator
254254
virtual ~_http_client_communicator() {}
255255

256256
// Asynchronously send a HTTP request and process the response.
257-
void async_send_request(std::shared_ptr<request_context> request)
257+
void async_send_request(const std::shared_ptr<request_context> &request)
258258
{
259259
if(m_client_config.guarantee_order())
260260
{
@@ -314,7 +314,7 @@ class _http_client_communicator
314314
virtual unsigned long open() = 0;
315315

316316
// HTTP client implementations must implement send_request.
317-
virtual void send_request(_In_ std::shared_ptr<request_context> request) = 0;
317+
virtual void send_request(_In_ const std::shared_ptr<request_context> &request) = 0;
318318

319319
// URI to connect to.
320320
const http::uri m_uri;
@@ -328,7 +328,7 @@ class _http_client_communicator
328328
pplx::extensibility::critical_section_t m_open_lock;
329329

330330
// Wraps opening the client around sending a request.
331-
void open_and_send_request(std::shared_ptr<request_context> request)
331+
void open_and_send_request(const std::shared_ptr<request_context> &request)
332332
{
333333
// First see if client needs to be opened.
334334
auto error = open_if_required();
@@ -370,16 +370,14 @@ class _http_client_communicator
370370
return error;
371371
}
372372

373-
void push_request(_In_opt_ std::shared_ptr<request_context> request)
373+
void push_request(const std::shared_ptr<request_context> &request)
374374
{
375-
if (request == nullptr) return;
376-
377375
pplx::extensibility::scoped_critical_section_t l(m_open_lock);
378376

379377
if(++m_scheduled == 1)
380378
{
381379
// Schedule a task to start sending.
382-
pplx::create_task([this, request]() -> void
380+
pplx::create_task([this, request]()
383381
{
384382
open_and_send_request(request);
385383
});
@@ -403,7 +401,7 @@ inline void request_context::finish()
403401
_ASSERTE(m_request._cancellation_token() != pplx::cancellation_token::none());
404402
m_request._cancellation_token().deregister_callback(m_cancellationRegistration);
405403
}
406-
404+
407405
m_http_client->finish_request();
408406
}
409407

0 commit comments

Comments
 (0)