/
Headers.pm
761 lines (478 loc) · 20.3 KB
/
Headers.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package Mojo::Headers;
use Mojo::Base -base;
use Carp qw(croak);
use Mojo::Util qw(header_params monkey_patch);
has max_line_size => sub { $ENV{MOJO_MAX_LINE_SIZE} || 8192 };
has max_lines => sub { $ENV{MOJO_MAX_LINES} || 100 };
# Common headers
my %NAMES = map { lc() => $_ } (
qw(Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges Access-Control-Allow-Origin Allow),
qw(Authorization Cache-Control Connection Content-Disposition Content-Encoding Content-Language Content-Length),
qw(Content-Location Content-Range Content-Security-Policy Content-Type Cookie DNT Date ETag Expect Expires Host),
qw(If-Modified-Since If-None-Match Last-Modified Link Location Origin Proxy-Authenticate Proxy-Authorization),
qw(Range Sec-WebSocket-Accept Sec-WebSocket-Extensions Sec-WebSocket-Key Sec-WebSocket-Protocol),
qw(Sec-WebSocket-Version Server Server-Timing Set-Cookie Status Strict-Transport-Security TE Trailer),
qw(Transfer-Encoding Upgrade User-Agent Vary WWW-Authenticate)
);
for my $header (keys %NAMES) {
my $name = $header;
$name =~ y/-/_/;
monkey_patch __PACKAGE__, $name, sub {
my $self = shift;
$self->{headers}{$header} = [@_] and return $self if @_;
return undef unless my $headers = $self->{headers}{$header};
return join ', ', @$headers;
};
}
# Hop-by-hop headers
my @HOP_BY_HOP
= map {lc} qw(Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailer Transfer-Encoding Upgrade);
sub add {
my ($self, $name) = (shift, shift);
tr/\x0d\x0a// and croak "Invalid characters in $name header" for @_;
# Make sure we have a normal case entry for name
my $key = lc $name;
$self->{names}{$key} //= $name unless $NAMES{$key};
push @{$self->{headers}{$key}}, @_;
return $self;
}
sub append {
my ($self, $name, $value) = @_;
my $old = $self->header($name);
return $self->header($name => defined $old ? "$old, $value" : $value);
}
sub clone {
my $self = shift;
my $clone = $self->new;
%{$clone->{names}} = %{$self->{names} // {}};
@{$clone->{headers}{$_}} = @{$self->{headers}{$_}} for keys %{$self->{headers}};
return $clone;
}
sub dehop {
my $self = shift;
delete @{$self->{headers}}{@HOP_BY_HOP};
return $self;
}
sub every_header { shift->{headers}{lc shift} // [] }
sub from_hash {
my ($self, $hash) = @_;
# Empty hash deletes all headers
delete $self->{headers} if keys %{$hash} == 0;
# Merge
for my $header (keys %$hash) {
my $value = $hash->{$header};
$self->add($header => ref $value eq 'ARRAY' ? @$value : $value);
}
return $self;
}
sub header {
my ($self, $name) = (shift, shift);
# Replace
return $self->remove($name)->add($name, @_) if @_;
return undef unless my $headers = $self->{headers}{lc $name};
return join ', ', @$headers;
}
sub is_finished { (shift->{state} // '') eq 'finished' }
sub is_limit_exceeded { !!shift->{limit} }
sub leftovers { delete shift->{buffer} }
sub links {
my ($self, $links) = @_;
return $self->link(join(', ', map {qq{<$links->{$_}>; rel="$_"}} sort keys %$links)) if $links;
my $header = $self->link // '';
my $data = {};
while ($header =~ s/^[,\s]*<(.+?)>//) {
my $target = $1;
(my $params, $header) = header_params $header;
$data->{$params->{rel}} //= {%$params, link => $target} if defined $params->{rel};
}
return $data;
}
sub names {
my $self = shift;
return [map { $NAMES{$_} || $self->{names}{$_} } sort keys %{$self->{headers}}];
}
sub parse {
my ($self, $chunk) = @_;
$self->{state} = 'headers';
$self->{buffer} .= $chunk;
my $headers = $self->{cache} //= [];
my $size = $self->max_line_size;
my $lines = $self->max_lines;
while ($self->{buffer} =~ s/^(.*?)\x0d?\x0a//) {
my $line = $1;
# Check line size limit
if ($+[0] > $size || @$headers >= $lines) {
@$self{qw(state limit)} = ('finished', 1);
return $self;
}
# New header
if ($line =~ /^(\S[^:]*):\s*(.*)$/) { push @$headers, [$1, $2] }
# Multi-line
elsif ($line =~ s/^\s+// && @$headers) { $headers->[-1][1] .= " $line" }
# Empty line
else {
$self->add(@$_) for @$headers;
@$self{qw(state cache)} = ('finished', []);
return $self;
}
}
# Check line size limit
@$self{qw(state limit)} = ('finished', 1) if length $self->{buffer} > $size;
return $self;
}
sub referer { shift->referrer(@_) }
sub referrer { shift->header(Referer => @_) }
sub remove {
my ($self, $name) = @_;
delete $self->{headers}{lc $name};
return $self;
}
sub to_hash {
my ($self, $multi) = @_;
return {map { $_ => $self->{headers}{lc $_} } @{$self->names}} if $multi;
return {map { $_ => $self->header($_) } @{$self->names}};
}
sub to_string {
my $self = shift;
# Make sure multi-line values are formatted correctly
my @headers;
for my $name (@{$self->names}) { push @headers, "$name: $_" for @{$self->{headers}{lc $name}} }
return join "\x0d\x0a", @headers;
}
1;
=encoding utf8
=head1 NAME
Mojo::Headers - HTTP headers
=head1 SYNOPSIS
use Mojo::Headers;
# Parse
my $headers = Mojo::Headers->new;
$headers->parse("Content-Length: 42\x0d\x0a");
$headers->parse("Content-Type: text/html\x0d\x0a\x0d\x0a");
say $headers->content_length;
say $headers->content_type;
# Build
my $headers = Mojo::Headers->new;
$headers->content_length(42);
$headers->content_type('text/plain');
say $headers->to_string;
=head1 DESCRIPTION
L<Mojo::Headers> is a container for HTTP headers, based on L<RFC 7230|https://tools.ietf.org/html/rfc7230> and L<RFC
7231|https://tools.ietf.org/html/rfc7231>.
=head1 ATTRIBUTES
L<Mojo::Headers> implements the following attributes.
=head2 max_line_size
my $size = $headers->max_line_size;
$headers = $headers->max_line_size(1024);
Maximum header line size in bytes, defaults to the value of the C<MOJO_MAX_LINE_SIZE> environment variable or C<8192>
(8KiB).
=head2 max_lines
my $num = $headers->max_lines;
$headers = $headers->max_lines(200);
Maximum number of header lines, defaults to the value of the C<MOJO_MAX_LINES> environment variable or C<100>.
=head1 METHODS
L<Mojo::Headers> inherits all methods from L<Mojo::Base> and implements the following new ones.
=head2 add
$headers = $headers->add(Foo => 'one value');
$headers = $headers->add(Foo => 'first value', 'second value');
Add header with one or more lines.
# "Vary: Accept
# Vary: Accept-Encoding"
$headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string;
=head2 append
$headers = $headers->append(Vary => 'Accept-Encoding');
Append value to header and flatten it if necessary.
# "Vary: Accept"
$headers->append(Vary => 'Accept')->to_string;
# "Vary: Accept, Accept-Encoding"
$headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string;
=head2 clone
my $clone = $headers->clone;
Return a new L<Mojo::Headers> object cloned from these headers.
=head2 dehop
$headers = $headers->dehop;
Remove hop-by-hop headers that should not be retransmitted.
=head2 every_header
my $all = $headers->every_header('Location');
Similar to L</"header">, but returns all headers sharing the same name as an array reference.
# Get first header value
say $headers->every_header('Location')->[0];
=head2 from_hash
$headers = $headers->from_hash({'Cookie' => 'a=b'});
$headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
$headers = $headers->from_hash({});
Parse headers from a hash reference, an empty hash removes all headers.
=head2 header
my $value = $headers->header('Foo');
$headers = $headers->header(Foo => 'one value');
$headers = $headers->header(Foo => 'first value', 'second value');
Get or replace the current header values.
=head2 is_finished
my $bool = $headers->is_finished;
Check if header parser is finished.
=head2 is_limit_exceeded
my $bool = $headers->is_limit_exceeded;
Check if headers have exceeded L</"max_line_size"> or L</"max_lines">.
=head2 leftovers
my $bytes = $headers->leftovers;
Get and remove leftover data from header parser.
=head2 names
my $names = $headers->names;
Return an array reference with all currently defined headers.
# Names of all headers
say for @{$headers->names};
=head2 parse
$headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
Parse formatted headers.
=head2 remove
$headers = $headers->remove('Foo');
Remove a header.
=head2 to_hash
my $single = $headers->to_hash;
my $multi = $headers->to_hash(1);
Turn headers into hash reference, array references to represent multiple headers with the same name are disabled by
default.
say $headers->to_hash->{DNT};
=head2 to_string
my $str = $headers->to_string;
Turn headers into a string, suitable for HTTP messages.
=head1 ADDITIONAL METHODS
Additionally, the following shortcuts are available, for accessing and manipulating commonly-used headers:
=head2 accept
my $accept = $headers->accept;
$headers = $headers->accept('application/json');
Get or replace current header value, shortcut for the C<Accept> header.
=head2 accept_charset
my $charset = $headers->accept_charset;
$headers = $headers->accept_charset('UTF-8');
Get or replace current header value, shortcut for the C<Accept-Charset> header.
=head2 accept_encoding
my $encoding = $headers->accept_encoding;
$headers = $headers->accept_encoding('gzip');
Get or replace current header value, shortcut for the C<Accept-Encoding> header.
=head2 accept_language
my $language = $headers->accept_language;
$headers = $headers->accept_language('de, en');
Get or replace current header value, shortcut for the C<Accept-Language> header.
=head2 accept_ranges
my $ranges = $headers->accept_ranges;
$headers = $headers->accept_ranges('bytes');
Get or replace current header value, shortcut for the C<Accept-Ranges> header.
=head2 access_control_allow_origin
my $origin = $headers->access_control_allow_origin;
$headers = $headers->access_control_allow_origin('*');
Get or replace current header value, shortcut for the C<Access-Control-Allow-Origin> header from L<Cross-Origin
Resource Sharing|https://www.w3.org/TR/cors/>.
=head2 allow
my $allow = $headers->allow;
$headers = $headers->allow('GET, POST');
Get or replace current header value, shortcut for the C<Allow> header.
=head2 authorization
my $authorization = $headers->authorization;
$headers = $headers->authorization('Basic Zm9vOmJhcg==');
Get or replace current header value, shortcut for the C<Authorization> header.
=head2 cache_control
my $cache_control = $headers->cache_control;
$headers = $headers->cache_control('max-age=1, no-cache');
Get or replace current header value, shortcut for the C<Cache-Control> header.
=head2 connection
my $connection = $headers->connection;
$headers = $headers->connection('close');
Get or replace current header value, shortcut for the C<Connection> header.
=head2 content_disposition
my $disposition = $headers->content_disposition;
$headers = $headers->content_disposition('foo');
Get or replace current header value, shortcut for the C<Content-Disposition> header.
=head2 content_encoding
my $encoding = $headers->content_encoding;
$headers = $headers->content_encoding('gzip');
Get or replace current header value, shortcut for the C<Content-Encoding> header.
=head2 content_language
my $language = $headers->content_language;
$headers = $headers->content_language('en');
Get or replace current header value, shortcut for the C<Content-Language> header.
=head2 content_length
my $len = $headers->content_length;
$headers = $headers->content_length(4000);
Get or replace current header value, shortcut for the C<Content-Length> header.
=head2 content_location
my $location = $headers->content_location;
$headers = $headers->content_location('http://127.0.0.1/foo');
Get or replace current header value, shortcut for the C<Content-Location> header.
=head2 content_range
my $range = $headers->content_range;
$headers = $headers->content_range('bytes 2-8/100');
Get or replace current header value, shortcut for the C<Content-Range> header.
=head2 content_security_policy
my $policy = $headers->content_security_policy;
$headers = $headers->content_security_policy('default-src https:');
Get or replace current header value, shortcut for the C<Content-Security-Policy> header from L<Content Security Policy
1.0|https://www.w3.org/TR/CSP/>.
=head2 content_type
my $type = $headers->content_type;
$headers = $headers->content_type('text/plain');
Get or replace current header value, shortcut for the C<Content-Type> header.
=head2 cookie
my $cookie = $headers->cookie;
$headers = $headers->cookie('f=b');
Get or replace current header value, shortcut for the C<Cookie> header from L<RFC
6265|https://tools.ietf.org/html/rfc6265>.
=head2 date
my $date = $headers->date;
$headers = $headers->date('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace current header value, shortcut for the C<Date> header.
=head2 dnt
my $dnt = $headers->dnt;
$headers = $headers->dnt(1);
Get or replace current header value, shortcut for the C<DNT> (Do Not Track) header, which has no specification yet, but
is very commonly used.
=head2 etag
my $etag = $headers->etag;
$headers = $headers->etag('"abc321"');
Get or replace current header value, shortcut for the C<ETag> header.
=head2 expect
my $expect = $headers->expect;
$headers = $headers->expect('100-continue');
Get or replace current header value, shortcut for the C<Expect> header.
=head2 expires
my $expires = $headers->expires;
$headers = $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT');
Get or replace current header value, shortcut for the C<Expires> header.
=head2 host
my $host = $headers->host;
$headers = $headers->host('127.0.0.1');
Get or replace current header value, shortcut for the C<Host> header.
=head2 if_modified_since
my $date = $headers->if_modified_since;
$headers = $headers->if_modified_since('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace current header value, shortcut for the C<If-Modified-Since> header.
=head2 if_none_match
my $etag = $headers->if_none_match;
$headers = $headers->if_none_match('"abc321"');
Get or replace current header value, shortcut for the C<If-None-Match> header.
=head2 last_modified
my $date = $headers->last_modified;
$headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace current header value, shortcut for the C<Last-Modified> header.
=head2 link
my $link = $headers->link;
$headers = $headers->link('<http://127.0.0.1/foo/3>; rel="next"');
Get or replace current header value, shortcut for the C<Link> header from L<RFC
5988|https://tools.ietf.org/html/rfc5988>.
=head2 links
my $links = $headers->links;
$headers = $headers->links({next => 'http://example.com/foo', prev => 'http://example.com/bar'});
Get or set web links from or to C<Link> header according to L<RFC 5988|http://tools.ietf.org/html/rfc5988>.
# Extract information about next page
say $headers->links->{next}{link};
say $headers->links->{next}{title};
=head2 location
my $location = $headers->location;
$headers = $headers->location('http://127.0.0.1/foo');
Get or replace current header value, shortcut for the C<Location> header.
=head2 origin
my $origin = $headers->origin;
$headers = $headers->origin('http://example.com');
Get or replace current header value, shortcut for the C<Origin> header from L<RFC
6454|https://tools.ietf.org/html/rfc6454>.
=head2 proxy_authenticate
my $authenticate = $headers->proxy_authenticate;
$headers = $headers->proxy_authenticate('Basic "realm"');
Get or replace current header value, shortcut for the C<Proxy-Authenticate> header.
=head2 proxy_authorization
my $authorization = $headers->proxy_authorization;
$headers = $headers->proxy_authorization('Basic Zm9vOmJhcg==');
Get or replace current header value, shortcut for the C<Proxy-Authorization> header.
=head2 range
my $range = $headers->range;
$headers = $headers->range('bytes=2-8');
Get or replace current header value, shortcut for the C<Range> header.
=head2 referer
my $referrer = $headers->referer;
$headers = $headers->referer('http://example.com');
Alias for L</"referrer">.
=head2 referrer
my $referrer = $headers->referrer;
$headers = $headers->referrer('http://example.com');
Get or replace current header value, shortcut for the C<Referer> header, there was a typo in L<RFC
2068|https://tools.ietf.org/html/rfc2068> which resulted in C<Referer> becoming an official header.
=head2 sec_websocket_accept
my $accept = $headers->sec_websocket_accept;
$headers = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
Get or replace current header value, shortcut for the C<Sec-WebSocket-Accept> header from L<RFC
6455|https://tools.ietf.org/html/rfc6455>.
=head2 sec_websocket_extensions
my $extensions = $headers->sec_websocket_extensions;
$headers = $headers->sec_websocket_extensions('foo');
Get or replace current header value, shortcut for the C<Sec-WebSocket-Extensions> header from L<RFC
6455|https://tools.ietf.org/html/rfc6455>.
=head2 sec_websocket_key
my $key = $headers->sec_websocket_key;
$headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
Get or replace current header value, shortcut for the C<Sec-WebSocket-Key> header from L<RFC
6455|https://tools.ietf.org/html/rfc6455>.
=head2 sec_websocket_protocol
my $proto = $headers->sec_websocket_protocol;
$headers = $headers->sec_websocket_protocol('sample');
Get or replace current header value, shortcut for the C<Sec-WebSocket-Protocol> header from L<RFC
6455|https://tools.ietf.org/html/rfc6455>.
=head2 sec_websocket_version
my $version = $headers->sec_websocket_version;
$headers = $headers->sec_websocket_version(13);
Get or replace current header value, shortcut for the C<Sec-WebSocket-Version> header from L<RFC
6455|https://tools.ietf.org/html/rfc6455>.
=head2 server
my $server = $headers->server;
$headers = $headers->server('Mojo');
Get or replace current header value, shortcut for the C<Server> header.
=head2 server_timing
my $timing = $headers->server_timing;
$headers = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
Get or replace current header value, shortcut for the C<Server-Timing> header from L<Server
Timing|https://www.w3.org/TR/server-timing/>.
=head2 set_cookie
my $cookie = $headers->set_cookie;
$headers = $headers->set_cookie('f=b; path=/');
Get or replace current header value, shortcut for the C<Set-Cookie> header from L<RFC
6265|https://tools.ietf.org/html/rfc6265>.
=head2 status
my $status = $headers->status;
$headers = $headers->status('200 OK');
Get or replace current header value, shortcut for the C<Status> header from L<RFC
3875|https://tools.ietf.org/html/rfc3875>.
=head2 strict_transport_security
my $policy = $headers->strict_transport_security;
$headers = $headers->strict_transport_security('max-age=31536000');
Get or replace current header value, shortcut for the C<Strict-Transport-Security> header from L<RFC
6797|https://tools.ietf.org/html/rfc6797>.
=head2 te
my $te = $headers->te;
$headers = $headers->te('chunked');
Get or replace current header value, shortcut for the C<TE> header.
=head2 trailer
my $trailer = $headers->trailer;
$headers = $headers->trailer('X-Foo');
Get or replace current header value, shortcut for the C<Trailer> header.
=head2 transfer_encoding
my $encoding = $headers->transfer_encoding;
$headers = $headers->transfer_encoding('chunked');
Get or replace current header value, shortcut for the C<Transfer-Encoding> header.
=head2 upgrade
my $upgrade = $headers->upgrade;
$headers = $headers->upgrade('websocket');
Get or replace current header value, shortcut for the C<Upgrade> header.
=head2 user_agent
my $agent = $headers->user_agent;
$headers = $headers->user_agent('Mojo/1.0');
Get or replace current header value, shortcut for the C<User-Agent> header.
=head2 vary
my $vary = $headers->vary;
$headers = $headers->vary('*');
Get or replace current header value, shortcut for the C<Vary> header.
=head2 www_authenticate
my $authenticate = $headers->www_authenticate;
$headers = $headers->www_authenticate('Basic realm="realm"');
Get or replace current header value, shortcut for the C<WWW-Authenticate> header.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut