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       <http://tools.ietf.org/html/rfc7230> and RFC 7231
27       <http://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       <http://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       <http://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 <http://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         $heders = $headers->dehop;
217
218       Remove hop-by-hop headers that should not be retransmitted. Note that
219       this method is EXPERIMENTAL and might change without warning!
220
221   dnt
222         my $dnt  = $headers->dnt;
223         $headers = $headers->dnt(1);
224
225       Get or replace current header value, shortcut for the "DNT" (Do Not
226       Track) header, which has no specification yet, but is very commonly
227       used.
228
229   etag
230         my $etag = $headers->etag;
231         $headers = $headers->etag('"abc321"');
232
233       Get or replace current header value, shortcut for the "ETag" header.
234
235   every_header
236         my $all = $headers->every_header('Location');
237
238       Similar to "header", but returns all headers sharing the same name as
239       an array reference.
240
241         # Get first header value
242         say $headers->every_header('Location')->[0];
243
244   expect
245         my $expect = $headers->expect;
246         $headers   = $headers->expect('100-continue');
247
248       Get or replace current header value, shortcut for the "Expect" header.
249
250   expires
251         my $expires = $headers->expires;
252         $headers    = $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT');
253
254       Get or replace current header value, shortcut for the "Expires" header.
255
256   from_hash
257         $headers = $headers->from_hash({'Cookie' => 'a=b'});
258         $headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
259         $headers = $headers->from_hash({});
260
261       Parse headers from a hash reference, an empty hash removes all headers.
262
263   header
264         my $value = $headers->header('Foo');
265         $headers  = $headers->header(Foo => 'one value');
266         $headers  = $headers->header(Foo => 'first value', 'second value');
267
268       Get or replace the current header values.
269
270   host
271         my $host = $headers->host;
272         $headers = $headers->host('127.0.0.1');
273
274       Get or replace current header value, shortcut for the "Host" header.
275
276   if_modified_since
277         my $date = $headers->if_modified_since;
278         $headers = $headers->if_modified_since('Sun, 17 Aug 2008 16:27:35 GMT');
279
280       Get or replace current header value, shortcut for the
281       "If-Modified-Since" header.
282
283   if_none_match
284         my $etag = $headers->if_none_match;
285         $headers = $headers->if_none_match('"abc321"');
286
287       Get or replace current header value, shortcut for the "If-None-Match"
288       header.
289
290   is_finished
291         my $bool = $headers->is_finished;
292
293       Check if header parser is finished.
294
295   is_limit_exceeded
296         my $bool = $headers->is_limit_exceeded;
297
298       Check if headers have exceeded "max_line_size" or "max_lines".
299
300   last_modified
301         my $date = $headers->last_modified;
302         $headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT');
303
304       Get or replace current header value, shortcut for the "Last-Modified"
305       header.
306
307   leftovers
308         my $bytes = $headers->leftovers;
309
310       Get and remove leftover data from header parser.
311
312   link
313         my $link = $headers->link;
314         $headers = $headers->link('<http://127.0.0.1/foo/3>; rel="next"');
315
316       Get or replace current header value, shortcut for the "Link" header
317       from RFC 5988 <http://tools.ietf.org/html/rfc5988>.
318
319   location
320         my $location = $headers->location;
321         $headers     = $headers->location('http://127.0.0.1/foo');
322
323       Get or replace current header value, shortcut for the "Location"
324       header.
325
326   names
327         my $names = $headers->names;
328
329       Return an array reference with all currently defined headers.
330
331         # Names of all headers
332         say for @{$headers->names};
333
334   origin
335         my $origin = $headers->origin;
336         $headers   = $headers->origin('http://example.com');
337
338       Get or replace current header value, shortcut for the "Origin" header
339       from RFC 6454 <http://tools.ietf.org/html/rfc6454>.
340
341   parse
342         $headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
343
344       Parse formatted headers.
345
346   proxy_authenticate
347         my $authenticate = $headers->proxy_authenticate;
348         $headers         = $headers->proxy_authenticate('Basic "realm"');
349
350       Get or replace current header value, shortcut for the
351       "Proxy-Authenticate" header.
352
353   proxy_authorization
354         my $authorization = $headers->proxy_authorization;
355         $headers          = $headers->proxy_authorization('Basic Zm9vOmJhcg==');
356
357       Get or replace current header value, shortcut for the
358       "Proxy-Authorization" header.
359
360   range
361         my $range = $headers->range;
362         $headers  = $headers->range('bytes=2-8');
363
364       Get or replace current header value, shortcut for the "Range" header.
365
366   referrer
367         my $referrer = $headers->referrer;
368         $headers     = $headers->referrer('http://example.com');
369
370       Get or replace current header value, shortcut for the "Referer" header,
371       there was a typo in RFC 2068 <http://tools.ietf.org/html/rfc2068> which
372       resulted in "Referer" becoming an official header.
373
374   remove
375         $headers = $headers->remove('Foo');
376
377       Remove a header.
378
379   sec_websocket_accept
380         my $accept = $headers->sec_websocket_accept;
381         $headers   = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
382
383       Get or replace current header value, shortcut for the
384       "Sec-WebSocket-Accept" header from RFC 6455
385       <http://tools.ietf.org/html/rfc6455>.
386
387   sec_websocket_extensions
388         my $extensions = $headers->sec_websocket_extensions;
389         $headers       = $headers->sec_websocket_extensions('foo');
390
391       Get or replace current header value, shortcut for the
392       "Sec-WebSocket-Extensions" header from RFC 6455
393       <http://tools.ietf.org/html/rfc6455>.
394
395   sec_websocket_key
396         my $key  = $headers->sec_websocket_key;
397         $headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
398
399       Get or replace current header value, shortcut for the
400       "Sec-WebSocket-Key" header from RFC 6455
401       <http://tools.ietf.org/html/rfc6455>.
402
403   sec_websocket_protocol
404         my $proto = $headers->sec_websocket_protocol;
405         $headers  = $headers->sec_websocket_protocol('sample');
406
407       Get or replace current header value, shortcut for the
408       "Sec-WebSocket-Protocol" header from RFC 6455
409       <http://tools.ietf.org/html/rfc6455>.
410
411   sec_websocket_version
412         my $version = $headers->sec_websocket_version;
413         $headers    = $headers->sec_websocket_version(13);
414
415       Get or replace current header value, shortcut for the
416       "Sec-WebSocket-Version" header from RFC 6455
417       <http://tools.ietf.org/html/rfc6455>.
418
419   server
420         my $server = $headers->server;
421         $headers   = $headers->server('Mojo');
422
423       Get or replace current header value, shortcut for the "Server" header.
424
425   server_timing
426         my $timing = $headers->server_timing;
427         $headers   = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
428
429       Get or replace current header value, shortcut for the "Server-Timing"
430       header from Server Timing <https://www.w3.org/TR/server-timing/>.
431
432   set_cookie
433         my $cookie = $headers->set_cookie;
434         $headers   = $headers->set_cookie('f=b; path=/');
435
436       Get or replace current header value, shortcut for the "Set-Cookie"
437       header from RFC 6265 <http://tools.ietf.org/html/rfc6265>.
438
439   status
440         my $status = $headers->status;
441         $headers   = $headers->status('200 OK');
442
443       Get or replace current header value, shortcut for the "Status" header
444       from RFC 3875 <http://tools.ietf.org/html/rfc3875>.
445
446   strict_transport_security
447         my $policy = $headers->strict_transport_security;
448         $headers   = $headers->strict_transport_security('max-age=31536000');
449
450       Get or replace current header value, shortcut for the
451       "Strict-Transport-Security" header from RFC 6797
452       <http://tools.ietf.org/html/rfc6797>.
453
454   te
455         my $te   = $headers->te;
456         $headers = $headers->te('chunked');
457
458       Get or replace current header value, shortcut for the "TE" header.
459
460   to_hash
461         my $single = $headers->to_hash;
462         my $multi  = $headers->to_hash(1);
463
464       Turn headers into hash reference, array references to represent
465       multiple headers with the same name are disabled by default.
466
467         say $headers->to_hash->{DNT};
468
469   to_string
470         my $str = $headers->to_string;
471
472       Turn headers into a string, suitable for HTTP messages.
473
474   trailer
475         my $trailer = $headers->trailer;
476         $headers    = $headers->trailer('X-Foo');
477
478       Get or replace current header value, shortcut for the "Trailer" header.
479
480   transfer_encoding
481         my $encoding = $headers->transfer_encoding;
482         $headers     = $headers->transfer_encoding('chunked');
483
484       Get or replace current header value, shortcut for the
485       "Transfer-Encoding" header.
486
487   upgrade
488         my $upgrade = $headers->upgrade;
489         $headers    = $headers->upgrade('websocket');
490
491       Get or replace current header value, shortcut for the "Upgrade" header.
492
493   user_agent
494         my $agent = $headers->user_agent;
495         $headers  = $headers->user_agent('Mojo/1.0');
496
497       Get or replace current header value, shortcut for the "User-Agent"
498       header.
499
500   vary
501         my $vary = $headers->vary;
502         $headers = $headers->vary('*');
503
504       Get or replace current header value, shortcut for the "Vary" header.
505
506   www_authenticate
507         my $authenticate = $headers->www_authenticate;
508         $headers         = $headers->www_authenticate('Basic realm="realm"');
509
510       Get or replace current header value, shortcut for the
511       "WWW-Authenticate" header.
512

SEE ALSO

514       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
515
516
517
518perl v5.30.1                      2020-01-30                  Mojo::Headers(3)
Impressum