Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
MultipartStreamParser.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <string_view>
4#include <functional>
5#include <fstream>
6#include <filesystem>
7
8namespace http {
9
14public:
15 using OnFieldCallback = std::function<void(const std::string& name, const std::string& value)>;
16 using OnFileCallback = std::function<void(const std::string& name, const std::string& filename, const std::string& content_type, const std::string& tmp_filepath)>;
17
24 MultipartStreamParser(const std::string& boundary, OnFieldCallback on_field, OnFileCallback on_file);
26
31 void feed(std::string_view chunk);
32
36 void end();
37
38private:
39 std::string boundary_;
40 std::string dash_boundary_;
41 OnFieldCallback on_field_;
42 OnFileCallback on_file_;
43
44 enum class State {
45 FINDING_BOUNDARY,
46 READING_HEADERS,
47 READING_BODY
48 };
49
50 State state_ = State::FINDING_BOUNDARY;
51 std::string buffer_;
52
53 std::string current_name_;
54 std::string current_filename_;
55 std::string current_content_type_;
56 std::string current_field_value_;
57 std::ofstream current_file_;
58 std::string current_tmp_filepath_;
59
60 void process_buffer();
61 void parse_headers(std::string_view headers);
62 void open_temp_file();
63 void close_current_part();
64};
65
66} // namespace http
Streaming parser for multipart/form-data.
Definition MultipartStreamParser.hpp:13
void feed(std::string_view chunk)
Feeds a chunk of data into the parser.
Definition MultipartStreamParser.cpp:16
std::function< void(const std::string &name, const std::string &value)> OnFieldCallback
Definition MultipartStreamParser.hpp:15
void end()
Signals the end of the input stream.
Definition MultipartStreamParser.cpp:21
std::function< void(const std::string &name, const std::string &filename, const std::string &content_type, const std::string &tmp_filepath)> OnFileCallback
Definition MultipartStreamParser.hpp:16
~MultipartStreamParser()
Definition MultipartStreamParser.cpp:12
Definition Http2Session.hpp:21