forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendfile.cpp
More file actions
468 lines (372 loc) · 10.6 KB
/
Sendfile.cpp
File metadata and controls
468 lines (372 loc) · 10.6 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "Sendfile.h"
#include "../../../utils/Utils.h"
#include <fstream>
namespace HttpServer
{
static std::string getMimeTypeByFileName(
const std::string &fileName,
const std::unordered_map<std::string, std::string> &mimesTypes
) noexcept {
const size_t ext_pos = fileName.rfind('.');
const std::string file_ext = std::string::npos != ext_pos
? Utils::getLowerString(fileName.substr(ext_pos + 1) )
: std::string();
auto const it_mime = mimesTypes.find(file_ext);
return mimesTypes.cend() == it_mime
? std::string("application/octet-stream")
: it_mime->second;
}
static std::vector<std::tuple<size_t, size_t> > getRanges(
const std::string &rangeHeader,
const size_t posSymEqual,
const size_t fileSize,
std::string *resultRangeHeader,
size_t *contentLength
) noexcept {
std::vector<std::tuple<size_t, size_t> > ranges;
*contentLength = 0;
size_t delimiter = posSymEqual; // rangeHeader.find('=');
const std::string range_unit_name(
rangeHeader.cbegin(),
rangeHeader.cbegin() + delimiter
);
static const std::unordered_map<std::string, size_t> ranges_units {
{ "bytes", 1 }
};
auto const it_unit = ranges_units.find(range_unit_name);
if (ranges_units.cend() == it_unit) {
return ranges;
}
const size_t range_unit = it_unit->second;
for (size_t str_pos; std::string::npos != delimiter; )
{
str_pos = delimiter + 1;
delimiter = rangeHeader.find(',', str_pos);
const size_t range_pos = rangeHeader.find('-', str_pos);
if (range_pos < delimiter)
{
const std::string range_begin_str(
rangeHeader.cbegin() + str_pos,
rangeHeader.cbegin() + range_pos
);
const std::string range_end_str(
rangeHeader.cbegin() + range_pos + 1,
std::string::npos == delimiter
? rangeHeader.cend()
: rangeHeader.cbegin() + delimiter
);
if (range_begin_str.empty() == false)
{
const size_t range_begin = std::strtoull(
range_begin_str.c_str(), nullptr, 10
) * range_unit;
if (range_begin < fileSize)
{
if (range_end_str.empty() == false)
{
size_t range_end = std::strtoull(
range_end_str.c_str(), nullptr, 10
) * range_unit;
if (range_end >= range_begin)
{
if (range_end > fileSize) {
range_end = fileSize;
}
const size_t length = range_end - range_begin + 1;
*contentLength += length;
*resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ',';
ranges.emplace_back(std::tuple<size_t, size_t> {
range_begin, length
});
}
}
else // if range_end_str empty
{
const size_t length = fileSize - range_begin;
*contentLength += length;
*resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(fileSize - 1) + ',';
ranges.emplace_back(std::tuple<size_t, size_t> {
range_begin, length
});
}
}
}
else if (range_end_str.empty() == false) // if range_begin_str empty
{
size_t range_end = std::strtoull(
range_end_str.c_str(), nullptr, 10
) * range_unit;
const size_t length = range_end < fileSize
? fileSize - range_end
: fileSize;
const size_t range_begin = fileSize - length;
range_end = fileSize - range_begin - 1;
*contentLength += length;
*resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ',';
ranges.emplace_back(std::tuple<size_t, size_t> {
range_begin, length
});
}
}
}
if (ranges.empty() == false) {
(*resultRangeHeader).back() = '/';
*resultRangeHeader = "bytes " + *resultRangeHeader + std::to_string(fileSize);
}
return ranges;
}
int Sendfile::transferFilePart(
const ServerProtocol &prot,
const struct Request &req,
const std::string &fileName,
const std::unordered_map<std::string, std::string> &mimesTypes,
const time_t fileTime,
const size_t fileSize,
const std::string &rangeHeader,
std::vector<std::pair<std::string, std::string> > &additionalHeaders,
const bool headersOnly
) noexcept {
const size_t pos_sym_equal = rangeHeader.find('=');
if (std::string::npos == pos_sym_equal) {
prot.sendHeaders(
Http::StatusCode::BAD_REQUEST,
additionalHeaders,
req.timeout
);
return 1;
}
std::string content_range_header;
size_t content_length;
const std::vector<std::tuple<size_t, size_t> > ranges = getRanges(
rangeHeader,
pos_sym_equal,
fileSize,
&content_range_header,
&content_length
);
if (0 == content_length) {
prot.sendHeaders(
Http::StatusCode::REQUESTED_RANGE_NOT_SATISFIABLE,
additionalHeaders,
req.timeout
);
return 2;
}
// Ranges transfer
std::ifstream file(fileName, std::ifstream::binary);
if ( ! file) {
file.close();
prot.sendHeaders(
Http::StatusCode::INTERNAL_SERVER_ERROR,
additionalHeaders,
req.timeout
);
return 3;
}
const std::string file_mime_type = getMimeTypeByFileName(fileName, mimesTypes);
additionalHeaders.emplace_back("content-type", file_mime_type);
additionalHeaders.emplace_back("content-length", std::to_string(content_length) );
additionalHeaders.emplace_back("accept-ranges", "bytes");
additionalHeaders.emplace_back("content-range", content_range_header);
additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(fileTime, true) );
// Отправить заголовки (206 Partial Content)
if (
prot.sendHeaders(
Http::StatusCode::PARTIAL_CONTENT,
additionalHeaders,
req.timeout,
headersOnly
) == false
) {
file.close();
return 4;
}
if (false == headersOnly)
{
size_t position, length;
std::vector<char> buf;
DataTransfer dt {
content_length,
0
};
for (auto const &range : ranges)
{
std::tie(position, length) = range;
buf.resize(
length < 512 * 1024
? length
: 512 * 1024
);
file.seekg(position, file.beg);
size_t send_size_left = length;
long send_size;
do {
if (send_size_left < 512 * 1024) {
buf.resize(send_size_left);
}
file.read(buf.data(), buf.size() );
auto const read_size = file.gcount();
send_size = prot.sendData(
buf.data(),
read_size,
req.timeout,
&dt
);
send_size_left -= send_size;
}
while (
false == file.eof() &&
false == file.fail() &&
send_size > 0 &&
send_size_left
);
}
}
file.close();
return 0;
}
/**
* Передача файла (или его части)
*/
int Sendfile::transferFile(
const ServerProtocol &prot,
const struct Request &req,
const std::string &fileName,
const std::unordered_map<std::string, std::string> &mimesTypes,
std::vector<std::pair<std::string, std::string> > &additionalHeaders,
const bool headersOnly
) noexcept {
// Get current time in GMT
additionalHeaders.emplace_back("date", Utils::getDatetimeAsString() );
time_t file_time;
size_t file_size;
// Получить размер файла и дату последнего изменения
if (System::getFileSizeAndTimeGmt(fileName, &file_size, &file_time) == false) {
prot.sendHeaders(
Http::StatusCode::NOT_FOUND,
additionalHeaders,
req.timeout
);
return 1;
}
// Check for If-Modified header
auto const it_modified = req.incoming_headers.find("if-modified-since");
// Если найден заголовок проверки изменения файла (проверить, изменялся ли файл)
if (req.incoming_headers.cend() != it_modified) {
const time_t time_in_request = Utils::rfc822DatetimeToTimestamp(it_modified->second);
if (file_time == time_in_request) {
prot.sendHeaders(
Http::StatusCode::NOT_MODIFIED,
additionalHeaders,
req.timeout
);
return 2;
}
}
auto const it_range = req.incoming_headers.find("range");
// Range transfer
if (req.incoming_headers.cend() != it_range) {
return transferFilePart(
prot,
req,
fileName,
mimesTypes,
file_time,
file_size,
it_range->second,
additionalHeaders,
headersOnly
);
}
// File transfer
std::ifstream file(fileName, std::ifstream::binary);
if ( ! file) {
file.close();
prot.sendHeaders(
Http::StatusCode::INTERNAL_SERVER_ERROR,
additionalHeaders,
req.timeout
);
return 3;
}
const std::string file_mime_type = getMimeTypeByFileName(fileName, mimesTypes);
additionalHeaders.emplace_back("content-type", file_mime_type);
additionalHeaders.emplace_back("content-length", std::to_string(file_size) );
additionalHeaders.emplace_back("accept-ranges", "bytes");
additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(file_time, true) );
// Отправить заголовки (200 OK)
if (
prot.sendHeaders(
Http::StatusCode::OK,
additionalHeaders,
req.timeout,
headersOnly || 0 == file_size
) == false
) {
file.close();
return 4;
}
// Отправить файл
if (false == headersOnly && file_size)
{
std::vector<char> buf(
file_size < 512 * 1024
? file_size
: 512 * 1024
);
long send_size;
DataTransfer dt {
file_size,
0
};
do {
file.read(buf.data(), buf.size() );
send_size = prot.sendData(
buf.data(),
file.gcount(),
req.timeout,
&dt
);
}
while (
false == file.eof() &&
false == file.fail() &&
send_size > 0 &&
(dt.full_size - dt.send_total)
);
}
file.close();
return 0;
}
bool Sendfile::isConnectionReuse(const struct Request &req) noexcept {
return (req.connection_params & ConnectionParams::CONNECTION_REUSE) == ConnectionParams::CONNECTION_REUSE;
}
void Sendfile::xSendfile(
const ServerProtocol &prot,
struct Request &req,
const std::unordered_map<std::string, std::string> &mimesTypes
) noexcept {
auto const it_x_sendfile = req.outgoing_headers.find("x-sendfile");
if (req.outgoing_headers.cend() != it_x_sendfile)
{
std::vector<std::pair<std::string, std::string> > additional_headers;
if (Transfer::ProtocolVariant::HTTP_1 == req.protocol_variant) {
if (isConnectionReuse(req) ) {
additional_headers.emplace_back("connection", "keep-alive");
additional_headers.emplace_back("keep-alive", "timeout=5; max=" + std::to_string(req.keep_alive_count) );
} else {
additional_headers.emplace_back("connection", "close");
}
}
const bool headers_only = ("head" == req.method);
transferFile(
prot,
req,
it_x_sendfile->second,
mimesTypes,
additional_headers,
headers_only
);
}
}
}