Skip to content

Commit

Permalink
Move definitions from http_helpers into http_msg, removing external d…
Browse files Browse the repository at this point in the history
…eclarations
  • Loading branch information
ras0219 authored and ras0219-msft committed Apr 8, 2016
1 parent 2b79bd3 commit 6f5f815
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 161 deletions.
21 changes: 0 additions & 21 deletions Release/include/cpprest/details/http_helpers.h
Expand Up @@ -60,27 +60,6 @@ namespace details
#undef DAT
};

/// <summary>
/// Determines whether or not the given content type is 'textual' according the feature specifications.
/// </summary>
bool is_content_type_textual(const utility::string_t &content_type);

/// <summary>
/// Determines whether or not the given content type is JSON according the feature specifications.
/// </summary>
bool is_content_type_json(const utility::string_t &content_type);

/// <summary>
/// Parses the given Content-Type header value to get out actual content type and charset.
/// If the charset isn't specified the default charset for the content type will be set.
/// </summary>
void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset);

/// <summary>
/// Gets the default charset for given content type. If the MIME type is not textual or recognized Latin1 will be returned.
/// </summary>
utility::string_t get_default_charset(const utility::string_t &content_type);

/// <summary>
/// Helper function to get the default HTTP reason phrase for a status code.
/// </summary>
Expand Down
139 changes: 0 additions & 139 deletions Release/src/http/common/http_helpers.cpp
Expand Up @@ -34,145 +34,6 @@ namespace web { namespace http
namespace details
{

bool is_content_type_one_of(const utility::string_t *first, const utility::string_t *last, const utility::string_t &value)
{
while (first != last)
{
if (utility::details::str_icmp(*first, value))
{
return true;
}
++first;
}
return false;
}

// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
// Not referring to mime_types to avoid static initialization order fiasco.
static const utility::string_t textual_types [] = {
U("message/http"),
U("application/json"),
U("application/xml"),
U("application/atom+xml"),
U("application/http"),
U("application/x-www-form-urlencoded")
};
#endif
bool is_content_type_textual(const utility::string_t &content_type)
{
#if !defined(_WIN32) || _MSC_VER >= 1900
static const utility::string_t textual_types [] = {
mime_types::message_http,
mime_types::application_json,
mime_types::application_xml,
mime_types::application_atom_xml,
mime_types::application_http,
mime_types::application_x_www_form_urlencoded
};
#endif

if (content_type.size() >= 4 && utility::details::str_icmp(content_type.substr(0, 4), _XPLATSTR("text")))
{
return true;
}
return (is_content_type_one_of(std::begin(textual_types), std::end(textual_types), content_type));
}

// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
// Not referring to mime_types to avoid static initialization order fiasco.
static const utility::string_t json_types [] = {
U("application/json"),
U("application/x-json"),
U("text/json"),
U("text/x-json"),
U("text/javascript"),
U("text/x-javascript"),
U("application/javascript"),
U("application/x-javascript")
};
#endif
bool is_content_type_json(const utility::string_t &content_type)
{
#if !defined(_WIN32) || _MSC_VER >= 1900
static const utility::string_t json_types [] = {
mime_types::application_json,
mime_types::application_xjson,
mime_types::text_json,
mime_types::text_xjson,
mime_types::text_javascript,
mime_types::text_xjavascript,
mime_types::application_javascript,
mime_types::application_xjavascript
};
#endif

return (is_content_type_one_of(std::begin(json_types), std::end(json_types), content_type));
}

void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset)
{
const size_t semi_colon_index = content_type.find_first_of(_XPLATSTR(";"));

// No charset specified.
if (semi_colon_index == utility::string_t::npos)
{
content = content_type;
trim_whitespace(content);
charset = get_default_charset(content);
return;
}

// Split into content type and second part which could be charset.
content = content_type.substr(0, semi_colon_index);
trim_whitespace(content);
utility::string_t possible_charset = content_type.substr(semi_colon_index + 1);
trim_whitespace(possible_charset);
const size_t equals_index = possible_charset.find_first_of(_XPLATSTR("="));

// No charset specified.
if (equals_index == utility::string_t::npos)
{
charset = get_default_charset(content);
return;
}

// Split and make sure 'charset'
utility::string_t charset_key = possible_charset.substr(0, equals_index);
trim_whitespace(charset_key);
if (!utility::details::str_icmp(charset_key, _XPLATSTR("charset")))
{
charset = get_default_charset(content);
return;
}
charset = possible_charset.substr(equals_index + 1);
// Remove the redundant ';' at the end of charset.
while (charset.back() == ';')
{
charset.pop_back();
}
trim_whitespace(charset);
if (charset.front() == _XPLATSTR('"') && charset.back() == _XPLATSTR('"'))
{
charset = charset.substr(1, charset.size() - 2);
trim_whitespace(charset);
}
}

utility::string_t get_default_charset(const utility::string_t &content_type)
{
// We are defaulting everything to Latin1 except JSON which is utf-8.
if (is_content_type_json(content_type))
{
return charset_types::utf8;
}
else
{
return charset_types::latin1;
}
}

// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
static const http_status_to_phrase idToPhraseMap [] = {
Expand Down
157 changes: 156 additions & 1 deletion Release/src/http/common/http_msg.cpp
Expand Up @@ -218,13 +218,168 @@ void http_msg_base::_complete(utility::size64_t body_size, const std::exception_
}
}

static bool is_content_type_one_of(const utility::string_t *first, const utility::string_t *last, const utility::string_t &value)
{
while (first != last)
{
if (utility::details::str_icmp(*first, value))
{
return true;
}
++first;
}
return false;
}

// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
// Not referring to mime_types to avoid static initialization order fiasco.
static const utility::string_t textual_types [] = {
U("message/http"),
U("application/json"),
U("application/xml"),
U("application/atom+xml"),
U("application/http"),
U("application/x-www-form-urlencoded")
};
#endif

/// <summary>
/// Determines whether or not the given content type is 'textual' according the feature specifications.
/// </summary>
static bool is_content_type_textual(const utility::string_t &content_type)
{
#if !defined(_WIN32) || _MSC_VER >= 1900
static const utility::string_t textual_types [] = {
mime_types::message_http,
mime_types::application_json,
mime_types::application_xml,
mime_types::application_atom_xml,
mime_types::application_http,
mime_types::application_x_www_form_urlencoded
};
#endif

if (content_type.size() >= 4 && utility::details::str_icmp(content_type.substr(0, 4), _XPLATSTR("text")))
{
return true;
}
return (is_content_type_one_of(std::begin(textual_types), std::end(textual_types), content_type));
}

// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
// Not referring to mime_types to avoid static initialization order fiasco.
static const utility::string_t json_types [] = {
U("application/json"),
U("application/x-json"),
U("text/json"),
U("text/x-json"),
U("text/javascript"),
U("text/x-javascript"),
U("application/javascript"),
U("application/x-javascript")
};
#endif

/// <summary>
/// Determines whether or not the given content type is JSON according the feature specifications.
/// </summary>
static bool is_content_type_json(const utility::string_t &content_type)
{
#if !defined(_WIN32) || _MSC_VER >= 1900
static const utility::string_t json_types [] = {
mime_types::application_json,
mime_types::application_xjson,
mime_types::text_json,
mime_types::text_xjson,
mime_types::text_javascript,
mime_types::text_xjavascript,
mime_types::application_javascript,
mime_types::application_xjavascript
};
#endif

return (is_content_type_one_of(std::begin(json_types), std::end(json_types), content_type));
}

/// <summary>
/// Gets the default charset for given content type. If the MIME type is not textual or recognized Latin1 will be returned.
/// </summary>
static utility::string_t get_default_charset(const utility::string_t &content_type)
{
// We are defaulting everything to Latin1 except JSON which is utf-8.
if (is_content_type_json(content_type))
{
return charset_types::utf8;
}
else
{
return charset_types::latin1;
}
}


/// <summary>
/// Parses the given Content-Type header value to get out actual content type and charset.
/// If the charset isn't specified the default charset for the content type will be set.
/// </summary>
static void parse_content_type_and_charset(const utility::string_t &content_type, utility::string_t &content, utility::string_t &charset)
{
const size_t semi_colon_index = content_type.find_first_of(_XPLATSTR(";"));

// No charset specified.
if (semi_colon_index == utility::string_t::npos)
{
content = content_type;
trim_whitespace(content);
charset = get_default_charset(content);
return;
}

// Split into content type and second part which could be charset.
content = content_type.substr(0, semi_colon_index);
trim_whitespace(content);
utility::string_t possible_charset = content_type.substr(semi_colon_index + 1);
trim_whitespace(possible_charset);
const size_t equals_index = possible_charset.find_first_of(_XPLATSTR("="));

// No charset specified.
if (equals_index == utility::string_t::npos)
{
charset = get_default_charset(content);
return;
}

// Split and make sure 'charset'
utility::string_t charset_key = possible_charset.substr(0, equals_index);
trim_whitespace(charset_key);
if (!utility::details::str_icmp(charset_key, _XPLATSTR("charset")))
{
charset = get_default_charset(content);
return;
}
charset = possible_charset.substr(equals_index + 1);
// Remove the redundant ';' at the end of charset.
while (charset.back() == ';')
{
charset.pop_back();
}
trim_whitespace(charset);
if (charset.front() == _XPLATSTR('"') && charset.back() == _XPLATSTR('"'))
{
charset = charset.substr(1, charset.size() - 2);
trim_whitespace(charset);
}
}

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)
{
if (!instream())
{
throw http_exception(stream_was_set_explicitly);
}

utility::string_t content, charset = charset_types::utf8;
if (!ignore_content_type)
{
Expand Down

0 comments on commit 6f5f815

Please sign in to comment.