Name

SPVM::Mojo::Content - HTTP content base class

Description

Mojo::Content class in SPVM is an abstract base class for HTTP content containers, based on RFC 7230 and RFC 7231, like Mojo::Content::MultiPart and Mojo::Content::Single.

Usage

use Mojo::Content;

class Mojo::Content::MyContent extends Mojo::Content {
  
}

Super Class

Mojo::EventEmitter

Events

body

Emitted once all headers have been parsed and the body starts.

Examples:

$content->on(body => method : void ($content : Mojo::Content) {
  if ($content->headers->header("X-No-MultiPart")) {
    $content->auto_upgrade(0);
  }
});

drain

Emitted once all data has been written.

Examples:

$content->on(drain => method : void ($content : Mojo::Content) {
  $content->write_chunk(Sys->time);
});

read

Emitted when a new chunk of content arrives.

Examples:

$content->on(read => method : void ($content : Mojo::Content, $bytes : string) {
  say "Streaming: $bytes";
});

Fields

auto_decompress

has auto_decompress : rw byte;

Decompress content automatically if "is_compressed" is true.

auto_relax

has auto_relax : rw byte;

Try to detect when relaxed parsing is necessary.

headers

has headers : rw Mojo::Headers;

Content headers, defaults to a Mojo::Headers object.

max_buffer_size

has max_buffer_size : rw int;

Maximum size in bytes of buffer for content parser, defaults to the value of the SPVM_MOJO_MAX_BUFFER_SIZE environment variable or 262144 (256KiB).

max_leftover_size

has max_leftover_size : rw int;

Maximum size in bytes of buffer for pipelined HTTP requests, defaults to the value of the SPVM_MOJO_MAX_LEFTOVER_SIZE environment variable or 262144 (256KiB).

relaxed

has relaxed : rw byte;

Activate relaxed parsing for responses that are terminated with a connection close.

skip_body

has skip_body : rw byte;

Skip body parsing and finish after headers.

Instance Methods

body_contains

method body_contains : int ($chunk : string);

Check if content contains a specific string. Meant to be overloaded in a subclass.

body_size

method body_size : int ();

Content size in bytes. Meant to be overloaded in a subclass.

boundary

method boundary : string ();

Extract multipart boundary from Content-Type header.

clone

method clone : Mojo::Content ();

Return a new Mojo::Content object cloned from this content if possible, otherwise return undef.

generate_body_chunk

method generate_body_chunk : string ($offset : int);

Generate dynamic content.

get_body_chunk

method get_body_chunk : string ($offset : int);

Get a chunk of content starting from a specific position. Meant to be overloaded in a subclass.

get_header_chunk

method get_header_chunk : string ($offset : int);

Get a chunk of the headers starting from a specific position. Note that this method finalizes the content.

header_size

method header_size : int ();

Size of headers in bytes. Note that this method finalizes the content.

headers_contain

method headers_contain : int ($chunk : string);

Check if headers contain a specific string. Note that this method finalizes the content.

is_chunked

method is_chunked : int ();

Check if Transfer-Encoding header indicates chunked transfer encoding.

is_compressed

method is_compressed : int ();

Check Content-Encoding header for gzip value.

is_dynamic

method is_dynamic : int ();

Check if content will be dynamically generated, which prevents "clone" from working.

is_finished

method is_finished : int ();

Check if parser is finished.

is_limit_exceeded

method is_limit_exceeded : int ();

Check if buffer has exceeded "max_buffer_size".

is_multipart

method is_multipart : int ();

False, this is not a Mojo::Content::MultiPart object.

is_parsing_body

method is_parsing_body : int ();

Check if body parsing started yet.

is_sse

method is_sse : int ();

Check if Content-Type header indicates Server-Sent Events (SSE). Note that this method is EXPERIMENTAL and may change without warning!

leftovers

method leftovers : string ();

Get leftover data from content parser.

parse

method parse : Mojo::Content ($chunk : string);

Parse content chunk.

parse_body

method parse_body : void ($chunk : string);

Parse body chunk and skip headers.

progress

method progress : int ();

Size of content already received from message in bytes.

write

method write : void ($chunk : string, $cb : Mojo::EventEmitter::Callback);

Write dynamic content non-blocking, the optional drain callback will be executed once all data has been written. Calling this method without a chunk of data will finalize the "headers" and allow for dynamic content to be written later. You can write an empty chunk of data at any time to end the stream.

write_chunk

method write_chunk : void ($chunk : string, $cb : Mojo::EventEmitter::Callback);

Write dynamic content non-blocking with chunked transfer encoding, the optional drain callback will be executed once all data has been written. Calling this method without a chunk of data will finalize the "headers" and allow for dynamic content to be written later. You can write an empty chunk of data at any time to end the stream.

write_sse

method write_sse : void ($event : Mojo::SSE::Event, $cb : Mojo::EventEmitter::Callback);

Write Server-Sent Event (SSE) non-blocking, the optional drain callback will be executed once all data has been written. Calling this method without an event will finalize the response headers and allow for events to be written later. Note that this method is EXPERIMENTAL and may change without warning!

Well Known Child Classes

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License