1Mojo::Headers(3)      User Contributed Perl Documentation     Mojo::Headers(3)
2
3
4

NAME

6       Mojo::Headers - HTTP headers
7

SYNOPSIS

9         use Mojo::Headers;
10
11         # Parse
12         my $headers = Mojo::Headers->new;
13         $headers->parse("Content-Length: 42\x0d\x0a");
14         $headers->parse("Content-Type: text/html\x0d\x0a\x0d\x0a");
15         say $headers->content_length;
16         say $headers->content_type;
17
18         # Build
19         my $headers = Mojo::Headers->new;
20         $headers->content_length(42);
21         $headers->content_type('text/plain');
22         say $headers->to_string;
23

DESCRIPTION

25       Mojo::Headers is a container for HTTP headers, based on RFC 7230
26       <https://tools.ietf.org/html/rfc7230> and RFC 7231
27       <https://tools.ietf.org/html/rfc7231>.
28

ATTRIBUTES

30       Mojo::Headers implements the following attributes.
31
32   max_line_size
33         my $size = $headers->max_line_size;
34         $headers = $headers->max_line_size(1024);
35
36       Maximum header line size in bytes, defaults to the value of the
37       "MOJO_MAX_LINE_SIZE" environment variable or 8192 (8KiB).
38
39   max_lines
40         my $num  = $headers->max_lines;
41         $headers = $headers->max_lines(200);
42
43       Maximum number of header lines, defaults to the value of the
44       "MOJO_MAX_LINES" environment variable or 100.
45

METHODS

47       Mojo::Headers inherits all methods from Mojo::Base and implements the
48       following new ones.
49
50   accept
51         my $accept = $headers->accept;
52         $headers   = $headers->accept('application/json');
53
54       Get or replace current header value, shortcut for the "Accept" header.
55
56   accept_charset
57         my $charset = $headers->accept_charset;
58         $headers    = $headers->accept_charset('UTF-8');
59
60       Get or replace current header value, shortcut for the "Accept-Charset"
61       header.
62
63   accept_encoding
64         my $encoding = $headers->accept_encoding;
65         $headers     = $headers->accept_encoding('gzip');
66
67       Get or replace current header value, shortcut for the "Accept-Encoding"
68       header.
69
70   accept_language
71         my $language = $headers->accept_language;
72         $headers     = $headers->accept_language('de, en');
73
74       Get or replace current header value, shortcut for the "Accept-Language"
75       header.
76
77   accept_ranges
78         my $ranges = $headers->accept_ranges;
79         $headers   = $headers->accept_ranges('bytes');
80
81       Get or replace current header value, shortcut for the "Accept-Ranges"
82       header.
83
84   access_control_allow_origin
85         my $origin = $headers->access_control_allow_origin;
86         $headers   = $headers->access_control_allow_origin('*');
87
88       Get or replace current header value, shortcut for the
89       "Access-Control-Allow-Origin" header from Cross-Origin Resource Sharing
90       <https://www.w3.org/TR/cors/>.
91
92   add
93         $headers = $headers->add(Foo => 'one value');
94         $headers = $headers->add(Foo => 'first value', 'second value');
95
96       Add header with one or more lines.
97
98         # "Vary: Accept
99         #  Vary: Accept-Encoding"
100         $headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string;
101
102   allow
103         my $allow = $headers->allow;
104         $headers  = $headers->allow('GET, POST');
105
106       Get or replace current header value, shortcut for the "Allow" header.
107
108   append
109         $headers = $headers->append(Vary => 'Accept-Encoding');
110
111       Append value to header and flatten it if necessary.
112
113         # "Vary: Accept"
114         $headers->append(Vary => 'Accept')->to_string;
115
116         # "Vary: Accept, Accept-Encoding"
117         $headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string;
118
119   authorization
120         my $authorization = $headers->authorization;
121         $headers          = $headers->authorization('Basic Zm9vOmJhcg==');
122
123       Get or replace current header value, shortcut for the "Authorization"
124       header.
125
126   cache_control
127         my $cache_control = $headers->cache_control;
128         $headers          = $headers->cache_control('max-age=1, no-cache');
129
130       Get or replace current header value, shortcut for the "Cache-Control"
131       header.
132
133   clone
134         my $clone = $headers->clone;
135
136       Return a new Mojo::Headers object cloned from these headers.
137
138   connection
139         my $connection = $headers->connection;
140         $headers       = $headers->connection('close');
141
142       Get or replace current header value, shortcut for the "Connection"
143       header.
144
145   content_disposition
146         my $disposition = $headers->content_disposition;
147         $headers        = $headers->content_disposition('foo');
148
149       Get or replace current header value, shortcut for the
150       "Content-Disposition" header.
151
152   content_encoding
153         my $encoding = $headers->content_encoding;
154         $headers     = $headers->content_encoding('gzip');
155
156       Get or replace current header value, shortcut for the
157       "Content-Encoding" header.
158
159   content_language
160         my $language = $headers->content_language;
161         $headers     = $headers->content_language('en');
162
163       Get or replace current header value, shortcut for the
164       "Content-Language" header.
165
166   content_length
167         my $len  = $headers->content_length;
168         $headers = $headers->content_length(4000);
169
170       Get or replace current header value, shortcut for the "Content-Length"
171       header.
172
173   content_location
174         my $location = $headers->content_location;
175         $headers     = $headers->content_location('http://127.0.0.1/foo');
176
177       Get or replace current header value, shortcut for the
178       "Content-Location" header.
179
180   content_range
181         my $range = $headers->content_range;
182         $headers  = $headers->content_range('bytes 2-8/100');
183
184       Get or replace current header value, shortcut for the "Content-Range"
185       header.
186
187   content_security_policy
188         my $policy = $headers->content_security_policy;
189         $headers   = $headers->content_security_policy('default-src https:');
190
191       Get or replace current header value, shortcut for the
192       "Content-Security-Policy" header from Content Security Policy 1.0
193       <https://www.w3.org/TR/CSP/>.
194
195   content_type
196         my $type = $headers->content_type;
197         $headers = $headers->content_type('text/plain');
198
199       Get or replace current header value, shortcut for the "Content-Type"
200       header.
201
202   cookie
203         my $cookie = $headers->cookie;
204         $headers   = $headers->cookie('f=b');
205
206       Get or replace current header value, shortcut for the "Cookie" header
207       from RFC 6265 <https://tools.ietf.org/html/rfc6265>.
208
209   date
210         my $date = $headers->date;
211         $headers = $headers->date('Sun, 17 Aug 2008 16:27:35 GMT');
212
213       Get or replace current header value, shortcut for the "Date" header.
214
215   dehop
216         $headers = $headers->dehop;
217
218       Remove hop-by-hop headers that should not be retransmitted.
219
220   dnt
221         my $dnt  = $headers->dnt;
222         $headers = $headers->dnt(1);
223
224       Get or replace current header value, shortcut for the "DNT" (Do Not
225       Track) header, which has no specification yet, but is very commonly
226       used.
227
228   etag
229         my $etag = $headers->etag;
230         $headers = $headers->etag('"abc321"');
231
232       Get or replace current header value, shortcut for the "ETag" header.
233
234   every_header
235         my $all = $headers->every_header('Location');
236
237       Similar to "header", but returns all headers sharing the same name as
238       an array reference.
239
240         # Get first header value
241         say $headers->every_header('Location')->[0];
242
243   expect
244         my $expect = $headers->expect;
245         $headers   = $headers->expect('100-continue');
246
247       Get or replace current header value, shortcut for the "Expect" header.
248
249   expires
250         my $expires = $headers->expires;
251         $headers    = $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT');
252
253       Get or replace current header value, shortcut for the "Expires" header.
254
255   from_hash
256         $headers = $headers->from_hash({'Cookie' => 'a=b'});
257         $headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
258         $headers = $headers->from_hash({});
259
260       Parse headers from a hash reference, an empty hash removes all headers.
261
262   header
263         my $value = $headers->header('Foo');
264         $headers  = $headers->header(Foo => 'one value');
265         $headers  = $headers->header(Foo => 'first value', 'second value');
266
267       Get or replace the current header values.
268
269   host
270         my $host = $headers->host;
271         $headers = $headers->host('127.0.0.1');
272
273       Get or replace current header value, shortcut for the "Host" header.
274
275   if_modified_since
276         my $date = $headers->if_modified_since;
277         $headers = $headers->if_modified_since('Sun, 17 Aug 2008 16:27:35 GMT');
278
279       Get or replace current header value, shortcut for the
280       "If-Modified-Since" header.
281
282   if_none_match
283         my $etag = $headers->if_none_match;
284         $headers = $headers->if_none_match('"abc321"');
285
286       Get or replace current header value, shortcut for the "If-None-Match"
287       header.
288
289   is_finished
290         my $bool = $headers->is_finished;
291
292       Check if header parser is finished.
293
294   is_limit_exceeded
295         my $bool = $headers->is_limit_exceeded;
296
297       Check if headers have exceeded "max_line_size" or "max_lines".
298
299   last_modified
300         my $date = $headers->last_modified;
301         $headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT');
302
303       Get or replace current header value, shortcut for the "Last-Modified"
304       header.
305
306   leftovers
307         my $bytes = $headers->leftovers;
308
309       Get and remove leftover data from header parser.
310
311   link
312         my $link = $headers->link;
313         $headers = $headers->link('<http://127.0.0.1/foo/3>; rel="next"');
314
315       Get or replace current header value, shortcut for the "Link" header
316       from RFC 5988 <https://tools.ietf.org/html/rfc5988>.
317
318   links
319         my $links = $headers->links;
320         $headers  = $headers->links({next => 'http://example.com/foo', prev => 'http://example.com/bar'});
321
322       Get or set web links from or to "Link" header according to RFC 5988
323       <http://tools.ietf.org/html/rfc5988>.
324
325         # Extract information about next page
326         say $headers->links->{next}{link};
327         say $headers->links->{next}{title};
328
329   location
330         my $location = $headers->location;
331         $headers     = $headers->location('http://127.0.0.1/foo');
332
333       Get or replace current header value, shortcut for the "Location"
334       header.
335
336   names
337         my $names = $headers->names;
338
339       Return an array reference with all currently defined headers.
340
341         # Names of all headers
342         say for @{$headers->names};
343
344   origin
345         my $origin = $headers->origin;
346         $headers   = $headers->origin('http://example.com');
347
348       Get or replace current header value, shortcut for the "Origin" header
349       from RFC 6454 <https://tools.ietf.org/html/rfc6454>.
350
351   parse
352         $headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
353
354       Parse formatted headers.
355
356   proxy_authenticate
357         my $authenticate = $headers->proxy_authenticate;
358         $headers         = $headers->proxy_authenticate('Basic "realm"');
359
360       Get or replace current header value, shortcut for the
361       "Proxy-Authenticate" header.
362
363   proxy_authorization
364         my $authorization = $headers->proxy_authorization;
365         $headers          = $headers->proxy_authorization('Basic Zm9vOmJhcg==');
366
367       Get or replace current header value, shortcut for the
368       "Proxy-Authorization" header.
369
370   range
371         my $range = $headers->range;
372         $headers  = $headers->range('bytes=2-8');
373
374       Get or replace current header value, shortcut for the "Range" header.
375
376   referer
377         my $referrer = $headers->referer;
378         $headers     = $headers->referer('http://example.com');
379
380       Alias for "referrer".
381
382   referrer
383         my $referrer = $headers->referrer;
384         $headers     = $headers->referrer('http://example.com');
385
386       Get or replace current header value, shortcut for the "Referer" header,
387       there was a typo in RFC 2068 <https://tools.ietf.org/html/rfc2068>
388       which resulted in "Referer" becoming an official header.
389
390   remove
391         $headers = $headers->remove('Foo');
392
393       Remove a header.
394
395   sec_websocket_accept
396         my $accept = $headers->sec_websocket_accept;
397         $headers   = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
398
399       Get or replace current header value, shortcut for the
400       "Sec-WebSocket-Accept" header from RFC 6455
401       <https://tools.ietf.org/html/rfc6455>.
402
403   sec_websocket_extensions
404         my $extensions = $headers->sec_websocket_extensions;
405         $headers       = $headers->sec_websocket_extensions('foo');
406
407       Get or replace current header value, shortcut for the
408       "Sec-WebSocket-Extensions" header from RFC 6455
409       <https://tools.ietf.org/html/rfc6455>.
410
411   sec_websocket_key
412         my $key  = $headers->sec_websocket_key;
413         $headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
414
415       Get or replace current header value, shortcut for the
416       "Sec-WebSocket-Key" header from RFC 6455
417       <https://tools.ietf.org/html/rfc6455>.
418
419   sec_websocket_protocol
420         my $proto = $headers->sec_websocket_protocol;
421         $headers  = $headers->sec_websocket_protocol('sample');
422
423       Get or replace current header value, shortcut for the
424       "Sec-WebSocket-Protocol" header from RFC 6455
425       <https://tools.ietf.org/html/rfc6455>.
426
427   sec_websocket_version
428         my $version = $headers->sec_websocket_version;
429         $headers    = $headers->sec_websocket_version(13);
430
431       Get or replace current header value, shortcut for the
432       "Sec-WebSocket-Version" header from RFC 6455
433       <https://tools.ietf.org/html/rfc6455>.
434
435   server
436         my $server = $headers->server;
437         $headers   = $headers->server('Mojo');
438
439       Get or replace current header value, shortcut for the "Server" header.
440
441   server_timing
442         my $timing = $headers->server_timing;
443         $headers   = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
444
445       Get or replace current header value, shortcut for the "Server-Timing"
446       header from Server Timing <https://www.w3.org/TR/server-timing/>.
447
448   set_cookie
449         my $cookie = $headers->set_cookie;
450         $headers   = $headers->set_cookie('f=b; path=/');
451
452       Get or replace current header value, shortcut for the "Set-Cookie"
453       header from RFC 6265 <https://tools.ietf.org/html/rfc6265>.
454
455   status
456         my $status = $headers->status;
457         $headers   = $headers->status('200 OK');
458
459       Get or replace current header value, shortcut for the "Status" header
460       from RFC 3875 <https://tools.ietf.org/html/rfc3875>.
461
462   strict_transport_security
463         my $policy = $headers->strict_transport_security;
464         $headers   = $headers->strict_transport_security('max-age=31536000');
465
466       Get or replace current header value, shortcut for the
467       "Strict-Transport-Security" header from RFC 6797
468       <https://tools.ietf.org/html/rfc6797>.
469
470   te
471         my $te   = $headers->te;
472         $headers = $headers->te('chunked');
473
474       Get or replace current header value, shortcut for the "TE" header.
475
476   to_hash
477         my $single = $headers->to_hash;
478         my $multi  = $headers->to_hash(1);
479
480       Turn headers into hash reference, array references to represent
481       multiple headers with the same name are disabled by default.
482
483         say $headers->to_hash->{DNT};
484
485   to_string
486         my $str = $headers->to_string;
487
488       Turn headers into a string, suitable for HTTP messages.
489
490   trailer
491         my $trailer = $headers->trailer;
492         $headers    = $headers->trailer('X-Foo');
493
494       Get or replace current header value, shortcut for the "Trailer" header.
495
496   transfer_encoding
497         my $encoding = $headers->transfer_encoding;
498         $headers     = $headers->transfer_encoding('chunked');
499
500       Get or replace current header value, shortcut for the
501       "Transfer-Encoding" header.
502
503   upgrade
504         my $upgrade = $headers->upgrade;
505         $headers    = $headers->upgrade('websocket');
506
507       Get or replace current header value, shortcut for the "Upgrade" header.
508
509   user_agent
510         my $agent = $headers->user_agent;
511         $headers  = $headers->user_agent('Mojo/1.0');
512
513       Get or replace current header value, shortcut for the "User-Agent"
514       header.
515
516   vary
517         my $vary = $headers->vary;
518         $headers = $headers->vary('*');
519
520       Get or replace current header value, shortcut for the "Vary" header.
521
522   www_authenticate
523         my $authenticate = $headers->www_authenticate;
524         $headers         = $headers->www_authenticate('Basic realm="realm"');
525
526       Get or replace current header value, shortcut for the
527       "WWW-Authenticate" header.
528

SEE ALSO

530       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
531
532
533
534perl v5.36.0                      2023-01-20                  Mojo::Headers(3)
Impressum