Skip to content

Commit c06afc5

Browse files
committed
A bunch of misc changes:
1. Removed duplicated storage of http_client base uri. 2. Added missing move constructor/assignment operator for uri. 3. Deleted unnecessary vector in build_pipeline. 4. Added noexcept to some move constructors. 5. Renamed _uri_components to uri_components. 6. Made uri_parser a static class.
1 parent af1c0e1 commit c06afc5

File tree

9 files changed

+96
-54
lines changed

9 files changed

+96
-54
lines changed

Release/include/cpprest/base_uri.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,39 @@
4040
namespace web {
4141
namespace details
4242
{
43-
struct _uri_components
43+
struct uri_components
4444
{
45-
_uri_components()
45+
uri_components()
4646
{
4747
m_path = _XPLATSTR("/");
4848
m_port = -1;
4949
}
5050

51+
uri_components(uri_components &&other) _noexcept :
52+
m_scheme(std::move(other.m_scheme)),
53+
m_host(std::move(other.m_host)),
54+
m_user_info(std::move(other.m_user_info)),
55+
m_path(std::move(other.m_path)),
56+
m_query(std::move(other.m_query)),
57+
m_fragment(std::move(other.m_fragment)),
58+
m_port(other.m_port)
59+
{}
60+
61+
uri_components & operator=(uri_components &&other) _noexcept
62+
{
63+
if (this != &other)
64+
{
65+
m_scheme = std::move(other.m_scheme);
66+
m_host = std::move(other.m_host);
67+
m_user_info = std::move(other.m_user_info);
68+
m_path = std::move(other.m_path);
69+
m_query = std::move(other.m_query);
70+
m_fragment = std::move(other.m_fragment);
71+
m_port = other.m_port;
72+
}
73+
return *this;
74+
}
75+
5176
_ASYNCRTIMP utility::string_t join();
5277

5378
utility::string_t m_scheme;
@@ -193,6 +218,27 @@ namespace web {
193218
/// <param name="uri_string">An encoded uri string to create the URI instance.</param>
194219
_ASYNCRTIMP uri(const utility::string_t &uri_string);
195220

221+
/// <summary>
222+
/// Move constructor.
223+
/// </summary>
224+
uri(uri &&other) _noexcept :
225+
m_uri(std::move(other.m_uri)),
226+
m_components(std::move(other.m_components))
227+
{}
228+
229+
/// <summary>
230+
/// Move assignment operator
231+
/// </summary>
232+
uri & operator=(uri &&other) _noexcept
233+
{
234+
if (this != &other)
235+
{
236+
m_uri = std::move(other.m_uri);
237+
m_components = std::move(m_components);
238+
}
239+
return *this;
240+
}
241+
196242
/// <summary>
197243
/// Get the scheme component of the URI as an encoded string.
198244
/// </summary>
@@ -358,7 +404,7 @@ namespace web {
358404
static utility::string_t encode_impl(const utility::string_t &raw, const std::function<bool(int)>& should_encode);
359405

360406
utility::string_t m_uri;
361-
details::_uri_components m_components;
407+
details::uri_components m_components;
362408
};
363409

364410
} // namespace web

Release/include/cpprest/http_client.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,21 @@ class http_client
377377
/// <returns>
378378
/// A base uri initialized in constructor
379379
/// </return>
380-
const uri& base_uri() const
381-
{
382-
return _base_uri;
383-
}
380+
const uri& base_uri() const;
384381

385382
/// <summary>
386383
/// Get client configuration object
387384
/// </summary>
388385
/// <returns>A reference to the client configuration object.</returns>
389-
_ASYNCRTIMP const http_client_config& client_config() const;
386+
const http_client_config& client_config() const;
390387

391388
/// <summary>
392389
/// Adds an HTTP pipeline stage to the client.
393390
/// </summary>
394391
/// <param name="handler">A function object representing the pipeline stage.</param>
395392
void add_handler(std::function<pplx::task<http_response>(http_request, std::shared_ptr<http::http_pipeline_stage>)> handler)
396393
{
397-
m_pipeline->append(std::make_shared< ::web::http::details::function_pipeline_wrapper>(handler));
394+
m_pipeline->append(std::make_shared<::web::http::details::function_pipeline_wrapper>(handler));
398395
}
399396

400397
/// <summary>
@@ -593,8 +590,6 @@ class http_client
593590
void build_pipeline(uri base_uri, http_client_config client_config);
594591

595592
std::shared_ptr<::web::http::http_pipeline> m_pipeline;
596-
597-
uri _base_uri;
598593
};
599594

600595
}}} // namespaces

Release/include/cpprest/http_client_impl.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ class _http_client_communicator
299299
return m_client_config;
300300
}
301301

302+
const uri & uri() const
303+
{
304+
return m_uri;
305+
}
306+
302307
protected:
303308
_http_client_communicator(http::uri address, http_client_config client_config)
304309
: m_uri(std::move(address)), m_client_config(std::move(client_config)), m_opened(false), m_scheduled(0)
@@ -437,15 +442,13 @@ void verify_uri(const uri &uri)
437442
} // namespace details
438443

439444
http_client::http_client(uri base_uri)
440-
:_base_uri(std::move(base_uri))
441445
{
442-
build_pipeline(_base_uri, http_client_config());
446+
build_pipeline(std::move(base_uri), http_client_config());
443447
}
444448

445449
http_client::http_client(uri base_uri, http_client_config client_config)
446-
:_base_uri(std::move(base_uri))
447450
{
448-
build_pipeline(_base_uri, std::move(client_config));
451+
build_pipeline(std::move(base_uri), std::move(client_config));
449452
}
450453

451454
void http_client::build_pipeline(uri base_uri, http_client_config client_config)
@@ -458,26 +461,27 @@ void http_client::build_pipeline(uri base_uri, http_client_config client_config)
458461
}
459462
details::verify_uri(base_uri);
460463

461-
std::vector<std::shared_ptr<http::http_pipeline_stage> > extra_handlers;
462-
463464
#if !defined(CPPREST_TARGET_XP) && !defined(_PHONE8_)
464-
extra_handlers.push_back(std::make_shared<oauth1::details::oauth1_handler>(client_config.oauth1()));
465+
add_handler(std::static_pointer_cast<http::http_pipeline_stage>(
466+
std::make_shared<oauth1::details::oauth1_handler>(client_config.oauth1())));
465467
#endif // !defined(CPPREST_TARGET_XP) && !defined(_PHONE8_)
466468

467-
extra_handlers.push_back(std::make_shared<oauth2::details::oauth2_handler>(client_config.oauth2()));
469+
add_handler(std::static_pointer_cast<http::http_pipeline_stage>(
470+
std::make_shared<oauth2::details::oauth2_handler>(client_config.oauth2())));
468471

469472
m_pipeline = ::web::http::http_pipeline::create_pipeline(std::make_shared<details::http_network_handler>(std::move(base_uri), std::move(client_config)));
470-
471-
for (auto& handler : extra_handlers)
472-
{
473-
add_handler(handler);
474-
}
475473
}
476474

477-
const http_client_config& http_client::client_config() const
475+
const http_client_config & http_client::client_config() const
478476
{
479-
auto* ph = static_cast<details::http_network_handler*>(m_pipeline->last_stage().get());
477+
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
480478
return ph->http_client_impl()->client_config();
481479
}
482480

481+
const uri & http_client::base_uri() const
482+
{
483+
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
484+
return ph->http_client_impl()->uri();
485+
}
486+
483487
}}} // namespaces

Release/include/cpprest/http_msg.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ class http_pipeline
12471247
{
12481248

12491249
}
1250+
12501251
/// <summary>
12511252
/// Create an http pipeline that consists of a linear chain of stages
12521253
/// </summary>
@@ -1304,8 +1305,6 @@ class http_pipeline
13041305
{
13051306
}
13061307

1307-
private:
1308-
13091308
// The vector of pipeline stages.
13101309
std::vector<std::shared_ptr<http_pipeline_stage>> m_stages;
13111310

Release/include/cpprest/json.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ namespace json
169169
/// <summary>
170170
/// Move constructor
171171
/// </summary>
172-
_ASYNCRTIMP value(value &&);
172+
_ASYNCRTIMP value(value &&) _noexcept ;
173173

174174
/// <summary>
175175
/// Assignment operator.
@@ -181,7 +181,7 @@ namespace json
181181
/// Move assignment operator.
182182
/// </summary>
183183
/// <returns>The JSON value object that contains the result of the assignment.</returns>
184-
_ASYNCRTIMP value &operator=(value &&);
184+
_ASYNCRTIMP value &operator=(value &&) _noexcept ;
185185

186186
// Static factories
187187

Release/include/cpprest/uri_builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ namespace web
203203
/// </summary>
204204
void clear()
205205
{
206-
m_uri = details::_uri_components();
206+
m_uri = details::uri_components();
207207
}
208208

209209
/// <summary>
@@ -263,6 +263,6 @@ namespace web
263263
_ASYNCRTIMP bool is_valid();
264264

265265
private:
266-
details::_uri_components m_uri;
266+
details::uri_components m_uri;
267267
};
268268
} // namespace web

Release/include/cpprest/uri_parser.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ namespace web { namespace details
3333
class uri_parser
3434
{
3535
public:
36-
uri_parser() {}
3736

3837
/// <summary>
3938
/// Parses the uri, attempting to determine its validity.
4039
///
4140
/// This function accepts both uris ('http://msn.com') and uri relative-references ('path1/path2?query')
4241
/// </summary>
43-
bool validate(const utility::string_t &encoded_string) const;
42+
static bool validate(const utility::string_t &encoded_string);
4443

4544
/// <summary>
4645
/// Parses the uri, setting each provided string to the value of that component. Components
@@ -49,9 +48,7 @@ namespace web { namespace details
4948
///
5049
/// This function accepts both uris ('http://msn.com') and uri relative-references ('path1/path2?query')
5150
/// </summary>
52-
bool parse(
53-
const utility::string_t &encoded_string,
54-
_uri_components &components) const;
51+
static bool parse(const utility::string_t &encoded_string, uri_components &components);
5552

5653
/// <summary>
5754
/// Unreserved characters are those that are allowed in a URI but do not have a reserved purpose. They include:
@@ -204,19 +201,23 @@ namespace web { namespace details
204201
}
205202

206203
private:
204+
uri_parser();
205+
uri_parser(const uri_parser &);
206+
uri_parser & operator=(const uri_parser &);
207+
207208
/// <summary>
208209
/// Parses the uri, setting the given pointers to locations inside the given buffer.
209210
/// 'encoded' is expected to point to an encoded zero-terminated string containing a uri
210211
/// </summary>
211-
bool inner_parse(
212+
static bool inner_parse(
212213
const utility::char_t *encoded,
213214
const utility::char_t **scheme_begin, const utility::char_t **scheme_end,
214215
const utility::char_t **uinfo_begin, const utility::char_t **uinfo_end,
215216
const utility::char_t **host_begin, const utility::char_t **host_end,
216217
_Out_ int *port,
217218
const utility::char_t **path_begin, const utility::char_t **path_end,
218219
const utility::char_t **query_begin, const utility::char_t **query_end,
219-
const utility::char_t **fragment_begin, const utility::char_t **fragment_end) const;
220+
const utility::char_t **fragment_begin, const utility::char_t **fragment_end);
220221

221222
static const std::locale loc;
222223
};

Release/src/uri/uri.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using namespace utility::conversions;
3434

3535
namespace web { namespace details
3636
{
37-
utility::string_t _uri_components::join()
37+
utility::string_t uri_components::join()
3838
{
3939
// canonicalize components first
4040

@@ -103,7 +103,7 @@ using namespace details;
103103

104104
uri::uri(const utility::string_t &uri_string)
105105
{
106-
if (!details::uri_parser().parse(uri_string, m_components))
106+
if (!details::uri_parser::parse(uri_string, m_components))
107107
{
108108
throw uri_exception("provided uri is invalid: " + utility::conversions::to_utf8string(uri_string));
109109
}
@@ -112,7 +112,7 @@ uri::uri(const utility::string_t &uri_string)
112112

113113
uri::uri(const utility::char_t *uri_string): m_uri(uri_string)
114114
{
115-
if (!details::uri_parser().parse(uri_string, m_components))
115+
if (!details::uri_parser::parse(uri_string, m_components))
116116
{
117117
throw uri_exception("provided uri is invalid: " + utility::conversions::to_utf8string(uri_string));
118118
}
@@ -314,7 +314,7 @@ std::map<utility::string_t, utility::string_t> uri::split_query(const utility::s
314314

315315
bool uri::validate(const utility::string_t &uri_string)
316316
{
317-
return uri_parser().validate(uri_string);
317+
return uri_parser::validate(uri_string);
318318
}
319319

320320
uri uri::authority() const

Release/src/uri/uri_parser.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace web { namespace details
3131

3232
const std::locale uri_parser::loc("C"); // use the C local to force the ASCII definitions of isalpha and friends
3333

34-
bool uri_parser::validate(const utility::string_t &encoded_string) const
34+
bool uri_parser::validate(const utility::string_t &encoded_string)
3535
{
3636
const utility::char_t *scheme_begin = nullptr;
3737
const utility::char_t *scheme_end = nullptr;
@@ -65,9 +65,7 @@ bool uri_parser::validate(const utility::string_t &encoded_string) const
6565
&fragment_end);
6666
}
6767

68-
bool uri_parser::parse(
69-
const utility::string_t &encoded_string,
70-
_uri_components &components) const
68+
bool uri_parser::parse(const utility::string_t &encoded_string, uri_components &components)
7169
{
7270
const utility::char_t *scheme_begin = nullptr;
7371
const utility::char_t *scheme_end = nullptr;
@@ -104,8 +102,8 @@ bool uri_parser::parse(
104102
components.m_scheme.assign(scheme_begin, scheme_end);
105103

106104
// convert scheme to lowercase
107-
std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [this](utility::char_t c) {
108-
return std::tolower(c, this->loc);
105+
std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [](utility::char_t c) {
106+
return std::tolower(c, loc);
109107
});
110108
}
111109
else
@@ -123,8 +121,8 @@ bool uri_parser::parse(
123121
components.m_host.assign(host_begin, host_end);
124122

125123
// convert host to lowercase
126-
std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [this](utility::char_t c) {
127-
return std::tolower(c, this->loc);
124+
std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [](utility::char_t c) {
125+
return std::tolower(c, loc);
128126
});
129127
}
130128
else
@@ -174,8 +172,7 @@ bool uri_parser::parse(
174172
else
175173
{
176174
return false;
177-
}
178-
175+
}
179176
}
180177

181178
bool uri_parser::inner_parse(
@@ -186,7 +183,7 @@ bool uri_parser::inner_parse(
186183
_Out_ int *port,
187184
const utility::char_t **path_begin, const utility::char_t **path_end,
188185
const utility::char_t **query_begin, const utility::char_t **query_end,
189-
const utility::char_t **fragment_begin, const utility::char_t **fragment_end) const
186+
const utility::char_t **fragment_begin, const utility::char_t **fragment_end)
190187
{
191188
*scheme_begin = nullptr;
192189
*scheme_end = nullptr;

0 commit comments

Comments
 (0)