forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_upload.cpp
More file actions
662 lines (534 loc) · 25.2 KB
/
file_upload.cpp
File metadata and controls
662 lines (534 loc) · 25.2 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <curl/curl.h>
#include <sys/stat.h>
#include <cassert>
#include <cstddef>
#include <fstream>
#include <memory>
#include <map>
#include <random>
#include <sstream>
#include <string>
#include <tuple>
#include "./httpserver.hpp"
#include "httpserver/string_utilities.hpp"
#include "./littletest.hpp"
using std::string;
using std::string_view;
using std::map;
using std::shared_ptr;
using std::vector;
using std::stringstream;
using httpserver::http_resource;
using httpserver::http_request;
using httpserver::http_response;
using httpserver::string_response;
using httpserver::file_response;
using httpserver::webserver;
using httpserver::create_webserver;
using httpserver::http::arg_map;
#ifdef HTTPSERVER_PORT
#define PORT HTTPSERVER_PORT
#else
#define PORT 8080
#endif // PORT
#define STR2(p) #p
#define STR(p) STR2(p)
#define PORT_STRING STR(PORT)
#ifdef HTTPSERVER_DATA_ROOT
#define ROOT STR(HTTPSERVER_DATA_ROOT)
#else
#define ROOT "."
#endif // HTTPSERVER_DATA_ROOT
static const char* TEST_CONTENT_FILENAME = "test_content";
static const char* TEST_CONTENT_FILEPATH = ROOT "/test_content";
static const char* FILENAME_IN_GET_CONTENT = "filename=\"test_content\"";
static const char* TEST_CONTENT = "test content of file\n";
static const char* TEST_KEY = "file";
static size_t TEST_CONTENT_SIZE = 21;
static const char* TEST_CONTENT_FILENAME_2 = "test_content_2";
static const char* TEST_CONTENT_FILEPATH_2 = ROOT "/test_content_2";
static const char* FILENAME_IN_GET_CONTENT_2 = "filename=\"test_content_2\"";
static const char* TEST_CONTENT_2 = "test content of second file\n";
static const char* TEST_KEY_2 = "file2";
static size_t TEST_CONTENT_SIZE_2 = 28;
static const char* TEST_PARAM_KEY = "param_key";
static const char* TEST_PARAM_VALUE = "Value of test param";
// The large file test_content_large is large enough to ensure
// that MHD splits the underlying request into several chunks.
static const char* LARGE_FILENAME_IN_GET_CONTENT = "filename=\"test_content_large\"";
static const char* LARGE_CONTENT_FILEPATH = ROOT "/test_content_large";
static const char* LARGE_KEY = "large_file";
static bool file_exists(const string &path) {
struct stat sb;
return (stat(path.c_str(), &sb) == 0);
}
static std::pair<CURLcode, int32_t> send_file_to_webserver(bool add_second_file, bool append_parameters) {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
curl_mime *form = curl_mime_init(curl);
curl_mimepart *field = curl_mime_addpart(form);
curl_mime_name(field, TEST_KEY);
curl_mime_filedata(field, TEST_CONTENT_FILEPATH);
if (add_second_file) {
field = curl_mime_addpart(form);
curl_mime_name(field, TEST_KEY_2);
curl_mime_filedata(field, TEST_CONTENT_FILEPATH_2);
}
if (append_parameters) {
field = curl_mime_addpart(form);
curl_mime_name(field, TEST_PARAM_KEY);
curl_mime_data(field, TEST_PARAM_VALUE, CURL_ZERO_TERMINATED);
}
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/upload");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
res = curl_easy_perform(curl);
long http_code = 0; // NOLINT [runtime/int]
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
curl_mime_free(form);
return {res, http_code};
}
static std::pair<CURLcode, int32_t> send_large_file(string* content, std::string args = "") {
// Generate a large (100K) file of random bytes. Upload the file with
// a curl request, then delete the file. The default chunk size of MHD
// appears to be around 16K, so 100K should be enough to trigger the
// behavior. Return the content via the pointer parameter so that test
// cases can make required checks for the content.
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
curl_mime *form = curl_mime_init(curl);
curl_mimepart *field = curl_mime_addpart(form);
std::ifstream infile(LARGE_CONTENT_FILEPATH);
std::stringstream buffer;
buffer << infile.rdbuf();
infile.close();
*content = buffer.str();
curl_mime_name(field, LARGE_KEY);
curl_mime_filedata(field, LARGE_CONTENT_FILEPATH);
std::string url = "localhost:" PORT_STRING "/upload";
if (!args.empty()) {
url.append(args);
}
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
res = curl_easy_perform(curl);
long http_code = 0; // NOLINT [runtime/int]
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
curl_mime_free(form);
return {res, http_code};
}
static std::tuple<bool, CURLcode, int32_t> send_file_via_put() {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl;
CURLcode res;
struct stat file_info;
FILE *fd;
fd = fopen(TEST_CONTENT_FILEPATH, "rb");
if (!fd) {
return {false, CURLcode{}, 0L};
}
if (fstat(fileno(fd), &file_info) != 0) {
return {false, CURLcode{}, 0L};
}
curl = curl_easy_init();
if (!curl) {
fclose(fd);
return {false, CURLcode{}, 0L};
}
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/upload");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) file_info.st_size);
res = curl_easy_perform(curl);
long http_code = 0; // NOLINT [runtime/int]
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
fclose(fd);
return {true, res, http_code};
}
class print_file_upload_resource : public http_resource {
public:
shared_ptr<http_response> render_POST(const http_request& req) {
content = req.get_content();
auto args_view = req.get_args();
// req may go out of scope, so we need to copy the values.
for (auto const& item : args_view) {
for (auto const & value : item.second.get_all_values()) {
args[string(item.first)].push_back(string(value));
}
}
files = req.get_files();
return std::make_shared<string_response>("OK", 201, "text/plain");
}
shared_ptr<http_response> render_PUT(const http_request& req) {
content = req.get_content();
auto args_view = req.get_args();
// req may go out of scope, so we need to copy the values.
for (auto const& item : args_view) {
for (auto const & value : item.second.get_all_values()) {
args[string(item.first)].push_back(string(value));
}
}
files = req.get_files();
return std::make_shared<string_response>("OK", 200, "text/plain");
}
const std::map<string, std::vector<string>, httpserver::http::arg_comparator> get_args() const {
return args;
}
const map<string, map<string, httpserver::http::file_info>> get_files() const {
return files;
}
const string get_content() const {
return content;
}
private:
std::map<std::string, std::vector<std::string>, httpserver::http::arg_comparator> args;
map<string, map<string, httpserver::http::file_info>> files;
string content;
};
LT_BEGIN_SUITE(file_upload_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(file_upload_suite)
LT_BEGIN_AUTO_TEST(file_upload_suite, check_files)
std::ifstream it;
it.open(TEST_CONTENT_FILEPATH);
LT_CHECK_EQ(it.is_open(), true);
it.open(TEST_CONTENT_FILEPATH_2);
LT_CHECK_EQ(it.is_open(), true);
it.open(LARGE_CONTENT_FILEPATH);
LT_CHECK_EQ(it.is_open(), true);
LT_END_AUTO_TEST(check_files)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk)
string upload_directory = ".";
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK)
.file_upload_dir(upload_directory)
.generate_random_filename_on_upload());
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(false, false);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
ws->stop();
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 1);
auto arg = args.begin();
LT_CHECK_EQ(arg->first, TEST_KEY);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 1);
map<string, map<string, httpserver::http::file_info>>::iterator file_key = files.begin();
LT_CHECK_EQ(file_key->first, TEST_KEY);
LT_CHECK_EQ(file_key->second.size(), 1);
map<string, httpserver::http::file_info>::iterator file = file_key->second.begin();
LT_CHECK_EQ(file->first, TEST_CONTENT_FILENAME);
LT_CHECK_EQ(file->second.get_file_size(), TEST_CONTENT_SIZE);
LT_CHECK_EQ(file->second.get_content_type(), httpserver::http::http_utils::application_octet_stream);
string expected_filename = upload_directory +
httpserver::http::http_utils::path_separator +
httpserver::http::http_utils::upload_filename_template;
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
expected_filename.substr(0, expected_filename.size() - 6));
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
LT_END_AUTO_TEST(file_upload_memory_and_disk)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_via_put)
string upload_directory = ".";
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK)
.file_upload_dir(upload_directory)
.generate_random_filename_on_upload());
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto ret = send_file_via_put();
LT_CHECK_EQ(std::get<1>(ret), 0);
LT_CHECK_EQ(std::get<2>(ret), 200);
LT_ASSERT_EQ(std::get<0>(ret), true);
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content, TEST_CONTENT);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 0);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 0);
ws->stop();
LT_END_AUTO_TEST(file_upload_memory_and_disk_via_put)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_additional_params)
string upload_directory = ".";
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK)
.file_upload_dir(upload_directory)
.generate_random_filename_on_upload());
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(false, true);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
ws->stop();
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_PARAM_KEY) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_PARAM_VALUE) != string::npos, true);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 2);
auto arg = args.begin();
LT_CHECK_EQ(arg->first, TEST_KEY);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT);
arg++;
LT_CHECK_EQ(arg->first, TEST_PARAM_KEY);
LT_CHECK_EQ(arg->second[0], TEST_PARAM_VALUE);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 1);
map<string, map<string, httpserver::http::file_info>>::iterator file_key = files.begin();
LT_CHECK_EQ(file_key->first, TEST_KEY);
LT_CHECK_EQ(file_key->second.size(), 1);
map<string, httpserver::http::file_info>::iterator file = file_key->second.begin();
LT_CHECK_EQ(file->first, TEST_CONTENT_FILENAME);
LT_CHECK_EQ(file->second.get_file_size(), TEST_CONTENT_SIZE);
LT_CHECK_EQ(file->second.get_content_type(), httpserver::http::http_utils::application_octet_stream);
string expected_filename = upload_directory +
httpserver::http::http_utils::path_separator +
httpserver::http::http_utils::upload_filename_template;
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
expected_filename.substr(0, expected_filename.size() - 6));
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
LT_END_AUTO_TEST(file_upload_memory_and_disk_additional_params)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files)
string upload_directory = ".";
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK)
.file_upload_dir(upload_directory)
.generate_random_filename_on_upload());
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(true, false);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
ws->stop();
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT_2) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_CONTENT_2) != string::npos, true);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 2);
auto arg = args.begin();
LT_CHECK_EQ(arg->first, TEST_KEY);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT);
arg++;
LT_CHECK_EQ(arg->first, TEST_KEY_2);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT_2);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 2);
map<string, map<string, httpserver::http::file_info>>::iterator file_key = files.begin();
LT_CHECK_EQ(file_key->first, TEST_KEY);
LT_CHECK_EQ(file_key->second.size(), 1);
map<string, httpserver::http::file_info>::iterator file = file_key->second.begin();
LT_CHECK_EQ(file->first, TEST_CONTENT_FILENAME);
LT_CHECK_EQ(file->second.get_file_size(), TEST_CONTENT_SIZE);
LT_CHECK_EQ(file->second.get_content_type(), httpserver::http::http_utils::application_octet_stream);
string expected_filename = upload_directory +
httpserver::http::http_utils::path_separator +
httpserver::http::http_utils::upload_filename_template;
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
expected_filename.substr(0, expected_filename.size() - 6));
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
file_key++;
LT_CHECK_EQ(file_key->first, TEST_KEY_2);
LT_CHECK_EQ(file_key->second.size(), 1);
file = file_key->second.begin();
LT_CHECK_EQ(file->first, TEST_CONTENT_FILENAME_2);
LT_CHECK_EQ(file->second.get_file_size(), TEST_CONTENT_SIZE_2);
LT_CHECK_EQ(file->second.get_content_type(), httpserver::http::http_utils::application_octet_stream);
expected_filename = upload_directory +
httpserver::http::http_utils::path_separator +
httpserver::http::http_utils::upload_filename_template;
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
expected_filename.substr(0, expected_filename.size() - 6));
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
LT_END_AUTO_TEST(file_upload_memory_and_disk_two_files)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only)
string upload_directory = ".";
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.no_put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY)
.file_upload_dir(upload_directory)
.generate_random_filename_on_upload());
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(false, false);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
ws->stop();
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.size(), 0);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 0);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 1);
map<string, map<string, httpserver::http::file_info>>::iterator file_key = files.begin();
LT_CHECK_EQ(file_key->first, TEST_KEY);
LT_CHECK_EQ(file_key->second.size(), 1);
map<string, httpserver::http::file_info>::iterator file = file_key->second.begin();
LT_CHECK_EQ(file->first, TEST_CONTENT_FILENAME);
LT_CHECK_EQ(file->second.get_file_size(), TEST_CONTENT_SIZE);
LT_CHECK_EQ(file->second.get_content_type(), httpserver::http::http_utils::application_octet_stream);
string expected_filename = upload_directory +
httpserver::http::http_utils::path_separator +
httpserver::http::http_utils::upload_filename_template;
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
expected_filename.substr(0, expected_filename.size() - 6));
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
LT_END_AUTO_TEST(file_upload_disk_only)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_incl_content)
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY));
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(false, false);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 1);
auto arg = args.begin();
LT_CHECK_EQ(arg->first, TEST_KEY);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(resource.get_files().size(), 0);
ws->stop();
LT_END_AUTO_TEST(file_upload_memory_only_incl_content)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content)
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY));
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
// Upload a large file to trigger the chunking behavior of MHD.
std::string file_content;
auto res = send_large_file(&file_content);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(LARGE_FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(file_content) != string::npos, true);
// The chunks of the file should be concatenated into the first
// arg value of the key.
auto const args = resource.get_args();
LT_CHECK_EQ(args.size(), 1);
auto const file_arg_iter = args.find(std::string_view(LARGE_KEY));
if (file_arg_iter == args.end()) {
LT_FAIL("file arg not found");
}
LT_CHECK_EQ(file_arg_iter->second.size(), 1);
LT_CHECK_EQ(file_arg_iter->second[0], file_content);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(resource.get_files().size(), 0);
ws->stop();
LT_END_AUTO_TEST(file_upload_large_content)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content_with_args)
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY));
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
// Upload a large file to trigger the chunking behavior of MHD.
// Include some additional args to make sure those are processed as well.
std::string file_content;
auto res = send_large_file(&file_content, "?arg1=hello&arg1=world");
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.find(LARGE_FILENAME_IN_GET_CONTENT) != string::npos, true);
LT_CHECK_EQ(actual_content.find(file_content) != string::npos, true);
auto const args = resource.get_args();
LT_CHECK_EQ(args.size(), 2);
auto const file_arg_iter = args.find(std::string_view(LARGE_KEY));
if (file_arg_iter == args.end()) {
LT_FAIL("file arg not found");
}
LT_CHECK_EQ(file_arg_iter->second.size(), 1);
LT_CHECK_EQ(file_arg_iter->second[0], file_content);
auto const other_arg_iter = args.find(std::string_view("arg1"));
if (other_arg_iter == args.end()) {
LT_FAIL("other arg(s) not found");
}
LT_CHECK_EQ(other_arg_iter->second.size(), 2);
LT_CHECK_EQ(other_arg_iter->second[0], "hello");
LT_CHECK_EQ(other_arg_iter->second[1], "world");
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(resource.get_files().size(), 0);
ws->stop();
LT_END_AUTO_TEST(file_upload_large_content_with_args)
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_excl_content)
auto ws = std::make_unique<webserver>(create_webserver(PORT)
.no_put_processed_data_to_content()
.file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY));
ws->start(false);
LT_CHECK_EQ(ws->is_running(), true);
print_file_upload_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("upload", &resource));
auto res = send_file_to_webserver(false, false);
LT_ASSERT_EQ(res.first, 0);
LT_ASSERT_EQ(res.second, 201);
string actual_content = resource.get_content();
LT_CHECK_EQ(actual_content.size(), 0);
auto args = resource.get_args();
LT_CHECK_EQ(args.size(), 1);
auto arg = args.begin();
LT_CHECK_EQ(arg->first, TEST_KEY);
LT_CHECK_EQ(arg->second[0], TEST_CONTENT);
map<string, map<string, httpserver::http::file_info>> files = resource.get_files();
LT_CHECK_EQ(files.size(), 0);
ws->stop();
LT_END_AUTO_TEST(file_upload_memory_only_excl_content)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()