Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5f22dfd
Add options to store files directly to the file system and to omit du…
ctomahogh Feb 8, 2022
916610a
Delete accidentially added newline in http_resource.hpp
ctomahogh Feb 9, 2022
d9adfcb
Removed unused function, fixed some code style and typos in comments
ctomahogh Feb 10, 2022
220fdd8
Removed asprintf in favor or string concatenation
ctomahogh Feb 10, 2022
52cb06f
Made http_utils::generate_random_upload_filename plattform independant
ctomahogh Feb 10, 2022
71d1f20
Input and output of http_utils::generate_random_upload_filename are c…
ctomahogh Feb 10, 2022
1f37cfa
Added options and functions for file upload to README.md
ctomahogh Feb 10, 2022
fac086a
Use correct include and functions in generate_random_upload_filename …
ctomahogh Feb 10, 2022
5843f5f
Add missing includes for windows, fixed wrong argument for _mktemp_s
ctomahogh Feb 10, 2022
bc5a4fe
Removed accidentially pushed debug code
ctomahogh Feb 11, 2022
8d7ea8e
Use a static const char* for the random file system template
ctomahogh Feb 11, 2022
329c066
Make output of get_or_create_file_info const and introduce new setters
ctomahogh Feb 11, 2022
07e9f4d
Use const variables for the webserver options
ctomahogh Feb 11, 2022
29ba743
Added an example for file upload
ctomahogh Feb 11, 2022
2606d61
Use a prefixed operator in for loop
ctomahogh Feb 11, 2022
8ab2972
Added safety check for use of strlen and some missing free()s
ctomahogh Feb 14, 2022
3880d8d
Changed file map to be a map of keys with a nested map of files
ctomahogh Feb 14, 2022
2e99b1d
Adjusted file_upload example to use new files map
ctomahogh Feb 14, 2022
fd7bab9
Updated description of files map in README.md
ctomahogh Feb 14, 2022
ccc67e8
Removed strlen from generate_random_upload_filename
ctomahogh Feb 14, 2022
799c201
Use a pointer to std::ofstream in modded_request
ctomahogh Feb 15, 2022
f329061
Moved struct file_info_s to class file_info
ctomahogh Feb 16, 2022
8b5c172
Setters of class file_info are private and class webserver is a frien…
ctomahogh Feb 17, 2022
cb802d1
Use const reference as parameter for set_file_system_file_name
ctomahogh Feb 17, 2022
c6151c4
Don't create zero length files if no file is uploaded
ctomahogh Feb 17, 2022
4cb49c8
Updated README.md as file_info is now a class
ctomahogh Feb 17, 2022
7f2c353
Some code styling and small refactorings after review
ctomahogh Feb 18, 2022
27fc9a8
Replaced std::exception with custom generateFilenameException
ctomahogh Feb 18, 2022
1cf683e
Some more comments and code style fixes after review
ctomahogh Feb 18, 2022
88d14aa
Some more code style changes after review
ctomahogh Feb 18, 2022
b3a39dd
Added error string to generateFilenameException
ctomahogh Feb 18, 2022
cbca260
Added comment for c functions in generate_random_upload_filename
ctomahogh Feb 18, 2022
f896540
Removed const qualifier from file_info::get_file_size()
ctomahogh Feb 20, 2022
897eab9
Merge branch 'etr:master' into upload_to_file_system
ctomahogh Feb 21, 2022
7b73c48
Added unit test for generate_random_upload_filename
ctomahogh Feb 21, 2022
4f2cecf
Removed filesystem include from unit tests again, as it would require…
ctomahogh Feb 21, 2022
c1878f5
Close open upload file after post processor is done
ctomahogh Feb 22, 2022
f189014
Added content_type and transfer_encoding to file_info
ctomahogh Feb 22, 2022
8e16406
Fixed file_upload example
ctomahogh Feb 22, 2022
4bcc846
Made filename_template and path_separator part of class http_utils
ctomahogh Feb 22, 2022
f4fb34f
Added integration test for file upload
ctomahogh Feb 25, 2022
0e90742
Fixed integration test for file upload
ctomahogh Feb 25, 2022
ee91ea9
Merge branch 'etr:master' into upload_to_file_system
ctomahogh Feb 26, 2022
ac91c63
Removed empty AUTO_TEST from file_upload integration test
ctomahogh Feb 26, 2022
f13d1fe
Use internal values for http_ressource class in integration tests
ctomahogh Mar 2, 2022
91b90e5
Added further integration tests for file upload
ctomahogh Mar 3, 2022
4de82c9
Added test_content_2 to configure script
ctomahogh Mar 3, 2022
acebdfa
Fixed memory leak in post_iterator
ctomahogh Mar 3, 2022
6fff5f0
Some code style fixed on file upload integration test
ctomahogh Mar 3, 2022
dbba47c
Use map definition instead of auto in integration test
ctomahogh Mar 3, 2022
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
Prev Previous commit
Next Next commit
Moved struct file_info_s to class file_info
  • Loading branch information
ctomahogh committed Feb 16, 2022
commit f329061531b80af37c515da85d7f0781a2e9dec1
4 changes: 2 additions & 2 deletions examples/file_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class file_upload_resource : public httpserver::http_resource {
post_response += "</td><td>";
post_response += files.first;
post_response += "</td><td>";
post_response += files.second.file_system_file_name;
post_response += files.second.get_file_system_file_name();
post_response += "</td><td>";
post_response += std::to_string(files.second.file_size);
post_response += std::to_string(files.second.get_file_size());
post_response += "</td></tr>\n";
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/
METASOURCES = AUTO
lib_LTLIBRARIES = libhttpserver.la
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp gettext.h
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp

AM_CXXFLAGS += -fPIC -Wall

Expand Down
43 changes: 43 additions & 0 deletions src/file_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 <string.h>
#include "httpserver/file_info.hpp"

namespace httpserver {
namespace http {

void file_info::set_file_system_file_name(const std::string file_system_file_name) {
_file_system_file_name = file_system_file_name;
}

void file_info::grow_file_size(size_t additional_file_size) {
_file_size += additional_file_size;
}
size_t file_info::get_file_size() const {
return _file_size;
}
const std::string file_info::get_file_system_file_name() const {
return _file_system_file_name;
}


} // namespace http
} // namespace httpserver
2 changes: 1 addition & 1 deletion src/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const std::map<std::string, std::string, http::arg_comparator> http_request::get
return arguments;
}

const file_info_s& http_request::get_or_create_file_info(const std::string& key, const std::string& upload_file_name) {
http::file_info& http_request::get_or_create_file_info(const std::string& key, const std::string& upload_file_name) {
return files[key][upload_file_name];
}

Expand Down
1 change: 1 addition & 0 deletions src/httpserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "httpserver/http_resource.hpp"
#include "httpserver/http_response.hpp"
#include "httpserver/http_utils.hpp"
#include "httpserver/file_info.hpp"
#include "httpserver/string_response.hpp"
#include "httpserver/webserver.hpp"

Expand Down
49 changes: 49 additions & 0 deletions src/httpserver/file_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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
*/

#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
#error "Only <httpserver.hpp> or <httpserverpp> can be included directly."
#endif

#ifndef SRC_HTTPSERVER_FILE_INFO_HPP_
#define SRC_HTTPSERVER_FILE_INFO_HPP_

#include <string>

namespace httpserver {

namespace http {

class file_info {
public:
void set_file_system_file_name(const std::string file_system_file_name);
void grow_file_size(size_t additional_file_size);
size_t get_file_size() const;
Comment thread
etr marked this conversation as resolved.
const std::string get_file_system_file_name() const;

private:
size_t _file_size;
std::string _file_system_file_name;
};

} // namespace http
} // namespace httpserver
#endif // SRC_HTTPSERVER_FILE_INFO_HPP_

25 changes: 4 additions & 21 deletions src/httpserver/http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <vector>

#include "httpserver/http_utils.hpp"
#include "httpserver/file_info.hpp"

struct MHD_Connection;

Expand Down Expand Up @@ -141,13 +142,13 @@ class http_request {
* @param upload_file_name the file name the user uploaded (this is the identifier for the map entry)
* @result a file info struct file_info_s
Comment thread
ctomahogh marked this conversation as resolved.
**/
const file_info_s& get_or_create_file_info(const std::string &key, const std::string& upload_file_name);
http::file_info& get_or_create_file_info(const std::string &key, const std::string& upload_file_name);
Copy link
Copy Markdown
Owner

@etr etr Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make the output const?

nit: also move the & close to the string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I see it, the output cannot be const, as in the using function (post_iterator()) the setters of the file_info class are called on the returned file_info.


/**
* Method used to get all files passed with the request.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

* @result result a map<string, file_info_s> > that will be filled with all files
**/
const std::map<std::string, std::map<std::string, file_info_s>> get_files() const {
const std::map<std::string, std::map<std::string, http::file_info>> get_files() const {
return files;
}

Expand Down Expand Up @@ -256,7 +257,7 @@ class http_request {
std::string path;
std::string method;
std::map<std::string, std::string, http::arg_comparator> args;
std::map<std::string, std::map<std::string, file_info_s>> files;
std::map<std::string, std::map<std::string, http::file_info>> files;
std::string content = "";
size_t content_size_limit = static_cast<size_t>(-1);
std::string version;
Expand Down Expand Up @@ -318,24 +319,6 @@ class http_request {
}
}

/**
* Method used to set the file system file name into the map by key.
* @param upload_file_name The file name the user uploaded (identifying the map entry)
* @param file_system_file_name The path to the file in the file system (the name may not be the original name depending on configuration of the webserver)
**/
void set_file_system_file_name(const std::string& key, const std::string& upload_file_name, const std::string& file_system_file_name) {
files[key][upload_file_name].file_system_file_name = file_system_file_name;
}

/**
* Method used grow the filesize of an entry in the map by key.
* @param upload_file_name The file name the user uploaded (identifying the map entry)
* @param additional_file_size The additional size as a number to be added to the entry.
**/
void grow_file_size(const std::string& key, const std::string& upload_file_name, size_t additional_file_size) {
files[key][upload_file_name].file_size += additional_file_size;
}

/**
* Method used to set the path requested.
* @param path The path searched by the request.
Expand Down
5 changes: 0 additions & 5 deletions src/httpserver/http_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ enum file_upload_target_T {
FILE_UPLOAD_MEMORY_AND_DISK,
};
Comment thread
ctomahogh marked this conversation as resolved.

struct file_info_s {
size_t file_size;
std::string file_system_file_name;
};

typedef void(*unescaper_ptr)(std::string&);

namespace http {
Expand Down
14 changes: 7 additions & 7 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,17 @@ MHD_Result webserver::post_iterator(void *cls, enum MHD_ValueKind kind,

if (filename && mr->ws->file_upload_target != FILE_UPLOAD_MEMORY_ONLY) {
// either get the existing file info struct or create a new one in the file map
const file_info_s &file_info = mr->dhr->get_or_create_file_info(key, filename);
http::file_info &file = mr->dhr->get_or_create_file_info(key, filename);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move & close to file_info

// if the file_system_file_name is not filled yet, this is a new entry and the name has to be set
// (either random or copy of the original filename)
if (file_info.file_system_file_name.empty()) {
if (file.get_file_system_file_name().empty()) {
if (mr->ws->generate_random_filename_on_upload) {
mr->dhr->set_file_system_file_name(key, filename, http_utils::generate_random_upload_filename(mr->ws->post_upload_dir));
file.set_file_system_file_name(http_utils::generate_random_upload_filename(mr->ws->post_upload_dir));
} else {
mr->dhr->set_file_system_file_name(key, filename, mr->ws->post_upload_dir + "/" + std::string(filename));
file.set_file_system_file_name(mr->ws->post_upload_dir + "/" + std::string(filename));
}
// to not append to an already existing file, delete an already existing file
unlink(file_info.file_system_file_name.c_str());
unlink(file.get_file_system_file_name().c_str());
}

// if multiple files are uploaded, a different filename indicates the start of a new file, so close the previous one
Expand All @@ -495,15 +495,15 @@ MHD_Result webserver::post_iterator(void *cls, enum MHD_ValueKind kind,
mr->upload_key = key;
mr->upload_filename = filename;
mr->upload_ostrm = new std::ofstream();
mr->upload_ostrm->open(file_info.file_system_file_name, std::ios::binary | std::ios::app);
mr->upload_ostrm->open(file.get_file_system_file_name(), std::ios::binary | std::ios::app);
}

if (size > 0) {
mr->upload_ostrm->write(data, size);
}

// update the file size in the map
mr->dhr->grow_file_size(key, filename, size);
file.grow_file_size(size);
}
return MHD_YES;
} catch(const std::exception& e) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will fix itself when we use the custom exception

Expand Down