Shellbox
Library and server for containerized shell execution
Loading...
Searching...
No Matches
Shellbox\Multipart\MultipartReader Class Reference

A streaming parser for multipart content. More...

Public Member Functions

 __construct (StreamInterface $stream, string $boundary, $hmacKey=false)
 
 readPreamble ()
 This must be called first on a new multipart stream, to discard any text before the first boundary.
 
 readEpilogue ()
 Call this after the last part to read the epilogue, which runs from the closing boundary to the end of the stream.
 
 readPartHeaders ()
 Read the part headers.
 
 readPartAsString ()
 Read a part to a string buffer.
 
 copyPartToStream (StreamInterface $outputStream)
 Copy a part to the specified stream, which is open for writing.
 
 readPartAsJson ( $headers)
 Read a part to a string and decode the JSON found within.
 
 getHash ()
 Get the HMAC of all the content in the stream.
 

Public Attributes

const CHUNK_SIZE = 8192
 

Detailed Description

A streaming parser for multipart content.

Typical usage: $reader = new MultipartReader( $stream, $boundary ); $reader->readPrologue(); while ( $headers = $reader->readPartHeaders() ) { $contents = $reader->readPartAsString(); } $reader->readEpilogue();

Constructor & Destructor Documentation

◆ __construct()

Shellbox\Multipart\MultipartReader::__construct ( StreamInterface $stream,
string $boundary,
$hmacKey = false )
Parameters
StreamInterface$stream
string$boundary
string | false$hmacKey

Member Function Documentation

◆ copyPartToStream()

Shellbox\Multipart\MultipartReader::copyPartToStream ( StreamInterface $outputStream)

Copy a part to the specified stream, which is open for writing.

Parameters
StreamInterface$outputStream

◆ getHash()

Shellbox\Multipart\MultipartReader::getHash ( )

Get the HMAC of all the content in the stream.

This must be called after the reader has reached the end of the input stream.

Returns
string
Exceptions
MultipartError

◆ readPartAsJson()

Shellbox\Multipart\MultipartReader::readPartAsJson ( $headers)

Read a part to a string and decode the JSON found within.

Parameters
array$headers
Returns
mixed
Exceptions
MultipartError

◆ readPartAsString()

Shellbox\Multipart\MultipartReader::readPartAsString ( )

Read a part to a string buffer.

Returns
string

◆ readPartHeaders()

Shellbox\Multipart\MultipartReader::readPartHeaders ( )

Read the part headers.

The Content-Disposition header is decoded, and its value is an associative array representing the header's parameters. Other headers are returned as strings.

The header names are converted to lower case.

Following the HTTP convention, we don't support line continuation. The writer we use (MultipartStream) doesn't use line continuation, so this lack of compliance should be harmless.

After reading the headers, the caller should call one of readPartAsString(), copyPartToStream() or readPartAsJson().

Returns
array|bool Decoded headers, or false if the closing boundary was reached.
Exceptions
MultipartError

◆ readPreamble()

Shellbox\Multipart\MultipartReader::readPreamble ( )

This must be called first on a new multipart stream, to discard any text before the first boundary.

Returns
string

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