Skip to content

Commit 6f5f815

Browse files
ras0219ras0219-msft
authored andcommitted
Move definitions from http_helpers into http_msg, removing external declarations
1 parent 2b79bd3 commit 6f5f815

File tree

3 files changed

+156
-161
lines changed

3 files changed

+156
-161
lines changed

Release/include/cpprest/details/http_helpers.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,6 @@ namespace details
6060
#undef DAT
6161
};
6262

63-
/// <summary>
64-
/// Determines whether or not the given content type is 'textual' according the feature specifications.
65-
/// </summary>
66-
bool is_content_type_textual(const utility::string_t &content_type);
67-
68-
/// <summary>
69-
/// Determines whether or not the given content type is JSON according the feature specifications.
70-
/// </summary>
71-
bool is_content_type_json(const utility::string_t &content_type);
72-
73-
/// <summary>
74-
/// Parses the given Content-Type header value to get out actual content type and charset.
75-
/// If the charset isn't specified the default charset for the content type will be set.
76-
/// </summary>
77-
void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset);
78-
79-
/// <summary>
80-
/// Gets the default charset for given content type. If the MIME type is not textual or recognized Latin1 will be returned.
81-
/// </summary>
82-
utility::string_t get_default_charset(const utility::string_t &content_type);
83-
8463
/// <summary>
8564
/// Helper function to get the default HTTP reason phrase for a status code.
8665
/// </summary>

Release/src/http/common/http_helpers.cpp

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -34,145 +34,6 @@ namespace web { namespace http
3434
namespace details
3535
{
3636

37-
bool is_content_type_one_of(const utility::string_t *first, const utility::string_t *last, const utility::string_t &value)
38-
{
39-
while (first != last)
40-
{
41-
if (utility::details::str_icmp(*first, value))
42-
{
43-
return true;
44-
}
45-
++first;
46-
}
47-
return false;
48-
}
49-
50-
// Remove once VS 2013 is no longer supported.
51-
#if defined(_WIN32) && _MSC_VER < 1900
52-
// Not referring to mime_types to avoid static initialization order fiasco.
53-
static const utility::string_t textual_types [] = {
54-
U("message/http"),
55-
U("application/json"),
56-
U("application/xml"),
57-
U("application/atom+xml"),
58-
U("application/http"),
59-
U("application/x-www-form-urlencoded")
60-
};
61-
#endif
62-
bool is_content_type_textual(const utility::string_t &content_type)
63-
{
64-
#if !defined(_WIN32) || _MSC_VER >= 1900
65-
static const utility::string_t textual_types [] = {
66-
mime_types::message_http,
67-
mime_types::application_json,
68-
mime_types::application_xml,
69-
mime_types::application_atom_xml,
70-
mime_types::application_http,
71-
mime_types::application_x_www_form_urlencoded
72-
};
73-
#endif
74-
75-
if (content_type.size() >= 4 && utility::details::str_icmp(content_type.substr(0, 4), _XPLATSTR("text")))
76-
{
77-
return true;
78-
}
79-
return (is_content_type_one_of(std::begin(textual_types), std::end(textual_types), content_type));
80-
}
81-
82-
// Remove once VS 2013 is no longer supported.
83-
#if defined(_WIN32) && _MSC_VER < 1900
84-
// Not referring to mime_types to avoid static initialization order fiasco.
85-
static const utility::string_t json_types [] = {
86-
U("application/json"),
87-
U("application/x-json"),
88-
U("text/json"),
89-
U("text/x-json"),
90-
U("text/javascript"),
91-
U("text/x-javascript"),
92-
U("application/javascript"),
93-
U("application/x-javascript")
94-
};
95-
#endif
96-
bool is_content_type_json(const utility::string_t &content_type)
97-
{
98-
#if !defined(_WIN32) || _MSC_VER >= 1900
99-
static const utility::string_t json_types [] = {
100-
mime_types::application_json,
101-
mime_types::application_xjson,
102-
mime_types::text_json,
103-
mime_types::text_xjson,
104-
mime_types::text_javascript,
105-
mime_types::text_xjavascript,
106-
mime_types::application_javascript,
107-
mime_types::application_xjavascript
108-
};
109-
#endif
110-
111-
return (is_content_type_one_of(std::begin(json_types), std::end(json_types), content_type));
112-
}
113-
114-
void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset)
115-
{
116-
const size_t semi_colon_index = content_type.find_first_of(_XPLATSTR(";"));
117-
118-
// No charset specified.
119-
if (semi_colon_index == utility::string_t::npos)
120-
{
121-
content = content_type;
122-
trim_whitespace(content);
123-
charset = get_default_charset(content);
124-
return;
125-
}
126-
127-
// Split into content type and second part which could be charset.
128-
content = content_type.substr(0, semi_colon_index);
129-
trim_whitespace(content);
130-
utility::string_t possible_charset = content_type.substr(semi_colon_index + 1);
131-
trim_whitespace(possible_charset);
132-
const size_t equals_index = possible_charset.find_first_of(_XPLATSTR("="));
133-
134-
// No charset specified.
135-
if (equals_index == utility::string_t::npos)
136-
{
137-
charset = get_default_charset(content);
138-
return;
139-
}
140-
141-
// Split and make sure 'charset'
142-
utility::string_t charset_key = possible_charset.substr(0, equals_index);
143-
trim_whitespace(charset_key);
144-
if (!utility::details::str_icmp(charset_key, _XPLATSTR("charset")))
145-
{
146-
charset = get_default_charset(content);
147-
return;
148-
}
149-
charset = possible_charset.substr(equals_index + 1);
150-
// Remove the redundant ';' at the end of charset.
151-
while (charset.back() == ';')
152-
{
153-
charset.pop_back();
154-
}
155-
trim_whitespace(charset);
156-
if (charset.front() == _XPLATSTR('"') && charset.back() == _XPLATSTR('"'))
157-
{
158-
charset = charset.substr(1, charset.size() - 2);
159-
trim_whitespace(charset);
160-
}
161-
}
162-
163-
utility::string_t get_default_charset(const utility::string_t &content_type)
164-
{
165-
// We are defaulting everything to Latin1 except JSON which is utf-8.
166-
if (is_content_type_json(content_type))
167-
{
168-
return charset_types::utf8;
169-
}
170-
else
171-
{
172-
return charset_types::latin1;
173-
}
174-
}
175-
17637
// Remove once VS 2013 is no longer supported.
17738
#if defined(_WIN32) && _MSC_VER < 1900
17839
static const http_status_to_phrase idToPhraseMap [] = {

Release/src/http/common/http_msg.cpp

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,168 @@ void http_msg_base::_complete(utility::size64_t body_size, const std::exception_
218218
}
219219
}
220220

221+
static bool is_content_type_one_of(const utility::string_t *first, const utility::string_t *last, const utility::string_t &value)
222+
{
223+
while (first != last)
224+
{
225+
if (utility::details::str_icmp(*first, value))
226+
{
227+
return true;
228+
}
229+
++first;
230+
}
231+
return false;
232+
}
233+
234+
// Remove once VS 2013 is no longer supported.
235+
#if defined(_WIN32) && _MSC_VER < 1900
236+
// Not referring to mime_types to avoid static initialization order fiasco.
237+
static const utility::string_t textual_types [] = {
238+
U("message/http"),
239+
U("application/json"),
240+
U("application/xml"),
241+
U("application/atom+xml"),
242+
U("application/http"),
243+
U("application/x-www-form-urlencoded")
244+
};
245+
#endif
246+
247+
/// <summary>
248+
/// Determines whether or not the given content type is 'textual' according the feature specifications.
249+
/// </summary>
250+
static bool is_content_type_textual(const utility::string_t &content_type)
251+
{
252+
#if !defined(_WIN32) || _MSC_VER >= 1900
253+
static const utility::string_t textual_types [] = {
254+
mime_types::message_http,
255+
mime_types::application_json,
256+
mime_types::application_xml,
257+
mime_types::application_atom_xml,
258+
mime_types::application_http,
259+
mime_types::application_x_www_form_urlencoded
260+
};
261+
#endif
262+
263+
if (content_type.size() >= 4 && utility::details::str_icmp(content_type.substr(0, 4), _XPLATSTR("text")))
264+
{
265+
return true;
266+
}
267+
return (is_content_type_one_of(std::begin(textual_types), std::end(textual_types), content_type));
268+
}
269+
270+
// Remove once VS 2013 is no longer supported.
271+
#if defined(_WIN32) && _MSC_VER < 1900
272+
// Not referring to mime_types to avoid static initialization order fiasco.
273+
static const utility::string_t json_types [] = {
274+
U("application/json"),
275+
U("application/x-json"),
276+
U("text/json"),
277+
U("text/x-json"),
278+
U("text/javascript"),
279+
U("text/x-javascript"),
280+
U("application/javascript"),
281+
U("application/x-javascript")
282+
};
283+
#endif
284+
285+
/// <summary>
286+
/// Determines whether or not the given content type is JSON according the feature specifications.
287+
/// </summary>
288+
static bool is_content_type_json(const utility::string_t &content_type)
289+
{
290+
#if !defined(_WIN32) || _MSC_VER >= 1900
291+
static const utility::string_t json_types [] = {
292+
mime_types::application_json,
293+
mime_types::application_xjson,
294+
mime_types::text_json,
295+
mime_types::text_xjson,
296+
mime_types::text_javascript,
297+
mime_types::text_xjavascript,
298+
mime_types::application_javascript,
299+
mime_types::application_xjavascript
300+
};
301+
#endif
302+
303+
return (is_content_type_one_of(std::begin(json_types), std::end(json_types), content_type));
304+
}
305+
306+
/// <summary>
307+
/// Gets the default charset for given content type. If the MIME type is not textual or recognized Latin1 will be returned.
308+
/// </summary>
309+
static utility::string_t get_default_charset(const utility::string_t &content_type)
310+
{
311+
// We are defaulting everything to Latin1 except JSON which is utf-8.
312+
if (is_content_type_json(content_type))
313+
{
314+
return charset_types::utf8;
315+
}
316+
else
317+
{
318+
return charset_types::latin1;
319+
}
320+
}
321+
322+
323+
/// <summary>
324+
/// Parses the given Content-Type header value to get out actual content type and charset.
325+
/// If the charset isn't specified the default charset for the content type will be set.
326+
/// </summary>
327+
static void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset)
328+
{
329+
const size_t semi_colon_index = content_type.find_first_of(_XPLATSTR(";"));
330+
331+
// No charset specified.
332+
if (semi_colon_index == utility::string_t::npos)
333+
{
334+
content = content_type;
335+
trim_whitespace(content);
336+
charset = get_default_charset(content);
337+
return;
338+
}
339+
340+
// Split into content type and second part which could be charset.
341+
content = content_type.substr(0, semi_colon_index);
342+
trim_whitespace(content);
343+
utility::string_t possible_charset = content_type.substr(semi_colon_index + 1);
344+
trim_whitespace(possible_charset);
345+
const size_t equals_index = possible_charset.find_first_of(_XPLATSTR("="));
346+
347+
// No charset specified.
348+
if (equals_index == utility::string_t::npos)
349+
{
350+
charset = get_default_charset(content);
351+
return;
352+
}
353+
354+
// Split and make sure 'charset'
355+
utility::string_t charset_key = possible_charset.substr(0, equals_index);
356+
trim_whitespace(charset_key);
357+
if (!utility::details::str_icmp(charset_key, _XPLATSTR("charset")))
358+
{
359+
charset = get_default_charset(content);
360+
return;
361+
}
362+
charset = possible_charset.substr(equals_index + 1);
363+
// Remove the redundant ';' at the end of charset.
364+
while (charset.back() == ';')
365+
{
366+
charset.pop_back();
367+
}
368+
trim_whitespace(charset);
369+
if (charset.front() == _XPLATSTR('"') && charset.back() == _XPLATSTR('"'))
370+
{
371+
charset = charset.substr(1, charset.size() - 2);
372+
trim_whitespace(charset);
373+
}
374+
}
375+
221376
utility::string_t details::http_msg_base::parse_and_check_content_type(bool ignore_content_type, const std::function<bool(const utility::string_t &)> &check_content_type)
222377
{
223378
if (!instream())
224379
{
225380
throw http_exception(stream_was_set_explicitly);
226381
}
227-
382+
228383
utility::string_t content, charset = charset_types::utf8;
229384
if (!ignore_content_type)
230385
{

0 commit comments

Comments
 (0)