Skip to content

Commit 0f1684c

Browse files
committed
HTTP message sending improvements.
1. Combined typedefs duplicated in compat files into basic_types.h. 2. Update documentation for http_request default constructor and made default to HTTP GET method to avoid ambiguity. Fixes CodePlex #273. 3. For sending HTTP request/response message bodies, which are potentially large amounts of user data I replaced our value with move functions with two overloads, one taking const reference and the other r-value reference and moving. 4. For sending HTTP request/response message bodies, I added overload to work with utf8 strings on Windows. We've received the customer complaints about cross platform strings many times. For a performance critical portion now customers and use UTF-8 for message bodies on all platforms efficiently. On non Windows platforms this saves unnecessary copy and moves in some cases. On Windows this saves a copy and string conversion.
1 parent 22b5ef3 commit 0f1684c

File tree

8 files changed

+253
-103
lines changed

8 files changed

+253
-103
lines changed

Release/include/compat/apple_compat.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#pragma once
2525

2626
#include <cstdint>
27-
#include <string>
2827
#include <sstream>
2928
#include <iostream>
3029
#define __cdecl
@@ -53,13 +52,3 @@
5352
#else
5453
#define CASABLANCA_DEPRECATED(x) __attribute__((deprecated(x)))
5554
#endif
56-
57-
#include <string>
58-
59-
typedef char16_t utf16char;
60-
typedef std::u16string utf16string;
61-
typedef std::basic_stringstream<char16_t> utf16stringstream;
62-
typedef std::basic_ostringstream<char16_t> utf16ostringstream;
63-
typedef std::basic_ostream<char16_t> utf16ostream;
64-
typedef std::basic_istream<char16_t> utf16istream;
65-
typedef std::basic_istringstream<char16_t> utf16istringstream;

Release/include/compat/linux_compat.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#pragma once
2424
#include <cstdint>
25-
#include <string>
2625
#include <sstream>
2726
#include <iostream>
2827
#define __cdecl __attribute__ ((cdecl))
@@ -55,13 +54,3 @@
5554
#define CASABLANCA_DEPRECATED(x) __attribute__((deprecated(x)))
5655
#endif
5756

58-
#include <string>
59-
60-
typedef char16_t utf16char;
61-
typedef std::u16string utf16string;
62-
typedef std::basic_stringstream<char16_t> utf16stringstream;
63-
typedef std::basic_ostringstream<char16_t> utf16ostringstream;
64-
typedef std::basic_ostream<char16_t> utf16ostream;
65-
typedef std::basic_istream<char16_t> utf16istream;
66-
typedef std::basic_istringstream<char16_t> utf16istringstream;
67-

Release/include/compat/windows_compat.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
****/
2727
#pragma once
2828

29-
#include <string>
30-
3129
#if _MSC_VER >= 1700
3230
// Support VS2012 SAL syntax only
3331
#include <sal.h>
@@ -43,12 +41,4 @@
4341
#define CASABLANCA_DEPRECATED(x)
4442
#else
4543
#define CASABLANCA_DEPRECATED(x) __declspec(deprecated(x))
46-
#endif
47-
48-
typedef wchar_t utf16char;
49-
typedef std::wstring utf16string;
50-
typedef std::wstringstream utf16stringstream;
51-
typedef std::wostringstream utf16ostringstream;
52-
typedef std::wostream utf16ostream;
53-
typedef std::wistream utf16istream;
54-
typedef std::wistringstream utf16istringstream;
44+
#endif

Release/include/cpprest/basic_types.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,29 @@ typedef std::stringstream stringstream_t;
9999
#endif // !_TURN_OFF_PLATFORM_STRING
100100

101101
}// namespace utility
102+
103+
using utf8char = char;
104+
using utf8string = std::string;
105+
using utf8stringstream = std::stringstream;
106+
using utf8ostringstream = std::ostringstream;
107+
using utf8ostream = std::ostream;
108+
using utf8istream = std::istream;
109+
using utf8istringstream = std::istringstream;
110+
111+
#ifdef _UTF16_STRINGS
112+
typedef wchar_t utf16char;
113+
typedef std::wstring utf16string;
114+
typedef std::wstringstream utf16stringstream;
115+
typedef std::wostringstream utf16ostringstream;
116+
typedef std::wostream utf16ostream;
117+
typedef std::wistream utf16istream;
118+
typedef std::wistringstream utf16istringstream;
119+
#else
120+
typedef char16_t utf16char;
121+
typedef std::u16string utf16string;
122+
typedef std::basic_stringstream<utf16char> utf16stringstream;
123+
typedef std::basic_ostringstream<utf16char> utf16ostringstream;
124+
typedef std::basic_ostream<utf16char> utf16ostream;
125+
typedef std::basic_istream<utf16char> utf16istream;
126+
typedef std::basic_istringstream<utf16char> utf16istringstream;
127+
#endif

Release/include/cpprest/http_client.h

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,19 +461,65 @@ class http_client
461461
}
462462

463463
/// <summary>
464-
/// Asynchronously sends an HTTP request.
464+
/// Asynchronously sends an HTTP request with a string body. Assumes the
465+
/// character encoding of the string is UTF-8.
466+
/// </summary>
467+
/// <param name="mtd">HTTP request method.</param>
468+
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
469+
/// <param name="content_type">A string holding the MIME type of the message body.</param>
470+
/// <param name="body_data">String containing the text to use in the message body.</param>
471+
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
472+
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
473+
pplx::task<http_response> request(
474+
const method &mtd,
475+
const utf8string &path_query_fragment,
476+
const utf8string &body_data,
477+
const utf8string &content_type = "text/plain; charset=utf-8",
478+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
479+
{
480+
http_request msg(mtd);
481+
msg.set_request_uri(::utility::conversions::to_string_t(path_query_fragment));
482+
msg.set_body(body_data, content_type);
483+
return request(msg, token);
484+
}
485+
486+
/// <summary>
487+
/// Asynchronously sends an HTTP request with a string body. Assumes the
488+
/// character encoding of the string is UTF-8.
489+
/// </summary>
490+
/// <param name="mtd">HTTP request method.</param>
491+
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
492+
/// <param name="content_type">A string holding the MIME type of the message body.</param>
493+
/// <param name="body_data">String containing the text to use in the message body.</param>
494+
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
495+
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
496+
pplx::task<http_response> request(
497+
const method &mtd,
498+
const utf8string &path_query_fragment,
499+
utf8string &&body_data,
500+
const utf8string &content_type = "text/plain; charset=utf-8",
501+
const pplx::cancellation_token &token = pplx::cancellation_token::none())
502+
{
503+
http_request msg(mtd);
504+
msg.set_request_uri(::utility::conversions::to_string_t(path_query_fragment));
505+
msg.set_body(std::move(body_data), content_type);
506+
return request(msg, token);
507+
}
508+
509+
/// <summary>
510+
/// Asynchronously sends an HTTP request with a string body. Assumes the
511+
/// character encoding of the string is UTF-16 will perform conversion to UTF-8.
465512
/// </summary>
466513
/// <param name="mtd">HTTP request method.</param>
467514
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
468515
/// <param name="content_type">A string holding the MIME type of the message body.</param>
469516
/// <param name="body_data">String containing the text to use in the message body.</param>
470517
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
471518
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
472-
// TODO overload???Not sure...
473519
pplx::task<http_response> request(
474520
const method &mtd,
475521
const utility::string_t &path_query_fragment,
476-
const utility::string_t &body_data,
522+
const utf16string &body_data,
477523
const utility::string_t &content_type = _XPLATSTR("text/plain"),
478524
const pplx::cancellation_token &token = pplx::cancellation_token::none())
479525
{
@@ -484,18 +530,57 @@ class http_client
484530
}
485531

486532
/// <summary>
487-
/// Asynchronously sends an HTTP request.
533+
/// Asynchronously sends an HTTP request with a string body. Assumes the
534+
/// character encoding of the string is UTF-8.
488535
/// </summary>
489536
/// <param name="mtd">HTTP request method.</param>
490537
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
491538
/// <param name="body_data">String containing the text to use in the message body.</param>
492539
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
493540
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
494-
// TODO overload not usre???
495541
pplx::task<http_response> request(
496542
const method &mtd,
497-
const utility::string_t &path_query_fragment,
498-
const utility::string_t &body_data,
543+
const utf8string &path_query_fragment,
544+
const utf8string &body_data,
545+
const pplx::cancellation_token &token)
546+
{
547+
return request(mtd, path_query_fragment, body_data, "text/plain; charset=utf-8", token);
548+
}
549+
550+
/// <summary>
551+
/// Asynchronously sends an HTTP request with a string body. Assumes the
552+
/// character encoding of the string is UTF-8.
553+
/// </summary>
554+
/// <param name="mtd">HTTP request method.</param>
555+
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
556+
/// <param name="body_data">String containing the text to use in the message body.</param>
557+
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
558+
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
559+
pplx::task<http_response> request(
560+
const method &mtd,
561+
const utf8string &path_query_fragment,
562+
utf8string &&body_data,
563+
const pplx::cancellation_token &token)
564+
{
565+
http_request msg(mtd);
566+
msg.set_request_uri(::utility::conversions::to_string_t(path_query_fragment));
567+
msg.set_body(std::move(body_data), "text/plain; charset=utf-8");
568+
return request(msg, token);
569+
}
570+
571+
/// <summary>
572+
/// Asynchronously sends an HTTP request with a string body. Assumes
573+
/// the character encoding of the string is UTF-16 will perform conversion to UTF-8.
574+
/// </summary>
575+
/// <param name="mtd">HTTP request method.</param>
576+
/// <param name="path_query_fragment">String containing the path, query, and fragment, relative to the http_client's base URI.</param>
577+
/// <param name="body_data">String containing the text to use in the message body.</param>
578+
/// <param name="token">Cancellation token for cancellation of this request operation.</param>
579+
/// <returns>An asynchronous operation that is completed once a response from the request is received.</returns>
580+
pplx::task<http_response> request(
581+
const method &mtd,
582+
const utf16string &path_query_fragment,
583+
const utf16string &body_data,
499584
const pplx::cancellation_token &token)
500585
{
501586
return request(mtd, path_query_fragment, body_data, _XPLATSTR("text/plain"), token);

0 commit comments

Comments
 (0)