Name

SPVM::Mojo::Headers - HTTP headers

Description

Mojo::Headers class in SPVM is a container for HTTP headers, based on RFC 7230 and RFC 7231.

Usage

use Mojo::Headers;

# Parse
my $headers = Mojo::Headers->new;
$headers->parse("Content-Length: 42\x0d\x0a");
$headers->parse("Content-Type: text/html\x0d\x0a\x0d\x0a");
say $headers->content_length;
say $headers->content_type;

# Build
my $headers = Mojo::Headers->new;
$headers->set_content_length(42);
$headers->set_content_type("text/plain");
say $headers->to_string;

Interfaces

Fields

max_line_size

has max_line_size : rw int;

Maximum header line size in bytes, defaults to the value of the SPVM_MOJO_MAX_LINE_SIZE environment variable or 8192 (8KiB).

Examples:

my $size = $headers->max_line_size;
$headers->set_max_line_size(1024);

max_lines

has max_lines : rw int;

Maximum number of header lines, defaults to the value of the SPVM_MOJO_MAX_LINES environment variable or 100.

Examples:

my $num  = $headers->max_lines;
$headers->set_max_lines(200);

Class Methods

new

static method new : Mojo::Headers ();

Creates a new Mojo::Headers object, and returns it.

Instance Methods

add

method add : void ($name : string, $value : object of string|Stringable|string[]);

Add header with one or more lines.

Examples:

$headers->add(Foo => "one value");
$headers->add(Foo => ["first value", "second value"]);

# "Vary: Accept
#  Vary: Accept-Encoding"
$headers->add(Vary => "Accept")
$headers->add(Vary => "Accept-Encoding");

append

method append : void ($name : string, $value : string);

Append value to header and flatten it if necessary.

Examples:

# "Vary: Accept"
$headers->append(Vary => "Accept")->to_string;

# "Vary: Accept, Accept-Encoding"
$headers->set_vary("Accept")
$headers->append(Vary => "Accept-Encoding");

clone

method clone : Mojo::Headers ();

Return a new Mojo::Headers object cloned from these headers.

Examples:

my $clone = $headers->clone;

dehop

method dehop : void ();

Remove hop-by-hop headers that should not be retransmitted.

Examples:

$headers->dehop;

every_header

method every_header : string[] ($name : string);

Similar to "header", but returns all headers sharing the same name as an array reference.

Examples:

my $all = $headers->every_header("Location");

# Get first header value
say $headers->every_header("Location")->[0];

from_hash

method from_hash : Mojo::Headers ($hash : Hash);

Parse headers from a hash reference, an empty hash removes all headers.

Examples:

$headers->from_hash(Hash->new({"Cookie" => "a=b"}));
$headers->from_hash(Hash->new({"Cookie" => ["a=b", "c=d"]}));
$headers->from_hash(Hash->new);

method header : string ($name : string);

Get the current header values.

Examples:

my $value = $headers->header("Foo");

set_header

method set_header : void ($name : string, $value : object of string|Stringable|string[]);

Replace the current header values.

Examples:

$headers->set_header(Foo => "one value");
$headers->set_header(Foo => ["first value", "second value"]);

is_finished

method is_finished : int ();

Check if header parser is finished.

Examples:

my $bool = $headers->is_finished;

is_limit_exceeded

method is_limit_exceeded : int ();

Check if headers have exceeded "max_line_size" or "max_lines".

Examples:

my $bool = $headers->is_limit_exceeded;

leftovers

method leftovers : string ();

Get and remove leftover data from header parser.

Examples:

my $bytes = $headers->leftovers;

names

method names : string[] ();

Return an array reference with all currently defined headers.

Examples:

my $names = $headers->names;

# Names of all headers
for my $_ (@{$headers->names}) {
  say $_;
}

parse

method parse : void ($chunk : string);

Parse formatted headers.

Examples:

$headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");

remove

method remove : void ($name : string);

Remove a header.

Examples:

$headers->remove("Foo");

to_hash

method to_hash : Hash ($multi : int = 0);

Turn headers into hash reference, array references to represent multiple headers with the same name are disabled by default.

Examples:

my $single = $headers->to_hash;
my $multi  = $headers->to_hash(1);

say $headers->to_hash->get_string("DNT");

to_string

method to_string : string ();

Turn headers into a string, suitable for HTTP messages.

Examples:

my $str = $headers->to_string;

accept

method accept : string ();

Get current header value, shortcut for the Accept header.

Examples:

my $accept = $headers->accept;

set_accept

method set_accept : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Accept header.

Examples:

$headers->set_accept("application/json");

accept_charset

method accept_charset : string ();

Get current header value, shortcut for the Accept-Charset header.

Examples:

my $charset = $headers->accept_charset;

set_accept_charset

method set_accept_charset : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Accept-Charset header.

Examples:

$headers->set_accept_charset("UTF-8");

accept_encoding

method accept_encoding : string ();

Get current header value, shortcut for the Accept-Encoding header.

Examples:

my $encoding = $headers->accept_encoding;

set_accept_encoding

method set_accept_encoding : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Accept-Encoding header.

Examples:

$headers->set_accept_encoding("gzip");

accept_language

method accept_language : string ();

Get current header value, shortcut for the Accept-Language header.

Examples:

my $language = $headers->accept_language;

set_accept_language

method set_accept_language : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Accept-Language header.

Examples:

$headers->set_accept_language("de, en");

accept_ranges

method accept_ranges : string ();

Get current header value, shortcut for the Accept-Ranges header.

Examples:

my $ranges = $headers->accept_ranges;

set_accept_ranges

method set_accept_ranges : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Accept-Ranges header.

Examples:

$headers->set_accept_ranges("bytes");

access_control_allow_origin

method access_control_allow_origin : string ();

Get current header value, shortcut for the Access-Control-Allow-Origin header from Cross-Origin Resource Sharing.

Examples:

my $origin = $headers->access_control_allow_origin;

set_access_control_allow_origin

method set_access_control_allow_origin : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Access-Control-Allow-Origin header from Cross-Origin Resource Sharing.

Examples:

$headers->set_access_control_allow_origin("*");

allow

method allow : string ();

Get current header value, shortcut for the Allow header.

Examples:

my $allow = $headers->allow;

set_allow

method set_allow : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Allow header.

Examples:

$headers->set_allow(["GET, POST"]);

authorization

method authorization : string ();

Ge current header value, shortcut for the Authorization header.

Examples:

my $authorization = $headers->authorization;

set_authorization

method set_authorization : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Authorization header.

Examples:

$headers->set_authorization("Basic Zm9vOmJhcg==");

cache_control

method cache_control : string ();

Get current header value, shortcut for the Cache-Control header.

Examples:

my $cache_control = $headers->cache_control;

set_cache_control

method set_cache_control : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Cache-Control header.

Examples:

$headers->set_cache_control("max-age=1, no-cache");

connection

method connection : string ();

Get current header value, shortcut for the Connection header.

Examples:

my $connection = $headers->connection;

set_connection

method set_connection : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Connection header.

Examples:

$headers->set_connection("close");

content_disposition

method content_disposition : string ();

Get current header value, shortcut for the Content-Disposition header.

Examples:

my $disposition = $headers->content_disposition;

set_content_disposition

method set_content_disposition : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Disposition header.

Examples:

$headers->set_content_disposition("foo");

content_encoding

method content_encoding : string ();

Get current header value, shortcut for the Content-Encoding header.

Examples:

my $encoding = $headers->content_encoding;

set_content_encoding

method set_content_encoding : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Encoding header.

Examples:

$headers->set_content_encoding("gzip");

content_language

method content_language : string ();

Get current header value, shortcut for the Content-Language header.

Examples:

my $language = $headers->content_language;

set_content_language

method set_content_language : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Language header.

Examples:

$headers->set_content_language("en");

content_length

method content_length : string ();

Get current header value, shortcut for the Content-Length header.

Examples:

my $len  = $headers->content_length;

set_content_length

method set_content_length : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Length header.

Examples:

$headers->set_content_length(4000);

content_location

method content_location : string ();

Get current header value, shortcut for the Content-Location header.

Examples:

my $location = $headers->content_location;

set_content_location

method set_content_location : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Location header.

Examples:

$headers->set_content_location("http://127.0.0.1/foo");

content_range

method content_range : string ();

Get current header value, shortcut for the Content-Range header.

Examples:

my $range = $headers->content_range;

set_content_range

method set_content_range : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Range header.

Examples:

$headers->set_content_range("bytes 2-8/100");

content_security_policy

method content_security_policy : string ();

Get current header value, shortcut for the Content-Security-Policy header from Content Security Policy 1.0.

Examples:

my $policy = $headers->content_security_policy;

set_content_security_policy

method set_content_security_policy : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Security-Policy header from Content Security Policy 1.0.

Examples:

$headers->set_content_security_policy("default-src https:");

content_type

method content_type : string ();

Get current header value, shortcut for the Content-Type header.

Examples:

my $type = $headers->content_type;

set_content_type

method set_content_type : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Content-Type header.

Examples:

$headers->set_content_type("text/plain");

method cookie : string ();

Get current header value, shortcut for the Cookie header from RFC 6265.

Examples:

my $cookie = $headers->cookie;

method set_cookie : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Cookie header from RFC 6265.

Examples:

$headers->set_cookie("f=b");

date

method date : string ();

Get current header value, shortcut for the Date header.

Examples:

my $date = $headers->date;

set_date

method set_date : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Date header.

Examples:

$headers->set_date("Sun, 17 Aug 2008 16:27:35 GMT");

dnt

method dnt : string ();

Get current header value, shortcut for the DNT (Do Not Track) header, which has no specification yet, but is very commonly used.

Examples:

my $dnt  = $headers->dnt;

set_dnt

method set_dnt : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the DNT (Do Not Track) header, which has no specification yet, but is very commonly used.

Examples:

$headers->set_dnt(1);

etag

method etag : string ();

Get current header value, shortcut for the ETag header.

Examples:

my $etag = $headers->etag;

set_etag

method set_etag : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the ETag header.

Examples:

$headers->set_etag("\"abc321\"");

expect

method expect : string ();

Get current header value, shortcut for the Expect header.

Examples:

my $expect = $headers->expect;

set_expect

method set_expect : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Expect header.

Examples:

$headers->set_expect("100-continue");

expires

method expires : string ();

Get current header value, shortcut for the Expires header.

Examples:

my $expires = $headers->expires;

set_expires

method set_expires : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Expires header.

Examples:

$headers->set_expires("Thu, 01 Dec 1994 16:00:00 GMT");

host

method host : string ();

Get current header value, shortcut for the Host header.

Examples:

my $host = $headers->host;

set_host

method set_host : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Host header.

Examples:

$headers->set_host("127.0.0.1");

if_modified_since

method if_modified_since : string ();

Get current header value, shortcut for the If-Modified-Since header.

method set_if_modified_since : void ($value : object of string|Stringable|string[]);

Examples:

my $date = $headers->if_modified_since;

set_if_modified_since

Replace current header value, shortcut for the If-Modified-Since header.

Examples:

$headers->set_if_modified_since("Sun, 17 Aug 2008 16:27:35 GMT");

if_none_match

method if_none_match : string ();

Get current header value, shortcut for the If-None-Match header.

Examples:

my $etag = $headers->if_none_match;

set_if_none_match

method set_if_none_match : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the If-None-Match header.

Examples:

$headers->set_if_none_match("\"abc321\"");

last_modified

method last_modified : string ();

Get current header value, shortcut for the Last-Modified header.

Examples:

my $date = $headers->last_modified;

set_last_modified

method set_last_modified : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Last-Modified header.

Examples:

$headers->set_last_modified("Sun, 17 Aug 2008 16:27:35 GMT");

method link : string ();

Get current header value, shortcut for the Link header from RFC5988.

Examples:

my $link = $headers->link;

method set_link : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Link header from RFC 5988.

Examples:

$headers->set_link("<http://127.0.0.1/foo/3>; rel=\"next\"");

method links : Hash of Hash of string ();

Get web links from or to Link header according to RFC 5988.

# Extract information about next page
say $headers->links->get("next")->get_string("link");
say $headers->links->get("next")->get_string("title");

Examples:

my $links = $headers->links;

method set_links : void ($links : object[]);

Set web links from or to Link header according to RFC 5988.

Examples:

$headers->set_links({next => "http://example.com/foo", prev => "http://example.com/bar"});

location

method location : string ();

Get current header value, shortcut for the Location header.

Examples:

my $location = $headers->location;

set_location

method set_location : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Location header.

Examples:

$headers->set_location("http://127.0.0.1/foo");

origin

method origin : string ();

Get current header value, shortcut for the Origin header from RFC 6454.

Examples:

my $origin = $headers->origin;

set_origin

method set_origin : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Origin header from RFC 6454.

Examples:

$headers->set_origin("http://example.com");

proxy_authenticate

method proxy_authenticate : string ();

Get current header value, shortcut for the Proxy-Authenticate header.

Examples:

my $authenticate = $headers->proxy_authenticate;

set_proxy_authenticate

method set_proxy_authenticate : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Proxy-Authenticate header.

Examples:

$headers->set_proxy_authenticate("Basic \"realm\"");

proxy_authorization

method proxy_authorization : string ();

Get current header value, shortcut for the Proxy-Authorization header.

Examples:

my $authorization = $headers->proxy_authorization;

set_proxy_authorization

method set_proxy_authorization : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Proxy-Authorization header.

Examples:

$headers->set_proxy_authorization("Basic Zm9vOmJhcg==");

range

method range : string ();

Get current header value, shortcut for the Range header.

Examples:

my $range = $headers->range;

set_range

method set_range : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Range header.

Examples:

$headers->set_range("bytes=2-8");

referer

method referer : string ();

Alias for "referrer".

Examples:

my $referrer = $headers->referer;

set_referer

method set_referer : void ($value : object of string|Stringable|string[]);

Alias for "set_referrer".

Examples:

$headers->set_referer("http://example.com");

referrer

method referrer : string ();

Get current header value, shortcut for the Referer header, there was a typo in RFC 2068 which resulted in Referer becoming an official header.

Examples:

my $referrer = $headers->referrer;

set_referrer

method set_referrer : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Referer header, there was a typo in RFC 2068 which resulted in Referer becoming an official header.

$headers->set_referrer("http://example.com");

sec_websocket_accept

method sec_websocket_accept : string ();

Get current header value, shortcut for the Sec-WebSocket-Accept header from RFC 6455.

Examples:

my $accept = $headers->sec_websocket_accept;

set_sec_websocket_accept

method set_sec_websocket_accept : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Sec-WebSocket-Accept header from RFC 6455.

Examples:

$headers->set_sec_websocket_accept("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");

sec_websocket_extensions

method sec_websocket_extensions : string ();

Get current header value, shortcut for the Sec-WebSocket-Extensions header from RFC 6455.

Examples:

my $extensions = $headers->sec_websocket_extensions;

set_sec_websocket_extensions

method set_sec_websocket_extensions : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Sec-WebSocket-Extensions header from RFC 6455.

Examples:

$headers->set_sec_websocket_extensions("foo");

sec_websocket_key

method sec_websocket_key : string ();

Get current header value, shortcut for the Sec-WebSocket-Key header from RFC 6455.

Examples:

my $key  = $headers->sec_websocket_key;

set_sec_websocket_key

method set_sec_websocket_key : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Sec-WebSocket-Key header from RFC 6455.

Examples:

$headers->set_sec_websocket_key("dGhlIHNhbXBsZSBub25jZQ==");

sec_websocket_protocol

method sec_websocket_protocol : string ();

Get current header value, shortcut for the Sec-WebSocket-Protocol header from RFC 6455.

Examples:

my $proto = $headers->sec_websocket_protocol;

set_sec_websocket_protocol

method set_sec_websocket_protocol : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Sec-WebSocket-Protocol header from RFC 6455.

Examples:

$headers->set_sec_websocket_protocol("sample");

sec_websocket_version

method sec_websocket_version : string ();

Get current header value, shortcut for the Sec-WebSocket-Version header from RFC 6455.

Examples:

my $version = $headers->sec_websocket_version;

set_sec_websocket_version

method set_sec_websocket_version : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Sec-WebSocket-Version header from RFC 6455.

Examples:

$headers->set_sec_websocket_version(13);

server

method server : string ();

Get current header value, shortcut for the Server header.

Examples:

my $server = $headers->server;

set_server

method set_server : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Server header.

Examples:

$headers->set_server("Mojo");

server_timing

method server_timing : string ();

Get current header value, shortcut for the Server-Timing header from Server Timing.

Examples:

my $timing = $headers->server_timing;

set_server_timing

method set_server_timing : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Server-Timing header from Server Timing.

Examples:

$headers->set_server_timing("app;desc=Mojolicious;dur=0.0001");

method get_set_cookie : string ();

Get current header value, shortcut for the Set-Cookie header from RFC 6265.

Examples:

my $cookie = $headers->set_cookie;

method set_set_cookie : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Set-Cookie header from RFC 6265.

Examples:

$headers->set_set_cookie("f=b; path=/");

status

method status : string ();

Get current header value, shortcut for the Status header from RFC 3875.

Examples:

my $status = $headers->status;

set_status

method set_status : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Status header from RFC 3875.

Examples:

$headers->set_status("200 OK");

strict_transport_security

method strict_transport_security : string ();

Get current header value, shortcut for the Strict-Transport-Security header from RFC 6797.

Examples:

my $policy = $headers->strict_transport_security;

set_strict_transport_security

method set_strict_transport_security : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Strict-Transport-Security header from RFC 6797.

Examples:

$headers->set_strict_transport_security("max-age=31536000");

te

method te : string ();

Get current header value, shortcut for the TE header.

Examples:

my $te   = $headers->te;

set_te

method set_te : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the TE header.

Examples:

$headers->set_te("chunked");

trailer

method trailer : string ();

Get current header value, shortcut for the Trailer header.

Examples:

my $trailer = $headers->trailer;

set_trailer

method set_trailer : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Trailer header.

Examples:

$headers->set_trailer("X-Foo");

transfer_encoding

method transfer_encoding : string ();

Get current header value, shortcut for the Transfer-Encoding header.

Examples:

my $encoding = $headers->transfer_encoding;

set_transfer_encoding

method set_transfer_encoding : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Transfer-Encoding header.

Examples:

$headers->set_transfer_encoding("chunked");

upgrade

method upgrade : string ();

Get current header value, shortcut for the Upgrade header.

Examples:

my $upgrade = $headers->upgrade;

set_upgrade

method set_upgrade : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the Upgrade header.

Examples:

$headers->set_upgrade("websocket");

user_agent

method user_agent : string ();

Get current header value, shortcut for the User-Agent header.

Examples:

my $agent = $headers->user_agent;

set_user_agent

method set_user_agent : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the User-Agent header.

Examples:

$headers->set_user_agent("Mojo/1.0");

vary

method vary : string ();

method set_vary : void ($value : object of string|Stringable|string[]);

Get current header value, shortcut for the Vary header.

Examples:

my $vary = $headers->vary;

set_vary

Replace current header value, shortcut for the Vary header.

Examples:

$headers->set_vary("*");

www_authenticate

method www_authenticate : string ();

Get current header value, shortcut for the WWW-Authenticate header.

Examples:

my $authenticate = $headers->www_authenticate;

set_www_authenticate

method set_www_authenticate : void ($value : object of string|Stringable|string[]);

Replace current header value, shortcut for the WWW-Authenticate header.

Examples:

$headers->set_www_authenticate("Basic realm=\"realm\"");

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License