Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::http::MultipartStreamParser Class Reference

Streaming parser for multipart/form-data. More...

#include <MultipartStreamParser.hpp>

Collaboration diagram for orbit::http::MultipartStreamParser:

Public Types

using OnFieldCallback = std::function< void(const std::string &name, const std::string &value)>
 
using OnFileCallback = std::function< void(const std::string &name, const std::string &filename, const std::string &content_type, const std::string &tmp_filepath)>
 filename is chosen by the client: never use it as a path.
 

Public Member Functions

 MultipartStreamParser (const std::string &boundary, OnFieldCallback on_field, OnFileCallback on_file, MultipartLimits limits={})
 Constructs a new MultipartStreamParser.
 
 ~MultipartStreamParser ()
 
 MultipartStreamParser (const MultipartStreamParser &)=delete
 
MultipartStreamParser & operator= (const MultipartStreamParser &)=delete
 
bool feed (std::string_view chunk)
 Feeds a chunk of data into the parser.
 
void end ()
 Signals the end of the input stream.
 
bool failed () const
 True if a limit was exceeded or a file could not be written.
 
const std::string & error () const
 
bool complete () const
 

Private Types

enum class  State { FINDING_BOUNDARY , READING_HEADERS , READING_BODY }
 

Private Member Functions

void process_buffer ()
 
void parse_headers (std::string_view headers)
 
bool open_temp_file ()
 
bool append_body (std::string_view data)
 
void close_current_part ()
 
void discard_current_part ()
 
void fail (const std::string &reason)
 

Private Attributes

std::string boundary_
 
std::string dash_boundary_
 
OnFieldCallback on_field_
 
OnFileCallback on_file_
 
MultipartLimits limits_
 
State state_ = State::FINDING_BOUNDARY
 
std::string buffer_
 
std::string current_name_
 
std::string current_filename_
 
std::string current_content_type_
 
std::string current_field_value_
 
int current_fd_ = -1
 
size_t current_size_ = 0
 
std::string current_tmp_filepath_
 
size_t total_size_ = 0
 
size_t parts_ = 0
 
bool failed_ = false
 
bool finished_ = false
 
std::string error_
 

Detailed Description

Streaming parser for multipart/form-data.

Files are written to unpredictable names, created exclusively with owner-only permissions. A file passed to the OnFileCallback belongs to the application, which should move or delete it; files of parts that did not complete (malformed input, a limit, or the parser being destroyed) are deleted by the parser and never reported.

Member Typedef Documentation

◆ OnFieldCallback

using orbit::http::MultipartStreamParser::OnFieldCallback = std::function<void(const std::string& name, const std::string& value)>

◆ OnFileCallback

using orbit::http::MultipartStreamParser::OnFileCallback = std::function<void(const std::string& name, const std::string& filename, const std::string& content_type, const std::string& tmp_filepath)>

filename is chosen by the client: never use it as a path.

Member Enumeration Documentation

◆ State

Enumerator
FINDING_BOUNDARY 
READING_HEADERS 
READING_BODY 

Constructor & Destructor Documentation

◆ MultipartStreamParser() [1/2]

orbit::http::MultipartStreamParser::MultipartStreamParser ( const std::string &  boundary,
OnFieldCallback  on_field,
OnFileCallback  on_file,
MultipartLimits  limits = {} 
)

Constructs a new MultipartStreamParser.

Parameters
boundaryThe boundary string from the Content-Type header.
on_fieldCallback invoked when a standard form field is parsed.
on_fileCallback invoked when a file is parsed and saved to a temporary location.
limitsSize limits and upload directory.

◆ ~MultipartStreamParser()

orbit::http::MultipartStreamParser::~MultipartStreamParser ( )

◆ MultipartStreamParser() [2/2]

orbit::http::MultipartStreamParser::MultipartStreamParser ( const MultipartStreamParser &  )
delete

Member Function Documentation

◆ append_body()

bool orbit::http::MultipartStreamParser::append_body ( std::string_view  data)
private

◆ close_current_part()

void orbit::http::MultipartStreamParser::close_current_part ( )
private

◆ complete()

bool orbit::http::MultipartStreamParser::complete ( ) const
inline

True once the closing boundary has been seen without a failure. A body that ended before it (a truncated upload) is not complete.

◆ discard_current_part()

void orbit::http::MultipartStreamParser::discard_current_part ( )
private

◆ end()

void orbit::http::MultipartStreamParser::end ( )

Signals the end of the input stream.

◆ error()

const std::string & orbit::http::MultipartStreamParser::error ( ) const
inline

◆ fail()

void orbit::http::MultipartStreamParser::fail ( const std::string &  reason)
private

◆ failed()

bool orbit::http::MultipartStreamParser::failed ( ) const
inline

True if a limit was exceeded or a file could not be written.

◆ feed()

bool orbit::http::MultipartStreamParser::feed ( std::string_view  chunk)

Feeds a chunk of data into the parser.

Parameters
chunkThe data chunk to parse.
Returns
false once the parse has failed; later chunks are ignored.

◆ open_temp_file()

bool orbit::http::MultipartStreamParser::open_temp_file ( )
private

◆ operator=()

MultipartStreamParser & orbit::http::MultipartStreamParser::operator= ( const MultipartStreamParser &  )
delete

◆ parse_headers()

void orbit::http::MultipartStreamParser::parse_headers ( std::string_view  headers)
private

◆ process_buffer()

void orbit::http::MultipartStreamParser::process_buffer ( )
private

Member Data Documentation

◆ boundary_

std::string orbit::http::MultipartStreamParser::boundary_
private

◆ buffer_

std::string orbit::http::MultipartStreamParser::buffer_
private

◆ current_content_type_

std::string orbit::http::MultipartStreamParser::current_content_type_
private

◆ current_fd_

int orbit::http::MultipartStreamParser::current_fd_ = -1
private

◆ current_field_value_

std::string orbit::http::MultipartStreamParser::current_field_value_
private

◆ current_filename_

std::string orbit::http::MultipartStreamParser::current_filename_
private

◆ current_name_

std::string orbit::http::MultipartStreamParser::current_name_
private

◆ current_size_

size_t orbit::http::MultipartStreamParser::current_size_ = 0
private

◆ current_tmp_filepath_

std::string orbit::http::MultipartStreamParser::current_tmp_filepath_
private

◆ dash_boundary_

std::string orbit::http::MultipartStreamParser::dash_boundary_
private

◆ error_

std::string orbit::http::MultipartStreamParser::error_
private

◆ failed_

bool orbit::http::MultipartStreamParser::failed_ = false
private

◆ finished_

bool orbit::http::MultipartStreamParser::finished_ = false
private

◆ limits_

MultipartLimits orbit::http::MultipartStreamParser::limits_
private

◆ on_field_

OnFieldCallback orbit::http::MultipartStreamParser::on_field_
private

◆ on_file_

OnFileCallback orbit::http::MultipartStreamParser::on_file_
private

◆ parts_

size_t orbit::http::MultipartStreamParser::parts_ = 0
private

◆ state_

State orbit::http::MultipartStreamParser::state_ = State::FINDING_BOUNDARY
private

◆ total_size_

size_t orbit::http::MultipartStreamParser::total_size_ = 0
private

The documentation for this class was generated from the following file: