Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
MultipartStreamParser.hpp
Go to the documentation of this file.
1#pragma once
3#include <cstddef>
4#include <string>
5#include <string_view>
6#include <functional>
7
8namespace orbit::http {
9
15 size_t max_file_size = 100 * 1024 * 1024;
16 size_t max_field_size = 1024 * 1024;
17 size_t max_total_size = 1024ull * 1024 * 1024;
18 size_t max_parts = 1000;
21 std::string upload_dir;
22};
23
31std::string multipart_boundary(std::string_view content_type);
32
43public:
44 using OnFieldCallback = std::function<void(const std::string& name, const std::string& value)>;
46 using OnFileCallback = std::function<void(const std::string& name, const std::string& filename, const std::string& content_type, const std::string& tmp_filepath)>;
47
55 MultipartStreamParser(const std::string& boundary, OnFieldCallback on_field, OnFileCallback on_file,
56 MultipartLimits limits = {});
58
61
67 bool feed(std::string_view chunk);
68
72 void end();
73
75 bool failed() const { return failed_; }
76 const std::string& error() const { return error_; }
79 bool complete() const { return finished_ && !failed_; }
80
81private:
82 std::string boundary_;
83 std::string dash_boundary_;
87
88 enum class State {
92 };
93
95 std::string buffer_;
96
97 std::string current_name_;
98 std::string current_filename_;
101 int current_fd_ = -1;
102 size_t current_size_ = 0;
104 size_t total_size_ = 0;
105 size_t parts_ = 0;
106 bool failed_ = false;
107 bool finished_ = false;
108 std::string error_;
109
111 void parse_headers(std::string_view headers);
113 bool append_body(std::string_view data);
116 void fail(const std::string& reason);
117};
118
119} // namespace http
Streaming parser for multipart/form-data.
Definition MultipartStreamParser.hpp:42
State state_
Definition MultipartStreamParser.hpp:94
std::string current_filename_
Definition MultipartStreamParser.hpp:98
size_t total_size_
Definition MultipartStreamParser.hpp:104
bool failed_
Definition MultipartStreamParser.hpp:106
const std::string & error() const
Definition MultipartStreamParser.hpp:76
MultipartLimits limits_
Definition MultipartStreamParser.hpp:86
std::string buffer_
Definition MultipartStreamParser.hpp:95
OnFieldCallback on_field_
Definition MultipartStreamParser.hpp:84
std::string current_name_
Definition MultipartStreamParser.hpp:97
void parse_headers(std::string_view headers)
bool complete() const
Definition MultipartStreamParser.hpp:79
std::string current_content_type_
Definition MultipartStreamParser.hpp:99
std::string dash_boundary_
Definition MultipartStreamParser.hpp:83
State
Definition MultipartStreamParser.hpp:88
void fail(const std::string &reason)
std::string current_field_value_
Definition MultipartStreamParser.hpp:100
std::function< void(const std::string &name, const std::string &value)> OnFieldCallback
Definition MultipartStreamParser.hpp:44
size_t current_size_
Definition MultipartStreamParser.hpp:102
bool finished_
Definition MultipartStreamParser.hpp:107
std::string current_tmp_filepath_
Definition MultipartStreamParser.hpp:103
bool feed(std::string_view chunk)
Feeds a chunk of data into the parser.
MultipartStreamParser(const std::string &boundary, OnFieldCallback on_field, OnFileCallback on_file, MultipartLimits limits={})
Constructs a new MultipartStreamParser.
size_t parts_
Definition MultipartStreamParser.hpp:105
bool failed() const
True if a limit was exceeded or a file could not be written.
Definition MultipartStreamParser.hpp:75
int current_fd_
Definition MultipartStreamParser.hpp:101
std::string error_
Definition MultipartStreamParser.hpp:108
std::string boundary_
Definition MultipartStreamParser.hpp:82
std::function< void(const std::string &name, const std::string &filename, const std::string &content_type, const std::string &tmp_filepath)> OnFileCallback
filename is chosen by the client: never use it as a path.
Definition MultipartStreamParser.hpp:46
MultipartStreamParser & operator=(const MultipartStreamParser &)=delete
OnFileCallback on_file_
Definition MultipartStreamParser.hpp:85
void end()
Signals the end of the input stream.
bool append_body(std::string_view data)
MultipartStreamParser(const MultipartStreamParser &)=delete
Definition Task.hpp:10
std::string multipart_boundary(std::string_view content_type)
Extracts the boundary parameter from a multipart Content-Type value, e.g. multipart/form-data; bounda...
Limits for MultipartStreamParser. Exceeding one fails the parse (see MultipartStreamParser::failed())...
Definition MultipartStreamParser.hpp:14
size_t max_file_size
Per uploaded file.
Definition MultipartStreamParser.hpp:15
size_t max_field_size
Per non-file field (kept in memory)
Definition MultipartStreamParser.hpp:16
std::string upload_dir
Definition MultipartStreamParser.hpp:21
size_t max_parts
Definition MultipartStreamParser.hpp:18
size_t max_total_size
Whole body.
Definition MultipartStreamParser.hpp:17