Separate Utilities

These are some useful utilities included with SHOW, but in their own header files so they’re optional.

Base-64 Encoding

These are utilities for handling base64-encoded strings, very commonly used for transporting binary data in web applications. They are included in show/base64.hpp.

string base64_encode(const std::string &o, const char *chars = base64_chars_standard)

Base64-encode a string o using the character set chars, which must point to a char array of length 64.

std::string base64_decode(const std::string &o, const char *chars = base64_chars_standard, show::base64_flags flags = 0x00)

Decode a base64-encoded string o using the character set chars, which must point to a char array of length 64. Throws a base64_decode_error if the input is not encoded against chars or has incorrect padding.

Incorrect padding can be ignored by passing show::base64_ignore_padding as the flags argument.

class base64_decode_error : public std::runtime_error

Thrown by base64_decode() when the input is not valid base64.

Note

base64_encode() shouldn’t throw an exception, as any string can be converted to base-64.

char *base64_chars_standard

The standard set of base64 characters for use with base64_encode() and base64_decode()

char *base64_chars_urlsafe

The URL_safe set of base64 characters for use with base64_encode() and base64_decode(), making the following replacements:

  • +-
  • /_

Multipart Content Support

Multipart content is used to send a number of data segments each with their own separate headers. As such, text and binary data can be mixed in the same message.

SHOW provides the following utilities for parsing multipart requests in show/multipart.hpp. Typically, the Content-Type header for these types of requests will look something like:

Content-Type: multipart/form-data; boundary=AaB03x

The boundary string must be extracted from the header to pass to multipart’s constructor. A simple example with no error handling:

const auto& header_value = request.headers()[ "Content-Type" ][ 0 ];
auto content_supertype = header_value.substr( 0, header_value.find( "/" ) )
if( content_supertype == "multipart" )
{
    show::multipart parser{
        request,
        header_value.substr( header_value.find( "boundary=" ) + 9 )
    };

    // Iterate over multipart data ...
}
else
    // Process data as single message ...
class multipart

class description

template<class String>
multipart(std::streambuf &buffer, String &&boundary)

Constructs a new multipart content parser.

The supplied buffer will typically be a request object, but because multipart content can contain other multipart content recursively it can also be a show::multipart::segment. The boundary variable is a perfectly-forwarded boundary string for the multipart data.

Throws std::invalid_argument if the boundary is an empty string.

See also

multipart::iterator begin()

Returns an iterator pointing to the first segment in the multipart content. Calling this more than once on the same multipart throws a std::logic_error.

See also

multipart::iterator end()

Returns an iterator representing the end of the multipart content.

const std::string &boundary()

The boundary string the multipart is using to split the content

const std::streambuf &buffer()

The buffer the multipart is reading from

class multipart::iterator

Iterator type for iterating over multipart data segments. Implements most of input iterator functionality, except that its value_type (multipart::segment) cannot be copied.

class multipart::segment : public std::streambuf

Represents a segment of data in the multipart content being iterated over. Cannot be copied.

const headers_type &headers()

The headers for this individual segment of data; does not include the request’s headers.

class multipart_parse_error : public request_parse_error

Thrown when creating a multipart, iterating over parts, or reading from a multipart::segment whenever the content violates the multipart format.