Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 102811e

Browse files
committed
Fixing a couple of locale issues with stringstreams usage.
1 parent b7909b3 commit 102811e

31 files changed

Lines changed: 210 additions & 22 deletions

Release/include/cpprest/asyncrt_utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,40 @@ namespace conversions
185185

186186
template <typename Source>
187187
utility::string_t print_string(const Source &val)
188+
{
189+
return print_string(val, std::locale());
190+
}
191+
192+
template <typename Source>
193+
utility::string_t print_string(const Source &val, const std::locale &loc)
188194
{
189195
utility::ostringstream_t oss;
196+
oss.imbue(loc);
190197
oss << val;
191198
if (oss.bad())
199+
{
192200
throw std::bad_cast();
201+
}
193202
return oss.str();
194203
}
204+
195205
template <typename Target>
196206
Target scan_string(const utility::string_t &str)
207+
{
208+
return scan_string<Target>(str, std::locale());
209+
}
210+
211+
template <typename Target>
212+
Target scan_string(const utility::string_t &str, const std::locale &loc)
197213
{
198214
Target t;
199215
utility::istringstream_t iss(str);
216+
iss.imbue(loc);
200217
iss >> t;
201218
if (iss.bad())
219+
{
202220
throw std::bad_cast();
221+
}
203222
return t;
204223
}
205224
}

Release/include/cpprest/details/http_server_asio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class hostport_listener
136136
m_all_connections_complete.set();
137137

138138
std::istringstream hostport_in(hostport);
139+
hostport_in.imbue(std::locale::classic());
139140

140141
std::getline(hostport_in, m_host, ':');
141142
std::getline(hostport_in, m_port);

Release/include/cpprest/http_headers.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace web { namespace http {
4040
/// <param name="ref">The value to bind to.</param>
4141
/// <returns><c>true</c> if the binding succeeds, <c>false</c> otherwise.</returns>
4242
template<typename key_type, typename _t>
43+
CASABLANCA_DEPRECATED("This API is deprecated and will be removed in a future release, std::istringstream instead.")
4344
bool bind(const key_type &text, _t &ref) // const
4445
{
4546
utility::istringstream_t iss(text);
@@ -61,6 +62,7 @@ bool bind(const key_type &text, _t &ref) // const
6162
/// <param name="ref">The value to bind to.</param>
6263
/// <returns><c>true</c> if the binding succeeds, <c>false</c> otherwise.</returns>
6364
template <typename key_type>
65+
CASABLANCA_DEPRECATED("This API is deprecated and will be removed in a future release.")
6466
bool bind(const key_type &text, utility::string_t &ref) //const
6567
{
6668
ref = text;
@@ -175,7 +177,7 @@ class http_headers
175177
}
176178

177179
/// <summary>
178-
/// Removes all elements from the hearders
180+
/// Removes all elements from the hearers.
179181
/// </summary>
180182
void clear() { m_headers.clear(); }
181183

@@ -227,10 +229,10 @@ class http_headers
227229
// Check to see if doesn't have a value.
228230
if(iter->second.empty())
229231
{
230-
http::bind(iter->second, value);
232+
bind(iter->second, value);
231233
return true;
232234
}
233-
return http::bind(iter->second, value);
235+
return bind(iter->second, value);
234236
}
235237
else
236238
{
@@ -239,7 +241,7 @@ class http_headers
239241
}
240242

241243
/// <summary>
242-
/// Returns an iterator refering to the first header field.
244+
/// Returns an iterator referring to the first header field.
243245
/// </summary>
244246
/// <returns>An iterator to the beginning of the HTTP headers</returns>
245247
iterator begin() { return m_headers.begin(); }
@@ -248,7 +250,7 @@ class http_headers
248250
/// <summary>
249251
/// Returns an iterator referring to the past-the-end header field.
250252
/// </summary>
251-
/// <returns>An iterator to the element past the end of the HTTP headers</returns>
253+
/// <returns>An iterator to the element past the end of the HTTP headers</returns>
252254
iterator end() { return m_headers.end(); }
253255
const_iterator end() const { return m_headers.end(); }
254256

@@ -302,6 +304,27 @@ class http_headers
302304

303305
private:
304306

307+
template<typename _t>
308+
bool bind(const key_type &text, _t &ref) const
309+
{
310+
utility::istringstream_t iss(text);
311+
iss.imbue(std::locale::classic());
312+
iss >> ref;
313+
if (iss.fail() || !iss.eof())
314+
{
315+
return false;
316+
}
317+
318+
return true;
319+
}
320+
321+
template <>
322+
bool bind(const key_type &text, ::utility::string_t &ref) const
323+
{
324+
ref = text;
325+
return true;
326+
}
327+
305328
// Headers are stored in a map with case insensitive key.
306329
std::map<utility::string_t, utility::string_t, _case_insensitive_cmp> m_headers;
307330
};

Release/include/cpprest/oauth1.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,7 @@ class oauth1_config
505505

506506
static utility::string_t _generate_timestamp()
507507
{
508-
utility::ostringstream_t os;
509-
os << utility::datetime::utc_timestamp();
510-
return os.str();
508+
return utility::conversions::print_string(utility::datetime::utc_timestamp(), std::locale::classic());
511509
}
512510

513511
_ASYNCRTIMP static std::vector<unsigned char> __cdecl _hmac_sha1(const utility::string_t& key, const utility::string_t& data);

Release/include/cpprest/uri_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ namespace web
237237
uri_builder &append_query(utility::string_t name, const T &value, bool do_encoding = true)
238238
{
239239
utility::ostringstream_t ss;
240+
ss.imbue(std::locale::classic());
240241
ss << name << _XPLATSTR("=") << value;
241242
return append_query(ss.str(), do_encoding);
242243
}

Release/src/http/client/http_client_asio.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
427427
{
428428
// If the connection is new (unresolved and unconnected socket), then start async
429429
// call to resolve first, leading eventually to request write.
430-
tcp::resolver::query query(host, utility::conversions::print_string(port));
430+
tcp::resolver::query query(host, utility::conversions::print_string(port, std::locale::classic()));
431431
auto client = std::static_pointer_cast<asio_client>(m_http_client);
432432
client->m_resolver.async_resolve(query, boost::bind(&asio_context::handle_resolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
433433
}
@@ -914,6 +914,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
914914
std::getline(response_stream, line);
915915

916916
std::istringstream octetLine(std::move(line));
917+
octetLine.imbue(std::locale::classic());
917918
int octets = 0;
918919
octetLine >> std::hex >> octets;
919920

Release/src/http/client/http_client_msg.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void details::_http_request::set_request_uri(const uri& relative)
7373
utility::string_t details::_http_request::to_string() const
7474
{
7575
utility::ostringstream_t buffer;
76+
buffer.imbue(std::locale::classic());
7677
buffer << m_method << _XPLATSTR(" ") << (this->m_uri.is_empty() ? _XPLATSTR("/") : this->m_uri.to_string()) << _XPLATSTR(" HTTP/1.1\r\n");
7778
buffer << http_msg_base::to_string();
7879
return buffer.str();
@@ -104,6 +105,7 @@ utility::string_t details::_http_response::to_string() const
104105
}
105106

106107
utility::ostringstream_t buffer;
108+
buffer.imbue(std::locale::classic());
107109
buffer << _XPLATSTR("HTTP/1.1 ") << m_status_code << _XPLATSTR(" ") << reason_phrase << _XPLATSTR("\r\n");
108110

109111
buffer << http_msg_base::to_string();

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ class winhttp_client : public _http_client_communicator
370370
if (uri.port() > 0)
371371
{
372372
utility::ostringstream_t ss;
373+
ss.imbue(std::locale::classic());
373374
ss << uri.host() << _XPLATSTR(":") << uri.port();
374375
proxy_str = ss.str();
375376
}

Release/src/http/common/http_msg.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ utility::size64_t http_headers::content_length() const
8080

8181
void http_headers::set_content_length(utility::size64_t length)
8282
{
83-
m_headers[http::header_names::content_length] = utility::conversions::print_string(length);
83+
m_headers[http::header_names::content_length] = utility::conversions::print_string(length, std::locale::classic());
8484
}
8585

8686
static const utility::char_t * stream_was_set_explicitly = _XPLATSTR("A stream was set on the message and extraction is not possible");
@@ -599,10 +599,11 @@ static utility::string_t convert_body_to_string_t(const utility::string_t &conte
599599
static utility::string_t http_headers_body_to_string(const http_headers &headers, concurrency::streams::istream instream)
600600
{
601601
utility::ostringstream_t buffer;
602+
buffer.imbue(std::locale::classic());
602603

603-
for (auto iter = headers.begin(); iter != headers.end(); ++iter)
604+
for (const auto &header : headers)
604605
{
605-
buffer << iter->first << _XPLATSTR(": ") << iter->second << CRLF;
606+
buffer << header.first << _XPLATSTR(": ") << header.second << CRLF;
606607
}
607608
buffer << CRLF;
608609

Release/src/http/listener/http_server_asio.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ pplx::task<void> http_linux_server::stop()
827827
std::pair<std::string,std::string> canonical_parts(const http::uri& uri)
828828
{
829829
std::ostringstream endpoint;
830+
endpoint.imbue(std::locale::classic());
830831
endpoint << uri::decode(uri.host()) << ":" << uri.port();
831832

832833
auto path = uri::decode(uri.path());

0 commit comments

Comments
 (0)