| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Generic.pm | ||
| 3 | ## Version v0.1.3 | ||
| 4 | ## Copyright(c) 2025 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/05/06 | ||
| 7 | ## Modified 2025/10/15 | ||
| 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::Generic; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTTP::Promise' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $VERSION $QV_ELEMENT $QV_VALUE ); | ||
| 22 | use Encode (); | ||
| 23 | use URI::Escape::XS (); | ||
| 24 | use Wanted; | ||
| 25 | use overload ( | ||
| 26 | '""' => 'as_string', | ||
| 27 | 'bool' => sub{1}, | ||
| 28 | # No fallback on purpose | ||
| 29 | ); | ||
| 30 | # Accept: audio/*; q=0.2, audio/basic | ||
| 31 | our $QV_ELEMENT = qr/(?:[^\;\,]+)/; | ||
| 32 | our $QV_VALUE = qr/(?:0(?:\.[0-9]{0,3})?|1(?:\.0{0,3})?)/; | ||
| 33 | our $VERSION = 'v0.1.3'; | ||
| 34 | }; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | |||
| 39 | sub as_string { return( shift->value ); } | ||
| 40 | |||
| 41 | sub field_name { return( shift->_set_get_scalar( '_name', @_ ) ); } | ||
| 42 | |||
| 43 | sub uri_escape_utf8 { return( URI::Escape::XS::uri_escape( Encode::encode( 'UTF-8', $_[1] ) ) ); } | ||
| 44 | |||
| 45 | # By default and superseded by inheriting classes such as Content-Type that has more | ||
| 46 | # elaborate value with parameters | ||
| 47 | sub value { return( shift->_set_get_scalar( '_value', @_ ) ); } | ||
| 48 | |||
| 49 | sub _field_name { return( shift->_set_get_scalar( '_name', @_ ) ); } | ||
| 50 | |||
| 51 | # rfc2231 <https://tools.ietf.org/html/rfc2231> | ||
| 52 | sub _filename_decode | ||
| 53 | { | ||
| 54 | my $self = shift( @_ ); | ||
| 55 | my $fname = shift( @_ ); | ||
| 56 | $self->_load_class( 'HTTP::Promise::Headers' ) || return( $self->pass_error ); | ||
| 57 | my( $new_fname, $charset, $lang ) = HTTP::Promise::Headers->decode_filename( $fname ); | ||
| 58 | if( defined( $new_fname ) ) | ||
| 59 | { | ||
| 60 | $fname = $new_fname; | ||
| 61 | } | ||
| 62 | return( wantarray() ? ( $fname, $charset, $lang ) : $fname ); | ||
| 63 | } | ||
| 64 | |||
| 65 | # rfc2231 <https://tools.ietf.org/html/rfc2231> | ||
| 66 | sub _filename_encode | ||
| 67 | { | ||
| 68 | my $self = shift( @_ ); | ||
| 69 | my $fname = shift( @_ ); | ||
| 70 | my $lang = shift( @_ ); | ||
| 71 | if( $fname =~ /[^\x00-\x7f]/ ) | ||
| 72 | { | ||
| 73 | $lang = '' if( !defined( $lang ) ); | ||
| 74 | return( sprintf( "UTF-8'${lang}'%s", $self->uri_escape_utf8( $fname ) ) ); | ||
| 75 | } | ||
| 76 | # Nothing to be done. We return undef on purpose to indicate nothing was done | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _hv { return( shift->_set_get_object_without_init( '_hv', 'Module::Generic::HeaderValue', @_ ) ); } | ||
| 81 | |||
| 82 | sub _hv_as_string | ||
| 83 | { | ||
| 84 | my $self = shift( @_ ); | ||
| 85 | my $hv = $self->_hv; | ||
| 86 | return( '' ) if( !$hv ); | ||
| 87 | return( $hv->as_string( @_ ) ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _get_header_value_object | ||
| 91 | { | ||
| 92 | my $self = shift( @_ ); | ||
| 93 | $self->_load_class( 'Module::Generic::HeaderValue' ) || | ||
| 94 | return( $self->pass_error ); | ||
| 95 | my $hv = Module::Generic::HeaderValue->new( shift( @_ ) ) || | ||
| 96 | return( $self->pass_error( Module::Generic::HeaderValue->error ) ); | ||
| 97 | return( $hv ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _make_boundary { return( Data::UUID->new->create_str ); } | ||
| 101 | |||
| 102 | sub _new_hv | ||
| 103 | { | ||
| 104 | my $self = shift( @_ ); | ||
| 105 | $self->_load_class( 'Module::Generic::HeaderValue' ) || return( $self->pass_error ); | ||
| 106 | return( Module::Generic::HeaderValue->new( @_ ) ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _new_qv_object | ||
| 110 | { | ||
| 111 | my $self = shift( @_ ); | ||
| 112 | my $o = HTTP::Promise::Field::QualityValue->new( @_ ); | ||
| 113 | return( $self->pass_error( HTTP::Promise::Field::QualityValue->error ) ) if( !defined( $o ) ); | ||
| 114 | return( $o ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _parse_header_value | ||
| 118 | { | ||
| 119 | my $self = shift( @_ ); | ||
| 120 | my $this = shift( @_ ); | ||
| 121 | return( $self->error( "No header value was provided to parse." ) ) if( !defined( $this ) || !length( "$this" ) ); | ||
| 122 | $self->_load_class( 'Module::Generic::HeaderValue' ) || | ||
| 123 | return( $self->pass_error ); | ||
| 124 | my $hv = Module::Generic::HeaderValue->new_from_header( $this, @_ ) || | ||
| 125 | return( $self->pass_error( Module::Generic::HeaderValue->error ) ); | ||
| 126 | return( $hv ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # rfc7231, section 5.3.1 | ||
| 130 | # <https://tools.ietf.org/html/rfc7231#section-5.3.1> | ||
| 131 | sub _parse_quality_value | ||
| 132 | { | ||
| 133 | my $self = shift( @_ ); | ||
| 134 | my $str = shift( @_ ); | ||
| 135 | return( $self->error( "No header value was provided to parse." ) ) if( !defined( $str ) || !length( "$str" ) ); | ||
| 136 | # No blank | ||
| 137 | $str =~ s/[[:blank:]\h]]+//g; | ||
| 138 | my $choices = $self->new_array; | ||
| 139 | # Credits: HTTP::AcceptLanguage from Kazuhiro Osawa | ||
| 140 | for my $def ( split( /,[[:blank:]\h]*/, $str ) ) | ||
| 141 | { | ||
| 142 | my( $element, $quality ) = $def =~ /\A($QV_ELEMENT)(?:;[[:blank:]\h]*[qQ]=($QV_VALUE))?\z/; | ||
| 143 | # rfc7231, section 5.3.1: | ||
| 144 | # "If no "q" parameter is present, the default weight is 1." | ||
| 145 | # rfc7231, section 5.3.5 | ||
| 146 | # "no value is the same as q=1" | ||
| 147 | # $quality = 1 unless( defined( $quality ) ); | ||
| 148 | # next unless( $element && $quality > 0 ); | ||
| 149 | next unless( $element ); | ||
| 150 | my $qv = $self->_new_qv_object( $element => $quality ); | ||
| 151 | $choices->push( $qv ); | ||
| 152 | } | ||
| 153 | return( $choices ); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _qstring_join | ||
| 157 | { | ||
| 158 | my $self = shift( @_ ); | ||
| 159 | my @parts = (); | ||
| 160 | foreach my $s ( @_ ) | ||
| 161 | { | ||
| 162 | $s =~ s/^"//; | ||
| 163 | $s =~ s/(?!\\)"$//; | ||
| 164 | $s =~ s/(?!\\)\"/\\"/g; | ||
| 165 | push( @parts, qq{"${s}"} ); | ||
| 166 | } | ||
| 167 | return( join( ', ', @parts ) ); | ||
| 168 | } | ||
| 169 | |||
| 170 | # Returns an array of tokens that were initially surrounded by double quotes, and | ||
| 171 | # separated by comma even if they contained double quotes inside. | ||
| 172 | # Example for Clear-Site-Data header field: | ||
| 173 | # "cache", "cookies", "storage", "executionContexts" | ||
| 174 | # "cache\"", "oh "la" la", "storage\", \"", "executionContexts" | ||
| 175 | sub _qstring_split | ||
| 176 | { | ||
| 177 | my $self = shift( @_ ); | ||
| 178 | my $str = shift( @_ ); | ||
| 179 | my @parts = split( /(?<=(?<!\\)\")[[:blank:]\h]*,[[:blank:]\h]*(?=\")/, $str ); | ||
| 180 | for( @parts ) | ||
| 181 | { | ||
| 182 | #substr( $_, 0, 1, '' ); | ||
| 183 | #substr( $_, -1, 1, '' ); | ||
| 184 | # s/^"|"$//g; | ||
| 185 | s/^"//; | ||
| 186 | s/"$//; | ||
| 187 | } | ||
| 188 | return( @parts ); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub _qv_add | ||
| 192 | { | ||
| 193 | my $self = shift( @_ ); | ||
| 194 | my( $elem, $val ) = @_; | ||
| 195 | my $qv = HTTP::Promise::Field::QualityValue->new( $elem => $val ) || | ||
| 196 | return( $self->pass_error( HTTP::Promise::Field::QualityValue->error ) ); | ||
| 197 | $self->elements->push( $qv ); | ||
| 198 | return( $qv ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _qv_as_string | ||
| 202 | { | ||
| 203 | my $self = shift( @_ ); | ||
| 204 | my $all = $self->elements; | ||
| 205 | return( '' ) if( $all->is_empty ); | ||
| 206 | my $res = $all->map(sub{ $_->as_string }); | ||
| 207 | return( $res->join( ', ' )->scalar ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _qv_elements { return( shift->_set_get_object_array_object( '_qv_elements', 'HTTP::Promise::Field::QualityValue', @_ ) ); } | ||
| 211 | |||
| 212 | sub _qv_get | ||
| 213 | { | ||
| 214 | my $self = shift( @_ ); | ||
| 215 | my $this = shift( @_ ); | ||
| 216 | return( $self->error( "No a property name to get was provided." ) ) if( !defined( $this ) || !length( "$this" ) ); | ||
| 217 | my $all = $self->elements; | ||
| 218 | if( $self->_is_a( $this => 'HTTP::Promise::Field::QualityValue' ) ) | ||
| 219 | { | ||
| 220 | my $pos = $all->pos( $this ); | ||
| 221 | return( $all->[$pos] ) if( defined( $pos ) ); | ||
| 222 | } | ||
| 223 | else | ||
| 224 | { | ||
| 225 | foreach( @$all ) | ||
| 226 | { | ||
| 227 | return( $_ ) if( $_->element eq $this ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | return( '' ); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub _qv_match | ||
| 234 | { | ||
| 235 | my $self = shift( @_ ); | ||
| 236 | my $this = shift( @_ ); | ||
| 237 | return( '' ) if( !defined( $this ) || !length( "$this" ) ); | ||
| 238 | $this = [split( /(?:[[:blank:]]+|[[:blank:]]*\,[[:blank:]]*)/, "$this" )] if( !$self->_is_array( $this ) && ( !ref( $this ) || overload::Method( $this => '""' ) ) ); | ||
| 239 | return( $self->error( "Invalid argument provided. Provide either an array reference or a string or something that stringifies." ) ) if( !$self->_is_array( $this ) ); | ||
| 240 | my $ordered = [map( lc( $_ ), @$this )]; | ||
| 241 | return( '' ) if( !scalar( @$ordered ) ); | ||
| 242 | my $acceptables = $self->can( 'sort' ) ? $self->sort : $self->_qv_sort; | ||
| 243 | my $ok = $self->new_array; | ||
| 244 | my $seen = {}; | ||
| 245 | foreach my $e ( @$acceptables ) | ||
| 246 | { | ||
| 247 | my $e_lc = $e->element->lc; | ||
| 248 | if( $e->element->index( '*' ) != -1 ) | ||
| 249 | { | ||
| 250 | my $wildcard_ok = $self->_qv_match_wildcard( $e_lc => $ordered, $this ); | ||
| 251 | return( $self->pass_error ) if( !defined( $wildcard_ok ) ); | ||
| 252 | $ok->push( $wildcard_ok->list ) if( !$wildcard_ok->is_empty ); | ||
| 253 | } | ||
| 254 | else | ||
| 255 | { | ||
| 256 | for( my $i = 0; $i < scalar( @$ordered ); $i++ ) | ||
| 257 | { | ||
| 258 | if( $e_lc eq $ordered->[$i] ) | ||
| 259 | { | ||
| 260 | # We'll return the caller's original value, not the lowercase one we use for comparison | ||
| 261 | $ok->push( $this->[$i] ); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | return( $ok->unique ); | ||
| 267 | } | ||
| 268 | |||
| 269 | # Works for language and content-type and content-encoding | ||
| 270 | sub _qv_match_wildcard | ||
| 271 | { | ||
| 272 | my $self = shift( @_ ); | ||
| 273 | # $proposals contain the value offered in lower case, whereas $original contains | ||
| 274 | # the original value and we return our value from there. Both $proposals and $original | ||
| 275 | # are of the same size. | ||
| 276 | my( $acceptable, $proposals, $original, $seen ) = @_; | ||
| 277 | return( $self->error( "Bad arguments. Usage: \$h->_qv_match_wildcard( \$acceptable, \$proposals, \$original )" ) ) unless( @_ == 3 ); | ||
| 278 | return( $self->error( "This is not a wildcard acceptable value." ) ) if( $acceptable->index( '*' ) == -1 ); | ||
| 279 | return( $self->error( "Proposed values must be an array reference." ) ) unless( $self->_is_array( $proposals ) ); | ||
| 280 | return( $self->error( "Original array of proposed values must be an array reference." ) ) unless( $self->_is_array( $original ) ); | ||
| 281 | my $ok = $self->new_array; | ||
| 282 | if( $acceptable->index( '/' ) != -1 ) | ||
| 283 | { | ||
| 284 | my( $main, $sub ) = $acceptable->element->split( qr/\// ); | ||
| 285 | for( my $i = 0; $i < scalar( @$proposals ); $i++ ) | ||
| 286 | { | ||
| 287 | my $supported = $proposals->[$i]; | ||
| 288 | my( $this_main, $this_sub ) = split( /\//, "$supported", 2 ); | ||
| 289 | if( $main eq '*' ) | ||
| 290 | { | ||
| 291 | if( $sub eq '*' ) | ||
| 292 | { | ||
| 293 | $ok->push( $original->[$i] ); | ||
| 294 | } | ||
| 295 | else | ||
| 296 | { | ||
| 297 | $ok->push( $original->[$i] ) if( $this_sub eq $sub ); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | elsif( $main eq $this_main ) | ||
| 301 | { | ||
| 302 | if( $sub eq '*' ) | ||
| 303 | { | ||
| 304 | $ok->push( $original->[$i] ); | ||
| 305 | } | ||
| 306 | else | ||
| 307 | { | ||
| 308 | $ok->push( $original->[$i] ) if( $this_sub eq $sub ); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | } | ||
| 313 | # simply return the proposal value since anything goes | ||
| 314 | else | ||
| 315 | { | ||
| 316 | $ok->push( $original->[0] ); | ||
| 317 | } | ||
| 318 | return( $ok ); | ||
| 319 | } | ||
| 320 | |||
| 321 | sub _qv_remove | ||
| 322 | { | ||
| 323 | my $self = shift( @_ ); | ||
| 324 | my $this = shift( @_ ); | ||
| 325 | my $all = $self->elements; | ||
| 326 | if( $self->_is_a( $this => 'HTTP::Promise::Field::QualityValue' ) ) | ||
| 327 | { | ||
| 328 | return( $all->delete( $this ) ); | ||
| 329 | } | ||
| 330 | else | ||
| 331 | { | ||
| 332 | my $e; | ||
| 333 | for( my $i = 0; $i < scalar( @$all ); $i++ ) | ||
| 334 | { | ||
| 335 | if( $all->[$i]->element eq "$this" ) | ||
| 336 | { | ||
| 337 | $e = $all->splice( $i, 1 ); | ||
| 338 | last; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | return( $e ); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | sub _qv_sort | ||
| 346 | { | ||
| 347 | my $self = shift( @_ ); | ||
| 348 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 349 | $opts->{asc} = 0 if( !exists( $opts->{asc} ) ); | ||
| 350 | my $all = $self->elements; | ||
| 351 | my $sorted = $opts->{asc} | ||
| 352 | ? $all->sort(sub{ ( $_[0]->value // 1 ) <=> ( $_[1]->value // 1 ) }) | ||
| 353 | : $all->sort(sub{ ( $_[1]->value // 1 ) <=> ( $_[0]->value // 1 ) }); | ||
| 354 | $self->elements( $sorted ); | ||
| 355 | return( $sorted ); | ||
| 356 | } | ||
| 357 | |||
| 358 | sub _set_get_param_boolean | ||
| 359 | { | ||
| 360 | my $self = shift( @_ ); | ||
| 361 | my $name = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 362 | my $hv = $self->_hv || return( $self->error( "Header value object could not be found!" ) ); | ||
| 363 | if( @_ ) | ||
| 364 | { | ||
| 365 | my $v = shift( @_ ); | ||
| 366 | if( $v ) | ||
| 367 | { | ||
| 368 | $hv->param( $name => undef ); | ||
| 369 | } | ||
| 370 | else | ||
| 371 | { | ||
| 372 | $hv->params->delete( $name ); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | return( $hv->param( $name ) ); | ||
| 376 | } | ||
| 377 | |||
| 378 | sub _set_get_param | ||
| 379 | { | ||
| 380 | my $self = shift( @_ ); | ||
| 381 | my $name = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 382 | my $hv = $self->_hv; | ||
| 383 | # If the HeaderValue object is not een set, and the caller just want to retrieve the | ||
| 384 | # value of a property, we return an empty string (undef is for errors) | ||
| 385 | return( '' ) if( !scalar( @_ ) && !$hv ); | ||
| 386 | return( $self->error( "Header value object (Module::Generic::HeaderValue) could not be found!" ) ) if( !$hv ); | ||
| 387 | if( @_ ) | ||
| 388 | { | ||
| 389 | $hv->param( $name => shift( @_ ) ); | ||
| 390 | } | ||
| 391 | return( $hv->param( $name ) ); | ||
| 392 | } | ||
| 393 | |||
| 394 | sub _set_get_params | ||
| 395 | { | ||
| 396 | my $self = shift( @_ ); | ||
| 397 | my $hv = $self->_hv || return( $self->error( "Header value object could not be found!" ) ); | ||
| 398 | my $params = $hv->params; | ||
| 399 | if( @_ ) | ||
| 400 | { | ||
| 401 | while( my( $n, $v ) = splice( @_, 0, 2 ) ) | ||
| 402 | { | ||
| 403 | $params->set( $n => $v ); | ||
| 404 | } | ||
| 405 | } | ||
| 406 | else | ||
| 407 | { | ||
| 408 | return( $params ); | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | sub _set_get_properties_as_string | ||
| 413 | { | ||
| 414 | my $self = shift( @_ ); | ||
| 415 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 416 | my $sep = $opts->{separator} || $opts->{sep} || ','; | ||
| 417 | my $eq = $opts->{equal} || '='; | ||
| 418 | my $params = $self->params; | ||
| 419 | my $props = $self->properties; | ||
| 420 | my $quotes = {}; | ||
| 421 | $quotes = $self->_needs_quotes if( $self->can( '_needs_quotes' ) ); | ||
| 422 | my @res = (); | ||
| 423 | no overloading '""'; | ||
| 424 | foreach( @$params ) | ||
| 425 | { | ||
| 426 | if( !exists( $props->{ $_ } ) ) | ||
| 427 | { | ||
| 428 | # warnings::warn( "Property is in our stack, but not in our repository of properties, skipping.\n" ) if( warnings::enabled( 'HTTP::Promise' ) ); | ||
| 429 | # warn( "Property is in our stack, but not in our repository of properties, skipping.\n" ) if( $self->_warnings_is_enabled ); | ||
| 430 | warn( "Property \"$_\" is in our stack, but not in our repository of properties, skipping.\n" ); | ||
| 431 | next; | ||
| 432 | } | ||
| 433 | # If the property exists in our repo, but has no value it is a boolean | ||
| 434 | push( @res, defined( $props->{ $_ } ) ? sprintf( "$_${eq}%s", ( $quotes->{ $_ } ? '"' : '' ) . $props->{ $_ } . ( $quotes->{ $_ } ? '"' : '' ) ) : $_ ); | ||
| 435 | } | ||
| 436 | return( join( "${sep} ", @res ) ); | ||
| 437 | } | ||
| 438 | |||
| 439 | # Used by Cache-Control | ||
| 440 | sub _set_get_property_boolean | ||
| 441 | { | ||
| 442 | my $self = shift( @_ ); | ||
| 443 | my $prop = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 444 | my $params = $self->params; | ||
| 445 | my $props = $self->properties; | ||
| 446 | my $pos = $params->pos( $prop ); | ||
| 447 | if( @_ ) | ||
| 448 | { | ||
| 449 | my $bool = shift( @_ ); | ||
| 450 | if( defined( $pos ) ) | ||
| 451 | { | ||
| 452 | if( defined( $bool ) && $bool ) | ||
| 453 | { | ||
| 454 | # Nothing to do, it is already there | ||
| 455 | # Making sure we have it in our properties hash as well | ||
| 456 | $props->{ $prop } = undef; | ||
| 457 | } | ||
| 458 | # Undefined or false properties get removed | ||
| 459 | else | ||
| 460 | { | ||
| 461 | $params->splice( $pos, 1 ); | ||
| 462 | $props->delete( $prop ); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | # Not there yet | ||
| 466 | else | ||
| 467 | { | ||
| 468 | if( defined( $bool ) && $bool ) | ||
| 469 | { | ||
| 470 | $params->push( $prop ); | ||
| 471 | $props->{ $prop } = undef; | ||
| 472 | } | ||
| 473 | # Nothing to do, it is not there yet | ||
| 474 | # Still make sure it is removed from the properties hash as well | ||
| 475 | else | ||
| 476 | { | ||
| 477 | $props->delete( $prop ); | ||
| 478 | } | ||
| 479 | } | ||
| 480 | return( $bool ); | ||
| 481 | } | ||
| 482 | else | ||
| 483 | { | ||
| 484 | return( defined( $pos ) ? 1 : 0 ); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | # Used by Cache-Control, Expect-CT | ||
| 489 | sub _set_get_property_number | ||
| 490 | { | ||
| 491 | my $self = shift( @_ ); | ||
| 492 | my $prop = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 493 | if( @_ ) | ||
| 494 | { | ||
| 495 | my $v = shift( @_ ); | ||
| 496 | return( $self->error( "The value provided for property \"${prop}\" is not a number." ) ) if( defined( $v ) && !$self->_is_integer( $v ) ); | ||
| 497 | return( $self->_set_get_property_value( $prop => $v ) ); | ||
| 498 | } | ||
| 499 | return( $self->_set_get_property_value( $prop ) ); | ||
| 500 | } | ||
| 501 | |||
| 502 | # Used by Expect-CT | ||
| 503 | sub _set_get_property_value | ||
| 504 | { | ||
| 505 | my $self = shift( @_ ); | ||
| 506 | my $prop = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 507 | my $opts = {}; | ||
| 508 | $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' ); | ||
| 509 | $opts->{needs_quotes} //= 0; | ||
| 510 | $opts->{maybe_boolean} //= 0; | ||
| 511 | my $params = $self->params; | ||
| 512 | my $props = $self->properties; | ||
| 513 | my $pos = $params->pos( $prop ); | ||
| 514 | if( @_ ) | ||
| 515 | { | ||
| 516 | my $v = shift( @_ ); | ||
| 517 | if( !defined( $v ) ) | ||
| 518 | { | ||
| 519 | $self->params->splice( $pos, 1 ) if( defined( $pos ) ); | ||
| 520 | return( $self->properties->delete( $prop ) ); | ||
| 521 | } | ||
| 522 | |||
| 523 | # Not there yet, add the value | ||
| 524 | if( !defined( $pos ) ) | ||
| 525 | { | ||
| 526 | $params->push( $prop ) if( !$opts->{maybe_boolean} || ( $opts->{maybe_boolean} && $v ) ); | ||
| 527 | if( exists( $opts->{maybe_boolean} ) && $opts->{maybe_boolean} ) | ||
| 528 | { | ||
| 529 | if( $v == 1 ) | ||
| 530 | { | ||
| 531 | $props->{ $prop } = undef; | ||
| 532 | } | ||
| 533 | elsif( !$v ) | ||
| 534 | { | ||
| 535 | $props->delete( $prop ); | ||
| 536 | } | ||
| 537 | else | ||
| 538 | { | ||
| 539 | $props->{ $prop } = $v; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | else | ||
| 543 | { | ||
| 544 | $props->{ $prop } = $v; | ||
| 545 | } | ||
| 546 | } | ||
| 547 | else | ||
| 548 | { | ||
| 549 | if( exists( $opts->{maybe_boolean} ) && $opts->{maybe_boolean} ) | ||
| 550 | { | ||
| 551 | if( !$v ) | ||
| 552 | { | ||
| 553 | $params->splice( $pos, 1 ); | ||
| 554 | $props->delete( $prop ); | ||
| 555 | } | ||
| 556 | elsif( $v == 1 ) | ||
| 557 | { | ||
| 558 | $props->{ $prop } = undef; | ||
| 559 | } | ||
| 560 | else | ||
| 561 | { | ||
| 562 | $props->{ $prop } = $v; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | else | ||
| 566 | { | ||
| 567 | $props->{ $prop } = $v; | ||
| 568 | } | ||
| 569 | } | ||
| 570 | # Used for non-standard properties during stringification | ||
| 571 | if( $opts->{needs_quotes} && $self->can( '_needs_quotes' ) ) | ||
| 572 | { | ||
| 573 | $self->_needs_quotes->set( $prop => 1 ); | ||
| 574 | } | ||
| 575 | return( $v ); | ||
| 576 | } | ||
| 577 | else | ||
| 578 | { | ||
| 579 | if( defined( $pos ) ) | ||
| 580 | { | ||
| 581 | return( | ||
| 582 | $opts->{maybe_boolean} | ||
| 583 | ? defined( $pos ) ? 1 : 0 | ||
| 584 | : $props->{ $prop } | ||
| 585 | ); | ||
| 586 | } | ||
| 587 | return( '' ); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | # Same as _set_get_param but with surrounding double quotes | ||
| 592 | sub _set_get_qparam | ||
| 593 | { | ||
| 594 | my $self = shift( @_ ); | ||
| 595 | my $name = shift( @_ ) || return( $self->error( "No parameter name was provided." ) ); | ||
| 596 | my $hv = $self->_hv || return( $self->error( "Header value object could not be found!" ) ); | ||
| 597 | my $v; | ||
| 598 | if( @_ ) | ||
| 599 | { | ||
| 600 | $v = shift( @_ ); | ||
| 601 | $v =~ s/^\"//; | ||
| 602 | $v =~ s/(?<!\\)\"$//; | ||
| 603 | $hv->param( $name => qq{"${v}"} ); | ||
| 604 | } | ||
| 605 | else | ||
| 606 | { | ||
| 607 | $v = $hv->param( $name ); | ||
| 608 | return( '' ) if( !defined( $v ) || !length( "$v" ) ); | ||
| 609 | $v =~ s/^\"//; | ||
| 610 | $v =~ s/(?<!\\)\"$//; | ||
| 611 | } | ||
| 612 | return( $v ); | ||
| 613 | } | ||
| 614 | |||
| 615 | sub _set_get_value | ||
| 616 | { | ||
| 617 | my $self = shift( @_ ); | ||
| 618 | my $hv = $self->_hv; | ||
| 619 | if( @_ ) | ||
| 620 | { | ||
| 621 | $hv->value( shift( @_ ) ); | ||
| 622 | } | ||
| 623 | return( $hv->value_data ); | ||
| 624 | } | ||
| 625 | |||
| 626 | # NOTE: sub FREEZE is inherited | ||
| 627 | |||
| 628 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 629 | |||
| 630 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 631 | |||
| 632 | # NOTE: sub THAW is inherited | ||
| 633 | |||
| 634 | # NOTE: HTTP::Promise::Field::QualityValue class | ||
| 635 | { | ||
| 636 | package | ||
| 637 | HTTP::Promise::Field::QualityValue; | ||
| 638 | BEGIN | ||
| 639 | { | ||
| 640 | use strict; | ||
| 641 | use warnings; | ||
| 642 | use parent qw( Module::Generic ); | ||
| 643 | use overload ( | ||
| 644 | '""' => 'as_string', | ||
| 645 | 'bool' => sub{1}, | ||
| 646 | ); | ||
| 647 | }; | ||
| 648 | |||
| 649 | sub as_string | ||
| 650 | { | ||
| 651 | my $self = shift( @_ ); | ||
| 652 | my $elem = $self->element; | ||
| 653 | my $val = $self->value; | ||
| 654 | return( $elem ) if( !defined( $val ) || !length( "${val}" ) ); | ||
| 655 | return( "${elem};q=${val}" ); | ||
| 656 | } | ||
| 657 | |||
| 658 | sub init | ||
| 659 | { | ||
| 660 | my $self = shift( @_ ); | ||
| 661 | my $elem = shift( @_ ); | ||
| 662 | return( $self->error( "No element was provided for this quality value." ) ) if( !defined( $elem ) || !length( "$elem" ) ); | ||
| 663 | my $val = shift( @_ ); | ||
| 664 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 665 | $self->element( $elem ); | ||
| 666 | $self->value( $val ); | ||
| 667 | return( $self ); | ||
| 668 | } | ||
| 669 | |||
| 670 | sub element { return( shift->_set_get_scalar_as_object( 'element', @_ ) ); } | ||
| 671 | |||
| 672 | sub value { return( shift->_set_get_number( { field => 'value', undef_ok => 1 }, @_ ) ); } | ||
| 673 | |||
| 674 | # NOTE: sub FREEZE is inherited | ||
| 675 | |||
| 676 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 677 | |||
| 678 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 679 | |||
| 680 | # NOTE: sub THAW is inherited | ||
| 681 | |||
| 682 | } | ||
| 683 | |||
| 684 | 1; | ||
| 685 | # NOTE: POD | ||
| 686 | __END__ | ||
| 687 | |||
| 688 | =encoding utf-8 | ||
| 689 | |||
| 690 | =head1 NAME | ||
| 691 | |||
| 692 | HTTP::Promise::Headers::Generic - Generic HTTP Header Class | ||
| 693 | |||
| 694 | =head1 SYNOPSIS | ||
| 695 | |||
| 696 | package HTTP::Promise::Headers::MyHeader; | ||
| 697 | use strict; | ||
| 698 | use warnings; | ||
| 699 | use parent qw( HTTP::Promise::Headers::Generic ); | ||
| 700 | |||
| 701 | =head1 VERSION | ||
| 702 | |||
| 703 | v0.1.3 | ||
| 704 | |||
| 705 | =head1 DESCRIPTION | ||
| 706 | |||
| 707 | This is a generic module to be inherited by HTTP header modules. See for example: 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> | ||
| 708 | |||
| 709 | =head1 METHODS | ||
| 710 | |||
| 711 | =head2 as_string | ||
| 712 | |||
| 713 | Return a string representation of this header field object. | ||
| 714 | |||
| 715 | =head2 field_name | ||
| 716 | |||
| 717 | Sets or gets the object headers field name | ||
| 718 | |||
| 719 | =head2 uri_escape_utf8 | ||
| 720 | |||
| 721 | Provided with some string and this returns the URI-escaped version of this using L<URI::Escape::XS> | ||
| 722 | |||
| 723 | =head2 value | ||
| 724 | |||
| 725 | By default and superseded by inheriting classes such as Content-Type that has more elaborate value with parameters | ||
| 726 | |||
| 727 | =head1 PRIVATE METHODS | ||
| 728 | |||
| 729 | =head2 _filename_decode | ||
| 730 | |||
| 731 | Provided with a filename, and this will decode it, if necessary, by calling L<HTTP::Promise::Headers/decode_filename> | ||
| 732 | |||
| 733 | It returns in list context the decoded filename, the character-set and language used and in scalar context the decoded filename. | ||
| 734 | |||
| 735 | If the filename did not need to be decoded, it will return the filename untouched, so this is quite safe to use. | ||
| 736 | |||
| 737 | See L<rfc2231|https://tools.ietf.org/html/rfc2231> | ||
| 738 | |||
| 739 | =head2 _filename_encode | ||
| 740 | |||
| 741 | Provided with a filename, and an optional language, and this will encode it, if necessary, following the L<rfc2231|https://tools.ietf.org/html/rfc2231> | ||
| 742 | |||
| 743 | If the filename did not need to be encoded, it returns C<undef>, so be sure to check for the return value. | ||
| 744 | |||
| 745 | See L<rfc2231|https://tools.ietf.org/html/rfc2231> | ||
| 746 | |||
| 747 | =head2 _hv | ||
| 748 | |||
| 749 | Sets or gets the L<header value object|Module::Generic::HeaderValue> | ||
| 750 | |||
| 751 | =head2 _hv_as_string | ||
| 752 | |||
| 753 | Returns the L<header value object|Module::Generic::HeaderValue> as a string, if a header value object is set, or an empty string otherwise. | ||
| 754 | |||
| 755 | =head2 _get_header_value_object | ||
| 756 | |||
| 757 | This instantiates a new L<header value object|Module::Generic::HeaderValue>, passing it whatever arguments were provided, and return the new object. | ||
| 758 | |||
| 759 | =head2 _make_boundary | ||
| 760 | |||
| 761 | Returns a new boundary using L<Data::UUID> | ||
| 762 | |||
| 763 | =head2 _new_hv | ||
| 764 | |||
| 765 | Does the same thing as L</_get_header_value_object> | ||
| 766 | |||
| 767 | =head2 _new_qv_object | ||
| 768 | |||
| 769 | This instantiates a new quality value object using C<HTTP::Promise::Field::QualityValue>, passing it whatever arguments were provided, and return the new object. | ||
| 770 | |||
| 771 | =head2 _parse_header_value | ||
| 772 | |||
| 773 | Provided with a string, and this instantiates a new L<header value object|Module::Generic::HeaderValue>, by calling L<Module::Generic::HeaderValue/new_from_header> passing it the string and any other arguments that were provided, and return the new object. | ||
| 774 | |||
| 775 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 776 | |||
| 777 | =head2 _parse_quality_value | ||
| 778 | |||
| 779 | Provided with a string representing a quality value, and this will parse it and return a new L<array object|Module::Generic::Array> | ||
| 780 | |||
| 781 | See L<rfc7231, section 5.3.1|https://tools.ietf.org/html/rfc7231#section-5.3.1> | ||
| 782 | |||
| 783 | =head2 _qstring_join | ||
| 784 | |||
| 785 | Provided with a list of strings and this will ensure any special characters are escaped before returning them as one string separated by comma. | ||
| 786 | |||
| 787 | See also L</_qstring_split> | ||
| 788 | |||
| 789 | =head2 _qstring_split | ||
| 790 | |||
| 791 | Provided with a string, and this will split it by comma, mindful of any special characters. | ||
| 792 | |||
| 793 | It returns an array of the parts split. | ||
| 794 | |||
| 795 | =head2 _qv_add | ||
| 796 | |||
| 797 | Provided with an element and its value, and this will instantiate a new C<HTTP::Promise::Field::QualityValue> object and add it to the list of objects contained with the method C<elements> (implemented in each specific header module) | ||
| 798 | |||
| 799 | =head2 _qv_as_string | ||
| 800 | |||
| 801 | This takes the list of all elements contained with the method C<elements> (implemented in each specific header module) and returns them as a string separated by comma. | ||
| 802 | |||
| 803 | =head2 _qv_elements | ||
| 804 | |||
| 805 | Sets or gets the L<array object|Module::Generic::Array> containing the list of quality values. | ||
| 806 | |||
| 807 | =head2 _qv_get | ||
| 808 | |||
| 809 | Provided with a quality value element, and this returns its corresponding object if it exists, or an empty string otherwise. | ||
| 810 | |||
| 811 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 812 | |||
| 813 | =head2 _qv_match | ||
| 814 | |||
| 815 | Provided with a string, and this returns an L<array object|Module::Generic::Array> of matching quality value objects in their order of preference. | ||
| 816 | |||
| 817 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 818 | |||
| 819 | =head2 _qv_match_wildcard | ||
| 820 | |||
| 821 | This method is used to do the actual work of matching a requested value such as C<fr-FR> or <text/html> depending on the type of header, against the ones announced in the header. | ||
| 822 | |||
| 823 | For example: | ||
| 824 | |||
| 825 | Accept: image/* | ||
| 826 | Accept: text/html | ||
| 827 | Accept: */* | ||
| 828 | Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8 | ||
| 829 | |||
| 830 | Accept-Encoding: gzip | ||
| 831 | |||
| 832 | Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 | ||
| 833 | |||
| 834 | Accept-Language: fr-FR, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5 | ||
| 835 | |||
| 836 | |||
| 837 | This takes an "acceptable" L<scalar object|Module::Generic::Scalar>, an L<array object|Module::Generic::Array> of proposed quality-value objects, and an L<array object|Module::Generic::Array> of original proposed value, and possibly an hash reference of already seen object address. | ||
| 838 | |||
| 839 | It returns an L<array object|Module::Generic::Array> of matching quality-value objects. | ||
| 840 | |||
| 841 | =head2 _qv_remove | ||
| 842 | |||
| 843 | Provided with a quality-value string or object, and this will remove it from the list of elements. | ||
| 844 | |||
| 845 | It returns the element removed, or upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 846 | |||
| 847 | =head2 _qv_sort | ||
| 848 | |||
| 849 | This takes an optional hash or hash reference of options and returns an L<array object|Module::Generic::Array> of sorted element by their quality-value. | ||
| 850 | |||
| 851 | Supported options are: | ||
| 852 | |||
| 853 | =over 4 | ||
| 854 | |||
| 855 | =item * C<asc> | ||
| 856 | |||
| 857 | Boolean. If true, the elements will be sorted in their ascending order, otherwise in their descending order. | ||
| 858 | |||
| 859 | =back | ||
| 860 | |||
| 861 | =head2 _set_get_param_boolean | ||
| 862 | |||
| 863 | In retrieval mode, this takes a header value parameter, and this returns its value. | ||
| 864 | |||
| 865 | In assignment mode, this takes a header value parameter, and a value, possibly C<undef> and assign it to the given parameter. | ||
| 866 | |||
| 867 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 868 | |||
| 869 | =head2 _set_get_param | ||
| 870 | |||
| 871 | In retrieval mode, this takes a header value parameter, and it returns its corresponding value. | ||
| 872 | |||
| 873 | In assignment mode, this takes a header value parameter, and a value and assign it. | ||
| 874 | |||
| 875 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 876 | |||
| 877 | =head2 _set_get_params | ||
| 878 | |||
| 879 | This takes a list of header-value parameter and their corresponding value and set them. | ||
| 880 | |||
| 881 | If no argument is provided, this returns the L<array object|Module::Generic::Array> containing all the header-value parameters. | ||
| 882 | |||
| 883 | =head2 _set_get_properties_as_string | ||
| 884 | |||
| 885 | This takes an hash or hash reference of options and returns the header-value parameters as a regular string. | ||
| 886 | |||
| 887 | Supported options are: | ||
| 888 | |||
| 889 | =over 4 | ||
| 890 | |||
| 891 | =item * C<equal> | ||
| 892 | |||
| 893 | =item * C<separator> or C<sep> | ||
| 894 | |||
| 895 | =back | ||
| 896 | |||
| 897 | =head2 _set_get_property_boolean | ||
| 898 | |||
| 899 | This sets or gets a boolean value for the given header-value property. | ||
| 900 | |||
| 901 | It returns the boolean value for the given property. | ||
| 902 | |||
| 903 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 904 | |||
| 905 | =head2 _set_get_property_number | ||
| 906 | |||
| 907 | This sets or gets a number for the given header-value property. | ||
| 908 | |||
| 909 | It returns the number value for the given property. | ||
| 910 | |||
| 911 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 912 | |||
| 913 | =head2 _set_get_property_value | ||
| 914 | |||
| 915 | This sets or gets a value for the given header-value property. | ||
| 916 | |||
| 917 | It returns the value for the given property. | ||
| 918 | |||
| 919 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 920 | |||
| 921 | =head2 _set_get_qparam | ||
| 922 | |||
| 923 | Sets or gets a quality-value parameter. If a value is provided, any double quote found at the bginning or end are removed. | ||
| 924 | |||
| 925 | It returns the current value. | ||
| 926 | |||
| 927 | Upon error, this sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 928 | |||
| 929 | =head2 _set_get_value | ||
| 930 | |||
| 931 | This sets or gets a header main value. | ||
| 932 | |||
| 933 | For example C<text/html> in C<text/html; charset=utf-8> | ||
| 934 | |||
| 935 | =head1 THREAD-SAFETY | ||
| 936 | |||
| 937 | This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||
| 938 | |||
| 939 | =head1 AUTHOR | ||
| 940 | |||
| 941 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 942 | |||
| 943 | =head1 SEE ALSO | ||
| 944 | |||
| 945 | 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> | ||
| 946 | |||
| 947 | =head1 COPYRIGHT & LICENSE | ||
| 948 | |||
| 949 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 950 | |||
| 951 | All rights reserved. | ||
| 952 | |||
| 953 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 954 | |||
| 955 | =cut |