Description
Is your feature request related to a problem? Please describe.
Since 3fd99c8 it's possible to let libhttpserver store files uploaded with forms (mime-type multipart/form-data) to disk, see the pull request #257 for reference. This whole new feature request is about cases where file upload target is set to FILE_UPLOAD_DISK_ONLY
or FILE_UPLOAD_MEMORY_AND_DISK
.
In certain circumstances it is necessary for an application to not store those files permanently, but remove them from disk again, after processing. Doing that in a render method associated with a registered resource is possible. However the file is always stored to disk, regardless if that render method is called at all. Multiple code paths in webserver::finalize_answer()
, mostly error cases, are possible leading to mr->dhrs
assigned by something else than the render callback supplied by the application. Simplest case is sending a (possibly forged) request to a not registered resource (HTTP status 404), but others are possible, too. That would mean the cleanup in your render method is not executed.
The actual target we use libhttpserver on is an embedded device with usable RAM smaller than files uploaded, so we have to store those files on disk. Those files can and should be removed again on every request, otherwise the filesystem would fill up quickly. This is especially true for manipulated requests with bad intent. A full filesystem can be considered as a denial of service situation.
Describe why the feature or enhancement you are proposing fits the library.
I propose some extension to libhttpserver for cleaning up all uploaded files of a request at the end of request processing. Thinking of the possibility of optionally hooking up a callback function provided by the application, where user can decide how to cleanup, and call that in an appropriate place.
Describe the solution you'd like
Extend the create_webserver
options by a function to set a custom cleanup function. That function would not be called if not set by the application. However if set it could be executed next to MHD_destroy_response(raw_response);
at the end of webserver::finalize_answer()
?
Describe alternatives you've considered
With the current API of libhttpserver always cleaning up would only be possible by providing custom methods for all of these:
- internal_error_page
- method_not_allowed_page
- not_found_page
And you had to put that same cleanup code into every render method in your application, no matter if it is supposed to handle file uploads at all.
This would lead to lots of duplicated code with high potential to miss some path.
Additional context
Same problem applies to the draft #246 not followed (for good reasons) in favor of #257.