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

Commit 9869462

Browse files
ras0219ras0219-msft
authored andcommitted
Localized conversion functions from http_helpers.h to http_msg.cpp
1 parent 6f5f815 commit 9869462

File tree

3 files changed

+140
-148
lines changed

3 files changed

+140
-148
lines changed

Release/include/cpprest/details/http_helpers.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ namespace details
6565
/// </summary>
6666
utility::string_t get_default_reason_phrase(status_code code);
6767

68-
/// <summary>
69-
/// Helper functions to convert a series of bytes from a charset to utf-8 or utf-16.
70-
/// These APIs deal with checking for and handling byte order marker (BOM).
71-
/// </summary>
72-
utility::string_t convert_utf16_to_string_t(utf16string src);
73-
utf16string convert_utf16_to_utf16(utf16string src);
74-
std::string convert_utf16_to_utf8(utf16string src);
75-
utility::string_t convert_utf16le_to_string_t(utf16string src, bool erase_bom);
76-
std::string convert_utf16le_to_utf8(utf16string src, bool erase_bom);
77-
utility::string_t convert_utf16be_to_string_t(utf16string src, bool erase_bom);
78-
std::string convert_utf16be_to_utf8(utf16string src, bool erase_bom);
79-
utf16string convert_utf16be_to_utf16le(utf16string src, bool erase_bom);
80-
8168
// simple helper functions to trim whitespace.
8269
_ASYNCRTIMP void __cdecl ltrim_whitespace(utility::string_t &str);
8370
_ASYNCRTIMP void __cdecl rtrim_whitespace(utility::string_t &str);

Release/src/http/common/http_helpers.cpp

Lines changed: 0 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -71,141 +71,6 @@ utility::string_t get_default_reason_phrase(status_code code)
7171
return phrase;
7272
}
7373

74-
// Helper function to determine byte order mark.
75-
enum endian_ness
76-
{
77-
little_endian,
78-
big_endian,
79-
unknown
80-
};
81-
static endian_ness check_byte_order_mark(const utf16string &str)
82-
{
83-
if (str.empty())
84-
{
85-
return unknown;
86-
}
87-
const unsigned char *src = (const unsigned char *) &str[0];
88-
89-
// little endian
90-
if (src[0] == 0xFF && src[1] == 0xFE)
91-
{
92-
return little_endian;
93-
}
94-
95-
// big endian
96-
else if (src[0] == 0xFE && src[1] == 0xFF)
97-
{
98-
return big_endian;
99-
}
100-
101-
return unknown;
102-
}
103-
104-
utility::string_t convert_utf16_to_string_t(utf16string src)
105-
{
106-
#ifdef _UTF16_STRINGS
107-
return convert_utf16_to_utf16(std::move(src));
108-
#else
109-
return convert_utf16_to_utf8(std::move(src));
110-
#endif
111-
}
112-
113-
std::string convert_utf16_to_utf8(utf16string src)
114-
{
115-
const endian_ness endian = check_byte_order_mark(src);
116-
switch (endian)
117-
{
118-
case little_endian:
119-
return convert_utf16le_to_utf8(std::move(src), true);
120-
case big_endian:
121-
return convert_utf16be_to_utf8(std::move(src), true);
122-
case unknown:
123-
// unknown defaults to big endian.
124-
return convert_utf16be_to_utf8(std::move(src), false);
125-
}
126-
__assume(0);
127-
}
128-
129-
utf16string convert_utf16_to_utf16(utf16string src)
130-
{
131-
const endian_ness endian = check_byte_order_mark(src);
132-
switch (endian)
133-
{
134-
case little_endian:
135-
src.erase(0, 1);
136-
return std::move(src);
137-
case big_endian:
138-
return convert_utf16be_to_utf16le(std::move(src), true);
139-
case unknown:
140-
// unknown defaults to big endian.
141-
return convert_utf16be_to_utf16le(std::move(src), false);
142-
}
143-
__assume(0);
144-
}
145-
146-
std::string convert_utf16le_to_utf8(utf16string src, bool erase_bom)
147-
{
148-
if (erase_bom && !src.empty())
149-
{
150-
src.erase(0, 1);
151-
}
152-
return utf16_to_utf8(std::move(src));
153-
}
154-
155-
utility::string_t convert_utf16le_to_string_t(utf16string src, bool erase_bom)
156-
{
157-
if (erase_bom && !src.empty())
158-
{
159-
src.erase(0, 1);
160-
}
161-
#ifdef _UTF16_STRINGS
162-
return std::move(src);
163-
#else
164-
return utf16_to_utf8(std::move(src));
165-
#endif
166-
}
167-
168-
// Helper function to change endian ness from big endian to little endian
169-
static utf16string big_endian_to_little_endian(utf16string src, bool erase_bom)
170-
{
171-
if (erase_bom && !src.empty())
172-
{
173-
src.erase(0, 1);
174-
}
175-
if (src.empty())
176-
{
177-
return std::move(src);
178-
}
179-
180-
const size_t size = src.size();
181-
for (size_t i = 0; i < size; ++i)
182-
{
183-
utf16char ch = src[i];
184-
src[i] = static_cast<utf16char>(ch << 8);
185-
src[i] = static_cast<utf16char>(src[i] | ch >> 8);
186-
}
187-
188-
return std::move(src);
189-
}
190-
191-
utility::string_t convert_utf16be_to_string_t(utf16string src, bool erase_bom)
192-
{
193-
#ifdef _UTF16_STRINGS
194-
return convert_utf16be_to_utf16le(std::move(src), erase_bom);
195-
#else
196-
return convert_utf16be_to_utf8(std::move(src), erase_bom);
197-
#endif
198-
}
199-
200-
std::string convert_utf16be_to_utf8(utf16string src, bool erase_bom)
201-
{
202-
return utf16_to_utf8(big_endian_to_little_endian(std::move(src), erase_bom));
203-
}
204-
205-
utf16string convert_utf16be_to_utf16le(utf16string src, bool erase_bom)
206-
{
207-
return big_endian_to_little_endian(std::move(src), erase_bom);
208-
}
20974

21075
void ltrim_whitespace(utility::string_t &str)
21176
{

Release/src/http/common/http_msg.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,146 @@ utility::string_t http_headers::content_type() const
4242
return result;
4343
}
4444

45+
46+
47+
/// Helper functions to convert a series of bytes from a charset to utf-8 or utf-16.
48+
/// These APIs deal with checking for and handling byte order marker (BOM).
49+
namespace {
50+
enum endian_ness
51+
{
52+
little_endian,
53+
big_endian,
54+
unknown
55+
};
56+
endian_ness check_byte_order_mark(const utf16string &str)
57+
{
58+
if (str.empty())
59+
{
60+
return unknown;
61+
}
62+
const unsigned char *src = (const unsigned char *) &str[0];
63+
64+
// little endian
65+
if (src[0] == 0xFF && src[1] == 0xFE)
66+
{
67+
return little_endian;
68+
}
69+
70+
// big endian
71+
else if (src[0] == 0xFE && src[1] == 0xFF)
72+
{
73+
return big_endian;
74+
}
75+
76+
return unknown;
77+
}
78+
79+
std::string convert_utf16le_to_utf8(utf16string src, bool erase_bom)
80+
{
81+
if (erase_bom && !src.empty())
82+
{
83+
src.erase(0, 1);
84+
}
85+
return utf16_to_utf8(std::move(src));
86+
}
87+
88+
utility::string_t convert_utf16le_to_string_t(utf16string src, bool erase_bom)
89+
{
90+
if (erase_bom && !src.empty())
91+
{
92+
src.erase(0, 1);
93+
}
94+
#ifdef _UTF16_STRINGS
95+
return std::move(src);
96+
#else
97+
return utf16_to_utf8(std::move(src));
98+
#endif
99+
}
100+
101+
// Helper function to change endian ness from big endian to little endian
102+
utf16string big_endian_to_little_endian(utf16string src, bool erase_bom)
103+
{
104+
if (erase_bom && !src.empty())
105+
{
106+
src.erase(0, 1);
107+
}
108+
if (src.empty())
109+
{
110+
return std::move(src);
111+
}
112+
113+
const size_t size = src.size();
114+
for (size_t i = 0; i < size; ++i)
115+
{
116+
utf16char ch = src[i];
117+
src[i] = static_cast<utf16char>(ch << 8);
118+
src[i] = static_cast<utf16char>(src[i] | ch >> 8);
119+
}
120+
121+
return std::move(src);
122+
}
123+
124+
std::string convert_utf16be_to_utf8(utf16string src, bool erase_bom)
125+
{
126+
return utf16_to_utf8(big_endian_to_little_endian(std::move(src), erase_bom));
127+
}
128+
129+
utf16string convert_utf16be_to_utf16le(utf16string src, bool erase_bom)
130+
{
131+
return big_endian_to_little_endian(std::move(src), erase_bom);
132+
}
133+
134+
utility::string_t convert_utf16be_to_string_t(utf16string src, bool erase_bom)
135+
{
136+
#ifdef _UTF16_STRINGS
137+
return convert_utf16be_to_utf16le(std::move(src), erase_bom);
138+
#else
139+
return convert_utf16be_to_utf8(std::move(src), erase_bom);
140+
#endif
141+
}
142+
143+
std::string convert_utf16_to_utf8(utf16string src)
144+
{
145+
const endian_ness endian = check_byte_order_mark(src);
146+
switch (endian)
147+
{
148+
case little_endian:
149+
return convert_utf16le_to_utf8(std::move(src), true);
150+
case big_endian:
151+
return convert_utf16be_to_utf8(std::move(src), true);
152+
case unknown:
153+
// unknown defaults to big endian.
154+
return convert_utf16be_to_utf8(std::move(src), false);
155+
}
156+
__assume(0);
157+
}
158+
159+
utf16string convert_utf16_to_utf16(utf16string src)
160+
{
161+
const endian_ness endian = check_byte_order_mark(src);
162+
switch (endian)
163+
{
164+
case little_endian:
165+
src.erase(0, 1);
166+
return std::move(src);
167+
case big_endian:
168+
return convert_utf16be_to_utf16le(std::move(src), true);
169+
case unknown:
170+
// unknown defaults to big endian.
171+
return convert_utf16be_to_utf16le(std::move(src), false);
172+
}
173+
__assume(0);
174+
}
175+
utility::string_t convert_utf16_to_string_t(utf16string src)
176+
{
177+
#ifdef _UTF16_STRINGS
178+
return convert_utf16_to_utf16(std::move(src));
179+
#else
180+
return convert_utf16_to_utf8(std::move(src));
181+
#endif
182+
}
183+
}
184+
45185
void http_headers::set_content_type(utility::string_t type)
46186
{
47187
m_headers[http::header_names::content_type] = std::move(type);

0 commit comments

Comments
 (0)