blib/lib/HTTP/Promise/Headers.pm untested

filter: Statementstmt 0.0% 0 / 1152 Branchbran 0.0% 0 / 566 Conditioncond 0.0% 0 / 515 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 206 Podpod 0.0% 0 / 159 totaltotal 0.0% 0 / 2598 CC 284 SCAR 53.5
CC
284
Cov
0%
error
1122.0
message
1056.0
pass_error
420.0
CRAP 209.9
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers.pm
3 ## Version v0.3.2
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/21
7 ## Modified 2025/10/25
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Headers;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( HTTP::XSHeaders );
21 use vars qw( $VERSION $EXCEPTION_CLASS $MOD_PERL $SUPPORTED $MOD_PATH );
22 use Cwd ();
23 use Encode;
24 use HTTP::Promise::Exception;
25 use HTTP::XSHeaders 0.400004;
26 use IO::File;
27 use Module::Generic::Global qw( :const );
28 use Scalar::Util;
29 use URI::Escape::XS ();
30 use Wanted;
31 if( exists( $ENV{MOD_PERL} )
32 &&
33 ( $MOD_PERL = $ENV{MOD_PERL} =~ /^mod_perl\/(\d+\.[\d\.]+)/ ) )
34 {
35 select( ( select( STDOUT ), $| = 1 )[ 0 ] );
36 require Apache2::Log;
37 # For _is_class_loaded method
38 require Apache2::Module;
39 require Apache2::ServerUtil;
40 require Apache2::RequestUtil;
41 require Apache2::ServerRec;
42 require ModPerl::Util;
43 require Apache2::Const;
44 Apache2::Const->import( compile => qw( :log OK ) );
45 }
46 use constant CRLF => "\015\012";
47 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
48 our $SUPPORTED = {};
49 our $VERSION = 'v0.3.2';
50 };
51  
52 use strict;
53 use warnings;
54  
55 my $stderr = IO::File->new;
56 $stderr->fdopen( fileno( STDERR ), 'w' );
57 $stderr->binmode( ':utf8' );
58 $stderr->autoflush( 1 );
59 my $stderr_raw = IO::File->new;
60 $stderr_raw->fdopen( fileno( STDERR ), 'w' );
61 $stderr_raw->binmode( ':raw' );
62 $stderr_raw->autoflush( 1 );
63  
64 our $MOD_PATH = Cwd::abs_path( $INC{ ( __PACKAGE__ =~ s{::}{/}gr ) . '.pm' } );
65  
66 # for mod in `ls -1 ./lib/HTTP/Promise/Headers`; do printf "%-32s => 'HTTP::Promise::Headers::%s',\n" $(echo $(basename $mod ".pm")|tr "[:upper:]" "[:lower:]") $(basename $mod ".pm"); done
67 # or
68 # perl -MModule::Generic::File=file -lE 'my $d=file("./lib/HTTP/Promise/Headers"); my $files=$d->content; $files->for(sub{ my$f=file($_); printf("%-32s => ''HTTP::Promise::Headers::%s'',\n", $f->basename(".pm")->lc, $f->basename(".pm")) })'
69 our $SUPPORTED =
70 {
71 accept => 'HTTP::Promise::Headers::Accept',
72 acceptencoding => 'HTTP::Promise::Headers::AcceptEncoding',
73 acceptlanguage => 'HTTP::Promise::Headers::AcceptLanguage',
74 altsvc => 'HTTP::Promise::Headers::AltSvc',
75 cachecontrol => 'HTTP::Promise::Headers::CacheControl',
76 clearsitedata => 'HTTP::Promise::Headers::ClearSiteData',
77 contentdisposition => 'HTTP::Promise::Headers::ContentDisposition',
78 contentrange => 'HTTP::Promise::Headers::ContentRange',
79 contentsecuritypolicy => 'HTTP::Promise::Headers::ContentSecurityPolicy',
80 contentsecuritypolicyreportonly => 'HTTP::Promise::Headers::ContentSecurityPolicyReportOnly',
81 contenttype => 'HTTP::Promise::Headers::ContentType',
82 cookie => 'HTTP::Promise::Headers::Cookie',
83 expectct => 'HTTP::Promise::Headers::ExpectCT',
84 forwarded => 'HTTP::Promise::Headers::Forwarded',
85 generic => 'HTTP::Promise::Headers::Generic',
86 keepalive => 'HTTP::Promise::Headers::KeepAlive',
87 link => 'HTTP::Promise::Headers::Link',
88 range => 'HTTP::Promise::Headers::Range',
89 servertiming => 'HTTP::Promise::Headers::ServerTiming',
90 stricttransportsecurity => 'HTTP::Promise::Headers::StrictTransportSecurity',
91 te => 'HTTP::Promise::Headers::TE',
92 wantdigest => 'HTTP::Promise::Headers::WantDigest',
93 };
94  
95 sub new
96 {
97 my $this = shift( @_ );
98 my $opts = {};
99 $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
100 my $self;
101 # try-catch
102 local $@;
103 eval
104 {
105 $self = $this->SUPER::new( @_ );
106 };
107 if( $@ )
108 {
109 return( $this->error( "Error instantiating an HTTP::Promise::Headers object: $@" ) );
110 }
111 $self->{default_type} = undef;
112 $self->{_init_strict_use_sub} = 1;
113 $self->{_exception_class} = $EXCEPTION_CLASS;
114 $self->debug( $opts->{debug} ) if( CORE::exists( $opts->{debug} ) );
115 $self->{_ctype_cached} = '';
116 return( $self );
117 }
118  
119 # e.g. text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
120 sub accept
121 {
122 my $self = shift( @_ );
123 if( @_ )
124 {
125 my $types = $self->_get_args_as_array( @_ );
126 $self->header( accept => $types );
127 CORE::delete( $self->{acceptables} );
128 }
129 return( $self->_set_get_one( 'Accept' ) );
130 }
131  
132 # Obsolete header that should not be used
133 sub accept_charset { return( shift->_set_get_one( 'Accept-Charset', @_ ) ); }
134  
135 # e.g. gzip, deflate, br
136 sub accept_encoding { return( shift->_set_get_multi( 'Accept-Encoding', @_ ) ); }
137  
138 # e.g.: en-GB,fr-FR;q=0.8,fr;q=0.6,ja;q=0.4,en;q=0.2
139 sub accept_language { return( shift->_set_get_multi( 'Accept-Language', @_ ) ); }
140  
141 # NOTE: Accept-Patch is a response header
142 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Patch>
143 sub accept_patch { return( shift->_set_get_one( 'Accept-Patch', @_ ) ); }
144  
145 # NOTE: Accept-Post is a response header
146 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Post>
147 sub accept_post { return( shift->_set_get_multi( 'Accept-Post', @_ ) ); }
148  
149 # NOTE: Accept-Tanges is a response header
150 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges>
151 sub accept_ranges { return( shift->_set_get_multi( 'Accept-Ranges', @_ ) ); }
152  
153 sub acceptables
154 {
155 my $self = shift( @_ );
156 return( $self->{acceptables} ) if( $self->{acceptables} );
157 my $accept_raw = $self->accept;
158 if( $accept_raw )
159 {
160 my $f = $self->new_field( accept => $accept_raw ) ||
161 return( $self->pass_error );
162 $self->{acceptables} = $f;
163 }
164 return( $self->{acceptables} );
165 }
166  
167 sub add { return( shift->push_header( @_ ) ); }
168  
169 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age>
170 sub age { return( shift->_set_get_one( 'Age', @_ ) ); }
171  
172 # NOTE: Allow is a response header
173 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Allow>
174 sub allow { return( shift->_set_get_multi( 'Allow', @_ ) ); }
175  
176 # Response header: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials>
177 sub allow_credentials { return( shift->_set_get_one( 'Access-Control-Allow-Credentials', @_ ) ); }
178  
179 # Response header <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers>
180 sub allow_headers { return( shift->_set_get_multi( 'Access-Control-Allow-Headers', @_ ) ); }
181  
182 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods>
183 sub allow_methods { return( shift->_set_get_one( 'Access-Control-Allow-Methods', @_ ) ); }
184  
185 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin>
186 sub allow_origin { return( shift->_set_get_one( 'Access-Control-Allow-Origin', @_ ) ); }
187  
188 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Alt-Svc>
189 sub alt_svc { return( shift->_set_get_multi( 'Alt-Svc', @_ ) ); }
190  
191 sub alternate_server
192 {
193 my $self = shift( @_ );
194 if( @_ )
195 {
196 # { name => 'h2', value => 'alt.example.com:443', ma => 2592000, persist => 1
197 my $def = $self->_get_args_as_hash( @_ );
198 my $name = CORE::delete( $def->{name} );
199 my $value = CORE::delete( $def->{value} );
200 my $f = $self->new_field( alt_svc => [$name => $value], $def ) ||
201 return( $self->pass_error );
202 $self->push_header( 'Alt-Svc' => "$f" );
203 }
204 else
205 {
206 my $all = $self->alt_svc;
207 return( $all ) if( !$all->length );
208 my $a = $self->new_array;
209 $all->foreach(sub
210 {
211 my $f = $self->new_field( alt_svc => $_ ) ||
212 return( $self->pass_error );
213 $a->push( $f );
214 });
215 return( $a );
216 }
217 }
218  
219 # NOTE: as_string() is inherited
220 # NOTE: unfortunately, HTTP::XSHeaders is not dealing with as_string properly
221 # It takes the given eol and replace simply any instance in-between line of \n with it,
222 # thus if you have something like: foo\r\nbar\r\n, it will end up with
223 # foo\r\r\nbar\r\n instead of foot\r\nbar\r\n
224 # Bug report #10 <https://github.com/p5pclub/http-xsheaders/issues/10>
225 # sub as_string { return( shift->SUPER::as_string( @_ ? @_ : ( CRLF ) ) ); }
226 sub as_string
227 {
228 my $self = shift( @_ );
229 my $type = $self->type;
230 # If the type is multipart, ensure we have a boundary set.
231 # This is a convenience for the user, who only needs to set the mime-type
232 # without having to worry about generating a boundary.
233 if( defined( $type ) && lc( [split( '/', $type, 2 )]->[0] ) eq 'multipart' )
234 {
235 my $boundary = $self->multipart_boundary;
236 unless( $boundary )
237 {
238 $boundary = $self->make_boundary;
239 my $ct = $self->new_field( 'Content-Type' => $type );
240 $ct->boundary( $boundary );
241 $self->content_type( "$ct" );
242 }
243 }
244 my $str = $self->SUPER::as_string( @_ ? @_ : ( CRLF ) );
245 if( index( $str, "\r\r\n" ) != -1 )
246 {
247 $str =~ s/\r\r\n/\r\n/g;
248 }
249 return( $str );
250 }
251  
252 # NOTE: authorization() is inherited
253 sub authorization { return( shift->_set_get_one( 'Authorization', @_ ) ); }
254  
255 # NOTE: authorization_basic() is inherited
256 sub authorization_basic { return( shift->_basic_auth( 'Authorization', @_ ) ); }
257  
258 sub boundary
259 {
260 my $self = shift( @_ );
261 if( @_ )
262 {
263 my $boundary = shift( @_ );
264 my $ct = $self->content_type;
265 $self->{boundary} = $boundary;
266 # If there is a content type set, add the charset to it; otherwise, just return
267 # User should set the content type before setting the charset
268 return( '' ) if( !length( $ct ) );
269 my $f = $self->new_field( content_type => $ct ) ||
270 return( $self->pass_error );
271 $f->param( boundary => $boundary );
272 $self->{type} = $f->type;
273 $self->content_type( $f );
274 $self->{_ctype_cached} = "$f";
275 }
276 unless( length( $self->{boundary} ) && $self->{_ctype_cached} eq $self->content_type )
277 {
278 my $ct = $self->content_type;
279 my $f = $self->new_field( content_type => ( defined( $ct ) ? "$ct" : () ) );
280 $self->{boundary} = $f->boundary;
281 $self->{type} = $f->type;
282 $self->{_ctype_cached} = $ct;
283 }
284 return( $self->{boundary} );
285 }
286  
287 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control>
288 sub cache_control { return( shift->_set_get_one( 'Cache-Control', @_ ) ); }
289  
290 sub charset
291 {
292 my $self = shift( @_ );
293 if( @_ )
294 {
295 my $charset = shift( @_ );
296 my $ct = $self->content_type;
297 $self->{charset} = $charset;
298 # If there is a content type set, add the charset to it; otherwise, just return
299 # User should set the content type before setting the charset
300 return( '' ) if( !length( $ct ) );
301 my $f = $self->new_field( content_type => $ct ) || return( $self->pass_error );
302 $f->param( charset => $charset );
303 $self->content_type( $f );
304 }
305 unless( length( $self->{charset} ) )
306 {
307 my $ct = $self->content_type;
308 my $f = $self->new_field( content_type => $ct );
309 $self->{charset} = $f->charset;
310 }
311 return( $self->{charset} );
312 }
313  
314 # NOTE: clear() is inherited
315  
316 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data>
317 sub clear_site_data { return( shift->_set_get_multi( 'Clear-Site-Data', @_ ) ); }
318  
319 sub client_date { return( shift->_date_header( 'Client-Date', @_ ) ); }
320  
321 # NOTE: clone() is inherited
322  
323 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection>
324 sub connection { return( shift->_set_get_one( 'Connection', @_ ) ); }
325  
326 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition>
327 sub content_disposition { return( shift->_set_get_one( 'Content-Disposition', @_ ) ); }
328  
329 # NOTE: content_encoding() is implemented in our parent class, but our implementation differs
330 sub content_encoding { return( shift->_set_get_one( 'Content-Encoding', @_ ) ); }
331  
332 # NOTE: content_is_html() is already implemented by our parent class, but our implementation of content_type() differs
333 sub content_is_html
334 {
335 my $self = shift( @_ );
336 my $type = $self->type;
337 return(0) if( !defined( $type ) || !length( "$type" ) );
338 $type = lc( $type );
339 return( $type eq 'text/html' || $self->content_is_xhtml );
340 }
341  
342 sub content_is_json
343 {
344 my $self = shift( @_ );
345 my $type = $self->type;
346 return(0) if( !defined( $type ) || !length( "$type" ) );
347 $type = lc( $type );
348 # Standard API error payload as per RFC9457: application/problem+json
349 return( $type eq 'application/json' || $type =~ m,^application/\w+\+json, );
350 }
351  
352 sub content_is_text
353 {
354 my $self = shift( @_ );
355 my $type = $self->content_type;
356 return(0) if( !defined( $type ) || !length( "$type" ) );
357 return( $$type =~ m,^text/,i );
358 }
359  
360 # NOTE: content_is_xhtml() is already implemented by our parent class, but our implementation of content_type() differs
361 sub content_is_xhtml
362 {
363 my $self = shift( @_ );
364 my $type = $self->type;
365 return(0) if( !defined( $type ) || !length( "$type" ) );
366 $type = lc( $type );
367 return( $type eq 'application/xhtml+xml' || $type eq 'application/vnd.wap.xhtml+xml' );
368 }
369  
370 # NOTE: content_is_xml() is already implemented by our parent class, but our implementation of content_type() differs
371 sub content_is_xml
372 {
373 my $self = shift( @_ );
374 my $type = $self->type;
375 return(0) if( !defined( $type ) || !length( "$type" ) );
376 $type = lc( $type );
377 return(1) if( $type eq 'text/xml' );
378 return(1) if( $type eq 'application/xml' );
379 return(1) if( $type =~ /\+xml$/ );
380 return(0);
381 }
382  
383 # NOTE: content_language() is implemented in our parent class, but our implementation differs
384 sub content_language { return( shift->_set_get_multi( 'Content-Language', @_ ) ); }
385  
386 # NOTE: content_length() is implemented in our parent class, but our implementation differs
387 sub content_length { return( shift->_set_get_one_number( 'Content-Length', @_ ) ); }
388  
389 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location>
390 sub content_location { return( shift->_set_get_one( 'Content-Location', @_ ) ); }
391  
392 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range>
393 sub content_range { return( shift->_set_get_one( 'Content-Range', @_ ) ); }
394  
395 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy>
396 sub content_security_policy { return( shift->_set_get_one( 'Content-Security-Policy', @_ ) ); }
397  
398 sub content_security_policy_report_only { return( shift->_set_get_one( 'Content-Security-Policy-Report-Only', @_ ) ); }
399  
400 # NOTE: content_type() is already implemented by our parent class, but we our implementation is more straightforward in line with the idea of setting and getting exactly the header field value.
401 # Arguably, it is wrong to expect the return value of content_type to be only the mime_type, thus there is the type() method for that
402 sub content_type
403 {
404 my $self = shift( @_ );
405 my $v;
406 if( @_ )
407 {
408 $v = shift( @_ );
409 $self->header( content_type => $v );
410 # Simple value, set the type() cache
411 if( index( $v, ';' ) == -1 )
412 {
413 $self->{type} = $v;
414 }
415 # Force type() to find the mime-type
416 else
417 {
418 $self->{type} = '';
419 }
420  
421 return( $self->new_scalar( ref( $v ) ? "$v" : \$v ) );
422 }
423 else
424 {
425 $v = $self->header( 'Content-Type' );
426 }
427  
428 if( defined( $v ) )
429 {
430 return( $self->new_scalar( ref( $v ) ? "$v" : \$v ) );
431 }
432 elsif( want( 'OBJECT' ) )
433 {
434 return( Module::Generic::Null->new );
435 }
436 else
437 {
438 return;
439 }
440 }
441  
442 # NOTE: content_type_charset() is inherited
443  
444 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy>
445 sub cross_origin_embedder_policy { return( shift->_set_get_one( 'Cross-Origin-Embedder-Policy', @_ ) ); }
446  
447 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy>
448 sub cross_origin_opener_policy { return( shift->_set_get_one( 'Cross-Origin-Opener-Policy', @_ ) ); }
449  
450 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy>
451 sub cross_origin_resource_policy { return( shift->_set_get_one( 'Cross-Origin-Resource-Policy', @_ ) ); }
452  
453 sub cspro { return( shift->content_security_policy_report_only( @_ ) ); }
454  
455 # NOTE: date() is already implemented by our parent class, but our implementation is more versatile and better
456 sub date { return( shift->_date_header( 'Date', @_ ) ); }
457  
458 # rfc2231 <https://datatracker.ietf.org/doc/html/rfc2231>
459 sub decode_filename
460 {
461 my $self = shift( @_ );
462 my $fname = shift( @_ );
463 return( $fname ) if( !defined( $fname ) || !length( $fname ) );
464 my( $charset, $lang );
465 if( $fname =~ /^(.*?)\'([^\']*)\'(.*?)$/ )
466 {
467 ( $charset, $lang, my $encoded_fname ) = ( $1, $2, $3 );
468 unless( lc( $charset ) eq 'utf8' || lc( $charset ) eq 'utf-8' )
469 {
470 return( $self->error( "Character set '$charset' is not supported for file name '$encoded_fname'" ) );
471 }
472 # The language parameter, if any, is discarded
473 $fname = Encode::decode_utf8( URI::Escape::XS::uri_unescape( $encoded_fname ) );
474 }
475 # rfc2047 encoded?
476 elsif( $fname =~ /^=\?(.+?)\?(.+?)\?(.+)\?=$/ )
477 {
478 $charset = $1;
479 my $encoding = uc( $2 );
480 my $encfile = $3;
481  
482 if( $encoding eq 'Q' || $encoding eq 'B' )
483 {
484 eval
485 {
486 if( $encoding eq 'Q' )
487 {
488 $encfile =~ s/_/ /g;
489 $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->pass_error );
490 my $s = HTTP::Promise::Stream->new( \$encfile, { decoding => 'quoted-printable' } ) ||
491 return( $self->pass_error( HTTP::Promise::Stream->error ) );
492 my $decoded = $s->decode;
493 return( $self->pass_error( $s->error ) ) if( !defined( $decoded ) );
494 $encfile = $decoded;
495 }
496 # $encoding eq 'B'
497 else
498 {
499 $self->_load_class( 'Crypt::Misc' ) || return( $self->pass_error );
500 $encfile = Crypt::Misc::decode_b64( $encfile );
501 }
502 };
503  
504 if( $@ )
505 {
506 # return( $self->error( "Error decoding content disposition file name: $e" ) );
507 warn( "Error decoding content disposition file name: $@" ) if( warnings::enabled( 'HTTP::Promise' ) );
508 }
509  
510 eval
511 {
512 $self->_load_class( 'Encode' ) || return( $self->pass_error );
513 $self->_load_class( 'Encode::Locale' ) || return( $self->pass_error );
514 Encode::from_to( $encfile, $charset, 'locale_fs' );
515 $fname = $encfile;
516 };
517  
518 if( $@ )
519 {
520 # return( $self->error( "Error encoding content disposition file name: $e" ) );
521 warn( "Error encoding content disposition file name from '$charset' to 'locale_fs': $@" ) if( warnings::enabled( 'HTTP::Promise' ) );
522 }
523 }
524 }
525 return( wantarray() ? ( $fname, $charset, $lang ) : $fname );
526 }
527  
528 sub debug
529 {
530 my $self = shift( @_ );
531 my $class = ( ref( $self ) || $self );
532 no strict 'refs';
533 if( @_ )
534 {
535 my $flag = shift( @_ );
536 $self->{debug} = $flag;
537 if( $self->{debug} &&
538 !$self->{debug_level} )
539 {
540 $self->{debug_level} = $self->{debug};
541 }
542 }
543 return( $self->{debug} || ${"$class\:\:DEBUG"} );
544 }
545  
546 sub default_type { return( shift->_set_get( 'default_type', @_ ) ); }
547  
548 sub delete { return( shift->remove_header( @_ ) ); }
549  
550 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Device-Memory>
551 sub device_memory { return( shift->_set_get_one( 'Device-Memory', @_ ) ); }
552  
553 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Digest>
554 sub digest { return( shift->_set_get_multi( 'Digest', @_ ) ); }
555  
556 # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/DNT
557 sub dnt { return( shift->_set_get_one( dnt => @_ ) ); }
558  
559 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Early-Data>
560 sub early_data { return( shift->_set_get_one( 'Early-Data', @_ ) ); }
561  
562 # rfc2231 <https://datatracker.ietf.org/doc/html/rfc2231>
563 sub encode_filename
564 {
565 my $self = shift( @_ );
566 my $fname = shift( @_ );
567 my $lang = shift( @_ );
568 if( $fname =~ /[^\w\.]+/ )
569 {
570 $lang = '' if( !defined( $lang ) );
571 return( sprintf( "UTF-8'${lang}'%s", $self->uri_escape_utf8( $fname ) ) );
572 }
573 # Nothing to be done. We return undef on purpose to indicate nothing was done
574 return;
575 }
576  
577 # Copied here from Module::Generic
578 sub error
579 {
580 my $self = shift( @_ );
581 my $class = ref( $self ) || $self;
582 my $i_am_blessed = Scalar::Util::blessed( $self ) ? 1 : 0;
583 no warnings;
584 our $MOD_PERL;
585 my $o;
586 no strict 'refs';
587 no warnings 'once';
588 my $want_return =
589 {
590 array => sub
591 {
592 return( [] );
593 },
594 code => sub
595 {
596 return( sub{} );
597 },
598 'glob' => sub
599 {
600 my $dummy = '';
601 open( my $tmp, '>', \$dummy );
602 return( $tmp );
603 },
604 hash => sub
605 {
606 return( {} );
607 },
608 object => sub
609 {
610 $self->_load_class( 'Module::Generic::Null' ) ||
611 die( "Unable to load Module::Generic::Null" );
612 my $null = Module::Generic::Null->new( $o, { debug => $self->{debug}, has_error => 1, wants => 'object' });
613 return( $null );
614 },
615 'scalar' => sub
616 {
617 my $dummy = undef;
618 return( \$dummy );
619 },
620 };
621  
622 my $want_what = Wanted::wantref();
623  
624 # Ensure this is lowercase and at the same time that this is defined
625 $want_what = lc( $want_what // '' );
626 # What type of expected value we support to prevent perl error upon undef.
627 # By default: object
628 my $want_ok = [qw( object )];
629  
630 my $err_key = HAS_THREADS ? join( ';', $class, $$, threads->tid ) : join( ';', $class, $$ );
631 my $repo = Module::Generic::Global->new( 'errors' => $class, key => $err_key );
632 if( @_ )
633 {
634 my $args = {};
635 # We got an object as first argument. It could be a child from our exception package or from another package
636 # Either way, we use it as it is
637 if( ( Scalar::Util::blessed( $_[0] ) && $_[0]->isa( 'Module::Generic::Exception' ) ) ||
638 Scalar::Util::blessed( $_[0] ) )
639 {
640 $o = shift( @_ );
641 }
642 elsif( ref( $_[0] ) eq 'HASH' )
643 {
644 $args = shift( @_ );
645 }
646 else
647 {
648 $args->{message} = join( '', map( ( ref( $_ ) eq 'CODE' && !$self->{_msg_no_exec_sub} ) ? $_->() : $_, @_ ) );
649 }
650 $args->{class} //= '';
651 my $max_len = ( CORE::exists( $self->{error_max_length} ) && $self->{error_max_length} =~ /^[-+]?\d+$/ )
652 ? $self->{error_max_length}
653 : 0;
654 $args->{message} = substr( $args->{message}, 0, $self->{error_max_length} ) if( $max_len > 0 && length( $args->{message} ) > $max_len );
655 # Reset it
656 $self->{_msg_no_exec_sub} = 0;
657 # Note Taken from Carp to find the right point in the stack to start from
658 my $caller_func;
659 $caller_func = \&{"CORE::GLOBAL::caller"} if( defined( &{"CORE::GLOBAL::caller"} ) );
660 if( !defined( $o ) )
661 {
662 my $ex_class = CORE::length( $args->{class} )
663 ? $args->{class}
664 : ( CORE::exists( $self->{_exception_class} ) && CORE::length( $self->{_exception_class} ) )
665 ? $self->{_exception_class}
666 : 'Module::Generic::Exception';
667 unless( $self->_is_class_loaded( $ex_class ) || scalar( keys( %{"${ex_class}\::"} ) ) )
668 {
669 my $pl = "use $ex_class;";
670 local $SIG{__DIE__} = sub{};
671 eval( $pl );
672 # We have to die, because we have an error within another error
673 die( __PACKAGE__ . "::error() is unable to load exception class \"$ex_class\": $@" ) if( $@ );
674 }
675 # $o = $self->{error} = ${ $class . '::ERROR' } = $ex_class->new( $args );
676 $o = $ex_class->new( $args );
677 }
678 $self->{error} = $o;
679 $repo->set( $o );
680 # For backward compatibility, so the user can access $My::Module::ERROR
681 ${ $class . '::ERROR' } = $o;
682  
683 my $r;
684 if( $MOD_PERL )
685 {
686 # try-catch
687 local $@;
688 eval
689 {
690 $r = Apache2::RequestUtil->request;
691 $r->warn( $o->as_string ) if( $r );
692 };
693 if( $@ )
694 {
695 print( STDERR "Error trying to get the global Apache2::ApacheRec: $@\n" );
696 }
697 }
698  
699 if( $r )
700 {
701 if( my $log_handler = $r->get_handlers( 'PerlPrivateErrorHandler' ) )
702 {
703 $log_handler->( $o );
704 }
705 else
706 {
707 $r->warn( $o->as_string ) if( warnings::enabled( 'HTTP::Promise' ) );
708 }
709 }
710 elsif( $self->{fatal} || ( defined( ${"${class}\::FATAL_ERROR"} ) && ${"${class}\::FATAL_ERROR"} ) )
711 {
712 # my $enc_str = eval{ Encode::encode( 'UTF-8', "$o", Encode::FB_CROAK ) };
713 # die( $@ ? $o : $enc_str );
714 die( $o );
715 }
716 elsif( warnings::enabled( 'HTTP::Promise' ) )
717 {
718 if( $r )
719 {
720 $r->warn( $o->as_string );
721 }
722 else
723 {
724 my $enc_str = eval{ Encode::encode( 'UTF-8', "$o", Encode::FB_CROAK ) };
725 # Display warnings if warnings for this class is registered and enabled or if not registered
726 warn( $@ ? $o : $enc_str );
727 }
728 }
729  
730 # https://metacpan.org/pod/Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef
731 # https://perlmonks.org/index.pl?node_id=741847
732 # Because in list context this would create a lit with one element undef()
733 # A bare return will return an empty list or an undef scalar
734 # return( undef() );
735 # return;
736 # As of 2019-10-13, Module::Generic version 0.6, we use this special package Module::Generic::Null to be returned in chain without perl causing the error that a method was called on an undefined value
737 # 2020-05-12: Added the no_return_null_object to instruct not to return a null object
738 # This is especially needed when an error is called from TIEHASH that returns a special object.
739 # A Null object would trigger a fatal perl segmentation fault
740 if( !$args->{no_return_null_object} &&
741 (
742 ( $want_what && CORE::exists( $want_return->{ $want_what } ) && scalar( grep( /^$want_what$/i, @$want_ok ) ) ) ||
743 $args->{object}
744 ) )
745 {
746 if( $args->{object} )
747 {
748 rreturn( $want_return->{object}->() );
749 }
750 else
751 {
752 rreturn( $want_return->{ $want_what }->() );
753 }
754 }
755 return;
756 }
757 if( $i_am_blessed )
758 {
759 # To avoid the perl error of 'called on undefined value' and so the user can do
760 # $o->error->_message for example without concerning himself/herself whether an exception object is actually set
761 if( !$self->{error} )
762 {
763 if( $want_what &&
764 CORE::exists( $want_return->{ $want_what } ) &&
765 scalar( grep( /^$want_what$/i, @$want_ok ) ) )
766 {
767 rreturn( $want_return->{ $want_what }->() );
768 }
769 }
770 else
771 {
772 return( $self->{error} );
773 }
774 }
775  
776 $o = $repo->get;
777 if( CORE::defined( $o ) && CORE::length( $o ) )
778 {
779 # Found an exception object using Module::Generic::Global
780 }
781 elsif( !CAN_THREADS &&
782 CORE::defined( ${ $class . '::ERROR' } ) &&
783 CORE::length( ${ $class . '::ERROR' } ) )
784 {
785 $o = ${ $class . '::ERROR' };
786 warn( "Accessing ${class}::ERROR is deprecated; use ${class}->error instead" ) if( warnings::enabled( 'HTTP::Promise' ) );
787 }
788 return( $o );
789 }
790  
791 sub etag { return( shift->_set_get_one( 'Etag', @_ ) ); }
792  
793 sub exists
794 {
795 my $self = shift( @_ );
796 my $field = shift( @_ ) || return(0);
797 my $rv = $self->header( $field );
798 return( defined( $rv ) ? 1 : 0 );
799 }
800  
801 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect>
802 sub expect { return( shift->_set_get_one( 'Expect', @_ ) ); }
803  
804 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect-CT>
805 sub expect_ct { return( shift->_set_get_multi( 'Expect-CT', @_ ) ); }
806  
807 # NOTE: expires() is inherited
808 sub expires { return( shift->_date_header( 'Expires', @_ ) ); }
809  
810 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers>
811 # e.g.: Access-Control-Expose-Headers: Content-Encoding, X-Kuma-Revision
812 sub expose_headers { return( shift->_set_get_multi( 'Access-Control-Expose-Headers', @_ ) ); }
813  
814 # NOTE: flatten() is inherited
815  
816 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded>
817 sub forwarded { return( shift->_set_get_one( 'Forwarded', @_ ) ); }
818  
819 # NOTE: from() is inherited
820 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/From>
821 sub from { return( shift->_set_get_one( 'From', @_ ) ); }
822  
823 sub get { return( shift->header( shift( @_ ) ) ); }
824  
825 # NOTE: header() is inherited
826  
827 # NOTE: header_field_names() is inherited
828  
829 sub host { return( shift->_set_get_one( 'Host', @_ ) ); }
830  
831 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match>
832 sub if_match { return( shift->_set_get_one( 'If-Match', @_ ) ); }
833  
834 # NOTE: if_modified_since() is inherited
835 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since>
836 sub if_modified_since { return( shift->_date_header( 'If-Modified-Since', @_ ) ); }
837  
838 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match>
839 sub if_none_match { return( shift->_set_get_one( 'If-None-Match', @_ ) ); }
840  
841 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Range>
842 sub if_range { return( shift->_set_get_one( 'If-Range', @_ ) ); }
843  
844 # NOTE: if_unmodified_since() is inherited
845 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since>
846 sub if_unmodified_since { return( shift->_date_header( 'If-Unmodified-Since', @_ ) ); }
847  
848 # NOTE: init_header() is inherited
849  
850 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive>
851 sub keep_alive { return( shift->_set_get_one( 'Keep-Alive', @_ ) ); }
852  
853 # NOTE: last_modified() is inherited
854 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified>
855 sub last_modified { return( shift->_date_header( 'Last-Modified', @_ ) ); }
856  
857 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link>
858 sub link { return( shift->_set_get_multi( 'Link', @_ ) ); }
859  
860 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location>
861 sub location { return( shift->_set_get_one( 'Location', @_ ) ); }
862  
863 sub make_boundary
864 {
865 my $self = shift( @_ );
866 $self->_load_class( 'Data::UUID' ) || return( $self->pass_error );
867 return( Data::UUID->new->create_str );
868 }
869  
870 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age>
871 sub max_age { return( shift->_set_get_number( 'Access-Control-Max-Age', @_ ) ); }
872  
873 sub message
874 {
875 my $self = shift( @_ );
876 my $class = ref( $self ) || $self;
877 no strict 'refs';
878 if( $self->{verbose} || $self->{debug} || ${ $class . '::DEBUG' } )
879 {
880 my $r;
881 if( $MOD_PERL )
882 {
883 # try-catch
884 local $@;
885 eval
886 {
887 $r = Apache2::RequestUtil->request;
888 };
889 if( $@ )
890 {
891 $stderr_raw->print( "Error trying to get the global Apache2::ApacheRec: $@\n" );
892 }
893 }
894  
895 my $ref;
896 $ref = $self->message_check( @_ );
897 return(1) if( !$ref );
898  
899 my $opts = {};
900 $opts = pop( @$ref ) if( ref( $ref->[-1] ) eq 'HASH' );
901  
902 my $stackFrame = $self->message_frame( (caller(1))[3] ) || 1;
903 $stackFrame = 1 unless( $stackFrame =~ /^\d+$/ );
904 $stackFrame-- if( $stackFrame );
905 $stackFrame++ if( ( (caller(1))[3] // '' ) eq 'HTTP::Promise::Headers::messagef' );
906 my( $pkg, $file, $line, @otherInfo ) = caller( $stackFrame );
907 my $sub = ( caller( $stackFrame + 1 ) )[3] // '';
908 my $sub2 = substr( $sub, rindex( $sub, '::' ) + 2 );
909 if( ref( $self->{_message_frame} ) eq 'HASH' )
910 {
911 if( exists( $self->{_message_frame}->{ $sub2 } ) )
912 {
913 my $frameNo = int( $self->{_message_frame}->{ $sub2 } );
914 if( $frameNo > 0 )
915 {
916 ( $pkg, $file, $line, $sub ) = caller( $frameNo );
917 $sub2 = substr( $sub, rindex( $sub, '::' ) + 2 );
918 }
919 }
920 }
921 if( $sub2 eq 'message' )
922 {
923 $stackFrame++;
924 ( $pkg, $file, $line, @otherInfo ) = caller( $stackFrame );
925 my $sub = ( caller( $stackFrame + 1 ) )[3] // '';
926 $sub2 = substr( $sub, rindex( $sub, '::' ) + 2 );
927 }
928 my $txt;
929 if( $opts->{message} )
930 {
931 if( ref( $opts->{message} ) eq 'ARRAY' )
932 {
933 $txt = join( '', map( ( ref( $_ ) eq 'CODE' && !$self->{_msg_no_exec_sub} ) ? $_->() : ( $_ // '' ), @{$opts->{message}} ) );
934 }
935 else
936 {
937 $txt = $opts->{message};
938 }
939 }
940 else
941 {
942 $txt = join( '', map( ( ref( $_ ) eq 'CODE' && !$self->{_msg_no_exec_sub} ) ? $_->() : ( $_ // '' ), @$ref ) );
943 }
944 # Reset it
945 $self->{_msg_no_exec_sub} = 0;
946 my $prefix = CORE::length( $opts->{prefix} ) ? $opts->{prefix} : '##';
947 no overloading;
948 $opts->{caller_info} = 1 if( !CORE::exists( $opts->{caller_info} ) || !CORE::length( $opts->{caller_info} ) );
949 my $proc_info = " [PID: $$]";
950 if( HAS_THREADS )
951 {
952 my $tid = threads->tid;
953 $proc_info .= ' -> [thread id ' . $tid . ']' if( $tid );
954 }
955 my $mesg_raw = $opts->{caller_info} ? ( "${pkg}::${sub2}( $self ) [$line]${proc_info}: " . $txt ) : $txt;
956 $mesg_raw =~ s/\n$//gs;
957 my $mesg = "${prefix} " . join( "\n${prefix} ", split( /\n/, $mesg_raw ) );
958  
959 my $info =
960 {
961 'formatted' => $mesg,
962 'message' => $txt,
963 'file' => $file,
964 'line' => $line,
965 'package' => $class,
966 'sub' => $sub2,
967 'level' => ( $_[0] =~ /^\d+$/ ? $_[0] : CORE::exists( $opts->{level} ) ? $opts->{level} : 0 ),
968 };
969 $info->{type} = $opts->{type} if( $opts->{type} );
970  
971 ## If Mod perl is activated AND we are not using a private log
972 if( $r && !${ "${class}::LOG_DEBUG" } )
973 {
974 if( my $log_handler = $r->get_handlers( 'PerlPrivateLogHandler' ) )
975 {
976 $log_handler->( $mesg_raw );
977 }
978 elsif( $self->{_log_handler} && ref( $self->{_log_handler} ) eq 'CODE' )
979 {
980 $self->{_log_handler}->( $info );
981 }
982 else
983 {
984 $r->log->debug( $mesg_raw );
985 }
986 }
987 # Using ModPerl Server to log
988 elsif( $MOD_PERL && !${ "${class}::LOG_DEBUG" } )
989 {
990 require Apache2::ServerUtil;
991 my $s = Apache2::ServerUtil->server;
992 $s->log->debug( $mesg );
993 }
994 # e.g. in our package, we could set the handler using the curry module like $self->{_log_handler} = $self->curry::log
995 elsif( !-t( STDIN ) && $self->{_log_handler} && ref( $self->{_log_handler} ) eq 'CODE' )
996 {
997 $self->{_log_handler}->( $info );
998 }
999 elsif( !-t( STDIN ) && ${ $class . '::MESSAGE_HANDLER' } && ref( ${ $class . '::MESSAGE_HANDLER' } ) eq 'CODE' )
1000 {
1001 my $h = ${ $class . '::MESSAGE_HANDLER' };
1002 $h->( $info );
1003 }
1004 # Otherwise just on the stderr
1005 else
1006 {
1007 if( $opts->{no_encoding} )
1008 {
1009 $stderr_raw->print( $mesg, "\n" );
1010 }
1011 else
1012 {
1013 $stderr->print( $mesg, "\n" );
1014 }
1015 }
1016 }
1017 return(1);
1018 }
1019  
1020 sub message_check
1021 {
1022 my $self = shift( @_ );
1023 my $class = ref( $self ) || $self;
1024 no warnings 'uninitialized';
1025 no strict 'refs';
1026 if( @_ )
1027 {
1028 if( $_[0] !~ /^\d/ )
1029 {
1030 # The last parameter is an options parameter which has the level property set
1031 if( ref( $_[-1] ) eq 'HASH' && CORE::exists( $_[-1]->{level} ) )
1032 {
1033 # Then let's use this
1034 }
1035 elsif( $self->{_message_default_level} =~ /^\d+$/ &&
1036 $self->{_message_default_level} > 0 )
1037 {
1038 unshift( @_, $self->{_message_default_level} );
1039 }
1040 else
1041 {
1042 unshift( @_, 1 );
1043 }
1044 }
1045 # If the first argument looks line a number, and there is more than 1 argument
1046 # and it is greater than 1, and greater than our current debug level
1047 # well, we do not output anything then...
1048 if( ( $_[0] =~ /^\d+$/ ||
1049 ( ref( $_[-1] ) eq 'HASH' &&
1050 CORE::exists( $_[-1]->{level} ) &&
1051 $_[-1]->{level} =~ /^\d+$/
1052 )
1053 ) && @_ > 1 )
1054 {
1055 my $message_level = 0;
1056 if( $_[0] =~ /^\d+$/ )
1057 {
1058 $message_level = shift( @_ );
1059 }
1060 elsif( ref( $_[-1] ) eq 'HASH' && CORE::exists( $_[-1]->{level} ) )
1061 {
1062 $message_level = $_[-1]->{level};
1063 }
1064 my $target_re = '';
1065 if( ref( ${ "${class}::DEBUG_TARGET" } ) eq 'ARRAY' )
1066 {
1067 $target_re = scalar( @${ "${class}::DEBUG_TARGET" } ) ? join( '|', @${ "${class}::DEBUG_TARGET" } ) : '';
1068 }
1069 if( int( $self->{debug} ) >= $message_level ||
1070 int( $self->{verbose} ) >= $message_level ||
1071 ( defined( ${ $class . '::DEBUG' } ) && ${ $class . '::DEBUG' } >= $message_level ) ||
1072 int( $self->{debug_level} ) >= $message_level ||
1073 int( $self->{debug} ) >= 100 ||
1074 ( length( $target_re ) && $class =~ /^$target_re$/ && ${ $class . '::GLOBAL_DEBUG' } >= $message_level ) )
1075 {
1076 return( [ @_ ] );
1077 }
1078 else
1079 {
1080 return(0);
1081 }
1082 }
1083 }
1084 return(0);
1085 }
1086  
1087 sub message_frame
1088 {
1089 my $self = shift( @_ );
1090 $self->{_message_frame } = {} if( !exists( $self->{_message_frame} ) );
1091 my $mf = $self->{_message_frame};
1092 if( @_ )
1093 {
1094 my $args = {};
1095 if( ref( $_[0] ) eq 'HASH' )
1096 {
1097 $args = shift( @_ );
1098 my @k = keys( %$args );
1099 @$mf{ @k } = @$args{ @k };
1100 }
1101 elsif( !( @_ % 2 ) )
1102 {
1103 $args = { @_ };
1104 my @k = keys( %$args );
1105 @$mf{ @k } = @$args{ @k };
1106 }
1107 elsif( scalar( @_ ) == 1 )
1108 {
1109 my $sub = shift( @_ );
1110 $sub = substr( $sub, rindex( $sub, '::' ) + 2 ) if( index( $sub, '::' ) != -1 );
1111 return( $mf->{ $sub } );
1112 }
1113 else
1114 {
1115 return( $self->error( "I was expecting a key => value pair such as routine => stack frame (integer)" ) );
1116 }
1117 }
1118 return( $mf );
1119 }
1120  
1121 # For compatibility for MIME::Decoder->head, itself used by HTTP::Promise::Entity
1122 # "some decoders need to know a little about the file they are encoding/decoding; e.g., x-uu likes to have the filename. The HEAD is any object which responds to messages like:
1123 # $head->mime_attr( 'content-disposition.filename' );
1124 sub mime_attr
1125 {
1126 my $self = shift( @_ );
1127 my( $attr, $value ) = @_;
1128 return if( !defined( $attr ) || !length( $attr ) );
1129 # Break attribute name up
1130 my( $tag, $subtag ) = split( /\./, $attr, 2 );
1131 my $v = $self->header( $tag );
1132 require Module::Generic::HeaderValue;
1133 my $hv;
1134 if( defined( $v ) && length( $v ) )
1135 {
1136 $hv = Module::Generic::HeaderValue->new_from_header( $v );
1137 return( $self->pass_error( Module::Generic::HeaderValue->error ) ) if( !defined( $hv ) );
1138 }
1139 if( scalar( @_ ) > 1 )
1140 {
1141 if( defined( $subtag ) )
1142 {
1143 return( $self->error( "There is no header field '$tag' currently set, so you cannot assign a value for '$subtag'." ) ) if( !defined( $hv ) );
1144 $hv->param( $subtag => $value );
1145 }
1146 else
1147 {
1148 if( defined( $hv ) )
1149 {
1150 $hv->value( $value );
1151 }
1152 else
1153 {
1154 $hv = Module::Generic::HeaderValue->new( $value ) ||
1155 return( $self->pass_error( Module::Generic::HeaderValue->error ) );
1156 }
1157 }
1158 $self->replace( $tag => "$hv" );
1159 return( $value );
1160 }
1161 else
1162 {
1163 return( '' ) if( !defined( $hv ) );
1164 return( defined( $subtag ) ? $hv->param( $subtag ) : $hv->value_data );
1165 }
1166 }
1167  
1168 # In HTTP parlance, the request may contain a Content-Encoding in multipart/form-data,
1169 # and the server response may contain Transfer-Encoding to indicate in which encoding the
1170 # response is provided.
1171 sub mime_encoding
1172 {
1173 my $self = shift( @_ );
1174 my $te = $self->header( 'Content-Encoding' ) || $self->header( 'Transfer-Encoding' );
1175 return( '' ) if( !defined( $te ) || !length( $te ) );
1176 my $enc = lc( $te );
1177 # 7-bit, 7_bit -> 7bit. Same for 8-bit, 8_bit
1178 $enc =~ s{^([78])[ _-]bit\Z}{$1bit};
1179 return( $enc );
1180 }
1181  
1182 sub mime_type
1183 {
1184 my $self = shift( @_ );
1185 my $default = shift( @_ ) || $self->default_type;
1186 my $ct = $self->type;
1187 if( !defined( $ct ) || !length( $ct ) )
1188 {
1189 return( $default ) if( defined( $default ) );
1190 return( '' );
1191 }
1192 return( $ct );
1193 }
1194  
1195 sub multipart_boundary
1196 {
1197 my $self = shift( @_ );
1198 my $ct = $self->content_type;
1199 return( '' ) unless( defined( $ct ) && length( "$ct" ) );
1200 # There is no attributes to this Content-Type, so no need to go further.
1201 return( '' ) if( index( $ct, ';' ) == -1 || index( $ct, 'boundary' ) == -1 );
1202 require Module::Generic::HeaderValue;
1203 my $hv = Module::Generic::HeaderValue->new_from_header( $ct ) ||
1204 return( $self->pass_error( Module::Generic::HeaderValue->error ) );
1205 my $boundary = $hv->param( 'boundary' );
1206 return( $boundary );
1207 }
1208  
1209 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/NEL>
1210 sub nel { return( shift->_set_get_one( 'NEL', @_ ) ); }
1211  
1212 sub new_array
1213 {
1214 my $self = shift( @_ );
1215 require Module::Generic::Array;
1216 return( Module::Generic::Array->new( @_ ) );
1217 }
1218  
1219 sub new_field
1220 {
1221 my $self = shift( @_ );
1222 my $field = shift( @_ );
1223 return( $self->error( "No header field name was provided." ) ) if( !defined( $field ) || !length( "$field" ) );
1224 unless( scalar( keys( %$SUPPORTED ) ) )
1225 {
1226 $self->_load_class( 'Module::Generic::File' ) || return( $self->pass_error );
1227 my $dir = Module::Generic::File->new( ( $MOD_PATH || __FILE__ ) )->extension( undef );
1228 $dir->open || return( $self->error( "Unable to open directory \"$dir\": ", $dir->error ) );
1229 my $f;
1230 while( defined( $f = $dir->read( exclude_invisible => 1, as_object => 1 ) ) )
1231 {
1232 next if( $f->extension ne 'pm' );
1233 my $base = $f->basename( '.pm' );
1234 $SUPPORTED->{ lc( $base ) } = "HTTP\::Promise\::Headers\::${base}";
1235 }
1236 }
1237 ( my $name = $field ) =~ s/[\-_]+//g;
1238 $name = lc( $name );
1239 return( $self->error( "Unsupported field \"$field\"." ) ) if( !exists( $SUPPORTED->{ $name } ) );
1240 my $class = $SUPPORTED->{ $name };
1241 $self->_load_class( $class ) || return( $self->pass_error );
1242 my $o = $class->new( @_ ) ||
1243 return( $self->pass_error( $class->error ) );
1244 return( $o );
1245 }
1246  
1247 sub new_number
1248 {
1249 my $self = shift( @_ );
1250 require Module::Generic::Number;
1251 return( Module::Generic::Number->new( @_ ) );
1252 }
1253  
1254 sub new_scalar
1255 {
1256 my $self = shift( @_ );
1257 require Module::Generic::Scalar;
1258 return( Module::Generic::Scalar->new( @_ ) );
1259 }
1260  
1261 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin>
1262 sub origin { return( shift->_set_get_one_uri( 'Origin', @_ ) ); }
1263  
1264 # Purpose is to get an error object thrown from, possibly another package,
1265 # and make it ours and pass it along
1266 # e.g.:
1267 # $self->pass_error
1268 # $self->pass_error( 'Some error that will be passed to error()' );
1269 # $self->pass_error( $error_object );
1270 # $self->pass_error( $error_object, { class => 'Some::ExceptionClass', code => 400 } );
1271 # $self->pass_error({ class => 'Some::ExceptionClass' });
1272 sub pass_error
1273 {
1274 my $self = shift( @_ );
1275 my $class = ref( $self ) || $self;
1276 my $opts = {};
1277 my $err;
1278 my $ex_class;
1279 my $code;
1280 my $callback;
1281 no strict 'refs';
1282 my $err_key = HAS_THREADS() ? join( ';', $class, $$, threads->tid ) : join( ';', $class, $$ );
1283 my $repo = Module::Generic::Global->new( 'errors' => $class, key => $err_key );
1284  
1285 if( scalar( @_ ) )
1286 {
1287 # Either an hash defining a new error and this will be passed along to error(); or
1288 # an hash with a single property: { class => 'Some::ExceptionClass' }
1289 if( scalar( @_ ) == 1 && ref( $_[0] ) eq 'HASH' )
1290 {
1291 $opts = $_[0];
1292 }
1293 else
1294 {
1295 # $self->pass_error( $error_object, { class => 'Some::ExceptionClass' } );
1296 if( scalar( @_ ) > 1 && ref( $_[-1] ) eq 'HASH' )
1297 {
1298 $opts = pop( @_ );
1299 }
1300 $err = $_[0];
1301 }
1302 }
1303 $err = $opts->{error} if( !defined( $err ) && CORE::exists( $opts->{error} ) && defined( $opts->{error} ) && CORE::length( $opts->{error} ) );
1304 # We set $ex_class only if the hash provided is a one-element hash and not an error-defining hash
1305 # $class = CORE::delete( $opts->{class} ) if( scalar( keys( %$opts ) ) == 1 && [keys( %$opts )]->[0] eq 'class' );
1306 $ex_class = $opts->{class} if( CORE::exists( $opts->{class} ) && defined( $opts->{class} ) && CORE::length( $opts->{class} ) );
1307 $code = $opts->{code} if( CORE::exists( $opts->{code} ) && defined( $opts->{code} ) && CORE::length( $opts->{code} ) );
1308 $callback = $opts->{callback} if( CORE::exists( $opts->{callback} ) && defined( $opts->{callback} ) && ref( $opts->{callback} ) );
1309  
1310 # called with no argument, most likely from the same class to pass on an error
1311 # set up earlier by another method; or
1312 # with an hash containing just one argument class => 'Some::ExceptionClass'
1313 if( !defined( $err ) && ( !scalar( @_ ) || defined( $ex_class ) ) )
1314 {
1315 my $error;
1316 if( Scalar::Util::blessed( $self ) )
1317 {
1318 $error = $self->{error};
1319 }
1320 elsif( $repo->exists )
1321 {
1322 $error = $repo->get;
1323 }
1324  
1325 if( !defined( $error ) )
1326 {
1327 warn( "No error object provided and no previous error set either! It seems the previous method call returned a simple undef\n", $self->_get_stack_trace );
1328 }
1329 else
1330 {
1331 $err = ( defined( $ex_class ) ? bless( $error => $ex_class ) : $error );
1332 $err->code( $code ) if( defined( $code ) );
1333 }
1334 }
1335 # An error object was provided
1336 elsif( defined( $err ) &&
1337 Scalar::Util::blessed( $err ) &&
1338 ( scalar( @_ ) == 1 ||
1339 ( scalar( @_ ) == 2 && defined( $ex_class ) )
1340 ) )
1341 {
1342 # If necessary, we re-bless the error object to the designated class
1343 my $o = ( defined( $ex_class ) ? bless( $err => $ex_class ) : $err );
1344 $self->{error} = $o;
1345 # We ned to be backward compatible, and allow the user to access $My::Module::ERROR
1346 $repo->set( $o );
1347 ${ $class . '::ERROR' } = $o;
1348 $self->{error}->code( $code ) if( defined( $code ) );
1349  
1350 if( $self->{fatal} || ( defined( ${"${class}\::FATAL_ERROR"} ) && ${"${class}\::FATAL_ERROR"} ) )
1351 {
1352 die( $self->{error} );
1353 }
1354 }
1355 # If the error provided is not an object, we call error to create one
1356 else
1357 {
1358 return( $self->error( @_ ) );
1359 }
1360  
1361 if( want( 'OBJECT' ) )
1362 {
1363 $self->_load_class( 'Module::Generic::Null' ) ||
1364 die( "Unable to load Module::Generic::Null" );
1365 my $null = Module::Generic::Null->new( $err, { debug => $self->{debug}, has_error => 1 });
1366 rreturn( $null );
1367 }
1368 return;
1369 }
1370  
1371 sub print
1372 {
1373 my $self = shift( @_ );
1374 my $fh = shift( @_ );
1375 my $opts = $self->_get_args_as_hash( @_ );
1376 my $eol = $opts->{eol} || CRLF;
1377 $fh ||= select;
1378 return( $self->error( "Filehandle provided ($fh) is not a proper filehandle and its not a HTTP::Promise::IO object." ) ) if( !$self->_is_glob( $fh ) && !$self->_is_a( $fh => 'HTTP::Promise::IO' ) );
1379 return( $fh->print( $self->as_string( $eol ) ) );
1380 }
1381  
1382 sub proxy { return( shift->_set_get_uri( 'proxy', @_ ) ); }
1383  
1384 # NOTE: proxy_authenticate() is inherited
1385 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authenticate>
1386 sub proxy_authenticate { return( shift->_set_get_one( 'Proxy-Authenticate', @_ ) ); }
1387  
1388 # NOTE: proxy_authorization() is inherited
1389 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization>
1390 sub proxy_authorization { return( shift->_set_get_one( 'Proxy-Authorization', @_ ) ); }
1391  
1392 # NOTE: proxy_authorization_basic() is inherited
1393  
1394 # NOTE: push_header() is inherited
1395  
1396 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range>
1397 sub range { return( shift->_set_get_multi( 'Range', @_ ) ); }
1398  
1399 sub recommended_filename
1400 {
1401 my $self = shift( @_ );
1402 foreach my $attr_name ( qw( content-disposition.filename* content-disposition.filename content-type.name ) )
1403 {
1404 my $value = $self->mime_attr( $attr_name );
1405 if( defined( $value ) &&
1406 $value ne '' &&
1407 $value =~ /\S/ )
1408 {
1409 return( $self->decode_filename( $value ) );
1410 }
1411 }
1412 return;
1413 }
1414  
1415 # NOTE: referer() and referrer() are inherited
1416 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer>
1417 sub referer { return( shift->_set_get_one_uri( 'Referer', @_ ) ); }
1418  
1419 sub referrer { return( shift->referer( @_ ) ); }
1420  
1421 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy>
1422 sub referrer_policy { return( shift->_set_get_one( 'Referrer-Policy', @_ ) ); }
1423  
1424 sub remove { return( shift->remove_header( @_ ) ); }
1425  
1426 # NOTE: remove_header() is inherited
1427  
1428 # NOTE: remove_content_headers() is inherited
1429  
1430 sub replace
1431 {
1432 my $self = shift( @_ );
1433 my( $field, $value ) = @_;
1434 unless( defined( $field ) && length( $field // '' ) )
1435 {
1436 return( $self->error( "No header name was provided." ) );
1437 }
1438 ( my $meth = $field ) =~ tr/-/_/;
1439 $meth = lc( $meth );
1440 if( my $ref = $self->can( $meth ) )
1441 {
1442 $ref->( $self, $value );
1443 }
1444 else
1445 {
1446 $self->header( $field => $value );
1447 }
1448 return( $self );
1449 }
1450  
1451 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Headers>
1452 sub request_headers { return( shift->_set_get_one( 'Access-Control-Request-Headers', @_ ) ); }
1453  
1454 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Method>
1455 sub request_method { return( shift->_set_get_one( 'Access-Control-Request-Method', @_ ) ); }
1456  
1457 sub request_timeout { return( shift->_set_get_number( 'request_timeout', @_ ) ); }
1458  
1459 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After>
1460 sub retry_after { return( shift->_set_get_one( 'Retry-After', @_ ) ); }
1461  
1462 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Save-Data>
1463 sub save_data { return( shift->_set_get_one( 'Save-Data', @_ ) ); }
1464  
1465 # NOTE: scan() is inherited
1466  
1467 # NOTE: server() is inherited
1468 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server>
1469 sub server { return( shift->_set_get_one( 'Server', @_ ) ); }
1470  
1471 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing>
1472 sub server_timing { return( shift->_set_get_one( 'Server-Timing', @_ ) ); }
1473  
1474 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie>
1475 sub set_cookie { return( shift->_set_get_one( 'Set-Cookie', @_ ) ); }
1476  
1477 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/SourceMap>
1478 sub sourcemap { return( shift->_set_get_one( 'SourceMap', @_ ) ); }
1479  
1480 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security>
1481 sub strict_transport_security { return( shift->_set_get_one( 'Strict-Transport-Security', @_ ) ); }
1482  
1483 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/TE>
1484 sub te { return( shift->_set_get_one( 'TE', @_ ) ); }
1485  
1486 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin>
1487 sub timing_allow_origin { return( shift->_set_get_multi( 'Timing-Allow-Origin', @_ ) ); }
1488  
1489 # NOTE: title() is inherited and sucks really
1490 sub title { return( shift->_set_get_one( 'Title', @_ ) ); }
1491  
1492 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Tk>
1493 sub tk { return( shift->_set_get_one( 'Tk', @_ ) ); }
1494  
1495 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer>
1496 sub trailer { return( shift->_set_get_one( 'Trailer', @_ ) ); }
1497  
1498 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding>
1499 sub transfer_encoding { return( shift->_set_get_one( 'Transfer-Encoding', @_ ) ); }
1500  
1501 sub type
1502 {
1503 my $self = shift( @_ );
1504 if( @_ )
1505 {
1506 # mime-type like text/html, text/plain or application/json, etc...
1507 my $type = shift( @_ );
1508 $self->{type} = $type;
1509 # We are being provided with content type parameters, such as charset=utf8, or version=1
1510 my $ct = $self->new_field( 'Content-Type' => $type, @_ );
1511 $self->header( 'Content-Type' => "$ct" );
1512 $self->{_ctype_cached} = "$ct";
1513 $self->{boundary} = $ct->boundary if( $ct->boundary );
1514 }
1515 # Cached
1516 elsif( CORE::length( $self->{type} ) && $self->{_ctype_cached} eq $self->content_type )
1517 {
1518 return( $self->{type} );
1519 }
1520 else
1521 {
1522 my $ctype_raw = $self->content_type;
1523 return if( !defined( $ctype_raw ) || !length( "$ctype_raw" ) );
1524 $self->{_ctype_cached} = $ctype_raw;
1525 # There is nothing, but the mime-type itself, so no need to bother
1526 if( index( $ctype_raw, ';' ) == -1 )
1527 {
1528 $self->{type} = $ctype_raw;
1529 $self->{boundary} = '';
1530 }
1531 else
1532 {
1533 # Content-Type: application/json; encoding=utf-8
1534 my $ct = $self->new_field( 'Content-Type' => $ctype_raw );
1535 return( $self->pass_error ) if( !defined( $ct ) );
1536 # Accept: application/json; version=1.0; charset=utf-8
1537 $self->{type} = lc( $ct->type );
1538 my $charset = $ct->charset;
1539 $charset = lc( $charset ) if( defined( $charset ) );
1540 $self->{charset} = $charset;
1541 $self->{boundary} = $ct->boundary if( $ct->boundary );
1542 }
1543 }
1544 return( $self->{type} );
1545 }
1546  
1547 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade>
1548 sub upgrade { return( shift->_set_get_multi( 'Upgrade', @_ ) ); }
1549  
1550 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade-Insecure-Requests>
1551 sub upgrade_insecure_requests { return( shift->_set_get_one( 'Upgrade-Insecure-Requests', @_ ) ); }
1552  
1553 sub uri_escape_utf8 { return( URI::Escape::XS::uri_escape( Encode::encode( 'UTF-8', $_[1] ) ) ); }
1554  
1555 # NOTE: user_agent() is inherited
1556 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent>
1557 sub user_agent { return( shift->_set_get_one( 'user_agent', @_ ) ); }
1558  
1559 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary>
1560 sub vary { return( shift->_set_get_multi( 'Vary', @_ ) ); }
1561  
1562 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Via>
1563 sub via { return( shift->_set_get_multi( 'Via', @_ ) ); }
1564  
1565 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Want-Digest>
1566 sub want_digest { return( shift->_set_get_multi( 'Want-Digest', @_ ) ); }
1567  
1568 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning>
1569 sub warning { return( shift->_set_get_one( 'Warning', @_ ) ); }
1570  
1571 # NOTE: www_authenticate() is superseded
1572 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate>
1573 sub www_authenticate { return( shift->_set_get_one( 'WWW-Authenticate', @_ ) ); }
1574  
1575 sub x { return( shift->_set_get_one( 'X-' . $_[0], @_ ) ); }
1576  
1577 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options>
1578 sub x_content_type_options { return( shift->_set_get_one( 'X-Content-Type-Options', @_ ) ); }
1579  
1580 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control>
1581 sub x_dns_prefetch_control { return( shift->_set_get_one( 'X-DNS-Prefetch-Control', @_ ) ); }
1582  
1583 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For>
1584 sub x_forwarded_for { return( shift->_set_get_one( 'X-Forwarded-For', @_ ) ); }
1585  
1586 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host>
1587 sub x_forwarded_host { return( shift->_set_get_one( 'X-Forwarded-Host', @_ ) ); }
1588  
1589 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto>
1590 sub x_forwarded_proto { return( shift->_set_get_one( 'X-Forwarded-Proto', @_ ) ); }
1591  
1592 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options>
1593 sub x_frame_options { return( shift->_set_get_one( 'X-Frame-Options', @_ ) ); }
1594  
1595 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection>
1596 sub x_xss_protection { return( shift->_set_get_one( 'X-XSS-Protection', @_ ) ); }
1597  
1598 sub _basic_auth
1599 {
1600 my $self = shift( @_ );
1601 my $field = shift( @_ );
1602 return( $self->error( "No field provided to get its basic authentication value." ) ) if( !defined( $field ) || !length( $field ) );
1603 $self->_load_class( 'Crypt::Misc' ) || return( $self->pass_error );
1604 if( @_ )
1605 {
1606 my( $user, $pwd ) = @_;
1607 return( $self->error( "Basic authorisation user name cannot contain ':'." ) ) if( index( $user, ':' ) != -1 );
1608 $pwd = '' if( !defined( $pwd ) );
1609 $self->header( $field => sprintf( 'Basic %s', Crypt::Misc::encode_b64( "${user}:${pwd}" ) ) );
1610 return( "${user}:${pwd}" );
1611 }
1612 else
1613 {
1614 my $v = $self->header( $field );
1615 return( $v ) if( !defined( $v ) && !want( 'OBJECT' ) );
1616 if( defined( $v ) && $v =~ /^[[:blank:]\h]*Basic[[:blank:]\h]+(.+?)$/ )
1617 {
1618 $v = Crypt::Misc::decode_b64( $1 );
1619 }
1620 return( wantarray() ? split( /:/, "$v" ) : $self->new_scalar( $v ) );
1621 }
1622 }
1623  
1624 sub _date_header
1625 {
1626 my $self = shift( @_ );
1627 my $f = shift( @_ );
1628 return( $self->error( "No field was provided to get or set its value." ) ) if( !defined( $f ) || !length( "$f" ) );
1629 if( @_ )
1630 {
1631 my $this = shift( @_ );
1632 return( $self->remove_header( "$f" ) ) if( !defined( $this ) );
1633 my $opts = $self->_get_args_as_hash( @_ );
1634 $opts->{time_zone} = 'GMT' if( !defined( $opts->{time_zone} ) || !length( $opts->{time_zone} ) );
1635 require Module::Generic::DateTime;
1636 require DateTime::Format::Strptime;
1637 if( ref( $this ) && Scalar::Util::blessed( $this ) && $this->isa( 'Module::Generic::DateTime' ) )
1638 {
1639 # Ok, pass through
1640 }
1641 elsif( ref( $this ) && Scalar::Util::blessed( $this ) && $this->isa( 'DateTime' ) )
1642 {
1643 $this = Module::Generic::DateTime->new( $this );
1644 }
1645 elsif( $this =~ /^\d+$/ )
1646 {
1647 # try-catch
1648 local $@;
1649 eval
1650 {
1651 $this = Module::Generic::DateTime->from_epoch( epoch => "$this", time_zone => $opts->{time_zone} );
1652 };
1653 if( $@ )
1654 {
1655 return( $self->error( "An error occurred while trying to get the DateTime object for epoch value '$this': $@" ) );
1656 }
1657 }
1658 else
1659 {
1660 return( $self->error( "I was expecting an integer representing a unix time or a DateTime object, but instead got '$this'." ) );
1661 }
1662  
1663 # try-catch
1664 local $@;
1665 eval
1666 {
1667 $this->set_time_zone( 'GMT' );
1668 my $fmt = DateTime::Format::Strptime->new(
1669 pattern => '%a, %d %b %Y %H:%M:%S GMT',
1670 locale => 'en_GB',
1671 time_zone => 'GMT',
1672 );
1673 $this->set_formatter( $fmt );
1674 $self->header( $f => $this );
1675 };
1676 if( $@ )
1677 {
1678 return( $self->error( "An error occurred while trying to format the datetime '$this': $@" ) );
1679 }
1680 return( $this );
1681 }
1682 else
1683 {
1684 my $v = $self->header( "$f" );
1685 return( $v ) if( !defined( $v ) || !length( "${v}" ) );
1686 if( !length( "$v" ) || ( ref( $v ) && !overload::Method( $v, '""' ) ) )
1687 {
1688 warn( "I do not know what to do with this supposedly datetime header $f with value '$v'\n" ) if( length( "$v" ) );
1689 return( '' );
1690 }
1691  
1692 # try-catch
1693 local $@;
1694 eval
1695 {
1696 unless( Scalar::Util::blessed( $v ) && $v->isa( 'Module::Generic::DateTime' ) )
1697 {
1698 # IE6 appends "; length = NNNN" on If-Modified-Since (can we handle it)
1699 # e.g.: Sat, 29 Oct 1994 19:43:31 GMT; length=34343
1700 if( index( $v, ';' ) != -1 )
1701 {
1702 $v = [split( /[[:blank:]\h]*;[[:blank:]\h]*/, "${v}", 2 )]->[0];
1703 }
1704 require Module::Generic;
1705 my $dt = Module::Generic->_parse_timestamp( "$v" );
1706 return( $self->pass_error( Module::Generic->error ) ) if( !defined( $dt ) );
1707 my $fmt = DateTime::Format::Strptime->new( pattern => '%s' );
1708 $dt->set_formatter( $fmt );
1709 $v = Module::Generic::DateTime->new( $dt );
1710 }
1711 };
1712 if( $@ )
1713 {
1714 return( $self->error( "An error occurred while parsing datetime '$v': $@" ) );
1715 }
1716 return( $v );
1717 }
1718 }
1719  
1720 sub _get_args_as_array
1721 {
1722 my $self = shift( @_ );
1723 return( [] ) if( !scalar( @_ ) );
1724 my $ref = [];
1725 if( scalar( @_ ) == 1 &&
1726 defined( $_[0] ) &&
1727 # Scalar::Util::reftype returns undef if the value is not a reference, which
1728 # causes a warning to pop up since we then compare an undefined value.
1729 ref( $_[0] ) &&
1730 Scalar::Util::reftype( $_[0] ) eq 'ARRAY' )
1731 {
1732 $ref = shift( @_ );
1733 }
1734 else
1735 {
1736 $ref = [ @_ ];
1737 }
1738 return( $ref );
1739 }
1740  
1741 sub _get_args_as_hash
1742 {
1743 my $self = shift( @_ );
1744 return( {} ) if( !scalar( @_ ) );
1745 no warnings 'uninitialized';
1746 my $ref = {};
1747 my $order = $self->new_array;
1748 my $need_list = want( 'LIST' ) ? 1 : 0;
1749 if( scalar( @_ ) == 1 && Scalar::Util::reftype( $_[0] ) eq 'HASH' )
1750 {
1751 $ref = shift( @_ );
1752 $order = $self->new_array( [sort( keys( %$ref ) )] ) if( $need_list );
1753 }
1754 elsif( !( scalar( @_ ) % 2 ) )
1755 {
1756 $ref = { @_ };
1757 if( $need_list )
1758 {
1759 for( my $i = 0; $i < scalar( @_ ); $i += 2 )
1760 {
1761 $order->push( $_[$i] );
1762 }
1763 }
1764 }
1765 return( $need_list ? ( $ref, $order ) : $ref );
1766 }
1767  
1768 sub _header_get
1769 {
1770 my $self = shift( @_ );
1771 my $f = shift( @_ );
1772 return( $self->error( "No header field was provided." ) ) if( !defined( $f ) || !length( "$f" ) );
1773 my @values = ();
1774 if( wantarray() )
1775 {
1776 @values = $self->header( "$f" );
1777 return if( !@values );
1778 return( @values );
1779 }
1780 else
1781 {
1782 return( $self->header( "$f" ) );
1783 }
1784 }
1785  
1786 sub _header_set
1787 {
1788 my $self = shift( @_ );
1789 return( $self->SUPER::error( "Uneven number of parameters provided. I was expecting field-value pairs." ) ) if( @_ % 2 );
1790 my @args = ();
1791 for( my $i = 0; $i < scalar( @_ ); $i += 2 )
1792 {
1793 my( $f, $v ) = @_[$i..$i+1];
1794 next if( !defined( $v ) );
1795 if( $self->_is_array( $v ) )
1796 {
1797 my $ref = $self->_is_object( $v ) ? [@$v] : $v;
1798 push( @args, "$f" => $ref );
1799 }
1800 else
1801 {
1802 push( @args, "$f" => $v );
1803 }
1804 }
1805  
1806 # try-catch
1807 local $@;
1808 eval
1809 {
1810 $self->header( @args );
1811 };
1812 if( $@ )
1813 {
1814 return( $self->error( "Error trying to set headers with values: $@" ) );
1815 }
1816 return( $self );
1817 }
1818  
1819 sub _is_a
1820 {
1821 my $self = shift( @_ );
1822 my $obj = shift( @_ );
1823 my $pkg = shift( @_ );
1824 no overloading;
1825 return if( !$obj || !$pkg );
1826 return if( !Scalar::Util::blessed( $obj ) );
1827 return( $obj->isa( $pkg ) );
1828 }
1829  
1830 sub _is_class_loaded
1831 {
1832 my $self = shift( @_ );
1833 my $class = shift( @_ );
1834 my $key = HAS_THREADS ? join( ';', $class, threads->tid() ) : $class;
1835 my $repo = Module::Generic::Global->new( 'loaded_classes' => ( ref( $self ) || $self ), key => $key );
1836 if( $MOD_PERL )
1837 {
1838 # https://perl.apache.org/docs/2.0/api/Apache2/Module.html#C_loaded_
1839 my $rv = Apache2::Module::loaded( $class );
1840 return(1) if( $rv );
1841 }
1842 # elsif( HAS_THREADS && $repo->exists )
1843 elsif( $repo->exists )
1844 {
1845 return(1);
1846 }
1847 else
1848 {
1849 ( my $pm = $class ) =~ s{::}{/}gs;
1850 $pm .= '.pm';
1851 if( CORE::exists( $INC{ $pm } ) )
1852 {
1853 $repo->set(1);
1854 return(1);
1855 }
1856 }
1857 no strict 'refs';
1858 my $ns = \%{ $class . '::' };
1859 if( exists( $ns->{ISA} ) ||
1860 exists( $ns->{BEGIN} ) ||
1861 (
1862 exists( $ns->{VERSION} ) &&
1863 Scalar::Util::reftype( \$ns->{VERSION} ) eq 'GLOB' &&
1864 defined( ${*{\$ns->{VERSION}}{SCALAR}} )
1865 ) )
1866 {
1867 $repo->set(1);
1868 return(1);
1869 }
1870 return(0);
1871 }
1872  
1873 sub _is_glob
1874 {
1875 return(0) if( scalar( @_ < 2 ) );
1876 return(0) if( !defined( $_[1] ) );
1877 my $type = Scalar::Util::reftype( $_[1] );
1878 return(0) if( !defined( $type ) );
1879 return( $type eq 'GLOB' );
1880 }
1881  
1882 sub _load_class
1883 {
1884 my $self = shift( @_ );
1885 my $class = shift( @_ ) || return( $self->error( "No package name was provided to load." ) );
1886  
1887 # Rigorous validation
1888 return( $self->error( "Class name is empty." ) ) unless( length( $class ) );
1889  
1890 my $opts = {};
1891 $opts = pop( @_ ) if ( ref( $_[-1] ) eq 'HASH' );
1892  
1893 # Validate options
1894 my %valid_opts = map { $_ => 1 } qw( caller version no_import );
1895 foreach my $key ( keys( %$opts ) )
1896 {
1897 return( $self->error( "Invalid option: $key" ) ) unless ( exists( $valid_opts{ $key } ) );
1898 }
1899 if( exists( $opts->{version} ) )
1900 {
1901 return( $self->error( "Version must be defined and non-empty." ) ) unless( defined( $opts->{version} ) && length( $opts->{version} ) );
1902 }
1903  
1904 my $args = $self->_get_args_as_array( @_ );
1905  
1906 # Get the caller's package so we load the module in context
1907 my $caller_class = $opts->{caller} || CORE::caller;
1908  
1909 # Return if already loaded (threads aware)
1910 my $is_loaded = $self->_is_class_loaded( $class );
1911 if( $is_loaded )
1912 {
1913 if( scalar( @$args ) )
1914 {
1915 my $pl = "package ${caller_class}; $class->import(" . ( scalar( @$args ) ? "'" . join( "', '", @$args ) . "'" : '' ) . ");";
1916 eval( $pl );
1917 return( $self->error( "Error importing class $class into caller's namespace ${caller_class}: $@" ) ) if( $@ );
1918 }
1919 return( $class );
1920 }
1921 # Use a shared variable for locking in threaded environments
1922 # What we retrieved from _is_class_loaded is not a threaded shared value, so we have to do this instead:
1923 my $key = HAS_THREADS ? join( ';', $class, threads->tid() ) : $class;
1924 my $repo = Module::Generic::Global->new( 'loaded_classes' => ( ref( $self ) || $self ), key => $key );
1925  
1926 # Load the module with thread safety
1927 {
1928 my $has_version = ( CORE::exists( $opts->{version} ) && CORE::length( $opts->{version} // '' ) ) ? 1 : 0;
1929 local $SIG{__DIE__} = sub{};
1930 local $@;
1931 my $pl = "package ${caller_class}; require $class;";
1932 eval( $pl );
1933 return( $self->error( "Unable to load package ${class}: $@\nCode executed was:\n${pl}" ) ) if( $@ );
1934 if( $has_version )
1935 {
1936 eval{ $class->VERSION( $opts->{version} ) };
1937 return( $self->error( "Insufficient version ", $class->VERSION(), ", for class $class. $opts->{version} minimum is required." ) ) if( $@ );
1938 }
1939  
1940 $pl = "package ${caller_class}; $class->import(" . ( scalar( @$args ) ? "'" . join( "', '", @$args ) . "'" : '' ) . ");";
1941 eval( $pl );
1942 return( $self->error( "Error importing class $class into caller's namespace ${caller_class}: $@" ) ) if( $@ );
1943 $repo->set(1);
1944 }
1945  
1946 return( $self->_is_class_loaded( $class ) ? $class : '' );
1947 }
1948  
1949 sub _set_get
1950 {
1951 my $self = shift( @_ );
1952 my $prop = shift( @_ );
1953 $self->{ $prop } = shift( @_ ) if( @_ );
1954 return( $self->{ $prop } );
1955 }
1956  
1957 # If there can be multiple instance of the given header
1958 sub _set_get_multi
1959 {
1960 my $self = shift( @_ );
1961 my $f = shift( @_ );
1962 return( $self->error( "No field was provided to get or set its value." ) ) if( !defined( $f ) || !length( "$f" ) );
1963 if( @_ )
1964 {
1965 my $v = shift( @_ );
1966 return( $self->remove_header( "$f" ) ) if( !defined( $v ) );
1967 # Too dangerous and unnecessary. The value type the user provides us defines how it will be set in the HTTP headers
1968 # An array reference means there will be possibly multiple instance of the header
1969 # A string, means there will be only one instance.
1970 # my $ref = Scalar::Util::reftype( $v ) eq 'ARRAY' ? $v : [split( /\,[[:blank:]\h]*/, $v)];
1971 # $self->header( "$f" => $ref );
1972 $self->header( "$f" => $v );
1973 return( $v );
1974 }
1975 else
1976 {
1977 my @vals = $self->header( "$f" );
1978 my $ref;
1979 if( @vals > 1 )
1980 {
1981 $ref = \@vals;
1982 }
1983 elsif( !defined( $vals[0] ) )
1984 {
1985 if( want( 'OBJECT' ) )
1986 {
1987 return( Module::Generic::Null->new );
1988 }
1989 else
1990 {
1991 return;
1992 }
1993 }
1994 else
1995 {
1996 $vals[0] =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
1997 $ref = [split( /\,[[:blank:]\h]*/, $vals[0] )];
1998 }
1999 # Thi is not such a good idea after all. It is better to return a list in list
2000 # context or a scalar object otherwise
2001 # return( $self->new_array( $ref ) );
2002 return( wantarray() ? @$ref : $self->new_scalar( join( ', ', @$ref ) ) );
2003 }
2004 }
2005  
2006 # If there can be only one instance of the given header
2007 sub _set_get_one
2008 {
2009 my $self = shift( @_ );
2010 my $f = shift( @_ );
2011 return( $self->error( "No field was provided to get or set its value." ) ) if( !defined( $f ) || !length( "$f" ) );
2012 if( @_ )
2013 {
2014 my $v = shift( @_ );
2015 return( $self->remove_header( "$f" ) ) if( !defined( $v ) );
2016 $self->header( "$f" => $v );
2017 return( $v );
2018 }
2019 else
2020 {
2021 my @vals = $self->header( "$f" );
2022 if( @vals > 1 )
2023 {
2024 # return( $self->new_array( \@vals ) );
2025 return( $self->new_scalar( join( ', ', @vals ) ) );
2026 }
2027 elsif( !defined( $vals[0] ) )
2028 {
2029 if( want( 'OBJECT' ) )
2030 {
2031 return( Module::Generic::Null->new );
2032 }
2033 else
2034 {
2035 return;
2036 }
2037 }
2038 else
2039 {
2040 $vals[0] =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
2041 return( $self->new_scalar( $vals[0] ) );
2042 }
2043 }
2044 }
2045  
2046 sub _set_get_one_number
2047 {
2048 my $self = shift( @_ );
2049 my $f = shift( @_ );
2050 return( $self->error( "No field was provided to get or set its value." ) ) if( !defined( $f ) || !length( "$f" ) );
2051 if( @_ )
2052 {
2053 my $v = shift( @_ );
2054 return( $self->remove_header( "$f" ) ) if( !defined( $v ) );
2055 $self->header( "$f" => $v );
2056 return( $v );
2057 }
2058 else
2059 {
2060 my $v = $self->header( "$f" );
2061 if( !defined( $v ) || !length( $v ) )
2062 {
2063 if( want( 'OBJECT' ) )
2064 {
2065 require Module::Generic::Null;
2066 my $null = Module::Generic::Null->new( '', { debug => $self->debug });
2067 rreturn( $null );
2068 }
2069 else
2070 {
2071 return( $v );
2072 }
2073 }
2074 # Ignore overflow values
2075 # 16 digits corresponding to 2^53-1 or 9007199254740991
2076 if( $v =~ /^[[:blank:]\h]*(\d{1,16})[[:blank:]\h]*$/ )
2077 {
2078 $v = $1;
2079 }
2080 else
2081 {
2082 return( '' );
2083 }
2084 return( $v ) if( ref( $v ) && !overload::Method( $v, '""' ) );
2085 return( $self->new_number( "$v" ) );
2086 }
2087 }
2088  
2089 sub _set_get_one_uri
2090 {
2091 my $self = shift( @_ );
2092 my $f = shift( @_ );
2093 return( $self->error( "No field was provided to get or set its value." ) ) if( !defined( $f ) || !length( "$f" ) );
2094 if( @_ )
2095 {
2096 my $v = shift( @_ );
2097 return( $self->remove_header( "$f" ) ) if( !defined( $v ) );
2098 $self->header( "$f" => $v );
2099 return( $v );
2100 }
2101 else
2102 {
2103 my $v = $self->header( "$f" );
2104 my $uri;
2105 # try-catch
2106 local $@;
2107 eval
2108 {
2109 require URI;
2110 $uri = URI->new( $v );
2111 };
2112 if( $@ )
2113 {
2114 return( $self->error( "Unable to create an URI object from the header value for \"$f\": $@" ) );
2115 }
2116 return( $uri );
2117 }
2118 }
2119  
2120 # $obj->_str_val( $some_object );
2121 # $obj->_str_val( undef );
2122 sub _str_val
2123 {
2124 no overloading;
2125 return( '' ) unless( scalar( @_ ) > 1 && defined( $_[1] ) );
2126 return( "$_[1]" );
2127 }
2128  
2129 # NOTE: For CBOR and Sereal
2130 sub FREEZE
2131 {
2132 my $self = CORE::shift( @_ );
2133 my $serialiser = CORE::shift( @_ ) // '';
2134 my $class = CORE::ref( $self );
2135 my $h = {};
2136 my $headers = [];
2137 my $order = [];
2138 $self->scan(sub
2139 {
2140 my( $f, $val ) = @_;
2141 if( CORE::exists( $h->{ $f } ) )
2142 {
2143 $h->{ $f } = [ $h->{ $f } ] unless( CORE::ref( $h->{ $f } ) eq 'ARRAY' );
2144 CORE::push( @{$h->{ $f }}, $val );
2145 }
2146 else
2147 {
2148 $h->{ $f } = $val;
2149 CORE::push( @$order, $f );
2150 }
2151 });
2152 foreach my $f ( @$order )
2153 {
2154 CORE::push( @$headers, $f, $h->{ $f } );
2155 }
2156 my %hash = %$self;
2157 $hash{_headers_to_restore} = $headers;
2158 # Return an array reference rather than a list so this works with Sereal and CBOR
2159 CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
2160 # But Storable want a list with the first element being the serialised element
2161 CORE::return( $class, \%hash );
2162 }
2163  
2164 sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
2165  
2166 # NOTE: Storable creates an instance of HTTP:::Promise::Headers. The only problem is that it does not work with XS module and that Storable discard whatever value is returned by STORABLE_thaw. See issue #19984 <https://github.com/Perl/perl5/issues/19984>
2167 # So instead, we use this hook to store some data into the object created by Storable, and we call STORABLE_thaw_post_processing() with it and take what it returns.
2168 sub STORABLE_thaw
2169 {
2170 my( $self, undef, $class, $hash ) = @_;
2171 $class //= CORE::ref( $self ) || $self;
2172 $hash //= {};
2173 $hash->{_class} = $class;
2174 $self->{_deserialisation_params} = $hash;
2175 # Useles to do more in STORABLE_thaw, because Storable anyway ignores the value returned
2176 # so we just store our hash of parameters for STORABLE_thaw_post_processing to do its actual job
2177 CORE::return( $self );
2178 }
2179  
2180 sub STORABLE_thaw_post_processing
2181 {
2182 my $obj = CORE::shift( @_ );
2183 my $hash = ( CORE::exists( $obj->{_deserialisation_params} ) && CORE::ref( $obj->{_deserialisation_params} ) eq 'HASH' )
2184 ? CORE::delete( $obj->{_deserialisation_params} )
2185 : {};
2186 my $class = CORE::delete( $hash->{_class} ) || CORE::ref( $obj ) || $obj;
2187 my $headers = CORE::ref( $hash->{_headers_to_restore} ) eq 'ARRAY'
2188 ? CORE::delete( $hash->{_headers_to_restore} )
2189 : [];
2190 my $new = $class->new( @$headers );
2191 foreach( CORE::keys( %$hash ) )
2192 {
2193 $new->{ $_ } = CORE::delete( $hash->{ $_ } );
2194 }
2195 CORE::return( $new );
2196 }
2197  
2198 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
2199 # NOTE: Storable calls STORABLE_thaw with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze. Then, after receiving $self from STORABLE_thaw, we call THAW which return a useable object. The one generated by Storable triggers the exception: "hl is not an instance of HTTP::XSHeader"
2200 sub THAW
2201 {
2202 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
2203 # 'CBOR' as the second value.
2204 my( $self, undef, @args ) = @_;
2205 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
2206 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
2207 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
2208 my $headers = ( CORE::exists( $hash->{_headers_to_restore} ) && CORE::ref( $hash->{_headers_to_restore} ) eq 'ARRAY' )
2209 ? CORE::delete( $hash->{_headers_to_restore} )
2210 : [];
2211  
2212 my $new = $class->new( @$headers );
2213 foreach( CORE::keys( %$hash ) )
2214 {
2215 next if( CORE::scalar( CORE::grep( $_, @$headers ) ) );
2216 $new->{ $_ } = CORE::delete( $hash->{ $_ } );
2217 }
2218 CORE::return( $new );
2219 }
2220  
2221 sub TO_JSON
2222 {
2223 my $self = shift( @_ );
2224 my $ref = [];
2225 $self->scan(sub
2226 {
2227 my( $header, $val ) = @_;
2228 CORE::push( @$ref, [ $header, $val ] );
2229 });
2230 return( $ref );
2231 }
2232  
2233 1;
2234 # NOTE: POD
2235 __END__
2236  
2237 =encoding utf-8
2238  
2239 =head1 NAME
2240  
2241 HTTP::Promise::Headers - HTTP Headers Class
2242  
2243 =head1 SYNOPSIS
2244  
2245 use HTTP::Promise::Headers;
2246 my $h = HTTP::Promise::Headers->new ||
2247 die( HTTP::Promise::Headers->error, "\n" );
2248  
2249 =head1 VERSION
2250  
2251 v0.3.2
2252  
2253 =head1 DESCRIPTION
2254  
2255 This class uses for the most part an XS module (L<HTTP::XSHeaders>) to be very fast, and yet provides a convenient and versatile interface to retrieve and manipulate HTTP headers.
2256  
2257 A number of classes has been created to have a more granular control on the creation of some header values. See L</SEE ALSO>
2258  
2259 All HTTP headers known today have their corresponding method that can be used to easily get or set their corresponding header value.
2260  
2261 =head1 CONSTRUCTOR
2262  
2263 =head2 new
2264  
2265 This instantiates a new L<HTTP::Promise::Headers> object. You might pass some initial
2266 attribute-value pairs as parameters to the constructor.
2267  
2268 For example:
2269  
2270 $h = HTTP::Headers->new(
2271 Date => 'Mon, 09 May 2022 09:00:00 GMT',
2272 Content_Type => 'text/html; charset=utf-8; version=5.0',
2273 Content_Base => 'http://www.example.org/'
2274 );
2275  
2276 The constructor arguments are passed to the L</header> method described below.
2277  
2278 =head1 METHODS
2279  
2280 =head2 add
2281  
2282 This is an alias for L</push_header>
2283  
2284 =head2 as_string
2285  
2286 Provided with an optional C<EOL> to be used as End-Of-Line character, and this will return a string representation of the headers. C<EOL> defaults to C<\015\012>. Embedded "\n" characters in header field values will be substituted with this line ending sequence.
2287  
2288 This uses L</scan> internally and use header field case as suggested by HTTP specifications. It will also follow recommended "Good Practice" of ordering the header fields. Long header values are not folded.
2289  
2290 =head2 authorization_basic
2291  
2292 This is a convenience method around the L</authorization> method for the C<Authorization> header using the "Basic Authentication Scheme".
2293  
2294 To set the related header value, you provide a login and an optional password, and this will set the C<Authorization> header value and return the current headers object for chaining.
2295  
2296 $h->authorization_basic( $user, $password );
2297  
2298 If no value is provided, this will get the curent value of the C<Authorization> header field, base64 decode it, and return the decoded string as a L<scalar object|Module::Generic::Scalar>, i.e. something like C<usernaem:password>
2299  
2300 my $str = $h->authorization_basic;
2301  
2302 =head2 boundary
2303  
2304 This is a convenience method to set or get the boundary, if any, used for multipart C<Content-Type>
2305  
2306 If provided, this will add the C<boundary> parameter with the given value to the C<Content-Type> header field.
2307  
2308 If no value is provided, this returns the current boundary, if any, or an empty string.
2309  
2310 =head2 charset
2311  
2312 This is a convenience method to set or get the charset associated with the C<Content-Type> header field.
2313  
2314 If provided, this will add the C<charset> parameter with the given value to the C<Content-Type> header field.
2315  
2316 If no value is provided, this returns the current charset, if any, or an empty string.
2317  
2318 =head2 clear
2319  
2320 This will remove all header fields.
2321  
2322 =for Pod::Coverage:: client_date
2323  
2324 =head2 clone
2325  
2326 Returns a copy of this L<HTTP::Promise::Headers> object.
2327  
2328 =head2 content_is_text
2329  
2330 Returns true if the C<Content-Type> mime-type is textual in nature, i.e. its first half is C<text>, false otherwise. For example: C<text/plain> or C<text/html>
2331  
2332 =head2 content_is_html
2333  
2334 Returns true if the C<Content-Type> mime-type is html, such as C<text/html>, false otherwise.
2335  
2336 =head2 content_is_json
2337  
2338 Returns true if the C<Content-Type> mime-type is C<application/json>, false otherwise.
2339  
2340 =head2 content_is_xhtml
2341  
2342 Returns true if the C<Content-Type> mime-type is C<application/xhtml+xml> or C<application/vnd.wap.xhtml+xml>, false otherwise.
2343  
2344 =head2 content_is_xml
2345  
2346 Returns true if the C<Content-Type> mime-type is C<text/xml> or C<application/xml>, or contains the keyword C<xml>, false otherwise.
2347  
2348 =head2 content_type_charset
2349  
2350 This is a legacy method and it returns the upper-cased charset specified in the Content-Type header.
2351 In list context return the lower-cased bare content type followed by the upper-cased charset.
2352 Both values will be C<undef> if not specified in the header.
2353  
2354 =head2 decode_filename
2355  
2356 This takes a file name from the C<Content-Disposition> header value typically and returns it decoded if it was encoded as per the L<rfc2231|https://tools.ietf.org/html/rfc2231>
2357  
2358 For example:
2359  
2360 Content-Disposition: form-data; name="fileField"; filename*=UTF-8''%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
2361  
2362 my $fname = $h->decode_filename( "UTF-8''%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt" );
2363  
2364 or
2365  
2366 Content-Disposition: form-data; name="fileField"; filename*=UTF-8'ja-JP'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
2367  
2368 my $fname = $h->decode_filename( "UTF-8'ja-JP'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt" );
2369  
2370 or encoded with quoted-printable
2371  
2372 Content-Disposition: attachment; filename="=?UTF-8?Q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB.txt?="
2373  
2374 my $fname = $h->decode_filename( "=?UTF-8?Q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB.txt?=" );
2375  
2376 or encoded with base64
2377  
2378 Content-Disposition: attachment; filename="=?UTF-8?B?44OV44Kh44Kk44OrLnR4dAo?="
2379  
2380 my $fname = $h->decode_filename( "=?UTF-8?B?44OV44Kh44Kk44OrLnR4dAo?=" );
2381  
2382 In the above example, the result for C<$fname> would yield C<ファイル.txt> (i.e. file.txt in Japanese)
2383  
2384 =head2 debug
2385  
2386 Sets or gets the debug value. If positive, this will trigger an output of debugging messages on the STDERR or in the web server log files. Be mindful that this slows down the script, so make sure to switch it off once you are done.
2387  
2388 =head2 default_type
2389  
2390 Sets or gets the default mime-type to be used.
2391  
2392 =head2 delete
2393  
2394 This is an alias for L</remove_header>
2395  
2396 =head2 encode_filename
2397  
2398 This takes a file name to be used in the C<Content-Disposition> header value, and an optional language iso 639 code (see L<rfc1766|https://tools.ietf.org/html/rfc1766>), and if it contains non ascii characters, it will utf-8 encode it and URI escape it according to L<rfc2231|https://tools.ietf.org/html/rfc2231> and return the newly encoded file name.
2399  
2400 If the file name did not require any encoding, it will return C<undef>, so you can write something like this:
2401  
2402 my $filename = q{マイファイル.txt};
2403 if( my $enc = $h->encode_filename( $filename ) )
2404 {
2405 $filename = $enc;
2406 # Now $filename is: UTF-8''%E3%83%9E%E3%82%A4%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
2407 }
2408  
2409 You can optionally pass a language code argument:
2410  
2411 if( my $enc = $h->encode_filename( $filename, 'ja-JP' ) )
2412 {
2413 $filename = $enc;
2414 # Now $filename is: UTF-8'ja-JP'%E3%83%9E%E3%82%A4%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
2415 }
2416  
2417 The C<Content-Disposition> header value would then contain a property C<filename*> (with the trailing wildcard).
2418  
2419 =head2 error
2420  
2421 Sets or gets an error and when set, this returns C<undef>. When no argument is provided, this returns the latest error set.
2422  
2423 The error returned is actually a L<HTTP::Promise::Exception> object.
2424  
2425 =head2 exists
2426  
2427 Provided with a header field name and this returns true if it exists, or false otherwise.
2428  
2429 =head2 flatten
2430  
2431 $h->flatten();
2432  
2433 Returns the list of pairs of keys and values.
2434  
2435 =head2 get
2436  
2437 This is an alias for L</header>, mainly used without argument.
2438  
2439 =head2 header
2440  
2441 $h->header( $field );
2442 $h->header( $field => $value );
2443 $h->header( $f1 => $v1, $f2 => $v2, ... );
2444  
2445 The following is an extract from the original L<HTTP::Headers> class.
2446  
2447 Sets or gets the value of one or more header fields.
2448 The header field name (C<$field>) is not case sensitive.
2449 To make the life easier for perl users who wants to avoid quoting before the => operator, you can use '_' as a replacement for '-' in header names.
2450  
2451 The L</header> method accepts multiple field-value (C<$field => $value>) pairs, which means that you can update several header field values with a single invocation.
2452  
2453 The C<$value> argument may be a plain string or an array reference of strings for a multi-valued field. If the C<$value> is provided as C<undef> then the field is removed.
2454  
2455 If the C<$value> is not given, then that header field will remain unchanged. In addition to being a string, C<$value> may be something that stringifies.
2456  
2457 The old value (or values) of the last of the header fields is returned. If no such field exists C<undef> will be returned.
2458  
2459 A multi-valued field will be returned as separate values in list context and will be concatenated with ", " as separator in scalar context.
2460 The HTTP spec (L<rfc7230|https://tools.ietf.org/html/rfc7230> which obsoleted L<rfc2616|https://tools.ietf.org/html/rfc2616>) promises that joining multiple values in this way will not change the semantic of a header field, but in practice there are cases like old-style Netscape cookies where "," is used as part of the syntax of a single field value.
2461  
2462 Examples:
2463  
2464 $h->header( MIME_Version => '1.0',
2465 User_Agent => 'My-Web-Client/0.01' );
2466 $h->header( Accept => "text/html, text/plain, image/*" );
2467 $h->header( Accept => [qw( text/html text/plain image/* )] );
2468 @accepts = $h->header( 'Accept' ); # get multiple values
2469 $accepts = $h->header( 'Accept' ); # get values as a single string
2470  
2471 =head2 header_field_names
2472  
2473 The following is an extract from the original L<HTTP::Headers> class.
2474  
2475 Returns the list of distinct names for the fields present in the header.
2476 The field names have case as suggested by HTTP spec, and the names are returned in the recommended "Good Practice" order.
2477  
2478 In scalar context return the number of distinct field names.
2479  
2480 =head2 init_header
2481  
2482 $h->init_header( $field => $value );
2483  
2484 Set the specified header to the given value, but only if no previous value for that field is set.
2485  
2486 The header field name (C<$field>) is not case sensitive and '_' can be used as a replacement for '-'.
2487  
2488 The $value argument may be a scalar or a reference to a list of scalars.
2489  
2490 =head2 make_boundary
2491  
2492 Returns a new boundary using L<Data::UUID>
2493  
2494 =for Pod::Coverage message
2495  
2496 =for Pod::Coverage message_check
2497  
2498 =for Pod::Coverage message_frame
2499  
2500 =head2 mime_attr
2501  
2502 Provided with a header field name and an attribute name separated by a dot, such as C<content-disposition.filename> and this will return the value for that attribute in this header.
2503  
2504 If a value is provided, it will be set, otherwise it will be returned.
2505  
2506 If no attribute is provided, it will set or get the header field main value.
2507  
2508 For example:
2509  
2510 Content-Disposition: attachment; filename="hello.txt"
2511 my $dispo = $h->mime_attr( 'content-disposition' );
2512  
2513 will return C<attachment>
2514  
2515 =head2 mime_encoding
2516  
2517 Returns the value of the C<Content-Encoding>, C<Transfer-Encoding> or C<Content-Transfer-Encoding> (the latter only exists in mail, not in HTTP)
2518  
2519 =head2 mime_type
2520  
2521 Returns the mime-type from the C<Content-Type> header value, or the one from L<default_type>, if it is set. If nothing is found, this returns an empty string (not C<undef>).
2522  
2523 =head2 multipart_boundary
2524  
2525 Returns the multipart boundary used, if any, or an empty string otherwise.
2526  
2527 my $boundary = $h->multipart_boundary;
2528 # or you can provide the Content-Type if you already have it; it will save a few cycle
2529 my $boundary = $h->multipart_boundary( $content_type );
2530  
2531 =head2 print
2532  
2533 Provided with a filehandle, or an L<HTTP::Promise::IO> object and this will print on it the string representation of the headers and return whatever value L<perlfunc/print> will return.
2534  
2535 =head2 proxy_authorization_basic
2536  
2537 =head2 push_header
2538  
2539 $h->push_header( $field => $value );
2540 $h->push_header( $f1 => $v1, $f2 => $v2, ... );
2541  
2542 Add a new value for the specified header. Previous values for the same header are retained.
2543  
2544 As for the L</header> method, the field name (C<$field>) is not case sensitive and '_' can be used as a replacement for '-'.
2545  
2546 The $value argument may be a scalar or a reference to a list of scalars.
2547  
2548 $header->push_header( Accept => 'image/jpeg' );
2549 $header->push_header( Accept => [ map( "image/$_", qw( gif png tiff ) )] );
2550  
2551 =head2 recommended_filename
2552  
2553 This returns the filename set in either C<Content-Disposition> with the C<filename> property or in C<Content-Type> with the C<name> property.
2554  
2555 If none exists, this returns C<undef>.
2556  
2557 =head2 remove
2558  
2559 This is an alias for L</remove_header>
2560  
2561 =head2 remove_content_headers
2562  
2563 This will remove all the headers used to describe the content of a message.
2564  
2565 All header field names prefixed with C<Content-> are included in this category, as well as C<Allow>, C<Expires> and
2566 C<Last-Modified>. L<rfc7230|https://tools.ietf.org/html/rfc7230> denotes these headers as I<Entity Header Fields>.
2567  
2568 The return value is a new L<HTTP::Promise::Headers> object that contains the removed headers only.
2569  
2570 =head2 remove_header
2571  
2572 my @values = $h->remove_header( $field, ... );
2573 my $total_values_removed = $h->remove_header( $field, ... );
2574  
2575 This function removes the header with the specified names.
2576  
2577 The header names (C<$field>) are not case sensitive and '_' can be used as a replacement for '-'.
2578  
2579 The return value is the values of the headers removed. In scalar context the number of headers removed is returned.
2580  
2581 Note that if you pass in multiple header names then it is generally not possible to tell which of the returned values belonged to which field.
2582  
2583 =head2 replace
2584  
2585 Provided with a header field name and a value and this replace whatever current value with the value provided.
2586  
2587 It returns the current object for chaining.
2588  
2589 =head2 request_timeout
2590  
2591 Sets or gets the request timeout. This takes an integer.
2592  
2593 =head2 scan
2594  
2595 $h->scan( \&process_header_field );
2596  
2597 Apply a subroutine to each header field in turn.
2598 The callback routine is called with two parameters; the name of the field and a single value (a string).
2599 If a header field is multi-valued, then the routine is called once for each value.
2600 The field name passed to the callback routine has case as suggested by HTTP spec, and the headers will be visited in the recommended "Good Practice" order.
2601  
2602 Any return values of the callback routine are ignored.
2603 The loop can be broken by raising an exception (C<die>), but the caller of scan() would have to trap the exception itself.
2604  
2605 =head2 type
2606  
2607 This sets or gets the C<Content-Type> header value when setting a value, and returns only the mime-type when retrieving the value.
2608  
2609 $h->type( 'text/plain' );
2610 # Assuming Content-Type: text/html; charset=utf-8
2611 my $type = $h->type; # text/html
2612  
2613 =head2 uri_escape_utf8
2614  
2615 Provided with a string and this returns an URI-escaped string using L<URI::Escape::XS>
2616  
2617 =head1 HTTP HEADERS METHODS
2618  
2619 =head2 accept
2620  
2621 $h->accept( q{text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8} );
2622 $h->accept( [qw( text/html application/xhtml+xml application/xml;q=0.9 */*;q=0.8 )] );
2623  
2624 Sets or gets the C<Accept> header field value. It takes either a string or an array or array reference of values.
2625  
2626 See L<rfc7231, section 5.3.2|https://tools.ietf.org/html/rfc7231#section-5.3.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept>
2627  
2628 See also L<HTTP::Promise::Headers::Accept>
2629  
2630 =head2 accept_charset
2631  
2632 $h->accept( 'utf-8' );
2633  
2634 Sets or gets the C<Accept-Charset> headers field value. It takes a single string value.
2635  
2636 You should know that the C<Accept-Charset> header is deprecated by HTTP standards and that no modern web browsers is sending nor any modern HTTP server recognising it.
2637  
2638 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset>
2639  
2640 =head2 accept_encoding
2641  
2642 $h->accept_encoding( 'gzip, deflate, br' );
2643 $h->accept_encoding( [qw( gzip deflate br )] );
2644 $h->accept_encoding( 'br;q=1.0, gzip;q=0.8, *;q=0.1' );
2645  
2646 Sets or gets the C<Accept-Encoding> header field value. It takes either a string or an array or array reference of values.
2647  
2648 See also L<HTTP::Promise::Headers::AcceptEncoding> to have a more granular control.
2649  
2650 Encoding header fields and their nuances:
2651  
2652 =over 4
2653  
2654 =item C<Accept-Encoding>
2655  
2656 The encodings accepted by the client.
2657  
2658 =item C<Content-Encoding>
2659  
2660 Contains the encodings that have been applied to the content, before transport
2661  
2662 =item C<TE>
2663  
2664 The encodings the user agent accepts.
2665  
2666 =item C<Transfer-Encoding>
2667  
2668 The encoding applied during transfer, such as C<chunked>
2669  
2670 =back
2671  
2672 See L<rfc7231, section 5.3.4|https://tools.ietf.org/html/rfc7231#section-5.3.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding>
2673  
2674 =head2 accept_language
2675  
2676 $h->accept_language( 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5' );
2677 $h->accept_language( [qw(fr-CH fr;q=0.9 en;q=0.8 de;q=0.7 *;q=0.5 )] );
2678  
2679 Sets or gets the C<Accept-Language> header field value. It takes either a string or an array or array reference of values.
2680  
2681 See also L<HTTP::Promise::Headers::AcceptLanguage> to have a more granular control.
2682  
2683 See L<rfc7231, section 5.3.5|https://tools.ietf.org/html/rfc7231#section-5.3.5> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language>
2684  
2685 =head2 accept_patch
2686  
2687 $h->accept_patch( 'application/example, text/example' );
2688 $h->accept_patch( [qw( application/example text/example )] );
2689 $h->accept_patch( 'text/example;charset=utf-8' );
2690 $h->accept_patch( 'application/merge-patch+json' );
2691  
2692 Sets or gets the C<Accept-Patch> header field value. It takes either a string or an array or array reference of values.
2693  
2694 This is a server response header.
2695  
2696 See L<rfc5789, section 3.1|https://tools.ietf.org/html/rfc5789#section-3.1>, L<rfc7231, section 4.3.4|https://tools.ietf.org/html/rfc7231#section-4.3.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Patch>
2697  
2698 =head2 accept_post
2699  
2700 $h->accept_post( 'application/example, text/example' );
2701 $h->accept_post( [qw( application/example text/example )] );
2702 $h->accept_post( 'image/webp' );
2703 $h->accept_post( '*/*' );
2704  
2705 Sets or gets the C<Accept-Post> header field value. It takes either a string or an array or array reference of values.
2706  
2707 This is a server response header.
2708  
2709 See L<rfc7231, section 4.3.3|https://tools.ietf.org/html/rfc7231#section-4.3.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Post>
2710  
2711 =head2 accept_ranges
2712  
2713 $h->accept_ranges(1234);
2714  
2715 Sets or gets the C<Accept-Ranges> header field value. It takes either a string or an array or array reference of values.
2716  
2717 This is a server response header.
2718  
2719 See L<rfc7233, section 2.3|https://tools.ietf.org/html/rfc7233#section-2.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges>
2720  
2721 =head2 acceptables
2722  
2723 This returns a new L<HTTP::Promise::Headers::Accept> object based on the content of the C<Accept> header value.
2724  
2725 =head2 age
2726  
2727 $h->age(1234);
2728  
2729 Sets or gets the C<Age> header field value. It takes a numeric value.
2730  
2731 This is a server response header.
2732  
2733 See L<rfc7234, section 5.1|https://tools.ietf.org/html/rfc7234#section-5.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age>
2734  
2735 =head2 allow
2736  
2737 $h->allow( 'GET, POST, HEAD' );
2738 $h->allow( [qw( GET POST HEAD )] );
2739  
2740 Sets or gets the C<Allow> header field value. It takes either a string or an array or array reference of values.
2741  
2742 This is a server response header.
2743  
2744 See L<rfc7231, section 7.4.1|https://tools.ietf.org/html/rfc7231#section-7.4.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age>
2745  
2746 =head2 allow_credentials
2747  
2748 # Access-Control-Allow-Credentials: true
2749 $h->allow_credentials( 'true' );
2750  
2751 Sets or gets the C<Access-Control-Allow-Credentials> header field value. It takes a string boolean value: C<true> or C<false>.
2752  
2753 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials>
2754  
2755 =head2 allow_headers
2756  
2757 # Access-Control-Allow-Headers: X-Custom-Header, Upgrade-Insecure-Requests
2758 $h->allow_headers( 'X-Custom-Header, Upgrade-Insecure-Requests' );
2759 $h->allow_headers( [qw( X-Custom-Header Upgrade-Insecure-Requests )] );
2760  
2761 Sets or gets the C<Access-Control-Allow-Headers> header field value. It takes either a string or an array or array reference of values.
2762  
2763 This is a server response header.
2764  
2765 See L<rfc7231, section 7.4.1|https://tools.ietf.org/html/rfc7231#section-7.4.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age>
2766  
2767 =head2 allow_methods
2768  
2769 # Access-Control-Allow-Methods: POST, GET, OPTIONS
2770 $h->allow_methods( 'POST, GET, OPTIONS' );
2771 $h->allow_methods( [qw( POST GET OPTIONS )] );
2772 # Access-Control-Allow-Methods: *
2773 $h->allow_methods( '*' );
2774  
2775 Sets or gets the C<Access-Control-Allow-Methods> header field value. It takes either a string or an array or array reference of values.
2776  
2777 This is a server response header.
2778  
2779 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods>
2780  
2781 =head2 allow_origin
2782  
2783 # Access-Control-Allow-Origin: *
2784 $h->allow_origin( '*' );
2785 # Access-Control-Allow-Origin: https://food.example.org
2786 $h->allow_origin( 'https://food.example.org' );
2787 # Access-Control-Allow-Origin: null
2788 $h->allow_origin( 'null' );
2789  
2790 Sets or gets the C<Access-Control-Allow-Origin> header field value. It takes a string value.
2791  
2792 This is a server response header.
2793  
2794 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods>
2795  
2796 =head2 alt_svc
2797  
2798 # Alt-Svc: h2=":443"; ma=2592000;
2799 $h->alt_svc( 'h2=":443"; ma=2592000' );
2800 # Alt-Svc: h2=":443"; ma=2592000; persist=1
2801 $h->alt_svc( 'h2=":443"; ma=2592000; persist=1' );
2802 # Alt-Svc: h2="alt.example.com:443", h2=":443"
2803 $h->alt_svc( 'h2="alt.example.com:443", h2=":443"' );
2804 # Alt-Svc: h3-25=":443"; ma=3600, h2=":443"; ma=3600
2805 $h->alt_svc( 'h3-25=":443"; ma=3600, h2=":443"; ma=3600' );
2806  
2807 Sets or gets the C<Alt-Svc> header field value. It takes either a string or an array or array reference of values.
2808  
2809 See also L<HTTP::Promise::Headers::AltSvc> to have a more granular control.
2810  
2811 See L<rfc7838, section 3|https://tools.ietf.org/html/rfc7838#section-3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Alt-Svc>
2812  
2813 =head2 alternate_server
2814  
2815 This is a convenience method for the header field C<Alt-Svc>.
2816  
2817 To et it value, you provide a hash or hash reference of properties, including C<name> and C<value> respectively for the protocol-id and the alternate authority.
2818  
2819 $h->alternate_server( name => 'h2', value => ':443', ma => 2592000, persist => 1 );
2820  
2821 would create the header value:
2822  
2823 Alt-Svc: h2=":443"; ma=2592000; persist=1
2824  
2825 Without any parameter, it creates a new L<HTTP::Promise::Headers::AltSvc> object for each C<Alt-Svc> header value and returns an L<array object|Module::Generic::Array> of all those L<HTTP::Promise::Headers::AltSvc> objects.
2826  
2827 =head2 authorization
2828  
2829 # Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
2830 $h->authorization( 'Basic YWxhZGRpbjpvcGVuc2VzYW1l' );
2831  
2832 Sets or gets the C<Authorization> header field value. It takes a string value.
2833  
2834 See also L</authorization_basic>
2835  
2836 See L<rfc7235, section 4.2|https://tools.ietf.org/html/rfc7235#section-4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization>
2837  
2838 =head2 cache_control
2839  
2840 # Cache-Control: max-age=604800
2841 $h->cache_control( 'max-age=604800' );
2842 # Cache-Control: s-maxage=604800
2843 $h->cache_control( 's-maxage=604800' );
2844 # Cache-Control: no-cache
2845 $h->cache_control( 'no-cache' );
2846 # Cache-Control: max-age=604800, must-revalidate
2847 $h->cache_control( 'max-age=604800, must-revalidate' );
2848 # Cache-Control: public, max-age=604800, immutable
2849 $h->cache_control( 'public, max-age=604800, immutable' );
2850  
2851 Sets or gets the C<Cache-Control> header field value. It takes either a string or an array or array reference of values.
2852  
2853 See also L<HTTP::Promise::Headers::CacheControl> to have a more granular control.
2854  
2855 See L<rfc7234, section 5.2|https://tools.ietf.org/html/rfc7234#section-5.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control>
2856  
2857 =head2 clear_site_data
2858  
2859 # Clear-Site-Data: "cache", "cookies", "storage", "executionContexts"
2860 $h->clear_site_data( q{"cache", "cookies", "storage", "executionContexts"} );
2861 $h->clear_site_data( [qw( cache cookies storage executionContexts )] );
2862  
2863 The Clear-Site-Data header accepts one or more directives. If all types of data should be cleared, the wildcard directive ("*") can be used.
2864  
2865 See also L<HTTP::Promise::Headers::ClearSiteData> to have a more granular control.
2866  
2867 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data>
2868  
2869 =head2 connection
2870  
2871 # Connection: keep-alive
2872 # Connection: close
2873  
2874 Sets or gets the C<Connection> header field value. It takes a string value.
2875  
2876 See L<rfc7230, section 6.1|https://tools.ietf.org/html/rfc7230#section-6.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection>
2877  
2878 =head2 content_disposition
2879  
2880 # Content-Disposition: inline
2881 # Content-Disposition: attachment
2882 # Content-Disposition: attachment; filename="filename.jpg"
2883 # Content-Disposition: form-data; name="fieldName"
2884 # Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
2885  
2886 Sets or gets the C<Content-Disposition> header field value. It takes a string value.
2887  
2888 See also L<HTTP::Promise::Headers::ContentDisposition> to have a more granular control.
2889  
2890 See L<rfc6266, section 4|https://tools.ietf.org/html/rfc6266#section-4>, L<rfc7578, section 4.2|https://tools.ietf.org/html/rfc7578#section-4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition>
2891  
2892 =head2 content_encoding
2893  
2894 # Content-Encoding: gzip
2895 # Content-Encoding: compress
2896 # Content-Encoding: deflate
2897 # Content-Encoding: br
2898  
2899 # Multiple, in the order in which they were applied
2900 # Content-Encoding: deflate, gzip
2901  
2902 Sets or gets the C<Cache-Encoding> header field value. It takes either a string or an array or array reference of values.
2903  
2904 Encoding header fields and their nuances:
2905  
2906 =over 4
2907  
2908 =item C<Accept-Encoding>
2909  
2910 The encodings accepted by the client.
2911  
2912 =item C<Content-Encoding>
2913  
2914 Contains the encodings that have been applied to the content, before transport
2915  
2916 =item C<TE>
2917  
2918 The encodings the user agent accepts.
2919  
2920 =item C<Transfer-Encoding>
2921  
2922 The encoding applied during transfer, such as C<chunked>
2923  
2924 =back
2925  
2926 See L<rfc7230, section 3.3.1|https://tools.ietf.org/html/rfc7230#section-3.3.1>:
2927 "Unlike Content-Encoding (L<Section 3.1.2.1 of [RFC7231]|https://tools.ietf.org/html/rfc7231#section-3.1.2.1>), Transfer-Encoding is a property of the message, not of the representation"
2928  
2929 See also L</accept_encoding>, L</transfer_encoding> and L</te> and this L<Stackoverflow discussion|https://stackoverflow.com/questions/11641923/transfer-encoding-gzip-vs-content-encoding-gzip>
2930  
2931 See L<rfc7231, section 3.1.2.2|https://tools.ietf.org/html/rfc7231#section-3.1.2.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding>
2932  
2933 =head2 content_language
2934  
2935 # Content-Language: de-DE
2936 # Content-Language: en-US
2937 $h->content_language( 'en-GB' );
2938 # Content-Language: de-DE, en-CA
2939 $h->content_language( 'de-DE, en-CA' );
2940 $h->content_language( [qw( de-DE en-CA )] );
2941  
2942 Sets or gets the C<Cache-Language> header field value. It takes either a string or an array or array reference of values.
2943  
2944 There is no enforcement on the value provided, so it is up to you to set the proper value or values.
2945  
2946 See L<rfc7231, section 3.1.3.2|https://tools.ietf.org/html/rfc7231#section-3.1.3.2>, L<rfc5646|https://tools.ietf.org/html/rfc5646> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Language>
2947  
2948 =head2 content_length
2949  
2950 # Content-Length: 72
2951 $h->content_length(72);
2952  
2953 Sets or gets the C<Connection> header field value. It takes a numeric value.
2954  
2955 See L<rfc7230, section 3.3.2|https://tools.ietf.org/html/rfc7230#section-3.3.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length>
2956  
2957 =head2 content_location
2958  
2959 # Content-Location: /some/where/file.html
2960 $h->content_location( '/some/where/file.html' );
2961  
2962 Sets or gets the C<Connection> header field value. It takes a numeric value.
2963  
2964 See L<rfc7231, section 3.1.4.2|https://tools.ietf.org/html/rfc7231#section-3.1.4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location>
2965  
2966 =head2 content_range
2967  
2968 # Content-Range: bytes 200-1000/67589
2969 # Unsatisfiable range value
2970 # Content-Range: bytes */1234
2971  
2972 Sets or gets the C<Content-Range> header field value. It takes a string value.
2973  
2974 See also L<HTTP::Promise::Headers::ContentRange> to have a more granular control.
2975  
2976 See L<rfc7233, section 4.2|https://tools.ietf.org/html/rfc7233#section-4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range>
2977  
2978 =head2 content_security_policy
2979  
2980 # Content-Security-Policy: default-src 'self' http://example.com;
2981 # connect-src 'none';
2982 # Content-Security-Policy: connect-src http://example.com/;
2983 # script-src http://example.com/
2984  
2985 Sets or gets the C<Content-Security-Policy> header field value. It takes a string value.
2986  
2987 See also L<HTTP::Promise::Headers::ContentSecurityPolicy> to have a more granular control.
2988  
2989 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy>
2990  
2991 =head2 content_security_policy_report_only
2992  
2993 # Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/
2994  
2995 Sets or gets the C<Content-Security-Policy-Report-Only> header field value. It takes a string value of properly formatted header value.
2996  
2997 See also L<HTTP::Promise::Headers::ContentSecurityPolicyReportOnly> to have a more granular control.
2998  
2999 =head2 content_type
3000  
3001 This sets or gets the C<Content-Type> header value. It takes a string value.
3002  
3003 If a value is provided, this will set the header value. If no value is provided, this simply return the header field value.
3004  
3005 See also L<HTTP::Promise::Headers::ContentType> to have a more granular control.
3006  
3007 See also L<rfc7233, section 4.1|https://tools.ietf.org/html/rfc7233#section-4.1>, L<rfc7231, section 3.1.1.5|https://tools.ietf.org/html/rfc7231#section-3.1.1.5> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types>, and L<this Mozilla documentation too|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type>
3008  
3009 =head2 cross_origin_embedder_policy
3010  
3011 # Cross-Origin-Embedder-Policy: require-corp
3012 # Cross-Origin-Opener-Policy: same-origin
3013  
3014 This sets or gets the C<Cross-Origin-Embedder-Policy> header value. It takes a string value.
3015  
3016 It can have either of the following value: C<require-corp> or C<same-origin>
3017  
3018 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy>
3019  
3020 =head2 cross_origin_opener_policy
3021  
3022 # Cross-Origin-Opener-Policy: unsafe-none
3023 # Cross-Origin-Opener-Policy: same-origin-allow-popups
3024 # Cross-Origin-Opener-Policy: same-origin
3025  
3026 This sets or gets the C<Cross-Origin-Opener-Policy> header value. It takes a string value.
3027  
3028 It can have either of the following value: C<unsafe-none> or C<same-origin-allow-popups> or C<same-origin>
3029  
3030 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy>
3031  
3032 =head2 cross_origin_resource_policy
3033  
3034 This sets or gets the C<Cross-Origin-Resource-Policy> header value. It takes a string value.
3035  
3036 It can have either of the following value: C<same-site> or C<same-origin> or C<same-origin>
3037  
3038 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy>
3039  
3040 For more example: L<https://resourcepolicy.fyi/>
3041  
3042 =head2 cspro
3043  
3044 This is an alias for L</content_security_policy_report_only>
3045  
3046 =head2 date
3047  
3048 This sets or gets the C<Date> header value. It takes a date string value, a unix timestamp or a L<DateTime> value.
3049  
3050 If no value is provided, it returns the current value of the C<Date> header field as a L<DateTime> object.
3051  
3052 =head2 device_memory
3053  
3054 # Device-Memory: 1
3055  
3056 This sets or gets the C<Device-Memory> header value. It takes a number.
3057  
3058 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Device-Memory>
3059  
3060 =head2 digest
3061  
3062 # Digest: sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
3063 # Digest: sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=,unixsum=30637
3064  
3065 This sets or gets the C<Digest> header value. It takes either a string or an array or array reference of properly formatted values.
3066  
3067 See L<draft rfc|https://tools.ietf.org/html/draft-ietf-httpbis-digest-headers-05#section-3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Digest>
3068  
3069 =head2 dnt
3070  
3071 # DNT: 0
3072 # DNT: 1
3073 # DNT: null
3074  
3075 This sets or gets the C<DNT> header value. It takes a string value.
3076  
3077 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/DNT>
3078  
3079 =head2 early_data
3080  
3081 # Early-Data: 1
3082  
3083 This sets or gets the C<Early-Data> header value. It takes a string value.
3084  
3085 See also L<rfc8470, section 5.1|https://tools.ietf.org/html/rfc8470#section-5.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Early-Data>
3086  
3087 =head2 etag
3088  
3089 # ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
3090 # ETag: W/"0815"
3091  
3092 This sets or gets the C<Etag> header value. It takes a string of properly formatted value.
3093  
3094 See also L<rfc7232, section 2.3|https://tools.ietf.org/html/rfc7232#section-2.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag>
3095  
3096 =head2 expect
3097  
3098 This sets or gets the C<Expect> header value. It takes a string of properly formatted value, typically C<100-continue>
3099  
3100 For example, before sending a very large file:
3101  
3102 PUT /some/where HTTP/1.1
3103 Host: origin.example.com
3104 Content-Type: video/h264
3105 Content-Length: 1234567890987
3106 Expect: 100-continue
3107  
3108 If the server is ok, it would return a C<100 Continue>
3109  
3110 See also L<rfc7231, section 5.1.1|https://tools.ietf.org/html/rfc7231#section-5.1.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect>, L<interesting article|https://www.bram.us/2020/04/14/about-the-http-expect-100-continue-header/>
3111  
3112 =head2 expect_ct
3113  
3114 # Expect-CT: max-age=86400, enforce, report-uri="https://foo.example.com/report"
3115 $h->expect_ct( q{max-age=86400, enforce, report-uri="https://foo.example.com/report"} );
3116 $h->expect_ct( [qw( max-age=86400 enforce report-uri="https://foo.example.com/report" )] );
3117  
3118 This sets or gets the C<Expect-CT> header value. It takes a string of properly formatted value.
3119  
3120 See also L<HTTP::Promise::Headers::ExpectCT> to have a more granular control.
3121  
3122 See also L<rfc draft|https://tools.ietf.org/html/draft-ietf-httpbis-expect-ct-08> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect-CT>
3123  
3124 =head2 expires
3125  
3126 This sets or gets the C<Expires> header value. It takes a date string value, a unix timestamp or a L<DateTime> value.
3127  
3128 If no value is provided, it returns the current value of the C<Date> header field as a L<DateTime> object.
3129  
3130 For example:
3131  
3132 Expires: Wed, 21 Oct 2015 07:28:00 GMT
3133  
3134 See also L<rfc7234, section 5.3|https://tools.ietf.org/html/rfc7234#section-5.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires>
3135  
3136 =head2 expose_headers
3137  
3138 This sets or gets the C<Expose-Headers> header value. It takes either a string or an array or array reference of properly formatted values.
3139  
3140 For example:
3141  
3142 Access-Control-Expose-Headers: *, Authorization
3143  
3144 See also L<rfc7234, section 5.3|https://tools.ietf.org/html/rfc7234#section-5.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers>
3145  
3146 =head2 forwarded
3147  
3148 This sets or gets the C<Forwarded> header value. It takes either a string or an array or array reference of properly formatted values.
3149  
3150 See also L<HTTP::Promise::Headers::Forwarded> to have a more granular control.
3151  
3152 For example:
3153  
3154 Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43
3155 # Values from multiple proxy servers can be appended using a comma
3156 Forwarded: for=192.0.2.43, for=198.51.100.17
3157  
3158 See also L<rfc7239, section 4|https://tools.ietf.org/html/rfc7239#section-4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded>
3159  
3160 =head2 from
3161  
3162 This sets or gets the C<From> header value. It takes a string value.
3163  
3164 For example:
3165  
3166 From: webmaster@example.org
3167  
3168 See also L<rfc7231, section 5.5.1|https://tools.ietf.org/html/rfc7231#section-5.5.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/From>
3169  
3170 =head2 host
3171  
3172 This sets or gets the C<Host> header value. It takes a string value.
3173  
3174 For example:
3175  
3176 Host: dev.example.org
3177  
3178 See also L<rfc7230, section 5.4|https://tools.ietf.org/html/rfc7230#section-5.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host>
3179  
3180 =head2 if_match
3181  
3182 This sets or gets the C<If-Match> header value. It takes a string value.
3183  
3184 For example:
3185  
3186 If-Match: "bfc13a64729c4290ef5b2c2730249c88ca92d82d"
3187 If-Match: "67ab43", "54ed21", "7892dd"
3188 If-Match: *
3189  
3190 See also L<rfc7232, section 3.1|https://tools.ietf.org/html/rfc7232#section-3.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match>
3191  
3192 =head2 if_modified_since
3193  
3194 This sets or gets the C<If-Modified-Since> header value. It takes a date string value, a unix timestamp or a L<DateTime> value.
3195  
3196 If no value is provided, it returns the current value of the C<Date> header field as a L<DateTime> object.
3197  
3198 For example:
3199  
3200 If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
3201  
3202 See also L<rfc7232, section 3.3|https://tools.ietf.org/html/rfc7232#section-3.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since>
3203  
3204 =head2 if_none_match
3205  
3206 This sets or gets the C<If-None-Match> header value. It takes a string value.
3207  
3208 For example:
3209  
3210 If-None-Match: "bfc13a64729c4290ef5b2c2730249c88ca92d82d"
3211 If-None-Match: W/"67ab43", "54ed21", "7892dd"
3212 If-None-Match: *
3213  
3214 See also L<rfc7232, section 3.2|https://tools.ietf.org/html/rfc7232#section-3.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since>
3215  
3216 =head2 if_range
3217  
3218 This sets or gets the C<If-Range> header value. It takes a string value.
3219  
3220 For example:
3221  
3222 If-Range: Wed, 21 Oct 2015 07:28:00 GMT
3223  
3224 See also L<rfc7233, section 3.2|https://tools.ietf.org/html/rfc7233#section-3.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Range>
3225  
3226 =head2 if_unmodified_since
3227  
3228 This sets or gets the C<If-Unmodified-Since> header value. It takes a date string value, a unix timestamp or a L<DateTime> value.
3229  
3230 If no value is provided, it returns the current value of the C<Date> header field as a L<DateTime> object.
3231  
3232 For example:
3233  
3234 If-Unmodified-Since: Wed, 21 Oct 2015 07:28:00 GMT
3235  
3236 See also L<rfc7232, section 3.4|https://tools.ietf.org/html/rfc7232#section-3.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since>
3237  
3238 =head2 keep_alive
3239  
3240 This sets or gets the C<Keep-Alive> header value. It takes either a string or an array or array reference of properly formatted values.
3241  
3242 See also L<HTTP::Promise::Headers::KeepAlive> to have a more granular control.
3243  
3244 Example response containing a Keep-Alive header:
3245  
3246 HTTP/1.1 200 OK
3247 Connection: Keep-Alive
3248 Content-Encoding: gzip
3249 Content-Type: text/html; charset=utf-8
3250 Date: Thu, 11 Aug 2016 15:23:13 GMT
3251 Keep-Alive: timeout=5, max=1000
3252 Last-Modified: Mon, 25 Jul 2016 04:32:39 GMT
3253 Server: Apache
3254  
3255 See also L<rfc7230, section A.1.2|https://tools.ietf.org/html/rfc7230#section-A.1.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive>
3256  
3257 =head2 last_modified
3258  
3259 This sets or gets the C<Last-Modified> header value. It takes a date string value, a unix timestamp or a L<DateTime> value.
3260  
3261 If no value is provided, it returns the current value of the C<Date> header field as a L<DateTime> object.
3262  
3263 For example:
3264  
3265 Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
3266  
3267 See also L<rfc7232, section 2.2|https://tools.ietf.org/html/rfc7232#section-2.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified>
3268  
3269 =head2 link
3270  
3271 This sets or gets the C<Link> header value. It takes a string value.
3272  
3273 See also L<HTTP::Promise::Headers::Link> to have a more granular control.
3274  
3275 Example:
3276  
3277 Link: <https://example.com>; rel="preconnect"
3278  
3279 See also L<rfc8288, section 3|https://tools.ietf.org/html/rfc8288#section-3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link>
3280  
3281 =head2 location
3282  
3283 This sets or gets the C<Location> header value. It takes a string value.
3284  
3285 Example:
3286  
3287 Location: /index.html
3288  
3289 See also L<rfc7231, section 7.1.2|https://tools.ietf.org/html/rfc7231#section-7.1.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location>
3290  
3291 =head2 max_age
3292  
3293 This sets or gets the C<Location> header value. It takes a numeric value.
3294  
3295 Example:
3296  
3297 Access-Control-Max-Age: 600
3298  
3299 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age>
3300  
3301 =head2 nel
3302  
3303 This sets or gets the C<NEL> header value. It takes a string of properly formatted json value.
3304  
3305 Example:
3306  
3307 NEL: { "report_to": "name_of_reporting_group", "max_age": 12345, "include_subdomains": false, "success_fraction": 0.0, "failure_fraction": 1.0 }
3308  
3309 See also L<rfc8288, section 3|https://tools.ietf.org/html/rfc8288#section-3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/NEL>
3310  
3311 =for Pod::Coverage new_array
3312  
3313 =for Pod::Coverage new_field
3314  
3315 =for Pod::Coverage new_number
3316  
3317 =for Pod::Coverage new_scalar
3318  
3319 =head2 origin
3320  
3321 This sets or gets the C<Origin> header value. It takes a string of properly formatted json value.
3322  
3323 Example:
3324  
3325 Origin: http://dev.example.org:80
3326  
3327 See also L<rfc6454, section 7|https://tools.ietf.org/html/rfc6454#section-7> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin>
3328  
3329 =for Pod::Coverage pass_error
3330  
3331 =head2 proxy
3332  
3333 Sets or gets the URI used for the proxy. It returns a L<URI> object.
3334  
3335 =head2 proxy_authenticate
3336  
3337 This sets or gets the C<Proxy-Authenticate> header value. It takes a string value.
3338  
3339 Example:
3340  
3341 Proxy-Authenticate: Basic realm="Access to the internal site"
3342  
3343 See also L<rfc6454, section 7|https://tools.ietf.org/html/rfc6454#section-7> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authenticate>
3344  
3345 =head2 proxy_authorization
3346  
3347 This sets or gets the C<Proxy-Authorization> header value. It takes a string value.
3348  
3349 Example:
3350  
3351 Proxy-Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
3352  
3353 See also L<rfc7235, section 4.4|https://tools.ietf.org/html/rfc7235#section-4.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization>
3354  
3355 =head2 range
3356  
3357 This sets or gets the C<Range> header value. It takes a string value.
3358  
3359 See also L<HTTP::Promise::Headers::Range> to have a more granular control.
3360  
3361 Example:
3362  
3363 Range: bytes=200-1000, 2000-6576, 19000-
3364  
3365 See also L<rfc7233, section 3.1|https://tools.ietf.org/html/rfc7233#section-3.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range>
3366  
3367 =head2 referer
3368  
3369 This sets or gets the C<Referer> header value. It takes a string value.
3370  
3371 Example:
3372  
3373 Referer: https://dev.example.org/some/where
3374 Referer: https://example.org/page?q=123
3375 Referer: https://example.org/
3376  
3377 See also L<rfc7231, section 5.5.2|https://tools.ietf.org/html/rfc7231#section-5.5.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer>
3378  
3379 =head2 referrer
3380  
3381 This is an alias for L</referer>
3382  
3383 =head2 referrer_policy
3384  
3385 This sets or gets the C<Referrer-Policy> header value. It takes a string value.
3386  
3387 The allowed values can be: C<no-referrer>, C<no-referrer-when-downgrade>, C<origin>, C<origin-when-cross-origin>, C<same-origin>, C<strict-origin>, C<strict-origin-when-cross-origin>, C<unsafe-url>
3388  
3389 Example:
3390  
3391 Referrer-Policy: no-referrer
3392 # With fallback
3393 Referrer-Policy: no-referrer, strict-origin-when-cross-origin
3394  
3395 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy>
3396  
3397 =head2 request_headers
3398  
3399 This sets or gets the C<Access-Control-Request-Headers> header value. It takes a string value.
3400  
3401 Example:
3402  
3403 Access-Control-Request-Headers: X-PINGOTHER, Content-Type
3404  
3405 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Headers>
3406  
3407 =head2 request_method
3408  
3409 This sets or gets the C<Access-Control-Request-Method> header value. It takes a string value.
3410  
3411 Example:
3412  
3413 Access-Control-Request-Method: POST
3414  
3415 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Method>
3416  
3417 =head2 retry_after
3418  
3419 This sets or gets the C<Retry-After> header value. It takes a string value.
3420  
3421 Example:
3422  
3423 Retry-After: Wed, 21 Oct 2015 07:28:00 GMT
3424 Retry-After: 120
3425  
3426 See also L<rfc7231, section 7.1.3|https://tools.ietf.org/html/rfc7231#section-7.1.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After>
3427  
3428 =head2 save_data
3429  
3430 This sets or gets the C<Save-Data> header value. It takes a string value.
3431  
3432 The value can be either C<on> or C<off>
3433  
3434 Example:
3435  
3436 Save-Data: on
3437  
3438 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Save-Data>
3439  
3440 =head2 server
3441  
3442 This sets or gets the C<Server> header value. It takes a string value.
3443  
3444 Example:
3445  
3446 Server: Apache/2.4.1 (Unix)
3447  
3448 See also L<rfc7231, section 7.4.2|https://tools.ietf.org/html/rfc7231#section-7.4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server>
3449  
3450 =head2 server_timing
3451  
3452 This sets or gets the C<Server> header value. It takes a string value.
3453  
3454 See also L<HTTP::Promise::Headers::ServerTiming> to have a more granular control.
3455  
3456 Example:
3457  
3458 # Single metric without value
3459 Server-Timing: missedCache
3460  
3461 # Single metric with value
3462 Server-Timing: cpu;dur=2.4
3463  
3464 # Single metric with description and value
3465 Server-Timing: cache;desc="Cache Read";dur=23.2
3466  
3467 # Two metrics with value
3468 Server-Timing: db;dur=53, app;dur=47.2
3469  
3470 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing>
3471  
3472 =head2 set_cookie
3473  
3474 This sets or gets the C<Set-Cookie> header value. It takes a string value.
3475  
3476 See also L<Cookie> to have a more granular control.
3477  
3478 Example:
3479  
3480 Set-Cookie: sessionId=38afes7a8
3481 Set-Cookie: __Secure-ID=123; Secure; Domain=example.com
3482 Set-Cookie: __Host-ID=123; Secure; Path=/
3483  
3484 See also L<rfc6265, section 4.1|https://tools.ietf.org/html/rfc6265#section-4.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie>
3485  
3486 =head2 sourcemap
3487  
3488 This sets or gets the C<SourceMap> header value. It takes a string value.
3489  
3490 Example:
3491  
3492 SourceMap: /path/to/file.js.map
3493  
3494 See also L<draft specifications|https://sourcemaps.info/spec.html> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/SourceMap>
3495  
3496 =head2 strict_transport_security
3497  
3498 This sets or gets the C<Strict-Transport-Security> header value. It takes a string value.
3499  
3500 See also L<HTTP::Promise::Headers::StrictTransportSecurity> to have a more granular control.
3501  
3502 Example:
3503  
3504 Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
3505  
3506 See also L<rfc6797, section 6.1|https://tools.ietf.org/html/rfc6797#section-6.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security>
3507  
3508 =head2 te
3509  
3510 This sets or gets the C<TE> header value. It takes a string value.
3511  
3512 See also L<HTTP::Promise::Headers::TE> to have a more granular control.
3513  
3514 Example:
3515  
3516 TE: deflate
3517 TE: gzip
3518 TE: trailers
3519  
3520 # Multiple directives, weighted with the quality value syntax:
3521 TE: trailers, deflate;q=0.5
3522  
3523 Notably, the value C<trailers> means the HTTP client support trailers, which are a set of headers sent after the body.
3524  
3525 Encoding header fields and their nuances:
3526  
3527 =over 4
3528  
3529 =item C<Accept-Encoding>
3530  
3531 The encodings accepted by the client.
3532  
3533 =item C<Content-Encoding>
3534  
3535 Contains the encodings that have been applied to the content, before transport
3536  
3537 =item C<TE>
3538  
3539 The encodings the user agent accepts.
3540  
3541 =item C<Transfer-Encoding>
3542  
3543 The encoding applied during transfer, such as C<chunked>
3544  
3545 =back
3546  
3547 See also L</transfer_encoding>, L<rfc7230, section 4.3|https://tools.ietf.org/html/rfc7230#section-4.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/TE>, L<article on trailers|https://httptoolkit.tech/blog/http-wtf/#http-trailers>
3548  
3549 =head2 timing_allow_origin
3550  
3551 This sets or gets the C<Timing-Allow-Origin> header value. It takes a string value.
3552  
3553 Example:
3554  
3555 Timing-Allow-Origin: *
3556 Timing-Allow-Origin: https://dev.example.org, https://example.com
3557  
3558 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin>
3559  
3560 =head2 title
3561  
3562 Sets or gets the C<Title> of the HTML document if that were the case. This is here for legacy.
3563  
3564 =head2 tk
3565  
3566 This sets or gets the deprecated C<Tk> header value. It takes a string value.
3567  
3568 Example:
3569  
3570 Tk: N
3571  
3572 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Tk>
3573  
3574 =head2 trailer
3575  
3576 This sets or gets the C<Trailer> header value. It takes a string value.
3577  
3578 Example:
3579  
3580 Trailer: Expires
3581  
3582 See also L<rfc7230, section 4.4|https://tools.ietf.org/html/rfc7230#section-4.4>, L<rfc7230, section 4.1.2|https://tools.ietf.org/html/rfc7230#section-4.1.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer>
3583  
3584 =head2 transfer_encoding
3585  
3586 This sets or gets the C<Transfer-Encoding> header value. It takes a string value.
3587  
3588 Example:
3589  
3590 Transfer-Encoding: chunked
3591 Transfer-Encoding: compress
3592 Transfer-Encoding: deflate
3593 Transfer-Encoding: gzip
3594  
3595 # Several values can be listed, separated by a comma
3596 Transfer-Encoding: gzip, chunked
3597  
3598 Encoding header fields and their nuances:
3599  
3600 =over 4
3601  
3602 =item C<Accept-Encoding>
3603  
3604 The encodings accepted by the client.
3605  
3606 =item C<Content-Encoding>
3607  
3608 Contains the encodings that have been applied to the content, before transport
3609  
3610 =item C<TE>
3611  
3612 The encodings the user agent accepts.
3613  
3614 =item C<Transfer-Encoding>
3615  
3616 The encoding applied during transfer, such as C<chunked>
3617  
3618 =back
3619  
3620 See L<rfc7230, section 3.3.1|https://tools.ietf.org/html/rfc7230#section-3.3.1>:
3621 "Unlike Content-Encoding (L<Section 3.1.2.1 of [RFC7231]|https://tools.ietf.org/html/rfc7231#section-3.1.2.1>), Transfer-Encoding is a property of the message, not of the representation"
3622  
3623 See also L</te>, L<rfc7230, section 3.3.1|https://tools.ietf.org/html/rfc7230#section-3.3.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding> and L<Wikipedia|https://en.wikipedia.org/wiki/Chunked_transfer_encoding>
3624  
3625 =head2 upgrade
3626  
3627 This sets or gets the C<Upgrade> header value. It takes a string value.
3628  
3629 Example:
3630  
3631 Connection: upgrade
3632 Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
3633  
3634 Connection: Upgrade
3635 Upgrade: websocket
3636  
3637 See also L<rfc7230, section 6.7|https://tools.ietf.org/html/rfc7230#section-6.7>, L<rfc7231, section 6.6.15|https://tools.ietf.org/html/rfc7231#section-6.6.15>, L<rfc7240, section 8.1.1|https://tools.ietf.org/html/rfc7240#section-8.1.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade>
3638  
3639 =head2 upgrade_insecure_requests
3640  
3641 This sets or gets the C<Upgrade-Insecure-Requests> header value. It takes a string value.
3642  
3643 Example:
3644  
3645 Upgrade-Insecure-Requests: 1
3646  
3647 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade-Insecure-Requests>
3648  
3649 =head2 user_agent
3650  
3651 This sets or gets the C<User-Agent> header value. It takes a string value.
3652  
3653 Example:
3654  
3655 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0
3656 User-Agent: curl/7.64.1
3657  
3658 See also L<rfc7231, section 5.5.3|https://tools.ietf.org/html/rfc7231#section-5.5.3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent>
3659  
3660 =head2 vary
3661  
3662 This sets or gets the C<Vary> header value. It takes a string value.
3663  
3664 Example:
3665  
3666 Vary: *
3667 Vary: Accept-Encoding, User-Agent
3668  
3669 See also L<rfc7231, section 7.1.4|https://tools.ietf.org/html/rfc7231#section-7.1.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary>
3670  
3671 =head2 via
3672  
3673 This sets or gets the C<Via> header value. It takes a string value.
3674  
3675 Example:
3676  
3677 Via: 1.1 vegur
3678 Via: HTTP/1.1 GWA
3679 Via: 1.0 fred, 1.1 p.example.net
3680  
3681 See also L<rfc7230, section 5.7.1|https://tools.ietf.org/html/rfc7230#section-5.7.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Via>
3682  
3683 =head2 want_digest
3684  
3685 This sets or gets the C<Want-Digest> header value. It takes a string value.
3686  
3687 Example:
3688  
3689 Want-Digest: sha-256
3690 Want-Digest: SHA-512;q=0.3, sha-256;q=1, md5;q=0
3691  
3692 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Want-Digest>
3693  
3694 =head2 warning
3695  
3696 This sets or gets the C<Warning> header value. It takes a string value.
3697  
3698 Example:
3699  
3700 Warning: 110 anderson/1.3.37 "Response is stale"
3701  
3702 See also L<rfc7234, section 5.5|https://tools.ietf.org/html/rfc7234#section-5.5> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning>
3703  
3704 =head2 www_authenticate
3705  
3706 This sets or gets the C<WWW-Authenticate> header value. It takes a string value.
3707  
3708 Example:
3709  
3710 WWW-Authenticate: Basic realm="Access to the staging site", charset="UTF-8"
3711 WWW-Authenticate: Digest
3712 realm="http-auth@example.org",
3713 qop="auth, auth-int",
3714 algorithm=SHA-256,
3715 nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
3716 opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
3717 WWW-Authenticate: Digest
3718 realm="http-auth@example.org",
3719 qop="auth, auth-int",
3720 algorithm=MD5,
3721 nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
3722 opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
3723  
3724 See also L<rfc7235, section 4.1|https://tools.ietf.org/html/rfc7235#section-4.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate>
3725  
3726 =head2 x
3727  
3728 Sets or gets an arbitrary C<X-*> header. For example:
3729  
3730 $h->x( 'Spip-Cache' => 3600 );
3731  
3732 would set the C<X-Spip-Cache> header value to C<3600>
3733  
3734 my $value = $h->x( 'Spip-Cache' );
3735  
3736 =head2 x_content_type_options
3737  
3738 This sets or gets the C<X-Content-Type-Options> header value. It takes a string value.
3739  
3740 Example:
3741  
3742 X-Content-Type-Options: nosniff
3743  
3744 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options>
3745  
3746 =head2 x_dns_prefetch_control
3747  
3748 This sets or gets the C<X-DNS-Prefetch-Control> header value. It takes a string value.
3749  
3750 Example:
3751  
3752 X-DNS-Prefetch-Control: on
3753 X-DNS-Prefetch-Control: off
3754  
3755 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control>
3756  
3757 =head2 x_forwarded_for
3758  
3759 This sets or gets the C<X-Forwarded-For> header value. It takes a string value.
3760  
3761 Example:
3762  
3763 X-Forwarded-For: 2001:db8:85a3:8d3:1319:8a2e:370:7348
3764 X-Forwarded-For: 203.0.113.195
3765 X-Forwarded-For: 203.0.113.195, 2001:db8:85a3:8d3:1319:8a2e:370:7348
3766  
3767 See also L</host>, L</forwarded>, L</x_forwarded_host>, L</x_forwarded_proto>, L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For>
3768  
3769 =head2 x_forwarded_host
3770  
3771 This sets or gets the C<X-Forwarded-Host> header value. It takes a string value.
3772  
3773 Example:
3774  
3775 X-Forwarded-Host: id42.example-cdn.com
3776  
3777 See also L</host>, L</forwarded>, L</x_forwarded_for>, L</x_forwarded_proto>, L<https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host>
3778  
3779 =head2 x_forwarded_proto
3780  
3781 This sets or gets the C<X-Forwarded-Proto> header value. It takes a string value.
3782  
3783 Example:
3784  
3785 X-Forwarded-Proto: https
3786  
3787 See also L</host>, L</forwarded>, L</x_forwarded_for>, L</x_forwarded_host>, L<https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto>
3788  
3789 =head2 x_frame_options
3790  
3791 This sets or gets the C<X-Frame-Options> header value. It takes a string value.
3792  
3793 Example:
3794  
3795 X-Frame-Options: DENY
3796 X-Frame-Options: SAMEORIGIN
3797  
3798 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options>
3799  
3800 =head2 x_xss_protection
3801  
3802 This sets or gets the C<X-XSS-Protection> header value. It takes a string value.
3803  
3804 Example:
3805  
3806 X-XSS-Protection: 0
3807 X-XSS-Protection: 1
3808 X-XSS-Protection: 1; mode=block
3809 X-XSS-Protection: 1; report=https://example.org/some/where
3810  
3811 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection>
3812  
3813 =for Pod::Coverage STORABLE_thaw_post_processing
3814  
3815 =head1 THREAD-SAFETY
3816  
3817 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
3818  
3819 =head1 AUTHOR
3820  
3821 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
3822  
3823 =head1 SEE ALSO
3824  
3825 L<Mozilla documentation on HTTP headers|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers>
3826  
3827 L<HTTP::Promise::Headers::AcceptEncoding>, L<HTTP::Promise::Headers::AcceptLanguage>, L<HTTP::Promise::Headers::Accept>, L<HTTP::Promise::Headers::AltSvc>, L<HTTP::Promise::Headers::CacheControl>, L<HTTP::Promise::Headers::ClearSiteData>, L<HTTP::Promise::Headers::ContentDisposition>, L<HTTP::Promise::Headers::ContentRange>, L<HTTP::Promise::Headers::ContentSecurityPolicy>, L<HTTP::Promise::Headers::ContentSecurityPolicyReportOnly>, L<HTTP::Promise::Headers::ContentType>, L<HTTP::Promise::Headers::Cookie>, L<HTTP::Promise::Headers::ExpectCT>, L<HTTP::Promise::Headers::Forwarded>, L<HTTP::Promise::Headers::Generic>, L<HTTP::Promise::Headers::KeepAlive>, L<HTTP::Promise::Headers::Link>, L<HTTP::Promise::Headers::Range>, L<HTTP::Promise::Headers::ServerTiming>, L<HTTP::Promise::Headers::StrictTransportSecurity>, L<HTTP::Promise::Headers::TE>
3828  
3829 L<rfc7230, section 3.2 on headers field names|https://tools.ietf.org/html/rfc7230#section-3.2>,
3830 L<rfc6838 on mime types|https://tools.ietf.org/html/rfc6838>
3831  
3832 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
3833  
3834 =head1 COPYRIGHT & LICENSE
3835  
3836 Copyright(c) 2022 DEGUEST Pte. Ltd.
3837  
3838 All rights reserved
3839 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
3840  
3841 =cut