Skip to content

Commit 6a52f3d

Browse files
committed
Making static uri_parse class into a namespace.
1 parent e9293d7 commit 6a52f3d

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

Release/include/cpprest/base_uri.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ namespace web {
4242
{
4343
struct uri_components
4444
{
45-
uri_components()
46-
{
47-
m_path = _XPLATSTR("/");
48-
m_port = -1;
49-
}
45+
uri_components() : m_path(_XPLATSTR("/")), m_port(-1)
46+
{}
5047

5148
uri_components(uri_components &&other) _noexcept :
5249
m_scheme(std::move(other.m_scheme)),

Release/include/cpprest/uri_parser.h

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@
2525

2626
#pragma once
2727

28-
#include <locale>
2928
#include <string>
3029

3130
namespace web { namespace details
3231
{
33-
class uri_parser
32+
namespace uri_parser
3433
{
35-
public:
3634

3735
/// <summary>
3836
/// Parses the uri, attempting to determine its validity.
3937
///
4038
/// This function accepts both uris ('http://msn.com') and uri relative-references ('path1/path2?query')
4139
/// </summary>
42-
static bool validate(const utility::string_t &encoded_string);
40+
bool validate(const utility::string_t &encoded_string);
4341

4442
/// <summary>
4543
/// Parses the uri, setting each provided string to the value of that component. Components
@@ -48,7 +46,7 @@ namespace web { namespace details
4846
///
4947
/// This function accepts both uris ('http://msn.com') and uri relative-references ('path1/path2?query')
5048
/// </summary>
51-
static bool parse(const utility::string_t &encoded_string, uri_components &components);
49+
bool parse(const utility::string_t &encoded_string, uri_components &components);
5250

5351
/// <summary>
5452
/// Unreserved characters are those that are allowed in a URI but do not have a reserved purpose. They include:
@@ -60,26 +58,17 @@ namespace web { namespace details
6058
/// - '_' (underscore)
6159
/// - '~' (tilde)
6260
/// </summary>
63-
static bool is_unreserved(int c)
61+
inline bool is_unreserved(int c)
6462
{
6563
return ::utility::details::is_alnum((char)c) || c == '-' || c == '.' || c == '_' || c == '~';
6664
}
6765

68-
/// <summary>
69-
/// Reserved characters includes the general delimiters and sub delimiters. Some characters
70-
/// are neither reserved nor unreserved, and must be percent-encoded.
71-
/// </summary>
72-
static bool is_reserved(int c)
73-
{
74-
return is_gen_delim(c) || is_sub_delim(c);
75-
}
76-
7766
/// <summary>
7867
/// General delimiters serve as the delimiters between different uri components.
7968
/// General delimiters include:
8069
/// - All of these :/?#[]@
8170
/// </summary>
82-
static bool is_gen_delim(int c)
71+
inline bool is_gen_delim(int c)
8372
{
8473
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' || c == ']' || c == '@';
8574
}
@@ -90,7 +79,7 @@ namespace web { namespace details
9079
/// uri segments. sub_delimiters include:
9180
/// - All of these !$&'()*+,;=
9281
/// </summary>
93-
static bool is_sub_delim(int c)
82+
inline bool is_sub_delim(int c)
9483
{
9584
switch (c)
9685
{
@@ -111,6 +100,15 @@ namespace web { namespace details
111100
}
112101
}
113102

103+
/// <summary>
104+
/// Reserved characters includes the general delimiters and sub delimiters. Some characters
105+
/// are neither reserved nor unreserved, and must be percent-encoded.
106+
/// </summary>
107+
inline bool is_reserved(int c)
108+
{
109+
return is_gen_delim(c) || is_sub_delim(c);
110+
}
111+
114112
/// <summary>
115113
/// Legal characters in the scheme portion include:
116114
/// - Any alphanumeric character
@@ -120,7 +118,7 @@ namespace web { namespace details
120118
///
121119
/// Note that the scheme must BEGIN with an alpha character.
122120
/// </summary>
123-
static bool is_scheme_character(int c)
121+
inline bool is_scheme_character(int c)
124122
{
125123
return ::utility::details::is_alnum((char)c) || c == '+' || c == '-' || c == '.';
126124
}
@@ -132,7 +130,7 @@ namespace web { namespace details
132130
/// - The sub-delimiters
133131
/// - ':' (colon)
134132
/// </summary>
135-
static bool is_user_info_character(int c)
133+
inline bool is_user_info_character(int c)
136134
{
137135
return is_unreserved(c) || is_sub_delim(c) || c == '%' || c == ':';
138136
}
@@ -146,7 +144,7 @@ namespace web { namespace details
146144
/// - '[' (open bracket)
147145
/// - ']' (close bracket)
148146
/// </summary>
149-
static bool is_host_character(int c)
147+
inline bool is_host_character(int c)
150148
{
151149
return is_unreserved(c) || is_sub_delim(c) || c == '%' || c == ':' || c == '[' || c == ']';
152150
}
@@ -161,7 +159,7 @@ namespace web { namespace details
161159
/// Note that we don't currently support:
162160
/// - IPv6 addresses (requires '[]')
163161
/// </summary>
164-
static bool is_authority_character(int c)
162+
inline bool is_authority_character(int c)
165163
{
166164
return is_unreserved(c) || is_sub_delim(c) || c == '%' || c == '@' || c == ':';
167165
}
@@ -174,7 +172,7 @@ namespace web { namespace details
174172
/// - ':' (colon)
175173
/// - '@' (ampersand)
176174
/// </summary>
177-
static bool is_path_character(int c)
175+
inline bool is_path_character(int c)
178176
{
179177
return is_unreserved(c) || is_sub_delim(c) || c == '%' || c == '/' || c == ':' || c == '@';
180178
}
@@ -184,7 +182,7 @@ namespace web { namespace details
184182
/// - Any path character
185183
/// - '?' (question mark)
186184
/// </summary>
187-
static bool is_query_character(int c)
185+
inline bool is_query_character(int c)
188186
{
189187
return is_path_character(c) || c == '?';
190188
}
@@ -194,22 +192,17 @@ namespace web { namespace details
194192
/// - Any path character
195193
/// - '?' (question mark)
196194
/// </summary>
197-
static bool is_fragment_character(int c)
195+
inline bool is_fragment_character(int c)
198196
{
199197
// this is intentional, they have the same set of legal characters
200198
return is_query_character(c);
201199
}
202200

203-
private:
204-
uri_parser();
205-
uri_parser(const uri_parser &);
206-
uri_parser & operator=(const uri_parser &);
207-
208201
/// <summary>
209202
/// Parses the uri, setting the given pointers to locations inside the given buffer.
210203
/// 'encoded' is expected to point to an encoded zero-terminated string containing a uri
211204
/// </summary>
212-
static bool inner_parse(
205+
bool inner_parse(
213206
const utility::char_t *encoded,
214207
const utility::char_t **scheme_begin, const utility::char_t **scheme_end,
215208
const utility::char_t **uinfo_begin, const utility::char_t **uinfo_end,
@@ -218,7 +211,5 @@ namespace web { namespace details
218211
const utility::char_t **path_begin, const utility::char_t **path_end,
219212
const utility::char_t **query_begin, const utility::char_t **query_end,
220213
const utility::char_t **fragment_begin, const utility::char_t **fragment_end);
221-
222-
static const std::locale loc;
223214
};
224215
}}

Release/src/uri/uri_parser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#include <locale>
2727
#include "cpprest/uri_parser.h"
2828

29-
namespace web { namespace details
29+
namespace web { namespace details { namespace uri_parser
3030
{
3131

32-
const std::locale uri_parser::loc("C"); // use the C local to force the ASCII definitions of isalpha and friends
32+
static std::locale g_clocale("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)
34+
bool 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,7 +65,7 @@ bool uri_parser::validate(const utility::string_t &encoded_string)
6565
&fragment_end);
6666
}
6767

68-
bool uri_parser::parse(const utility::string_t &encoded_string, uri_components &components)
68+
bool parse(const utility::string_t &encoded_string, uri_components &components)
6969
{
7070
const utility::char_t *scheme_begin = nullptr;
7171
const utility::char_t *scheme_end = nullptr;
@@ -103,7 +103,7 @@ bool uri_parser::parse(const utility::string_t &encoded_string, uri_components &
103103

104104
// convert scheme to lowercase
105105
std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [](utility::char_t c) {
106-
return std::tolower(c, loc);
106+
return std::tolower(c, g_clocale);
107107
});
108108
}
109109
else
@@ -122,7 +122,7 @@ bool uri_parser::parse(const utility::string_t &encoded_string, uri_components &
122122

123123
// convert host to lowercase
124124
std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [](utility::char_t c) {
125-
return std::tolower(c, loc);
125+
return std::tolower(c, g_clocale);
126126
});
127127
}
128128
else
@@ -175,7 +175,7 @@ bool uri_parser::parse(const utility::string_t &encoded_string, uri_components &
175175
}
176176
}
177177

178-
bool uri_parser::inner_parse(
178+
bool inner_parse(
179179
const utility::char_t *encoded,
180180
const utility::char_t **scheme_begin, const utility::char_t **scheme_end,
181181
const utility::char_t **uinfo_begin, const utility::char_t **uinfo_end,
@@ -366,4 +366,4 @@ bool uri_parser::inner_parse(
366366
return true;
367367
}
368368

369-
}}
369+
}}}

0 commit comments

Comments
 (0)