Skip to content

Commit d11b23e

Browse files
authored
Merge pull request #1913 from karenetheridge/ether/header-docs
separate header shortcuts from other accessors
2 parents 2c8d1b6 + fab6ab9 commit d11b23e

File tree

1 file changed

+120
-115
lines changed

1 file changed

+120
-115
lines changed

lib/Mojo/Headers.pm

Lines changed: 120 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,126 @@ Maximum number of header lines, defaults to the value of the C<MOJO_MAX_LINES> e
236236
237237
L<Mojo::Headers> inherits all methods from L<Mojo::Base> and implements the following new ones.
238238
239+
=head2 add
240+
241+
$headers = $headers->add(Foo => 'one value');
242+
$headers = $headers->add(Foo => 'first value', 'second value');
243+
244+
Add header with one or more lines.
245+
246+
# "Vary: Accept
247+
# Vary: Accept-Encoding"
248+
$headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string;
249+
250+
=head2 append
251+
252+
$headers = $headers->append(Vary => 'Accept-Encoding');
253+
254+
Append value to header and flatten it if necessary.
255+
256+
# "Vary: Accept"
257+
$headers->append(Vary => 'Accept')->to_string;
258+
259+
# "Vary: Accept, Accept-Encoding"
260+
$headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string;
261+
262+
=head2 clone
263+
264+
my $clone = $headers->clone;
265+
266+
Return a new L<Mojo::Headers> object cloned from these headers.
267+
268+
=head2 dehop
269+
270+
$headers = $headers->dehop;
271+
272+
Remove hop-by-hop headers that should not be retransmitted.
273+
274+
=head2 every_header
275+
276+
my $all = $headers->every_header('Location');
277+
278+
Similar to L</"header">, but returns all headers sharing the same name as an array reference.
279+
280+
# Get first header value
281+
say $headers->every_header('Location')->[0];
282+
283+
=head2 from_hash
284+
285+
$headers = $headers->from_hash({'Cookie' => 'a=b'});
286+
$headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
287+
$headers = $headers->from_hash({});
288+
289+
Parse headers from a hash reference, an empty hash removes all headers.
290+
291+
=head2 header
292+
293+
my $value = $headers->header('Foo');
294+
$headers = $headers->header(Foo => 'one value');
295+
$headers = $headers->header(Foo => 'first value', 'second value');
296+
297+
Get or replace the current header values.
298+
299+
=head2 is_finished
300+
301+
my $bool = $headers->is_finished;
302+
303+
Check if header parser is finished.
304+
305+
=head2 is_limit_exceeded
306+
307+
my $bool = $headers->is_limit_exceeded;
308+
309+
Check if headers have exceeded L</"max_line_size"> or L</"max_lines">.
310+
311+
=head2 leftovers
312+
313+
my $bytes = $headers->leftovers;
314+
315+
Get and remove leftover data from header parser.
316+
317+
=head2 names
318+
319+
my $names = $headers->names;
320+
321+
Return an array reference with all currently defined headers.
322+
323+
# Names of all headers
324+
say for @{$headers->names};
325+
326+
=head2 parse
327+
328+
$headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
329+
330+
Parse formatted headers.
331+
332+
=head2 remove
333+
334+
$headers = $headers->remove('Foo');
335+
336+
Remove a header.
337+
338+
=head2 to_hash
339+
340+
my $single = $headers->to_hash;
341+
my $multi = $headers->to_hash(1);
342+
343+
Turn headers into hash reference, array references to represent multiple headers with the same name are disabled by
344+
default.
345+
346+
say $headers->to_hash->{DNT};
347+
348+
=head2 to_string
349+
350+
my $str = $headers->to_string;
351+
352+
Turn headers into a string, suitable for HTTP messages.
353+
354+
355+
=head1 ADDITIONAL METHODS
356+
357+
Additionally, the following shortcuts are available, for accessing and manipulating commonly-used headers:
358+
239359
=head2 accept
240360
241361
my $accept = $headers->accept;
@@ -279,36 +399,13 @@ Get or replace current header value, shortcut for the C<Accept-Ranges> header.
279399
Get or replace current header value, shortcut for the C<Access-Control-Allow-Origin> header from L<Cross-Origin
280400
Resource Sharing|https://www.w3.org/TR/cors/>.
281401
282-
=head2 add
283-
284-
$headers = $headers->add(Foo => 'one value');
285-
$headers = $headers->add(Foo => 'first value', 'second value');
286-
287-
Add header with one or more lines.
288-
289-
# "Vary: Accept
290-
# Vary: Accept-Encoding"
291-
$headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string;
292-
293402
=head2 allow
294403
295404
my $allow = $headers->allow;
296405
$headers = $headers->allow('GET, POST');
297406
298407
Get or replace current header value, shortcut for the C<Allow> header.
299408
300-
=head2 append
301-
302-
$headers = $headers->append(Vary => 'Accept-Encoding');
303-
304-
Append value to header and flatten it if necessary.
305-
306-
# "Vary: Accept"
307-
$headers->append(Vary => 'Accept')->to_string;
308-
309-
# "Vary: Accept, Accept-Encoding"
310-
$headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string;
311-
312409
=head2 authorization
313410
314411
my $authorization = $headers->authorization;
@@ -323,12 +420,6 @@ Get or replace current header value, shortcut for the C<Authorization> header.
323420
324421
Get or replace current header value, shortcut for the C<Cache-Control> header.
325422
326-
=head2 clone
327-
328-
my $clone = $headers->clone;
329-
330-
Return a new L<Mojo::Headers> object cloned from these headers.
331-
332423
=head2 connection
333424
334425
my $connection = $headers->connection;
@@ -408,12 +499,6 @@ Get or replace current header value, shortcut for the C<Cookie> header from L<RF
408499
409500
Get or replace current header value, shortcut for the C<Date> header.
410501
411-
=head2 dehop
412-
413-
$headers = $headers->dehop;
414-
415-
Remove hop-by-hop headers that should not be retransmitted.
416-
417502
=head2 dnt
418503
419504
my $dnt = $headers->dnt;
@@ -429,15 +514,6 @@ is very commonly used.
429514
430515
Get or replace current header value, shortcut for the C<ETag> header.
431516
432-
=head2 every_header
433-
434-
my $all = $headers->every_header('Location');
435-
436-
Similar to L</"header">, but returns all headers sharing the same name as an array reference.
437-
438-
# Get first header value
439-
say $headers->every_header('Location')->[0];
440-
441517
=head2 expect
442518
443519
my $expect = $headers->expect;
@@ -452,22 +528,6 @@ Get or replace current header value, shortcut for the C<Expect> header.
452528
453529
Get or replace current header value, shortcut for the C<Expires> header.
454530
455-
=head2 from_hash
456-
457-
$headers = $headers->from_hash({'Cookie' => 'a=b'});
458-
$headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
459-
$headers = $headers->from_hash({});
460-
461-
Parse headers from a hash reference, an empty hash removes all headers.
462-
463-
=head2 header
464-
465-
my $value = $headers->header('Foo');
466-
$headers = $headers->header(Foo => 'one value');
467-
$headers = $headers->header(Foo => 'first value', 'second value');
468-
469-
Get or replace the current header values.
470-
471531
=head2 host
472532
473533
my $host = $headers->host;
@@ -489,31 +549,13 @@ Get or replace current header value, shortcut for the C<If-Modified-Since> heade
489549
490550
Get or replace current header value, shortcut for the C<If-None-Match> header.
491551
492-
=head2 is_finished
493-
494-
my $bool = $headers->is_finished;
495-
496-
Check if header parser is finished.
497-
498-
=head2 is_limit_exceeded
499-
500-
my $bool = $headers->is_limit_exceeded;
501-
502-
Check if headers have exceeded L</"max_line_size"> or L</"max_lines">.
503-
504552
=head2 last_modified
505553
506554
my $date = $headers->last_modified;
507555
$headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT');
508556
509557
Get or replace current header value, shortcut for the C<Last-Modified> header.
510558
511-
=head2 leftovers
512-
513-
my $bytes = $headers->leftovers;
514-
515-
Get and remove leftover data from header parser.
516-
517559
=head2 link
518560
519561
my $link = $headers->link;
@@ -540,15 +582,6 @@ Get or set web links from or to C<Link> header according to L<RFC 5988|http://to
540582
541583
Get or replace current header value, shortcut for the C<Location> header.
542584
543-
=head2 names
544-
545-
my $names = $headers->names;
546-
547-
Return an array reference with all currently defined headers.
548-
549-
# Names of all headers
550-
say for @{$headers->names};
551-
552585
=head2 origin
553586
554587
my $origin = $headers->origin;
@@ -557,12 +590,6 @@ Return an array reference with all currently defined headers.
557590
Get or replace current header value, shortcut for the C<Origin> header from L<RFC
558591
6454|https://tools.ietf.org/html/rfc6454>.
559592
560-
=head2 parse
561-
562-
$headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
563-
564-
Parse formatted headers.
565-
566593
=head2 proxy_authenticate
567594
568595
my $authenticate = $headers->proxy_authenticate;
@@ -599,12 +626,6 @@ Alias for L</"referrer">.
599626
Get or replace current header value, shortcut for the C<Referer> header, there was a typo in L<RFC
600627
2068|https://tools.ietf.org/html/rfc2068> which resulted in C<Referer> becoming an official header.
601628
602-
=head2 remove
603-
604-
$headers = $headers->remove('Foo');
605-
606-
Remove a header.
607-
608629
=head2 sec_websocket_accept
609630
610631
my $accept = $headers->sec_websocket_accept;
@@ -691,22 +712,6 @@ Get or replace current header value, shortcut for the C<Strict-Transport-Securit
691712
692713
Get or replace current header value, shortcut for the C<TE> header.
693714
694-
=head2 to_hash
695-
696-
my $single = $headers->to_hash;
697-
my $multi = $headers->to_hash(1);
698-
699-
Turn headers into hash reference, array references to represent multiple headers with the same name are disabled by
700-
default.
701-
702-
say $headers->to_hash->{DNT};
703-
704-
=head2 to_string
705-
706-
my $str = $headers->to_string;
707-
708-
Turn headers into a string, suitable for HTTP messages.
709-
710715
=head2 trailer
711716
712717
my $trailer = $headers->trailer;

0 commit comments

Comments
 (0)