Name
SPVM::Mojo::Message - HTTP message base class
Description
Mojo::Message class in SPVM is an abstract base class for HTTP message containers, based on RFC 7230, RFC 7231 and RFC 2388, like Mojo::Message::Request and Mojo::Message::Response.
This class is an abstract class for child classes.
Usage
class Mojo::Message::MyMessage extends Mojo::Message {
}
Interfaces
Events
finish
Emitted after message building or parsing is finished.
my $before = Sys->time;
$msg->on(finish => method : void ($msg : Mojo::Message) {
$msg->headers->set_header("X-Parser-Time" => Sys->time - $before);
});
progress
Emitted when message building or parsing makes progress.
# Building
$msg->on(progress => method : void ($msg : Mojo::Message, $state : string, $offset : Int) { say "Building \"$state\" at offset " . (int)$offset });
# Parsing
$msg->on(progress => method : void ($msg : Mojo::Message) {
});
Fields
content
has content : rw Mojo::Content;
Message content, defaults to a Mojo::Content::Single object.
max_line_size
has max_line_size : rw int;
Maximum start-line size in bytes, defaults to the value of the SPVM_MOJO_MAX_LINE_SIZE
environment variable or 8192
(8KiB).
max_message_size
has max_message_size : rw int;
Maximum message size in bytes, defaults to the value of the SPVM_MOJO_MAX_MESSAGE_SIZE
environment variable or 16777216
(16MiB). Setting the value to 0
will allow messages of indefinite size.
version
has version : rw string;
HTTP version of message, defaults to 1.1
.
Instance Methods
body
method body : string ();
Slurp "content".
set_body
method set_body : void ($body : string);
Replace "content".
body_params
method body_params : Mojo::Parameters ();
POST
parameters extracted from application/x-www-form-urlencoded
or multipart/form-data
message body, usually a Mojo::Parameters object. Note that this method caches all data, so it should not be called before the entire message body has been received. Parts of the message body need to be loaded into memory to parse POST
parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests and a 2GiB limit for responses by default.
# Get POST parameter names and values
my $hash = $msg->body_params->to_hash;
body_size
method body_size : int ();
Content size in bytes.
build_body
method build_body : string ();
Render whole body with "get_body_chunk".
build_headers
method build_headers : string ();
Render all headers with "get_header_chunk".
build_start_line
method build_start_line : string ();
Render start-line with "get_start_line_chunk".
cookie
method cookie : Mojo::Cookie ($name : string);
Access message cookies, usually Mojo::Cookie::Request or Mojo::Cookie::Response objects. If there are multiple cookies sharing the same name, and you want to access more than just the last one, you can use "every_cookie". Note that this method caches all data, so it should not be called before all headers have been received.
# Get cookie value
say $msg->cookie("foo")->value;
cookies
method cookies : Mojo::Cookie[] ();
Access message cookies. Meant to be overloaded in a subclass.
every_cookie
method every_cookie : Mojo::Cookie[] ($name : string);
Similar to "cookie", but returns all message cookies sharing the same name as an array reference.
# Get first cookie value
say $msg->every_cookie("foo")->[0]->value;
every_upload
method every_upload : Mojo::Upload[] ($name : string);
Similar to "upload", but returns all file uploads sharing the same name as an array reference.
# Get content of first uploaded file
say $msg->every_upload("foo")->[0]->asset->slurp;
extract_start_line
method extract_start_line : int ($str_ref : string[]);
Extract start-line from string. Meant to be overloaded in a subclass.
finish
method finish : void ();
fix_headers
Finish message parser/generator.
method fix_headers : void ();
Make sure message has all required headers.
get_body_chunk
method get_body_chunk : string ($offset : int);
Get a chunk of body data starting from a specific position. Note that it might not be possible to get the same chunk twice if content was generated dynamically.
get_header_chunk
method get_header_chunk : string ($offset : int);
Get a chunk of header data, starting from a specific position. Note that this method finalizes the message.
get_start_line_chunk
method get_start_line_chunk : string ($offset : int);
Get a chunk of start-line data starting from a specific position. Meant to be overloaded in a subclass.
header_size
method header_size : int ();
Size of headers in bytes. Note that this method finalizes the message.
headers
method headers : Mojo::Headers ();
Message headers, usually a Mojo::Headers object.
# Longer version
my $headers = $msg->content->headers;
is_finished
method is_finished : int ();
Check if message parser/generator is finished.
is_limit_exceeded
method is_limit_exceeded : int ();
Check if message has exceeded "max_line_size", "max_message_size", Mojo::Content#max_buffer_size field or Mojo::Headers#max_line_size field.
json
method json : object ();
Decode JSON message body directly using JSON if possible, an undef
return value indicates a bare null
or that decoding failed.
Note that this method caches all data, so it should not be called before the entire message body has been received. The whole message body needs to be loaded into memory to parse it, so you have to make sure it is not excessively large. There's a 16MiB limit for requests and a 2GiB limit for responses by default.
# Extract JSON values
say $msg->json->(Hash)->get("foo")->(Hash)->get("bar")->(List)->get(23)->(string);
parse
method parse : void ($chunk : string);
Parse message chunk.
save_to
method save_to : void ($path : string);
Save message body to a file.
start_line_size
method start_line_size : int ();
Size of the start-line in bytes. Meant to be overloaded in a subclass.
text
method text : string ();
Retrieve "body".
to_string
method to_string : string ();
Render whole message. Note that this method finalizes the message, and that it might not be possible to render the same message twice if content was generated dynamically.
upload
method upload : Mojo::Upload ($name : string);
Access multipart/form-data
file uploads, usually Mojo::Upload objects. If there are multiple uploads sharing the same name, and you want to access more than just the last one, you can use "every_upload". Note that this method caches all data, so it should not be called before the entire message body has been received.
# Get content of uploaded file
say $msg->upload("foo")->asset->slurp;
uploads
method uploads : Mojo::Upload[] ();
All multipart/form-data
file uploads, usually Mojo::Upload objects.
# Names of all uploads
for my $_ (@{$msg->uploads}) {
say $_->name;
}
Well Known Child Classes
See Also
Copyright & License
Copyright (c) 2025 Yuki Kimoto
MIT License