This repository was archived by the owner on Jun 14, 2024. It is now read-only.
forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp_helpers.cpp
More file actions
450 lines (375 loc) · 12.7 KB
/
http_helpers.cpp
File metadata and controls
450 lines (375 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Implementation Details of the http.h layer of messaging
*
* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
// CPPREST_EXCLUDE_COMPRESSION is set if we're on a platform that supports compression but we want to explicitly disable it.
// CPPREST_EXCLUDE_WEBSOCKETS is a flag that now essentially means "no external dependencies". TODO: Rename
#if __APPLE__
#include "TargetConditionals.h"
#if defined(TARGET_OS_MAC)
#if !defined(CPPREST_EXCLUDE_COMPRESSION)
#define CPPREST_HTTP_COMPRESSION
#endif // !defined(CPPREST_EXCLUDE_COMPRESSION)
#endif // defined(TARGET_OS_MAC)
#elif defined(_WIN32) && (!defined(WINAPI_FAMILY) || WINAPI_PARTITION_DESKTOP)
#if !defined(CPPREST_EXCLUDE_WEBSOCKETS) && !defined(CPPREST_EXCLUDE_COMPRESSION)
#define CPPREST_HTTP_COMPRESSION
#endif // !defined(CPPREST_EXCLUDE_WEBSOCKETS) && !defined(CPPREST_EXCLUDE_COMPRESSION)
#endif
#if defined(CPPREST_HTTP_COMPRESSION)
#include <zlib.h>
#endif
#include "internal_http_helpers.h"
using namespace web;
using namespace utility;
using namespace utility::conversions;
namespace web { namespace http
{
namespace details
{
// Remove once VS 2013 is no longer supported.
#if defined(_WIN32) && _MSC_VER < 1900
static const http_status_to_phrase idToPhraseMap [] = {
#define _PHRASES
#define DAT(a,b,c) {status_codes::a, c},
#include "cpprest/details/http_constants.dat"
#undef _PHRASES
#undef DAT
};
#endif
utility::string_t get_default_reason_phrase(status_code code)
{
#if !defined(_WIN32) || _MSC_VER >= 1900
// Future improvement: why is this stored as an array of structs instead of a map
// indexed on the status code for faster lookup?
// Not a big deal because it is uncommon to not include a reason phrase.
static const http_status_to_phrase idToPhraseMap [] = {
#define _PHRASES
#define DAT(a,b,c) {status_codes::a, c},
#include "cpprest/details/http_constants.dat"
#undef _PHRASES
#undef DAT
};
#endif
utility::string_t phrase;
for (const auto &elm : idToPhraseMap)
{
if (elm.id == code)
{
phrase = elm.phrase;
break;
}
}
return phrase;
}
size_t chunked_encoding::add_chunked_delimiters(_Out_writes_(buffer_size) uint8_t *data, _In_ size_t buffer_size, size_t bytes_read)
{
size_t offset = 0;
if (buffer_size < bytes_read + http::details::chunked_encoding::additional_encoding_space)
{
throw http_exception(_XPLATSTR("Insufficient buffer size."));
}
if (bytes_read == 0)
{
offset = 7;
data[7] = '0';
data[8] = '\r'; data[9] = '\n'; // The end of the size.
data[10] = '\r'; data[11] = '\n'; // The end of the message.
}
else
{
char buffer[9];
#ifdef _WIN32
sprintf_s(buffer, sizeof(buffer), "%8IX", bytes_read);
#else
snprintf(buffer, sizeof(buffer), "%8zX", bytes_read);
#endif
memcpy(&data[0], buffer, 8);
while (data[offset] == ' ') ++offset;
data[8] = '\r'; data[9] = '\n'; // The end of the size.
data[10 + bytes_read] = '\r'; data[11 + bytes_read] = '\n'; // The end of the chunk.
}
return offset;
}
static const std::array<bool,128> valid_chars =
{{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //16-31
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, //32-47
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, //48-63
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //64-79
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, //80-95
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //96-111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 //112-127
}};
// Checks if the method contains any invalid characters
bool validate_method(const utility::string_t& method)
{
for (const auto &ch : method)
{
size_t ch_sz = static_cast<size_t>(ch);
if (ch_sz >= 128)
return false;
if (!valid_chars[ch_sz])
return false;
}
return true;
}
namespace compression
{
#if defined(CPPREST_HTTP_COMPRESSION)
class compression_base_impl
{
public:
compression_base_impl(compression_algorithm alg) : m_alg(alg), m_zLibState(Z_OK)
{
memset(&m_zLibStream, 0, sizeof(m_zLibStream));
}
size_t read_output(size_t input_offset, size_t available_input, size_t total_out_before, uint8_t* temp_buffer, data_buffer& output)
{
input_offset += (available_input - stream().avail_in);
auto out_length = stream().total_out - total_out_before;
output.insert(output.end(), temp_buffer, temp_buffer + out_length);
return input_offset;
}
bool is_complete() const
{
return state() == Z_STREAM_END;
}
bool has_error() const
{
return !is_complete() && state() != Z_OK;
}
int state() const
{
return m_zLibState;
}
void set_state(int state)
{
m_zLibState = state;
}
compression_algorithm algorithm() const
{
return m_alg;
}
z_stream& stream()
{
return m_zLibStream;
}
int to_zlib_alg(compression_algorithm alg)
{
return static_cast<int>(alg);
}
private:
const compression_algorithm m_alg;
std::atomic<int> m_zLibState{ Z_OK };
z_stream m_zLibStream;
};
class stream_decompressor::stream_decompressor_impl : public compression_base_impl
{
public:
stream_decompressor_impl(compression_algorithm alg) : compression_base_impl(alg)
{
set_state(inflateInit2(&stream(), to_zlib_alg(alg)));
}
~stream_decompressor_impl()
{
inflateEnd(&stream());
}
data_buffer decompress(const uint8_t* input, size_t input_size)
{
if (input == nullptr || input_size == 0)
{
set_state(Z_BUF_ERROR);
return data_buffer();
}
// Need to guard against attempting to decompress when we're already finished or encountered an error!
if (is_complete() || has_error())
{
set_state(Z_STREAM_ERROR);
return data_buffer();
}
const size_t BUFFER_SIZE = 1024;
unsigned char temp_buffer[BUFFER_SIZE];
data_buffer output;
output.reserve(input_size * 3);
size_t input_offset{ 0 };
while (state() == Z_OK && input_offset < input_size)
{
auto total_out_before = stream().total_out;
auto available_input = input_size - input_offset;
stream().next_in = const_cast<uint8_t*>(&input[input_offset]);
stream().avail_in = static_cast<int>(available_input);
stream().next_out = temp_buffer;
stream().avail_out = BUFFER_SIZE;
set_state(inflate(&stream(), Z_PARTIAL_FLUSH));
if (has_error())
{
return data_buffer();
}
input_offset = read_output(input_offset, available_input, total_out_before, temp_buffer, output);
}
return output;
}
};
class stream_compressor::stream_compressor_impl : public compression_base_impl
{
public:
stream_compressor_impl(compression_algorithm alg) : compression_base_impl(alg)
{
const int level = Z_DEFAULT_COMPRESSION;
if (alg == compression_algorithm::gzip)
{
set_state(deflateInit2(&stream(), level, Z_DEFLATED, to_zlib_alg(alg), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY));
}
else if (alg == compression_algorithm::deflate)
{
set_state(deflateInit(&stream(), level));
}
}
web::http::details::compression::data_buffer compress(const uint8_t* input, size_t input_size, bool finish)
{
if (input == nullptr || input_size == 0)
{
set_state(Z_BUF_ERROR);
return data_buffer();
}
if (state() != Z_OK)
{
set_state(Z_STREAM_ERROR);
return data_buffer();
}
data_buffer output;
output.reserve(input_size);
const size_t BUFFER_SIZE = 1024;
uint8_t temp_buffer[BUFFER_SIZE];
size_t input_offset{ 0 };
auto flush = Z_NO_FLUSH;
while (flush == Z_NO_FLUSH)
{
auto total_out_before = stream().total_out;
auto available_input = input_size - input_offset;
if (available_input == 0)
{
flush = finish ? Z_FINISH : Z_PARTIAL_FLUSH;
}
else
{
stream().avail_in = static_cast<int>(available_input);
stream().next_in = const_cast<uint8_t*>(&input[input_offset]);
}
do
{
stream().next_out = temp_buffer;
stream().avail_out = BUFFER_SIZE;
set_state(deflate(&stream(), flush));
if (has_error())
{
return data_buffer();
}
input_offset = read_output(input_offset, available_input, total_out_before, temp_buffer, output);
} while (stream().avail_out == 0);
}
return output;
}
~stream_compressor_impl()
{
deflateEnd(&stream());
}
};
#else // Stub impl for when compression is not supported
class compression_base_impl
{
public:
bool has_error() const
{
return true;
}
};
class stream_compressor::stream_compressor_impl : public compression_base_impl
{
public:
stream_compressor_impl(compression_algorithm) {}
compression::data_buffer compress(const uint8_t* data, size_t size, bool)
{
return data_buffer(data, data + size);
}
};
class stream_decompressor::stream_decompressor_impl : public compression_base_impl
{
public:
stream_decompressor_impl(compression_algorithm) {}
compression::data_buffer decompress(const uint8_t* data, size_t size)
{
return data_buffer(data, data + size);
}
};
#endif
bool __cdecl stream_decompressor::is_supported()
{
#if !defined(CPPREST_HTTP_COMPRESSION)
return false;
#else
return true;
#endif
}
stream_decompressor::stream_decompressor(compression_algorithm alg)
: m_pimpl(std::make_shared<stream_decompressor::stream_decompressor_impl>(alg))
{
}
compression::data_buffer stream_decompressor::decompress(const data_buffer& input)
{
if (input.empty())
{
return data_buffer();
}
return m_pimpl->decompress(&input[0], input.size());
}
web::http::details::compression::data_buffer stream_decompressor::decompress(const uint8_t* input, size_t input_size)
{
return m_pimpl->decompress(input, input_size);
}
bool stream_decompressor::has_error() const
{
return m_pimpl->has_error();
}
bool __cdecl stream_compressor::is_supported()
{
#if !defined(CPPREST_HTTP_COMPRESSION)
return false;
#else
return true;
#endif
}
stream_compressor::stream_compressor(compression_algorithm alg)
: m_pimpl(std::make_shared<stream_compressor::stream_compressor_impl>(alg))
{
}
compression::data_buffer stream_compressor::compress(const data_buffer& input, bool finish)
{
if (input.empty())
{
return compression::data_buffer();
}
return m_pimpl->compress(&input[0], input.size(), finish);
}
web::http::details::compression::data_buffer stream_compressor::compress(const uint8_t* input, size_t input_size, bool finish)
{
return m_pimpl->compress(input, input_size, finish);
}
bool stream_compressor::has_error() const
{
return m_pimpl->has_error();
}
} // namespace compression
} // namespace details
}} // namespace web::http