Home
HTTP module

FormWriter

FormWriter is an HTTP client connection adapter for writing HTML forms.

FormWriter

#include <icy/http/form.h>
class FormWriter

Defined in src/http/include/icy/http/form.h:42

Inherits: NVCollection, PacketStreamAdapter, Startable

FormWriter is an HTTP client connection adapter for writing HTML forms.

This class runs in its own thread so as not to block the event loop while uploading big files. Class members are not synchronized hence they should not be accessed while the form is sending, not that there would be any reason to do so.

List of all members

NameKindOwner
FormPartfriendDeclared here
FilePartfriendDeclared here
StringPartfriendDeclared here
emittervariableDeclared here
~FormWriterfunctionDeclared here
addPartfunctionDeclared here
startfunctionDeclared here
stopfunctionDeclared here
completefunctionDeclared here
cancelledfunctionDeclared here
prepareSubmitfunctionDeclared here
calculateMultipartContentLengthfunctionDeclared here
writeUrlfunctionDeclared here
writeMultipartChunkfunctionDeclared here
writeAsyncfunctionDeclared here
setEncodingfunctionDeclared here
encodingfunctionDeclared here
setBoundaryfunctionDeclared here
boundaryfunctionDeclared here
connectionfunctionDeclared here
ENCODING_URLvariableDeclared here
ENCODING_MULTIPART_FORMvariableDeclared here
ENCODING_MULTIPART_RELATEDvariableDeclared here
createfunctionDeclared here
_streamvariableDeclared here
_runnervariableDeclared here
_encodingvariableDeclared here
_boundaryvariableDeclared here
_partsvariableDeclared here
_filesLengthvariableDeclared here
_writeStatevariableDeclared here
_initialvariableDeclared here
_completevariableDeclared here
FormWriterfunctionDeclared here
FormWriterfunctionDeclared here
operator=functionDeclared here
FormWriterfunctionDeclared here
operator=functionDeclared here
writePartHeaderfunctionDeclared here
writeEndfunctionDeclared here
updateProgressfunctionDeclared here
NVCollectionfunctionInherited from NVCollection
NVCollectionfunctionInherited from NVCollection
NVCollectionfunctionInherited from NVCollection
~NVCollectionfunctionInherited from NVCollection
operator=functionInherited from NVCollection
operator=functionInherited from NVCollection
operator[]functionInherited from NVCollection
setfunctionInherited from NVCollection
addfunctionInherited from NVCollection
addfunctionInherited from NVCollection
getfunctionInherited from NVCollection
getfunctionInherited from NVCollection
hasfunctionInherited from NVCollection
findfunctionInherited from NVCollection
beginfunctionInherited from NVCollection
endfunctionInherited from NVCollection
emptyfunctionInherited from NVCollection
sizefunctionInherited from NVCollection
erasefunctionInherited from NVCollection
clearfunctionInherited from NVCollection
MaptypedefInherited from NVCollection
IteratortypedefInherited from NVCollection
ConstIteratortypedefInherited from NVCollection
_mapvariableInherited from NVCollection
PacketStreamAdapterfunctionInherited from PacketStreamAdapter
~PacketStreamAdapterfunctionInherited from PacketStreamAdapter
emitfunctionInherited from PacketStreamAdapter
emitfunctionInherited from PacketStreamAdapter
emitfunctionInherited from PacketStreamAdapter
emitfunctionInherited from PacketStreamAdapter
emitfunctionInherited from PacketStreamAdapter
getEmitterfunctionInherited from PacketStreamAdapter
retentionfunctionInherited from PacketStreamAdapter
onStreamStateChangefunctionInherited from PacketStreamAdapter
_emittervariableInherited from PacketStreamAdapter
PacketStreamAdapterfunctionInherited from PacketStreamAdapter
operator=functionInherited from PacketStreamAdapter
PacketStreamAdapterfunctionInherited from PacketStreamAdapter
operator=functionInherited from PacketStreamAdapter
startfunctionInherited from Startable
stopfunctionInherited from Startable

Inherited from NVCollection

KindNameDescription
functionNVCollection inline
functionNVCollection inline
functionNVCollection inline noexcept
function~NVCollection virtual inline
functionoperator=Assigns the name-value pairs of another NVCollection to this one.
functionoperator= noexcept
functionoperator[] constReturns the value of the (first) name-value pair with the given name.
functionsetSets the value of the (first) name-value pair with the given name.
functionaddAdds a new name-value pair with the given name and value.
functionaddAdds a new name-value pair using move semantics.
functionget constReturns the value of the first name-value pair with the given name.
functionget constReturns the value of the first name-value pair with the given name. If no value with the given name has been found, the defaultValue is returned.
functionhas constReturns true if there is at least one name-value pair with the given name.
functionfind constReturns an iterator pointing to the first name-value pair with the given name.
functionbegin constReturns an iterator pointing to the begin of the name-value pair collection.
functionend constReturns an iterator pointing to the end of the name-value pair collection.
functionempty constReturns true iff the header does not have any content.
functionsize constReturns the number of name-value pairs in the collection.
functioneraseRemoves all name-value pairs with the given name.
functionclearRemoves all name-value pairs and their values.
typedefMap
typedefIterator
typedefConstIterator
variable_map

Inherited from PacketStreamAdapter

KindNameDescription
functionPacketStreamAdapterConstruct the adapter, binding it to the given packet signal.
function~PacketStreamAdapter virtual inline
functionemit virtualEmit a mutable raw buffer as a packet.
functionemit virtualEmit a read-only raw buffer as a packet (data is copied internally).
functionemit virtualEmit a string as a packet (data is copied internally).
functionemit virtualEmit a flag-only packet carrying no payload data.
functionemit virtualEmit an existing packet directly onto the outgoing signal.
functiongetEmitterReturns a reference to the outgoing packet signal.
functionretention virtual constReturns how this adapter treats incoming packet lifetime. Most adapters are synchronous and therefore only borrow the packet for the current call chain. Queue-style adapters override this to advertise that they clone before deferred use. Callers may treat the first adapter reporting Cloned or Retained as the explicit ownership boundary in the stream graph.
functiononStreamStateChange virtual inlineCalled by the PacketStream to notify when the internal Stream state changes. On receiving the Stopped state, it is the responsibility of the adapter to have ceased all outgoing packet transmission, especially in multi-thread scenarios.
variable_emitter
functionPacketStreamAdapterNonCopyable and NonMovable.
functionoperator=Deleted assignment operator.
functionPacketStreamAdapterDeleted constructor.
functionoperator=Deleted assignment operator.

Inherited from Startable

KindNameDescription
functionstart virtualStarts the object (e.g. begins processing or listening).
functionstop virtualStops the object (e.g. halts processing or closes resources).

Friends


FormPart

friend class FormPart

Defined in src/http/include/icy/http/form.h:152


FilePart

friend class FilePart

Defined in src/http/include/icy/http/form.h:153


StringPart

friend class StringPart

Defined in src/http/include/icy/http/form.h:154

Public Attributes

ReturnNameDescription
PacketSignalemitterThe outgoing packet emitter.

emitter

PacketSignal emitter

Defined in src/http/include/icy/http/form.h:122

The outgoing packet emitter.

Public Methods

ReturnNameDescription
~FormWriter virtualDestroys the FormWriter.
voidaddPartAdds a part or file attachment to the multipart form.
voidstart virtualStarts the sending thread.
voidstop virtualStops the sending thread.
boolcomplete constReturns true if the request is complete.
boolcancelled constReturns true if the request is cancelled.
voidprepareSubmitPrepares the outgoing HTTP request object for submitting the form.
uint64_tcalculateMultipartContentLengthProcesses the entire form body and computes its total byte length. Only meaningful for multipart/form-data when not using chunked encoding.
voidwriteUrlWrites the complete "application/x-www-form-urlencoded" encoded body to ostr. All key-value pairs from the NVCollection base are percent-encoded and joined with '&'.
voidwriteMultipartChunkWrites the next pending multipart chunk to the connection. Non-blocking; intended to be called repeatedly from the event loop until all parts have been sent.
voidwriteAsyncWrites the next message chunk from the background runner thread. Called by the Runner; do not call directly.
voidsetEncodingSets the MIME encoding used for submitting the form. Must be set before prepareSubmit() is called.
const std::string &encoding constReturns the encoding used for posting the form.
voidsetBoundarySets the MIME boundary string used to delimit multipart form parts. If not set, a random boundary is generated by prepareSubmit(). Must be set before prepareSubmit() is called.
const std::string &boundary constReturns the MIME boundary used for writing multipart form data.
ConnectionStream &connectionThe associated HTTP client connection.

~FormWriter

virtual

virtual ~FormWriter()

Defined in src/http/include/icy/http/form.h:57

Destroys the FormWriter.


addPart

void addPart(const std::string & name, FormPart * part)

Defined in src/http/include/icy/http/form.h:65

Adds a part or file attachment to the multipart form.

The FormWriter takes ownership of part and deletes it when done. Parts are only sent when the encoding is "multipart/form-data".

Parameters

  • name Form field name for this part.

  • part Part to add. Ownership is transferred.


start

virtual

virtual void start()

Defined in src/http/include/icy/http/form.h:68

Starts the sending thread.

Reimplements

stop

virtual

virtual void stop()

Defined in src/http/include/icy/http/form.h:71

Stops the sending thread.

Reimplements

complete

const

bool complete() const

Defined in src/http/include/icy/http/form.h:74

Returns true if the request is complete.


cancelled

const

bool cancelled() const

Defined in src/http/include/icy/http/form.h:77

Returns true if the request is cancelled.


prepareSubmit

void prepareSubmit()

Defined in src/http/include/icy/http/form.h:80

Prepares the outgoing HTTP request object for submitting the form.


calculateMultipartContentLength

uint64_t calculateMultipartContentLength()

Defined in src/http/include/icy/http/form.h:85

Processes the entire form body and computes its total byte length. Only meaningful for multipart/form-data when not using chunked encoding.

Returns

Total content length in bytes.


writeUrl

void writeUrl(std::ostream & ostr)

Defined in src/http/include/icy/http/form.h:90

Writes the complete "application/x-www-form-urlencoded" encoded body to ostr. All key-value pairs from the NVCollection base are percent-encoded and joined with '&'.

Parameters

  • ostr Output stream to write to.

writeMultipartChunk

void writeMultipartChunk()

Defined in src/http/include/icy/http/form.h:95

Writes the next pending multipart chunk to the connection. Non-blocking; intended to be called repeatedly from the event loop until all parts have been sent.


writeAsync

void writeAsync()

Defined in src/http/include/icy/http/form.h:99

Writes the next message chunk from the background runner thread. Called by the Runner; do not call directly.


setEncoding

void setEncoding(const std::string & encoding)

Defined in src/http/include/icy/http/form.h:104

Sets the MIME encoding used for submitting the form. Must be set before prepareSubmit() is called.

Parameters

  • encoding MIME type: ENCODING_URL or ENCODING_MULTIPART_FORM.

encoding

const

const std::string & encoding() const

Defined in src/http/include/icy/http/form.h:107

Returns the encoding used for posting the form.


setBoundary

void setBoundary(const std::string & boundary)

Defined in src/http/include/icy/http/form.h:113

Sets the MIME boundary string used to delimit multipart form parts. If not set, a random boundary is generated by prepareSubmit(). Must be set before prepareSubmit() is called.

Parameters

  • boundary Boundary string (without leading "--").

boundary

const

const std::string & boundary() const

Defined in src/http/include/icy/http/form.h:116

Returns the MIME boundary used for writing multipart form data.


connection

ConnectionStream & connection()

Defined in src/http/include/icy/http/form.h:119

The associated HTTP client connection.

Public Static Attributes

ReturnNameDescription
const char *ENCODING_URL static"application/x-www-form-urlencoded"
const char *ENCODING_MULTIPART_FORM static"multipart/form-data"
const char *ENCODING_MULTIPART_RELATED static"multipart/related" http://tools.ietf.org/html/rfc2387

ENCODING_URL

static

const char * ENCODING_URL

Defined in src/http/include/icy/http/form.h:124

"application/x-www-form-urlencoded"


ENCODING_MULTIPART_FORM

static

const char * ENCODING_MULTIPART_FORM

Defined in src/http/include/icy/http/form.h:125

"multipart/form-data"


static

const char * ENCODING_MULTIPART_RELATED

Defined in src/http/include/icy/http/form.h:126

"multipart/related" http://tools.ietf.org/html/rfc2387

Public Static Methods

ReturnNameDescription
FormWriter *create staticCreates a FormWriter for the given connection and encoding.

create

static

static FormWriter * create(ConnectionStream & conn, const std::string & encoding = FormWriter::ENCODING_URL)

Defined in src/http/include/icy/http/form.h:54

Creates a FormWriter for the given connection and encoding.

Encoding must be either "application/x-www-form-urlencoded" (which is the default) or "multipart/form-data".

Parameters

  • conn The HTTP connection stream to write form data to.

  • encoding MIME encoding type.

Returns

Heap-allocated FormWriter. The caller owns the returned pointer.

Protected Attributes

ReturnNameDescription
ConnectionStream &_stream
std::shared_ptr< Runner >_runner
std::string_encoding
std::string_boundary
PartQueue_parts
uint64_t_filesLength
int_writeState
bool_initial
bool_complete

_stream

ConnectionStream & _stream

Defined in src/http/include/icy/http/form.h:165


_runner

std::shared_ptr< Runner > _runner

Defined in src/http/include/icy/http/form.h:166


_encoding

std::string _encoding

Defined in src/http/include/icy/http/form.h:167


_boundary

std::string _boundary

Defined in src/http/include/icy/http/form.h:168


_parts

PartQueue _parts

Defined in src/http/include/icy/http/form.h:169


_filesLength

uint64_t _filesLength

Defined in src/http/include/icy/http/form.h:170


_writeState

int _writeState

Defined in src/http/include/icy/http/form.h:171


_initial

bool _initial

Defined in src/http/include/icy/http/form.h:172


_complete

bool _complete

Defined in src/http/include/icy/http/form.h:173

Protected Methods

ReturnNameDescription
FormWriterCreates the FormWriter that uses the given encoding.
FormWriterDeleted constructor.
FormWriterDeleted constructor.
voidwritePartHeaderWrites the message boundary std::string, followed by the message header to the output stream.
voidwriteEndWrites the final boundary std::string to the output stream.
voidupdateProgress virtualUpdates the upload progress via the associated ConnectionStream object.

FormWriter

FormWriter(ConnectionStream & conn, std::shared_ptr< Runner > runner, const std::string & encoding = FormWriter::ENCODING_URL)

Defined in src/http/include/icy/http/form.h:130

Creates the FormWriter that uses the given encoding.


FormWriter

FormWriter(const FormWriter &) = delete

Defined in src/http/include/icy/http/form.h:133

Deleted constructor.


FormWriter

FormWriter(FormWriter &&) = delete

Defined in src/http/include/icy/http/form.h:135

Deleted constructor.


writePartHeader

void writePartHeader(const NVCollection & header, std::ostream & ostr)

Defined in src/http/include/icy/http/form.h:140

Writes the message boundary std::string, followed by the message header to the output stream.


writeEnd

void writeEnd(std::ostream & ostr)

Defined in src/http/include/icy/http/form.h:143

Writes the final boundary std::string to the output stream.


updateProgress

virtual

virtual void updateProgress(int nread)

Defined in src/http/include/icy/http/form.h:150

Updates the upload progress via the associated ConnectionStream object.