|
NAMEMojo::Headers - HTTP headersSYNOPSISuse 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->content_length(42); $headers->content_type('text/plain'); say $headers->to_string; DESCRIPTIONMojo::Headers is a container for HTTP headers, based on RFC 7230 <https://tools.ietf.org/html/rfc7230> and RFC 7231 <https://tools.ietf.org/html/rfc7231>.ATTRIBUTESMojo::Headers implements the following attributes.max_line_sizemy $size = $headers->max_line_size; $headers = $headers->max_line_size(1024); Maximum header line size in bytes, defaults to the value of the "MOJO_MAX_LINE_SIZE" environment variable or 8192 (8KiB). max_linesmy $num = $headers->max_lines; $headers = $headers->max_lines(200); Maximum number of header lines, defaults to the value of the "MOJO_MAX_LINES" environment variable or 100. METHODSMojo::Headers inherits all methods from Mojo::Base and implements the following new ones.acceptmy $accept = $headers->accept; $headers = $headers->accept('application/json'); Get or replace current header value, shortcut for the "Accept" header. accept_charsetmy $charset = $headers->accept_charset; $headers = $headers->accept_charset('UTF-8'); Get or replace current header value, shortcut for the "Accept-Charset" header. accept_encodingmy $encoding = $headers->accept_encoding; $headers = $headers->accept_encoding('gzip'); Get or replace current header value, shortcut for the "Accept-Encoding" header. accept_languagemy $language = $headers->accept_language; $headers = $headers->accept_language('de, en'); Get or replace current header value, shortcut for the "Accept-Language" header. accept_rangesmy $ranges = $headers->accept_ranges; $headers = $headers->accept_ranges('bytes'); Get or replace current header value, shortcut for the "Accept-Ranges" header. access_control_allow_originmy $origin = $headers->access_control_allow_origin; $headers = $headers->access_control_allow_origin('*'); Get or replace current header value, shortcut for the "Access-Control-Allow-Origin" header from Cross-Origin Resource Sharing <https://www.w3.org/TR/cors/>. add$headers = $headers->add(Foo => 'one value'); $headers = $headers->add(Foo => 'first value', 'second value'); Add header with one or more lines. # "Vary: Accept # Vary: Accept-Encoding" $headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string; allowmy $allow = $headers->allow; $headers = $headers->allow('GET, POST'); Get or replace current header value, shortcut for the "Allow" header. append$headers = $headers->append(Vary => 'Accept-Encoding'); Append value to header and flatten it if necessary. # "Vary: Accept" $headers->append(Vary => 'Accept')->to_string; # "Vary: Accept, Accept-Encoding" $headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string; authorizationmy $authorization = $headers->authorization; $headers = $headers->authorization('Basic Zm9vOmJhcg=='); Get or replace current header value, shortcut for the "Authorization" header. cache_controlmy $cache_control = $headers->cache_control; $headers = $headers->cache_control('max-age=1, no-cache'); Get or replace current header value, shortcut for the "Cache-Control" header. clonemy $clone = $headers->clone; Return a new Mojo::Headers object cloned from these headers. connectionmy $connection = $headers->connection; $headers = $headers->connection('close'); Get or replace current header value, shortcut for the "Connection" header. content_dispositionmy $disposition = $headers->content_disposition; $headers = $headers->content_disposition('foo'); Get or replace current header value, shortcut for the "Content-Disposition" header. content_encodingmy $encoding = $headers->content_encoding; $headers = $headers->content_encoding('gzip'); Get or replace current header value, shortcut for the "Content-Encoding" header. content_languagemy $language = $headers->content_language; $headers = $headers->content_language('en'); Get or replace current header value, shortcut for the "Content-Language" header. content_lengthmy $len = $headers->content_length; $headers = $headers->content_length(4000); Get or replace current header value, shortcut for the "Content-Length" header. content_locationmy $location = $headers->content_location; $headers = $headers->content_location('http://127.0.0.1/foo'); Get or replace current header value, shortcut for the "Content-Location" header. content_rangemy $range = $headers->content_range; $headers = $headers->content_range('bytes 2-8/100'); Get or replace current header value, shortcut for the "Content-Range" header. content_security_policymy $policy = $headers->content_security_policy; $headers = $headers->content_security_policy('default-src https:'); Get or replace current header value, shortcut for the "Content-Security-Policy" header from Content Security Policy 1.0 <https://www.w3.org/TR/CSP/>. content_typemy $type = $headers->content_type; $headers = $headers->content_type('text/plain'); Get or replace current header value, shortcut for the "Content-Type" header. cookiemy $cookie = $headers->cookie; $headers = $headers->cookie('f=b'); Get or replace current header value, shortcut for the "Cookie" header from RFC 6265 <https://tools.ietf.org/html/rfc6265>. datemy $date = $headers->date; $headers = $headers->date('Sun, 17 Aug 2008 16:27:35 GMT'); Get or replace current header value, shortcut for the "Date" header. dehop$headers = $headers->dehop; Remove hop-by-hop headers that should not be retransmitted. dntmy $dnt = $headers->dnt; $headers = $headers->dnt(1); Get or replace current header value, shortcut for the "DNT" (Do Not Track) header, which has no specification yet, but is very commonly used. etagmy $etag = $headers->etag; $headers = $headers->etag('"abc321"'); Get or replace current header value, shortcut for the "ETag" header. every_headermy $all = $headers->every_header('Location'); Similar to "header", but returns all headers sharing the same name as an array reference. # Get first header value say $headers->every_header('Location')->[0]; expectmy $expect = $headers->expect; $headers = $headers->expect('100-continue'); Get or replace current header value, shortcut for the "Expect" header. expiresmy $expires = $headers->expires; $headers = $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT'); Get or replace current header value, shortcut for the "Expires" header. from_hash$headers = $headers->from_hash({'Cookie' => 'a=b'}); $headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']}); $headers = $headers->from_hash({}); Parse headers from a hash reference, an empty hash removes all headers. headermy $value = $headers->header('Foo'); $headers = $headers->header(Foo => 'one value'); $headers = $headers->header(Foo => 'first value', 'second value'); Get or replace the current header values. hostmy $host = $headers->host; $headers = $headers->host('127.0.0.1'); Get or replace current header value, shortcut for the "Host" header. if_modified_sincemy $date = $headers->if_modified_since; $headers = $headers->if_modified_since('Sun, 17 Aug 2008 16:27:35 GMT'); Get or replace current header value, shortcut for the "If-Modified-Since" header. if_none_matchmy $etag = $headers->if_none_match; $headers = $headers->if_none_match('"abc321"'); Get or replace current header value, shortcut for the "If-None-Match" header. is_finishedmy $bool = $headers->is_finished; Check if header parser is finished. is_limit_exceededmy $bool = $headers->is_limit_exceeded; Check if headers have exceeded "max_line_size" or "max_lines". last_modifiedmy $date = $headers->last_modified; $headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT'); Get or replace current header value, shortcut for the "Last-Modified" header. leftoversmy $bytes = $headers->leftovers; Get and remove leftover data from header parser. linkmy $link = $headers->link; $headers = $headers->link('<http://127.0.0.1/foo/3>; rel="next"'); Get or replace current header value, shortcut for the "Link" header from RFC 5988 <https://tools.ietf.org/html/rfc5988>. locationmy $location = $headers->location; $headers = $headers->location('http://127.0.0.1/foo'); Get or replace current header value, shortcut for the "Location" header. namesmy $names = $headers->names; Return an array reference with all currently defined headers. # Names of all headers say for @{$headers->names}; originmy $origin = $headers->origin; $headers = $headers->origin('http://example.com'); Get or replace current header value, shortcut for the "Origin" header from RFC 6454 <https://tools.ietf.org/html/rfc6454>. parse$headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a"); Parse formatted headers. proxy_authenticatemy $authenticate = $headers->proxy_authenticate; $headers = $headers->proxy_authenticate('Basic "realm"'); Get or replace current header value, shortcut for the "Proxy-Authenticate" header. proxy_authorizationmy $authorization = $headers->proxy_authorization; $headers = $headers->proxy_authorization('Basic Zm9vOmJhcg=='); Get or replace current header value, shortcut for the "Proxy-Authorization" header. rangemy $range = $headers->range; $headers = $headers->range('bytes=2-8'); Get or replace current header value, shortcut for the "Range" header. referermy $referrer = $headers->referer; $headers = $headers->referer('http://example.com'); Alias for "referrer". referrermy $referrer = $headers->referrer; $headers = $headers->referrer('http://example.com'); Get or replace current header value, shortcut for the "Referer" header, there was a typo in RFC 2068 <https://tools.ietf.org/html/rfc2068> which resulted in "Referer" becoming an official header. remove$headers = $headers->remove('Foo'); Remove a header. sec_websocket_acceptmy $accept = $headers->sec_websocket_accept; $headers = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo='); Get or replace current header value, shortcut for the "Sec-WebSocket-Accept" header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. sec_websocket_extensionsmy $extensions = $headers->sec_websocket_extensions; $headers = $headers->sec_websocket_extensions('foo'); Get or replace current header value, shortcut for the "Sec-WebSocket-Extensions" header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. sec_websocket_keymy $key = $headers->sec_websocket_key; $headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ=='); Get or replace current header value, shortcut for the "Sec-WebSocket-Key" header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. sec_websocket_protocolmy $proto = $headers->sec_websocket_protocol; $headers = $headers->sec_websocket_protocol('sample'); Get or replace current header value, shortcut for the "Sec-WebSocket-Protocol" header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. sec_websocket_versionmy $version = $headers->sec_websocket_version; $headers = $headers->sec_websocket_version(13); Get or replace current header value, shortcut for the "Sec-WebSocket-Version" header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. servermy $server = $headers->server; $headers = $headers->server('Mojo'); Get or replace current header value, shortcut for the "Server" header. server_timingmy $timing = $headers->server_timing; $headers = $headers->server_timing('app;desc=Mojolicious;dur=0.0001'); Get or replace current header value, shortcut for the "Server-Timing" header from Server Timing <https://www.w3.org/TR/server-timing/>. set_cookiemy $cookie = $headers->set_cookie; $headers = $headers->set_cookie('f=b; path=/'); Get or replace current header value, shortcut for the "Set-Cookie" header from RFC 6265 <https://tools.ietf.org/html/rfc6265>. statusmy $status = $headers->status; $headers = $headers->status('200 OK'); Get or replace current header value, shortcut for the "Status" header from RFC 3875 <https://tools.ietf.org/html/rfc3875>. strict_transport_securitymy $policy = $headers->strict_transport_security; $headers = $headers->strict_transport_security('max-age=31536000'); Get or replace current header value, shortcut for the "Strict-Transport-Security" header from RFC 6797 <https://tools.ietf.org/html/rfc6797>. temy $te = $headers->te; $headers = $headers->te('chunked'); Get or replace current header value, shortcut for the "TE" header. to_hashmy $single = $headers->to_hash; my $multi = $headers->to_hash(1); Turn headers into hash reference, array references to represent multiple headers with the same name are disabled by default. say $headers->to_hash->{DNT}; to_stringmy $str = $headers->to_string; Turn headers into a string, suitable for HTTP messages. trailermy $trailer = $headers->trailer; $headers = $headers->trailer('X-Foo'); Get or replace current header value, shortcut for the "Trailer" header. transfer_encodingmy $encoding = $headers->transfer_encoding; $headers = $headers->transfer_encoding('chunked'); Get or replace current header value, shortcut for the "Transfer-Encoding" header. upgrademy $upgrade = $headers->upgrade; $headers = $headers->upgrade('websocket'); Get or replace current header value, shortcut for the "Upgrade" header. user_agentmy $agent = $headers->user_agent; $headers = $headers->user_agent('Mojo/1.0'); Get or replace current header value, shortcut for the "User-Agent" header. varymy $vary = $headers->vary; $headers = $headers->vary('*'); Get or replace current header value, shortcut for the "Vary" header. www_authenticatemy $authenticate = $headers->www_authenticate; $headers = $headers->www_authenticate('Basic realm="realm"'); Get or replace current header value, shortcut for the "WWW-Authenticate" header. SEE ALSOMojolicious, Mojolicious::Guides, <https://mojolicious.org>.
Visit the GSP FreeBSD Man Page Interface. |