| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Cookie.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/05/07 | ||
| 7 | ## Modified 2022/05/07 | ||
| 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::Cookie; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTTP::Promise' ); | ||
| 20 | use parent qw( HTTP::Promise::Headers::Generic ); | ||
| 21 | our $VERSION = 'v0.1.0'; | ||
| 22 | }; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | sub init | ||
| 28 | { | ||
| 29 | my $self = shift( @_ ); | ||
| 30 | $self->{cookies} = []; | ||
| 31 | @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) ); | ||
| 32 | if( @_ ) | ||
| 33 | { | ||
| 34 | my $this = shift( @_ ); | ||
| 35 | my $ref = $self->_is_array( $this ) ? $this : [split( /(?<!\\)\;[[:blank:]\h]*/, $this )]; | ||
| 36 | $self->cookies( $ref ); | ||
| 37 | } | ||
| 38 | $self->{_init_strict_use_sub} = 1; | ||
| 39 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 40 | $self->_field_name( 'Cookie' ); | ||
| 41 | return( $self ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub as_string { return( shift->cookies->join( '; ' )->scalar ); } | ||
| 45 | |||
| 46 | sub cookies { return( shift->_set_get_array_as_object( 'cookies', @_ ) ); } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | # NOTE: POD | ||
| 50 | __END__ | ||
| 51 | |||
| 52 | =encoding utf-8 | ||
| 53 | |||
| 54 | =head1 NAME | ||
| 55 | |||
| 56 | HTTP::Promise::Headers::Cookie - Cookie Header Field | ||
| 57 | |||
| 58 | =head1 SYNOPSIS | ||
| 59 | |||
| 60 | use HTTP::Promise::Headers::Cookie; | ||
| 61 | my $c = HTTP::Promise::Headers::Cookie->new( 'name=value; name2=value2; name3=value3' ) || | ||
| 62 | die( HTTP::Promise::Headers::Cookie->error, "\n" ); | ||
| 63 | # or | ||
| 64 | my $c = HTTP::Promise::Headers::Cookie->new( [qw( name=value name2=value2 name3=value3 )] ); | ||
| 65 | $c->cookies->push( 'name4=value4' ); | ||
| 66 | $c->cookies->remove( 'name2=value2' ); | ||
| 67 | say "$c"; | ||
| 68 | # This would return: name=value; name3=value3; name4=value4 | ||
| 69 | |||
| 70 | =head1 VERSION | ||
| 71 | |||
| 72 | v0.1.0 | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | L<HTTP::Promise::Headers::Cookie> implements a simple interface to store the cookie name-value pairs sent by the user agent to the server. It uses L</cookies> which returns a L<Module::Generic::Array> to manage and contain all the cookie name-value pairs. It is up to you to provide the right cookie string. | ||
| 77 | |||
| 78 | You can and maybe should use L<Cookie::Jar> to help you manage a cookie jar and format cookies. | ||
| 79 | |||
| 80 | =head1 CONSTRUCTOR | ||
| 81 | |||
| 82 | =head2 new | ||
| 83 | |||
| 84 | Provided with an optional string or array reference and this will instantiate a new object and return it. | ||
| 85 | |||
| 86 | If a string is provided, it is expected to be properly formatted, such as: | ||
| 87 | |||
| 88 | name=value; name2=value2; name3=value3 | ||
| 89 | |||
| 90 | It will be properly split and added to L</cookies>. If an array is provided, it will be directly added to L</cookies>. | ||
| 91 | |||
| 92 | =head1 METHODS | ||
| 93 | |||
| 94 | =head2 as_string | ||
| 95 | |||
| 96 | Returns a string representation for this C<Cookie> header field. | ||
| 97 | |||
| 98 | =head2 cookies | ||
| 99 | |||
| 100 | Returns an L<array object|Module::Generic::Array> containing all the cookies. | ||
| 101 | |||
| 102 | =head1 THREAD-SAFETY | ||
| 103 | |||
| 104 | This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 109 | |||
| 110 | =head1 SEE ALSO | ||
| 111 | |||
| 112 | L<Cookie>, L<Cookie::Jar>, L<rfc6265, section 5.4|https://tools.ietf.org/html/rfc6265#section-5.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie> | ||
| 113 | |||
| 114 | 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> | ||
| 115 | |||
| 116 | =head1 COPYRIGHT & LICENSE | ||
| 117 | |||
| 118 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 119 | |||
| 120 | All rights reserved. | ||
| 121 | |||
| 122 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 123 | |||
| 124 | =cut |