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

SEE ALSO

508       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
509
510
511
512perl v5.28.0                      2018-09-09                  Mojo::Headers(3)
Impressum