Skip to content

Commit acd96cf

Browse files
authored
Burninate internal use of ostringstream, and done a ref (#905)
* Burninate internal use of ostringstream, and make compressors' done parameter required. * Remove errant ::details. * Remove even more errant ::details.
1 parent 8484f5d commit acd96cf

File tree

16 files changed

+305
-271
lines changed

16 files changed

+305
-271
lines changed

Release/include/cpprest/http_compression.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class compress_provider
5151
size_t output_size,
5252
operation_hint hint,
5353
size_t& input_bytes_processed,
54-
bool* done = nullptr) = 0;
54+
bool& done) = 0;
5555
virtual pplx::task<operation_result> compress(
5656
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size, operation_hint hint) = 0;
5757
virtual void reset() = 0;
@@ -71,7 +71,7 @@ class decompress_provider
7171
size_t output_size,
7272
operation_hint hint,
7373
size_t& input_bytes_processed,
74-
bool* done = nullptr) = 0;
74+
bool& done) = 0;
7575
virtual pplx::task<operation_result> decompress(
7676
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size, operation_hint hint) = 0;
7777
virtual void reset() = 0;

Release/include/cpprest/json.h

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,11 @@ namespace json
714714
private:
715715
std::string _message;
716716
public:
717-
json_exception(const utility::char_t * const &message) : _message(utility::conversions::to_utf8string(message)) { }
717+
json_exception(const char * const message) : _message(message) { }
718+
#ifdef _UTF16_STRINGS
719+
json_exception(const wchar_t * const message) : _message(utility::conversions::utf16_to_utf8(message)) { }
720+
#endif // _UTF16_STRINGS
721+
json_exception(std::string&& message) : _message(std::move(message)) { }
718722

719723
// Must be narrow string because it derives from std::exception
720724
const char* what() const CPPREST_NOEXCEPT
@@ -930,7 +934,7 @@ namespace json
930934
{
931935
if (index >= m_elements.size())
932936
{
933-
throw json_exception(_XPLATSTR("index out of bounds"));
937+
throw json_exception("index out of bounds");
934938
}
935939
m_elements.erase(m_elements.begin() + index);
936940
}
@@ -943,7 +947,7 @@ namespace json
943947
json::value& at(size_type index)
944948
{
945949
if (index >= m_elements.size())
946-
throw json_exception(_XPLATSTR("index out of bounds"));
950+
throw json_exception("index out of bounds");
947951

948952
return m_elements[index];
949953
}
@@ -956,7 +960,7 @@ namespace json
956960
const json::value& at(size_type index) const
957961
{
958962
if (index >= m_elements.size())
959-
throw json_exception(_XPLATSTR("index out of bounds"));
963+
throw json_exception("index out of bounds");
960964

961965
return m_elements[index];
962966
}
@@ -1145,7 +1149,7 @@ namespace json
11451149
auto iter = find_by_key(key);
11461150
if (iter == m_elements.end())
11471151
{
1148-
throw web::json::json_exception(_XPLATSTR("Key not found"));
1152+
throw web::json::json_exception("Key not found");
11491153
}
11501154

11511155
m_elements.erase(iter);
@@ -1161,7 +1165,7 @@ namespace json
11611165
auto iter = find_by_key(key);
11621166
if (iter == m_elements.end())
11631167
{
1164-
throw web::json::json_exception(_XPLATSTR("Key not found"));
1168+
throw web::json::json_exception("Key not found");
11651169
}
11661170

11671171
return iter->second;
@@ -1177,7 +1181,7 @@ namespace json
11771181
auto iter = find_by_key(key);
11781182
if (iter == m_elements.end())
11791183
{
1180-
throw web::json::json_exception(_XPLATSTR("Key not found"));
1184+
throw web::json::json_exception("Key not found");
11811185
}
11821186

11831187
return iter->second;
@@ -1464,14 +1468,14 @@ namespace json
14641468
virtual std::unique_ptr<_Value> _copy_value() = 0;
14651469

14661470
virtual bool has_field(const utility::string_t &) const { return false; }
1467-
virtual value get_field(const utility::string_t &) const { throw json_exception(_XPLATSTR("not an object")); }
1468-
virtual value get_element(array::size_type) const { throw json_exception(_XPLATSTR("not an array")); }
1471+
virtual value get_field(const utility::string_t &) const { throw json_exception("not an object"); }
1472+
virtual value get_element(array::size_type) const { throw json_exception("not an array"); }
14691473

1470-
virtual value &index(const utility::string_t &) { throw json_exception(_XPLATSTR("not an object")); }
1471-
virtual value &index(array::size_type) { throw json_exception(_XPLATSTR("not an array")); }
1474+
virtual value &index(const utility::string_t &) { throw json_exception("not an object"); }
1475+
virtual value &index(array::size_type) { throw json_exception("not an array"); }
14721476

1473-
virtual const value &cnst_index(const utility::string_t &) const { throw json_exception(_XPLATSTR("not an object")); }
1474-
virtual const value &cnst_index(array::size_type) const { throw json_exception(_XPLATSTR("not an array")); }
1477+
virtual const value &cnst_index(const utility::string_t &) const { throw json_exception("not an object"); }
1478+
virtual const value &cnst_index(array::size_type) const { throw json_exception("not an array"); }
14751479

14761480
// Common function used for serialization to strings and streams.
14771481
virtual void serialize_impl(std::string& str) const
@@ -1494,18 +1498,18 @@ namespace json
14941498

14951499
virtual json::value::value_type type() const { return json::value::Null; }
14961500

1497-
virtual bool is_integer() const { throw json_exception(_XPLATSTR("not a number")); }
1498-
virtual bool is_double() const { throw json_exception(_XPLATSTR("not a number")); }
1499-
1500-
virtual const json::number& as_number() { throw json_exception(_XPLATSTR("not a number")); }
1501-
virtual double as_double() const { throw json_exception(_XPLATSTR("not a number")); }
1502-
virtual int as_integer() const { throw json_exception(_XPLATSTR("not a number")); }
1503-
virtual bool as_bool() const { throw json_exception(_XPLATSTR("not a boolean")); }
1504-
virtual json::array& as_array() { throw json_exception(_XPLATSTR("not an array")); }
1505-
virtual const json::array& as_array() const { throw json_exception(_XPLATSTR("not an array")); }
1506-
virtual json::object& as_object() { throw json_exception(_XPLATSTR("not an object")); }
1507-
virtual const json::object& as_object() const { throw json_exception(_XPLATSTR("not an object")); }
1508-
virtual const utility::string_t& as_string() const { throw json_exception(_XPLATSTR("not a string")); }
1501+
virtual bool is_integer() const { throw json_exception("not a number"); }
1502+
virtual bool is_double() const { throw json_exception("not a number"); }
1503+
1504+
virtual const json::number& as_number() { throw json_exception("not a number"); }
1505+
virtual double as_double() const { throw json_exception("not a number"); }
1506+
virtual int as_integer() const { throw json_exception("not a number"); }
1507+
virtual bool as_bool() const { throw json_exception("not a boolean"); }
1508+
virtual json::array& as_array() { throw json_exception("not an array"); }
1509+
virtual const json::array& as_array() const { throw json_exception("not an array"); }
1510+
virtual json::object& as_object() { throw json_exception("not an object"); }
1511+
virtual const json::object& as_object() const { throw json_exception("not an object"); }
1512+
virtual const utility::string_t& as_string() const { throw json_exception("not a string"); }
15091513

15101514
virtual size_t size() const { return 0; }
15111515

Release/src/http/client/http_client_asio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ class asio_context final : public request_context, public std::enable_shared_fro
15301530
size_t got;
15311531
size_t inbytes = 0;
15321532
size_t outbytes = 0;
1533-
bool done = false;
1533+
bool done;
15341534

15351535
try
15361536
{
@@ -1547,7 +1547,7 @@ class asio_context final : public request_context, public std::enable_shared_fro
15471547
output.size() - outbytes,
15481548
web::http::compression::operation_hint::has_more,
15491549
processed,
1550-
&done);
1550+
done);
15511551
inbytes += processed;
15521552
outbytes += got;
15531553
} while (got && !done);

Release/src/http/client/http_client_msg.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
****/
1313
#include "stdafx.h"
1414
#include "../common/internal_http_helpers.h"
15+
#include "cpprest/asyncrt_utils.h"
1516

1617
namespace web { namespace http
1718
{
@@ -61,29 +62,41 @@ void details::_http_request::set_request_uri(const uri& relative)
6162

6263
utility::string_t details::_http_request::to_string() const
6364
{
64-
utility::ostringstream_t buffer;
65-
buffer.imbue(std::locale::classic());
66-
buffer << m_method << _XPLATSTR(" ") << (this->m_uri.is_empty() ? _XPLATSTR("/") : this->m_uri.to_string()) << _XPLATSTR(" HTTP/1.1\r\n");
67-
buffer << http_msg_base::to_string();
68-
return buffer.str();
65+
utility::string_t result(m_method);
66+
result += _XPLATSTR(' ');
67+
if (this->m_uri.is_empty())
68+
{
69+
result += _XPLATSTR('/');
70+
}
71+
else
72+
{
73+
result += this->m_uri.to_string();
74+
}
75+
76+
result += _XPLATSTR(" HTTP/1.1\r\n");
77+
result += http_msg_base::to_string();
78+
return result;
6979
}
7080

7181
utility::string_t details::_http_response::to_string() const
7282
{
83+
utility::string_t result(_XPLATSTR("HTTP/1.1 "));
84+
result += utility::conversions::details::to_string_t(m_status_code);
85+
result += ' ';
7386
// If the user didn't explicitly set a reason phrase then we should have it default
7487
// if they used one of the standard known status codes.
75-
auto reason_phrase = m_reason_phrase;
76-
if(reason_phrase.empty())
88+
if (m_reason_phrase.empty())
7789
{
78-
reason_phrase = get_default_reason_phrase(status_code());
90+
result += get_default_reason_phrase(status_code());
91+
}
92+
else
93+
{
94+
result += m_reason_phrase;
7995
}
8096

81-
utility::ostringstream_t buffer;
82-
buffer.imbue(std::locale::classic());
83-
buffer << _XPLATSTR("HTTP/1.1 ") << m_status_code << _XPLATSTR(" ") << reason_phrase << _XPLATSTR("\r\n");
84-
85-
buffer << http_msg_base::to_string();
86-
return buffer.str();
97+
result += _XPLATSTR("\r\n");
98+
result += http_msg_base::to_string();
99+
return result;
87100
}
88101

89102
}} // namespace web::http

Release/src/http/client/http_client_winrt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ class HttpRequestCallback final :
160160
std::wstring msg(L"IXMLHttpRequest2Callback::OnError: ");
161161
msg.append(std::to_wstring(hrError));
162162
msg.append(L": ");
163-
msg.append(utility::conversions::to_string_t(utility::details::windows_category().message(hrError)));
163+
msg.append(utility::conversions::to_string_t(
164+
utility::details::windows_category().message(hrError)));
164165
m_request->report_error(hrError, msg);
165166
}
166167
else

0 commit comments

Comments
 (0)