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   location
319         my $location = $headers->location;
320         $headers     = $headers->location('http://127.0.0.1/foo');
321
322       Get or replace current header value, shortcut for the "Location"
323       header.
324
325   names
326         my $names = $headers->names;
327
328       Return an array reference with all currently defined headers.
329
330         # Names of all headers
331         say for @{$headers->names};
332
333   origin
334         my $origin = $headers->origin;
335         $headers   = $headers->origin('http://example.com');
336
337       Get or replace current header value, shortcut for the "Origin" header
338       from RFC 6454 <https://tools.ietf.org/html/rfc6454>.
339
340   parse
341         $headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
342
343       Parse formatted headers.
344
345   proxy_authenticate
346         my $authenticate = $headers->proxy_authenticate;
347         $headers         = $headers->proxy_authenticate('Basic "realm"');
348
349       Get or replace current header value, shortcut for the
350       "Proxy-Authenticate" header.
351
352   proxy_authorization
353         my $authorization = $headers->proxy_authorization;
354         $headers          = $headers->proxy_authorization('Basic Zm9vOmJhcg==');
355
356       Get or replace current header value, shortcut for the
357       "Proxy-Authorization" header.
358
359   range
360         my $range = $headers->range;
361         $headers  = $headers->range('bytes=2-8');
362
363       Get or replace current header value, shortcut for the "Range" header.
364
365   referer
366         my $referrer = $headers->referer;
367         $headers     = $headers->referer('http://example.com');
368
369       Alias for "referrer".
370
371   referrer
372         my $referrer = $headers->referrer;
373         $headers     = $headers->referrer('http://example.com');
374
375       Get or replace current header value, shortcut for the "Referer" header,
376       there was a typo in RFC 2068 <https://tools.ietf.org/html/rfc2068>
377       which resulted in "Referer" becoming an official header.
378
379   remove
380         $headers = $headers->remove('Foo');
381
382       Remove a header.
383
384   sec_websocket_accept
385         my $accept = $headers->sec_websocket_accept;
386         $headers   = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
387
388       Get or replace current header value, shortcut for the
389       "Sec-WebSocket-Accept" header from RFC 6455
390       <https://tools.ietf.org/html/rfc6455>.
391
392   sec_websocket_extensions
393         my $extensions = $headers->sec_websocket_extensions;
394         $headers       = $headers->sec_websocket_extensions('foo');
395
396       Get or replace current header value, shortcut for the
397       "Sec-WebSocket-Extensions" header from RFC 6455
398       <https://tools.ietf.org/html/rfc6455>.
399
400   sec_websocket_key
401         my $key  = $headers->sec_websocket_key;
402         $headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
403
404       Get or replace current header value, shortcut for the
405       "Sec-WebSocket-Key" header from RFC 6455
406       <https://tools.ietf.org/html/rfc6455>.
407
408   sec_websocket_protocol
409         my $proto = $headers->sec_websocket_protocol;
410         $headers  = $headers->sec_websocket_protocol('sample');
411
412       Get or replace current header value, shortcut for the
413       "Sec-WebSocket-Protocol" header from RFC 6455
414       <https://tools.ietf.org/html/rfc6455>.
415
416   sec_websocket_version
417         my $version = $headers->sec_websocket_version;
418         $headers    = $headers->sec_websocket_version(13);
419
420       Get or replace current header value, shortcut for the
421       "Sec-WebSocket-Version" header from RFC 6455
422       <https://tools.ietf.org/html/rfc6455>.
423
424   server
425         my $server = $headers->server;
426         $headers   = $headers->server('Mojo');
427
428       Get or replace current header value, shortcut for the "Server" header.
429
430   server_timing
431         my $timing = $headers->server_timing;
432         $headers   = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
433
434       Get or replace current header value, shortcut for the "Server-Timing"
435       header from Server Timing <https://www.w3.org/TR/server-timing/>.
436
437   set_cookie
438         my $cookie = $headers->set_cookie;
439         $headers   = $headers->set_cookie('f=b; path=/');
440
441       Get or replace current header value, shortcut for the "Set-Cookie"
442       header from RFC 6265 <https://tools.ietf.org/html/rfc6265>.
443
444   status
445         my $status = $headers->status;
446         $headers   = $headers->status('200 OK');
447
448       Get or replace current header value, shortcut for the "Status" header
449       from RFC 3875 <https://tools.ietf.org/html/rfc3875>.
450
451   strict_transport_security
452         my $policy = $headers->strict_transport_security;
453         $headers   = $headers->strict_transport_security('max-age=31536000');
454
455       Get or replace current header value, shortcut for the
456       "Strict-Transport-Security" header from RFC 6797
457       <https://tools.ietf.org/html/rfc6797>.
458
459   te
460         my $te   = $headers->te;
461         $headers = $headers->te('chunked');
462
463       Get or replace current header value, shortcut for the "TE" header.
464
465   to_hash
466         my $single = $headers->to_hash;
467         my $multi  = $headers->to_hash(1);
468
469       Turn headers into hash reference, array references to represent
470       multiple headers with the same name are disabled by default.
471
472         say $headers->to_hash->{DNT};
473
474   to_string
475         my $str = $headers->to_string;
476
477       Turn headers into a string, suitable for HTTP messages.
478
479   trailer
480         my $trailer = $headers->trailer;
481         $headers    = $headers->trailer('X-Foo');
482
483       Get or replace current header value, shortcut for the "Trailer" header.
484
485   transfer_encoding
486         my $encoding = $headers->transfer_encoding;
487         $headers     = $headers->transfer_encoding('chunked');
488
489       Get or replace current header value, shortcut for the
490       "Transfer-Encoding" header.
491
492   upgrade
493         my $upgrade = $headers->upgrade;
494         $headers    = $headers->upgrade('websocket');
495
496       Get or replace current header value, shortcut for the "Upgrade" header.
497
498   user_agent
499         my $agent = $headers->user_agent;
500         $headers  = $headers->user_agent('Mojo/1.0');
501
502       Get or replace current header value, shortcut for the "User-Agent"
503       header.
504
505   vary
506         my $vary = $headers->vary;
507         $headers = $headers->vary('*');
508
509       Get or replace current header value, shortcut for the "Vary" header.
510
511   www_authenticate
512         my $authenticate = $headers->www_authenticate;
513         $headers         = $headers->www_authenticate('Basic realm="realm"');
514
515       Get or replace current header value, shortcut for the
516       "WWW-Authenticate" header.
517

SEE ALSO

519       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
520
521
522
523perl v5.36.0                      2022-07-22                  Mojo::Headers(3)
Impressum