1Mojo::Headers(3) User Contributed Perl Documentation Mojo::Headers(3)
2
3
4
6 Mojo::Headers - HTTP headers
7
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
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
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
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 $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 <http://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 <http://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 referrer
366 my $referrer = $headers->referrer;
367 $headers = $headers->referrer('http://example.com');
368
369 Get or replace current header value, shortcut for the "Referer" header,
370 there was a typo in RFC 2068 <http://tools.ietf.org/html/rfc2068> which
371 resulted in "Referer" becoming an official header.
372
373 remove
374 $headers = $headers->remove('Foo');
375
376 Remove a header.
377
378 sec_websocket_accept
379 my $accept = $headers->sec_websocket_accept;
380 $headers = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
381
382 Get or replace current header value, shortcut for the
383 "Sec-WebSocket-Accept" header from RFC 6455
384 <http://tools.ietf.org/html/rfc6455>.
385
386 sec_websocket_extensions
387 my $extensions = $headers->sec_websocket_extensions;
388 $headers = $headers->sec_websocket_extensions('foo');
389
390 Get or replace current header value, shortcut for the
391 "Sec-WebSocket-Extensions" header from RFC 6455
392 <http://tools.ietf.org/html/rfc6455>.
393
394 sec_websocket_key
395 my $key = $headers->sec_websocket_key;
396 $headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
397
398 Get or replace current header value, shortcut for the
399 "Sec-WebSocket-Key" header from RFC 6455
400 <http://tools.ietf.org/html/rfc6455>.
401
402 sec_websocket_protocol
403 my $proto = $headers->sec_websocket_protocol;
404 $headers = $headers->sec_websocket_protocol('sample');
405
406 Get or replace current header value, shortcut for the
407 "Sec-WebSocket-Protocol" header from RFC 6455
408 <http://tools.ietf.org/html/rfc6455>.
409
410 sec_websocket_version
411 my $version = $headers->sec_websocket_version;
412 $headers = $headers->sec_websocket_version(13);
413
414 Get or replace current header value, shortcut for the
415 "Sec-WebSocket-Version" header from RFC 6455
416 <http://tools.ietf.org/html/rfc6455>.
417
418 server
419 my $server = $headers->server;
420 $headers = $headers->server('Mojo');
421
422 Get or replace current header value, shortcut for the "Server" header.
423
424 server_timing
425 my $timing = $headers->server_timing;
426 $headers = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
427
428 Get or replace current header value, shortcut for the "Server-Timing"
429 header from Server Timing <https://www.w3.org/TR/server-timing/>.
430
431 set_cookie
432 my $cookie = $headers->set_cookie;
433 $headers = $headers->set_cookie('f=b; path=/');
434
435 Get or replace current header value, shortcut for the "Set-Cookie"
436 header from RFC 6265 <http://tools.ietf.org/html/rfc6265>.
437
438 status
439 my $status = $headers->status;
440 $headers = $headers->status('200 OK');
441
442 Get or replace current header value, shortcut for the "Status" header
443 from RFC 3875 <http://tools.ietf.org/html/rfc3875>.
444
445 strict_transport_security
446 my $policy = $headers->strict_transport_security;
447 $headers = $headers->strict_transport_security('max-age=31536000');
448
449 Get or replace current header value, shortcut for the
450 "Strict-Transport-Security" header from RFC 6797
451 <http://tools.ietf.org/html/rfc6797>.
452
453 te
454 my $te = $headers->te;
455 $headers = $headers->te('chunked');
456
457 Get or replace current header value, shortcut for the "TE" header.
458
459 to_hash
460 my $single = $headers->to_hash;
461 my $multi = $headers->to_hash(1);
462
463 Turn headers into hash reference, array references to represent
464 multiple headers with the same name are disabled by default.
465
466 say $headers->to_hash->{DNT};
467
468 to_string
469 my $str = $headers->to_string;
470
471 Turn headers into a string, suitable for HTTP messages.
472
473 trailer
474 my $trailer = $headers->trailer;
475 $headers = $headers->trailer('X-Foo');
476
477 Get or replace current header value, shortcut for the "Trailer" header.
478
479 transfer_encoding
480 my $encoding = $headers->transfer_encoding;
481 $headers = $headers->transfer_encoding('chunked');
482
483 Get or replace current header value, shortcut for the
484 "Transfer-Encoding" header.
485
486 upgrade
487 my $upgrade = $headers->upgrade;
488 $headers = $headers->upgrade('websocket');
489
490 Get or replace current header value, shortcut for the "Upgrade" header.
491
492 user_agent
493 my $agent = $headers->user_agent;
494 $headers = $headers->user_agent('Mojo/1.0');
495
496 Get or replace current header value, shortcut for the "User-Agent"
497 header.
498
499 vary
500 my $vary = $headers->vary;
501 $headers = $headers->vary('*');
502
503 Get or replace current header value, shortcut for the "Vary" header.
504
505 www_authenticate
506 my $authenticate = $headers->www_authenticate;
507 $headers = $headers->www_authenticate('Basic realm="realm"');
508
509 Get or replace current header value, shortcut for the
510 "WWW-Authenticate" header.
511
513 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
514
515
516
517perl v5.32.0 2020-07-28 Mojo::Headers(3)