Skip to content

Commit 982793d

Browse files
committed
address review comments
1 parent cdc2948 commit 982793d

File tree

13 files changed

+164
-178
lines changed

13 files changed

+164
-178
lines changed

Release/cmake/cpprest_find_brotli.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function(cpprest_find_brotli)
33
return()
44
endif()
55

6-
find_package(UNOFFICIAL-BROTLI REQUIRED)
6+
find_package(unofficial-brotli REQUIRED)
77

88
add_library(cpprestsdk_brotli_internal INTERFACE)
99
target_link_libraries(cpprestsdk_brotli_internal INTERFACE unofficial::brotli::brotlienc unofficial::brotli::brotlidec unofficial::brotli::brotlicommon)

Release/cmake/cpprestsdk-config.in.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if(@CPPREST_USES_ZLIB@)
44
endif()
55

66
if(@CPPREST_USES_BROTLI@)
7-
find_dependency(UNOFFICIAL-BROTLI)
7+
find_dependency(unofficial-brotli)
88
endif()
99

1010
if(@CPPREST_USES_OPENSSL@)

Release/include/cpprest/http_compression.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,20 @@ _ASYNCRTIMP bool supported();
115115
/// <summary>
116116
// String constants for each built-in compression algorithm, for convenient use with the factory functions
117117
/// </summary>
118-
class algorithm
118+
namespace algorithm
119119
{
120-
public:
121-
_ASYNCRTIMP static const utility::string_t GZIP;
122-
_ASYNCRTIMP static const utility::string_t DEFLATE;
123-
_ASYNCRTIMP static const utility::string_t BROTLI;
124-
125-
/// <summary>
126-
/// Test whether cpprestsdk was built with built-in compression support and
127-
/// the supplied string matches a supported built-in algorithm
128-
/// <param name="algorithm">The name of the algorithm to test for built-in support.</param>
129-
/// <returns>True if cpprestsdk was built with built-in compression support and
130-
/// the supplied string matches a supported built-in algorithm, and false if not.</returns>
131-
/// <summary>
132-
_ASYNCRTIMP static bool supported(const utility::string_t& algorithm);
120+
constexpr utility::char_t *GZIP = _XPLATSTR("gzip");
121+
constexpr utility::char_t *DEFLATE = _XPLATSTR("deflate");
122+
constexpr utility::char_t *BROTLI = _XPLATSTR("br");
133123

134-
private:
135-
algorithm() {}
124+
/// <summary>
125+
/// Test whether cpprestsdk was built with built-in compression support and
126+
/// the supplied string matches a supported built-in algorithm
127+
/// <param name="algorithm">The name of the algorithm to test for built-in support.</param>
128+
/// <returns>True if cpprestsdk was built with built-in compression support and
129+
/// the supplied string matches a supported built-in algorithm, and false if not.</returns>
130+
/// <summary>
131+
_ASYNCRTIMP bool supported(const utility::string_t& algorithm);
136132
};
137133

138134
/// <summary>
@@ -255,12 +251,6 @@ _ASYNCRTIMP std::shared_ptr<decompress_factory> make_decompress_factory(
255251

256252
namespace details
257253
{
258-
namespace builtin
259-
{
260-
// Internal-only helper function
261-
const std::vector<std::shared_ptr<decompress_factory>> get_decompress_factories();
262-
} // namespace builtin
263-
264254
/// <summary>
265255
/// Header type enum for use with compressor and decompressor header parsing and building functions
266256
/// </summary>

Release/include/cpprest/http_headers.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class http_headers
219219
return false;
220220
}
221221

222-
return bind_impl(iter->second, value) || iter->second.empty();
222+
return details::bind_impl(iter->second, value) || iter->second.empty();
223223
}
224224

225225
/// <summary>
@@ -285,9 +285,14 @@ class http_headers
285285
_ASYNCRTIMP void set_date(const utility::datetime& date);
286286

287287
private:
288+
// Headers are stored in a map with case insensitive key.
289+
inner_container m_headers;
290+
};
288291

289-
template<typename _t>
290-
bool bind_impl(const key_type &text, _t &ref) const
292+
namespace details
293+
{
294+
template<typename key_type, typename _t>
295+
bool bind_impl(const key_type &text, _t &ref)
291296
{
292297
utility::istringstream_t iss(text);
293298
iss.imbue(std::locale::classic());
@@ -300,20 +305,18 @@ class http_headers
300305
return true;
301306
}
302307

303-
bool bind_impl(const key_type &text, utf16string &ref) const
308+
template<typename key_type>
309+
bool bind_impl(const key_type &text, utf16string &ref)
304310
{
305311
ref = utility::conversions::to_utf16string(text);
306312
return true;
307313
}
308314

309-
bool bind_impl(const key_type &text, std::string &ref) const
315+
template<typename key_type>
316+
bool bind_impl(const key_type &text, std::string &ref)
310317
{
311318
ref = utility::conversions::to_utf8string(text);
312319
return true;
313320
}
314-
315-
// Headers are stored in a map with case insensitive key.
316-
inner_container m_headers;
317-
};
318-
321+
}
319322
}}

Release/include/cpprest/http_msg.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ class http_msg_base
383383
/// Determine the remaining input stream length
384384
/// </summary>
385385
/// <returns>
386-
/// size_t::max if the stream's remaining length cannot be determined
386+
/// std::numeric_limits<size_t>::max() if the stream's remaining length cannot be determined
387387
/// length if the stream's remaining length (which may be 0) can be determined
388388
/// </returns>
389389
/// <remarks>
@@ -396,7 +396,7 @@ class http_msg_base
396396
/// Determine the content length
397397
/// </summary>
398398
/// <returns>
399-
/// size_t::max if there is content with unknown length (transfer_encoding:chunked)
399+
/// std::numeric_limits<size_t>::max() if there is content with unknown length (transfer_encoding:chunked)
400400
/// 0 if there is no content
401401
/// length if there is content with known length
402402
/// </returns>
@@ -410,7 +410,7 @@ class http_msg_base
410410
/// Determine the content length, and, if necessary, manage compression in the Transfer-Encoding header
411411
/// </summary>
412412
/// <returns>
413-
/// size_t::max if there is content with unknown length (transfer_encoding:chunked)
413+
/// std::numeric_limits<size_t>::max() if there is content with unknown length (transfer_encoding:chunked)
414414
/// 0 if there is no content
415415
/// length if there is content with known length
416416
/// </returns>
@@ -739,7 +739,7 @@ class http_response
739739
/// <param name="stream">A readable, open asynchronous stream.</param>
740740
/// <param name="content_type">A string holding the MIME type of the message body.</param>
741741
/// <remarks>
742-
/// This cannot be used in conjunction with any other means of setting the body of the request.
742+
/// This cannot be used in conjunction with any external means of setting the body of the request.
743743
/// The stream will not be read until the message is sent.
744744
/// </remarks>
745745
void set_body(const concurrency::streams::istream &stream, const utility::string_t &content_type = _XPLATSTR("application/octet-stream"))
@@ -755,7 +755,7 @@ class http_response
755755
/// <param name="content_length">The size of the data to be sent in the body.</param>
756756
/// <param name="content_type">A string holding the MIME type of the message body.</param>
757757
/// <remarks>
758-
/// This cannot be used in conjunction with any other means of setting the body of the request.
758+
/// This cannot be used in conjunction with any external means of setting the body of the request.
759759
/// The stream will not be read until the message is sent.
760760
/// </remarks>
761761
void set_body(const concurrency::streams::istream &stream, utility::size64_t content_length, const utility::string_t &content_type = _XPLATSTR("application/octet-stream"))
@@ -1222,7 +1222,7 @@ class http_request
12221222
/// </summary>
12231223
/// <param name="compressor">A pointer to an instantiated compressor of the desired type.</param>
12241224
/// <remarks>
1225-
/// This cannot be used in conjunction with any other means of compression. The Transfer-Encoding
1225+
/// This cannot be used in conjunction with any external means of compression. The Transfer-Encoding
12261226
/// header will be managed internally, and must not be set by the client.
12271227
/// </remarks>
12281228
void set_compressor(std::unique_ptr<http::compression::compress_provider> compressor)
@@ -1238,7 +1238,7 @@ class http_request
12381238
/// True if a built-in compressor was instantiated, otherwise false.
12391239
/// </returns>
12401240
/// <remarks>
1241-
/// This cannot be used in conjunction with any other means of compression. The Transfer-Encoding
1241+
/// This cannot be used in conjunction with any external means of compression. The Transfer-Encoding
12421242
/// header will be managed internally, and must not be set by the client.
12431243
/// </remarks>
12441244
bool set_compressor(utility::string_t algorithm)
@@ -1297,7 +1297,7 @@ class http_request
12971297
/// The collection of factory classes itself.
12981298
/// </returns>
12991299
/// <remarks>
1300-
/// This cannot be used in conjunction with any other means of decompression. The TE
1300+
/// This cannot be used in conjunction with any external means of decompression. The TE
13011301
/// header must not be set by the client, as it will be managed internally.
13021302
/// </remarks>
13031303
const std::vector<std::shared_ptr<http::compression::decompress_factory>> &decompress_factories() const

Release/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ endif()
7272

7373
# Compression component
7474
if(CPPREST_EXCLUDE_COMPRESSION)
75+
if(NOT CPPREST_EXCLUDE_BROTLI)
76+
message(FATAL_ERROR "Use of Brotli requires compression to be enabled")
77+
endif()
7578
target_compile_definitions(cpprest PRIVATE -DCPPREST_EXCLUDE_COMPRESSION=1)
7679
else()
7780
cpprest_find_zlib()

Release/src/http/client/http_client_asio.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,29 +1520,26 @@ class asio_context final : public request_context, public std::enable_shared_fro
15201520

15211521
bool decompress(const uint8_t* input, size_t input_size, std::vector<uint8_t>& output)
15221522
{
1523-
size_t processed;
1524-
size_t got;
1525-
size_t inbytes;
1526-
size_t outbytes;
1527-
bool done;
1528-
15291523
// Need to guard against attempting to decompress when we're already finished or encountered an error!
15301524
if (input == nullptr || input_size == 0)
15311525
{
15321526
return false;
15331527
}
15341528

1535-
inbytes = 0;
1536-
outbytes = 0;
1537-
done = false;
1529+
size_t processed;
1530+
size_t got;
1531+
size_t inbytes = 0;
1532+
size_t outbytes = 0;
1533+
bool done = false;
1534+
15381535
try
15391536
{
15401537
output.resize(input_size * 3);
15411538
do
15421539
{
15431540
if (inbytes)
15441541
{
1545-
output.resize(output.size() + (input_size > 1024 ? input_size : 1024));
1542+
output.resize(output.size() + std::max(input_size, static_cast<size_t>(1024)));
15461543
}
15471544
got = m_decompressor->decompress(input + inbytes,
15481545
input_size - inbytes,

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ class memory_holder
182182
{
183183
uint8_t* m_externalData;
184184
std::vector<uint8_t> m_internalData;
185-
size_t m_length;
185+
size_t m_size;
186186

187187
public:
188-
memory_holder() : m_externalData(nullptr), m_length(0)
188+
memory_holder() : m_externalData(nullptr), m_size(0)
189189
{
190190
}
191191

@@ -202,7 +202,7 @@ class memory_holder
202202
{
203203
assert(block != nullptr);
204204
m_externalData = block;
205-
m_length = length;
205+
m_size = length;
206206
}
207207

208208
inline bool is_internally_allocated() const
@@ -215,9 +215,9 @@ class memory_holder
215215
return is_internally_allocated() ? &m_internalData[0] : m_externalData ;
216216
}
217217

218-
inline size_t length() const
218+
inline size_t size() const
219219
{
220-
return is_internally_allocated() ? m_internalData.size() : m_length;
220+
return is_internally_allocated() ? m_internalData.size() : m_size;
221221
}
222222
};
223223

@@ -1311,13 +1311,7 @@ class winhttp_client final : public _http_client_communicator
13111311
{
13121312
uint8_t *buffer;
13131313

1314-
if (!decompressor)
1315-
{
1316-
auto writebuf = pContext->_get_writebuffer();
1317-
pContext->allocate_reply_space(writebuf.alloc(chunkSize), chunkSize);
1318-
buffer = pContext->m_body_data.get();
1319-
}
1320-
else
1314+
if (decompressor)
13211315
{
13221316
// m_buffer holds the compressed data; we'll decompress into the caller's buffer later
13231317
if (pContext->m_compression_state.m_buffer.capacity() < chunkSize)
@@ -1326,6 +1320,12 @@ class winhttp_client final : public _http_client_communicator
13261320
}
13271321
buffer = pContext->m_compression_state.m_buffer.data();
13281322
}
1323+
else
1324+
{
1325+
auto writebuf = pContext->_get_writebuffer();
1326+
pContext->allocate_reply_space(writebuf.alloc(chunkSize), chunkSize);
1327+
buffer = pContext->m_body_data.get();
1328+
}
13291329

13301330
if (!WinHttpReadData(
13311331
pContext->m_request_handle,
@@ -1350,15 +1350,15 @@ class winhttp_client final : public _http_client_communicator
13501350
{
13511351
// We could allocate less than a chunk for the compressed data here, though that
13521352
// would result in more trips through this path for not-so-compressible data...
1353-
if (p_request_context->m_body_data.length() > http::details::chunked_encoding::additional_encoding_space)
1353+
if (p_request_context->m_body_data.size() > http::details::chunked_encoding::additional_encoding_space)
13541354
{
13551355
// If we've previously allocated space for the compressed data, don't reduce it
1356-
chunk_size = p_request_context->m_body_data.length() - http::details::chunked_encoding::additional_encoding_space;
1356+
chunk_size = p_request_context->m_body_data.size() - http::details::chunked_encoding::additional_encoding_space;
13571357
}
13581358
else if (p_request_context->m_remaining_to_write != std::numeric_limits<size_t>::max())
13591359
{
13601360
// Choose a semi-intelligent size based on how much total data is left to compress
1361-
chunk_size = std::min((size_t)p_request_context->m_remaining_to_write+128, p_request_context->m_http_client->client_config().chunksize());
1361+
chunk_size = std::min(static_cast<size_t>(p_request_context->m_remaining_to_write)+128, p_request_context->m_http_client->client_config().chunksize());
13621362
}
13631363
else
13641364
{
@@ -1369,7 +1369,7 @@ class winhttp_client final : public _http_client_communicator
13691369
else
13701370
{
13711371
// We're not compressing; use the smaller of the remaining data (if known) and the configured (or default) chunk size
1372-
chunk_size = std::min((size_t)p_request_context->m_remaining_to_write, p_request_context->m_http_client->client_config().chunksize());
1372+
chunk_size = std::min(static_cast<size_t>(p_request_context->m_remaining_to_write), p_request_context->m_http_client->client_config().chunksize());
13731373
}
13741374
p_request_context->allocate_request_space(nullptr, chunk_size + http::details::chunked_encoding::additional_encoding_space);
13751375

@@ -1590,7 +1590,7 @@ class winhttp_client final : public _http_client_communicator
15901590
}
15911591
else
15921592
{
1593-
length = std::min((size_t)p_request_context->m_remaining_to_write, p_request_context->m_http_client->client_config().chunksize());
1593+
length = std::min(static_cast<size_t>(p_request_context->m_remaining_to_write), p_request_context->m_http_client->client_config().chunksize());
15941594
if (p_request_context->m_compression_state.m_buffer.capacity() < length)
15951595
{
15961596
p_request_context->m_compression_state.m_buffer.reserve(length);
@@ -2182,7 +2182,7 @@ class winhttp_client final : public _http_client_communicator
21822182

21832183
if (p_request_context->m_decompressor)
21842184
{
2185-
size_t chunk_size = std::max((size_t)bytesRead, p_request_context->m_http_client->client_config().chunksize());
2185+
size_t chunk_size = std::max(static_cast<size_t>(bytesRead), p_request_context->m_http_client->client_config().chunksize());
21862186
p_request_context->m_compression_state.m_bytes_read = static_cast<size_t>(bytesRead);
21872187
p_request_context->m_compression_state.m_chunk_bytes = 0;
21882188

0 commit comments

Comments
 (0)