Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/httpserver/create_webserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ class create_webserver {
return *this;
}

create_webserver& post_upload_dir(const std::string& post_upload_dir) {
_post_upload_dir = post_upload_dir;
return *this;
}

create_webserver& single_resource() {
_single_resource = true;
return *this;
Expand Down Expand Up @@ -360,6 +365,7 @@ class create_webserver {
bool _regex_checking = true;
bool _ban_system_enabled = true;
bool _post_process_enabled = true;
std::string _post_upload_dir = "";
bool _deferred_enabled = false;
bool _single_resource = false;
bool _tcp_nodelay = false;
Expand Down
4 changes: 4 additions & 0 deletions src/httpserver/details/modded_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef SRC_HTTPSERVER_DETAILS_MODDED_REQUEST_HPP_
#define SRC_HTTPSERVER_DETAILS_MODDED_REQUEST_HPP_

#include <fstream>
#include <string>
#include <memory>

Expand All @@ -47,6 +48,9 @@ struct modded_request {
bool second = false;
bool has_body = false;

std::string fname;
std::ofstream ostrm;

modded_request() = default;

modded_request(const modded_request& b) = default;
Expand Down
1 change: 1 addition & 0 deletions src/httpserver/webserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class webserver {
const bool regex_checking;
const bool ban_system_enabled;
const bool post_process_enabled;
const std::string post_upload_dir;
const bool deferred_enabled;
bool single_resource;
bool tcp_nodelay;
Expand Down
32 changes: 27 additions & 5 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ webserver::webserver(const create_webserver& params):
regex_checking(params._regex_checking),
ban_system_enabled(params._ban_system_enabled),
post_process_enabled(params._post_process_enabled),
post_upload_dir(params._post_upload_dir),
deferred_enabled(params._deferred_enabled),
single_resource(params._single_resource),
tcp_nodelay(params._tcp_nodelay),
Expand Down Expand Up @@ -454,13 +455,32 @@ MHD_Result webserver::post_iterator(void *cls, enum MHD_ValueKind kind,
const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
// Parameter needed to respect MHD interface, but not needed here.
std::ignore = kind;
std::ignore = filename;
std::ignore = content_type;
std::ignore = transfer_encoding;
std::ignore = off;

struct details::modded_request* mr = (struct details::modded_request*) cls;
mr->dhr->set_arg(key, mr->dhr->get_arg(key) + std::string(data, size));

if (filename == nullptr) {
mr->dhr->set_arg(key, mr->dhr->get_arg(key) + std::string(data, size));
} else {
// TODO(LeSpocky): basename only.
mr->dhr->set_arg(key, filename);

// (re)open file
if (!mr->fname.empty() && 0 != strcmp(filename, mr->fname.c_str())) {
mr->ostrm.close();
}

if (!mr->ostrm.is_open()) {
mr->ostrm.open(filename, std::ios::binary | std::ios::app);
mr->fname = filename;
}

// append data to file
if (size > 0) mr->ostrm.write(data, size);
}

return MHD_YES;
}

Expand Down Expand Up @@ -527,9 +547,11 @@ MHD_Result webserver::requests_answer_second_step(MHD_Connection* connection, co
#ifdef DEBUG
std::cout << "Writing content: " << std::string(upload_data, *upload_data_size) << std::endl;
#endif // DEBUG
mr->dhr->grow_content(upload_data, *upload_data_size);

if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
if (mr->pp != NULL) {
MHD_post_process(mr->pp, upload_data, *upload_data_size);
} else {
mr->dhr->grow_content(upload_data, *upload_data_size);
}
}

*upload_data_size = 0;
Expand Down