Skip to content

Commit acc3e6a

Browse files
authored
Cleanup files uploaded on request deletion (#266)
* http_request: Remove uploaded files in destructor If the user needs those files, she should copy/move theme in the request handler. References: #264 Signed-off-by: Alexander Dahl <[email protected]> * test: integ: file_upload: Remove redundant file delete Not needed anymore. http_request destructor does cleanup now. * test: integ: file_upload: Check uploaded files are deleted * test: integ: file_upload: Stop webserver earlier The webserver runs in a different thread than the test, therefor deleting the uploaded files and checking if they are deleted might lead into a race condition, and thus into tests failing sometimes, but not always. Moving the webserver stop and destructor behind the curl action should be safe, every information needed for the test is copied to or in the resource handler.
1 parent 31dfed7 commit acc3e6a

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/http_request.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,13 @@ std::ostream &operator<< (std::ostream &os, const http_request &r) {
256256
return os;
257257
}
258258

259+
http_request::~http_request() {
260+
for ( const auto &file_key : this->get_files() ) {
261+
for ( const auto &files : file_key.second ) {
262+
// C++17 has std::filesystem::remove()
263+
remove(files.second.get_file_system_file_name().c_str());
264+
}
265+
}
266+
}
267+
259268
} // namespace httpserver

src/httpserver/http_request.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct MHD_Connection;
4646

4747
namespace httpserver {
4848

49+
namespace details { struct modded_request; }
50+
4951
/**
5052
* Class representing an abstraction for an Http Request. It is used from classes using these apis to receive information through http protocol.
5153
**/
@@ -254,6 +256,8 @@ class http_request {
254256
http_request& operator=(const http_request& b) = default;
255257
http_request& operator=(http_request&& b) = default;
256258

259+
~http_request();
260+
257261
std::string path;
258262
std::string method;
259263
std::map<std::string, std::string, http::arg_comparator> args;
@@ -356,6 +360,7 @@ class http_request {
356360
const std::map<std::string, std::string, http::header_comparator> get_headerlike_values(enum MHD_ValueKind kind) const;
357361

358362
friend class webserver;
363+
friend struct details::modded_request;
359364
};
360365

361366
std::ostream &operator<< (std::ostream &os, const http_request &r);

test/integ/file_upload.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ static size_t TEST_CONTENT_SIZE_2 = 28;
5959
static const char* TEST_PARAM_KEY = "param_key";
6060
static const char* TEST_PARAM_VALUE = "Value of test param";
6161

62+
static bool file_exists(const string &path) {
63+
struct stat sb;
64+
65+
return (stat(path.c_str(), &sb) == 0);
66+
}
67+
6268
static CURLcode send_file_to_webserver(bool add_second_file, bool append_parameters) {
6369
curl_global_init(CURL_GLOBAL_ALL);
6470

@@ -191,6 +197,9 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk)
191197
CURLcode res = send_file_to_webserver(false, false);
192198
LT_ASSERT_EQ(res, 0);
193199

200+
ws->stop();
201+
delete ws;
202+
194203
string actual_content = resource.get_content();
195204
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
196205
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
@@ -216,10 +225,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk)
216225
httpserver::http::http_utils::upload_filename_template;
217226
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
218227
expected_filename.substr(0, expected_filename.size() - 6));
219-
unlink(file->second.get_file_system_file_name().c_str());
220-
221-
ws->stop();
222-
delete ws;
228+
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
223229
LT_END_AUTO_TEST(file_upload_memory_and_disk)
224230

225231
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_via_put)
@@ -271,6 +277,9 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_additional_par
271277
CURLcode res = send_file_to_webserver(false, true);
272278
LT_ASSERT_EQ(res, 0);
273279

280+
ws->stop();
281+
delete ws;
282+
274283
string actual_content = resource.get_content();
275284
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
276285
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
@@ -301,10 +310,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_additional_par
301310
httpserver::http::http_utils::upload_filename_template;
302311
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
303312
expected_filename.substr(0, expected_filename.size() - 6));
304-
unlink(file->second.get_file_system_file_name().c_str());
305-
306-
ws->stop();
307-
delete ws;
313+
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
308314
LT_END_AUTO_TEST(file_upload_memory_and_disk_additional_params)
309315

310316
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files)
@@ -325,6 +331,9 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files)
325331
CURLcode res = send_file_to_webserver(true, false);
326332
LT_ASSERT_EQ(res, 0);
327333

334+
ws->stop();
335+
delete ws;
336+
328337
string actual_content = resource.get_content();
329338
LT_CHECK_EQ(actual_content.find(FILENAME_IN_GET_CONTENT) != string::npos, true);
330339
LT_CHECK_EQ(actual_content.find(TEST_CONTENT) != string::npos, true);
@@ -355,7 +364,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files)
355364
httpserver::http::http_utils::upload_filename_template;
356365
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
357366
expected_filename.substr(0, expected_filename.size() - 6));
358-
unlink(file->second.get_file_system_file_name().c_str());
367+
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
359368

360369
file_key++;
361370
LT_CHECK_EQ(file_key->first, TEST_KEY_2);
@@ -370,11 +379,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files)
370379
httpserver::http::http_utils::upload_filename_template;
371380
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
372381
expected_filename.substr(0, expected_filename.size() - 6));
373-
unlink(file->second.get_file_system_file_name().c_str());
374-
375-
376-
ws->stop();
377-
delete ws;
382+
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
378383
LT_END_AUTO_TEST(file_upload_memory_and_disk_two_files)
379384

380385
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only)
@@ -395,6 +400,9 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only)
395400
CURLcode res = send_file_to_webserver(false, false);
396401
LT_ASSERT_EQ(res, 0);
397402

403+
ws->stop();
404+
delete ws;
405+
398406
string actual_content = resource.get_content();
399407
LT_CHECK_EQ(actual_content.size(), 0);
400408

@@ -416,10 +424,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only)
416424
httpserver::http::http_utils::upload_filename_template;
417425
LT_CHECK_EQ(file->second.get_file_system_file_name().substr(0, file->second.get_file_system_file_name().size() - 6),
418426
expected_filename.substr(0, expected_filename.size() - 6));
419-
unlink(file->second.get_file_system_file_name().c_str());
420-
421-
ws->stop();
422-
delete ws;
427+
LT_CHECK_EQ(file_exists(file->second.get_file_system_file_name()), false);
423428
LT_END_AUTO_TEST(file_upload_disk_only)
424429

425430
LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_incl_content)

0 commit comments

Comments
 (0)