AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::rest::Multipart Struct Reference

#include <multipartCodec.hpp>

Collaboration diagram for anch::rest::Multipart:

Static Public Member Functions

static std::string_view mediaType () noexcept
static anch::rest::FormLimits & limits (anch::codec::Side side)
static anch::codec::MediaType contentType (const anch::rest::Form &form)
template<anch::rest::FormMapped T>
static anch::codec::MediaType contentType (const T &value)
static std::string randomBoundary ()
static void serialize (const anch::rest::Form &form, std::ostream &out, const anch::codec::MediaType &contentType, anch::codec::Side side)
static anch::rest::BodyStream stream (const anch::rest::Form &form, const anch::codec::MediaType &contentType, anch::codec::Side side)
static void deserialize (anch::rest::Form &form, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)
template<anch::rest::FormMapped T>
static void serialize (const T &value, std::ostream &out, const anch::codec::MediaType &contentType, anch::codec::Side side)
template<anch::rest::FormMapped T>
static anch::rest::BodyStream stream (const T &value, const anch::codec::MediaType &contentType, anch::codec::Side side)
template<anch::rest::FormMapped T>
static void deserialize (T &value, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)

Detailed Description

multipart/form-data codec for Form (RFC 7578), parts in memory.

Writing: a random boundary is chosen by contentType (it does not appear in parts), names and file names are escaped as browsers do (double quote, CR and LF are percent-encoded), Content-Type is written for files and for fields which are not text/plain , other part headers are written as they are (CR and LF are refused).

Parts larger than FormLimits::maxMemoryPartSize are read in temporary files (deleted with their last Part copy) and file parts are written from their file: large bodies do not need memory. The client sends bodies with stream .

Reading: boundary parameter is required, delimiters end with CRLF, preamble and epilogue are ignored, close delimiter is required. Each part needs a Content-Disposition: form-data header with a name ; filename* (RFC 5987, UTF-8) has priority over filename . Part Content-Type is text/plain by default, application/octet-stream for files. Other part headers are kept ( Content-Transfer-Encoding is not applied). Malformed bodies raise std::invalid_argument ( 400 on server side), bodies over limits raise anch::codec::ContentTooLarge ( 413 ).

Types with a FormMapper are read and written through a Form .

api.post("/pets/{id}/photo", [](uint64_t id, anch::rest::Body<anch::rest::Form> form) {
const anch::rest::Part* photo = form->part("photo"); // file name, media type, content
return savePhoto(id, photo);
})
.consumes<anch::rest::Multipart>();
upload.add("description", "Rex").addFile("photo", "rex.png", anch::codec::MediaType("image", "png"), content);
client.post("/pets/{id}/photo", 42).body<anch::rest::Multipart>(upload).send();
Author
Vincent Lachenal
Since
0.2

Member Function Documentation

◆ contentType() [1/2]

anch::codec::MediaType anch::rest::Multipart::contentType ( const anch::rest::Form & form)
static

Get body media type: multipart/form-data with a random boundary which does not appear in form

Parameters
formthe form
Returns
the media type
Here is the call graph for this function:

◆ contentType() [2/2]

anch::codec::MediaType anch::rest::Multipart::contentType ( const T & value)
static

Get body media type of a value mapped with its FormMapper : multipart/form-data with a random boundary (serialize refuses it if it appears in a part)

Template Parameters
Tthe value type
Parameters
valuethe value
Returns
the media type
Here is the call graph for this function:

◆ deserialize() [1/2]

void anch::rest::Multipart::deserialize ( anch::rest::Form & form,
std::istream & input,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Read form

Parameters
formthe form to fill
inputthe input stream
contentTypethe body media type (with its boundary)
sidethe side which reads form
Exceptions
std::invalid_argumentwhen body is malformed
ContentTooLargewhen body is over limits
Here is the call graph for this function:

◆ deserialize() [2/2]

void anch::rest::Multipart::deserialize ( T & value,
std::istream & input,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Read value mapped with its FormMapper

Template Parameters
Tthe value type
Parameters
valuethe value to fill
inputthe input stream
contentTypethe body media type (with its boundary)
sidethe side which reads value
Here is the call graph for this function:

◆ limits()

anch::rest::FormLimits & anch::rest::Multipart::limits ( anch::codec::Side side)
static

Get limits of a side, to change before handling requests. Default: 16 MiB body, 1000 parts, 16 MiB part, 16 KiB part headers, 1 MiB part in memory (larger parts are written in temporary files).

Parameters
sidethe side
Returns
the limits
Here is the call graph for this function:

◆ mediaType()

std::string_view anch::rest::Multipart::mediaType ( )
staticnoexcept

Media type

Returns
multipart/form-data

◆ randomBoundary()

std::string anch::rest::Multipart::randomBoundary ( )
static

Get random boundary: "anch-" and 32 alphanumeric characters

Returns
the boundary
Here is the call graph for this function:

◆ serialize() [1/2]

void anch::rest::Multipart::serialize ( const anch::rest::Form & form,
std::ostream & out,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Write form

Parameters
formthe form
outthe output stream
contentTypethe body media type (with its boundary)
sidethe side which writes form
Exceptions
std::invalid_argumentwhen boundary is missing or invalid, when it appears in a part or when a part header has CR or LF
Here is the call graph for this function:

◆ serialize() [2/2]

void anch::rest::Multipart::serialize ( const T & value,
std::ostream & out,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Write value mapped with its FormMapper

Template Parameters
Tthe value type
Parameters
valuethe value
outthe output stream
contentTypethe body media type (with its boundary)
sidethe side which writes value
Here is the call graph for this function:

◆ stream() [1/2]

anch::rest::BodyStream anch::rest::Multipart::stream ( const anch::rest::Form & form,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Get form body as a stream: parts are read when the stream is read (files are not loaded in memory), body size is known

Parameters
formthe form (copied in the stream, parts files are shared)
contentTypethe body media type (with its boundary)
sidethe side which writes form
Returns
the body stream and its size
Exceptions
std::invalid_argumentwhen boundary is missing or invalid, when it appears in a part in memory or when a part header has CR or LF
Here is the call graph for this function:

◆ stream() [2/2]

anch::rest::BodyStream anch::rest::Multipart::stream ( const T & value,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Get body of a value mapped with its FormMapper as a stream

Template Parameters
Tthe value type
Parameters
valuethe value
contentTypethe body media type (with its boundary)
sidethe side which writes value
Returns
the body stream and its size
Here is the call graph for this function:

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