| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/ServerTiming.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/05/08 | ||
| 7 | ## Modified 2022/05/08 | ||
| 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::ServerTiming; | ||
| 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 | @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) ); | ||
| 31 | if( @_ ) | ||
| 32 | { | ||
| 33 | my $this = shift( @_ ); | ||
| 34 | my $hv; | ||
| 35 | if( index( $this, ';' ) != -1 ) | ||
| 36 | { | ||
| 37 | $hv = $self->_parse_header_value( $this ); | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | $hv = $self->_new_hv( $this ); | ||
| 42 | } | ||
| 43 | return( $self->pass_error ) if( !defined( $hv ) ); | ||
| 44 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 45 | $hv->param( $_ => $opts->{ $_ } ) for( keys( %$opts ) ); | ||
| 46 | $self->_hv( $hv ); | ||
| 47 | } | ||
| 48 | $self->{_init_strict_use_sub} = 1; | ||
| 49 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 50 | $self->_field_name( 'Server-Timing' ); | ||
| 51 | return( $self ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub as_string { return( shift->_hv_as_string( @_ ) ); } | ||
| 55 | |||
| 56 | sub desc { return( shift->_set_get_param( 'desc', @_ ) ); } | ||
| 57 | |||
| 58 | sub dur { return( shift->_set_get_param( 'dur', @_ ) ); } | ||
| 59 | |||
| 60 | sub name | ||
| 61 | { | ||
| 62 | my $self = shift( @_ ); | ||
| 63 | my $hv = $self->_hv; | ||
| 64 | if( @_ ) | ||
| 65 | { | ||
| 66 | my $this = shift( @_ ); | ||
| 67 | if( $hv ) | ||
| 68 | { | ||
| 69 | $hv->value( $this ); | ||
| 70 | } | ||
| 71 | else | ||
| 72 | { | ||
| 73 | $hv = $self->_new_hv( $this ); | ||
| 74 | $self->_hv( $hv ); | ||
| 75 | } | ||
| 76 | return( $this); | ||
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | return( '' ) if( !$hv ); | ||
| 81 | return( $hv->value_data ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub param { return( shift->_set_get_param( @_ ) ); } | ||
| 86 | |||
| 87 | sub params { return( shift->_set_get_params( @_ ) ); } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | # NOTE: POD | ||
| 91 | __END__ | ||
| 92 | |||
| 93 | =encoding utf-8 | ||
| 94 | |||
| 95 | =head1 NAME | ||
| 96 | |||
| 97 | HTTP::Promise::Headers::ServerTiming - Server-Timing Header Field | ||
| 98 | |||
| 99 | =head1 SYNOPSIS | ||
| 100 | |||
| 101 | use HTTP::Promise::Headers::ServerTiming; | ||
| 102 | my $srv = HTTP::Promise::Headers::ServerTiming->new || | ||
| 103 | die( HTTP::Promise::Headers::ServerTiming->error, "\n" ); | ||
| 104 | $srv->name( 'cache' ); | ||
| 105 | $srv->dur(2.4); | ||
| 106 | $srv->desc( 'Cache Read' ); | ||
| 107 | |||
| 108 | =head1 VERSION | ||
| 109 | |||
| 110 | v0.1.0 | ||
| 111 | |||
| 112 | =head1 DESCRIPTION | ||
| 113 | |||
| 114 | The following is an extract from Mozilla documentation. | ||
| 115 | |||
| 116 | The Server-Timing header communicates one or more metrics and descriptions for a given request-response cycle. | ||
| 117 | |||
| 118 | Example: | ||
| 119 | |||
| 120 | # Single metric without value | ||
| 121 | Server-Timing: missedCache | ||
| 122 | |||
| 123 | # Single metric with value | ||
| 124 | Server-Timing: cpu;dur=2.4 | ||
| 125 | |||
| 126 | # Single metric with description and value | ||
| 127 | Server-Timing: cache;desc="Cache Read";dur=23.2 | ||
| 128 | |||
| 129 | # Two metrics with value | ||
| 130 | Server-Timing: db;dur=53, app;dur=47.2 | ||
| 131 | |||
| 132 | =head1 METHODS | ||
| 133 | |||
| 134 | =head2 as_string | ||
| 135 | |||
| 136 | Returns a string representation of the C<Server-Timing> object. | ||
| 137 | |||
| 138 | =head2 desc | ||
| 139 | |||
| 140 | Sets or gets the server timing description. | ||
| 141 | |||
| 142 | =head2 dur | ||
| 143 | |||
| 144 | Sets or gets the duration | ||
| 145 | |||
| 146 | =head2 name | ||
| 147 | |||
| 148 | Sets or gets the server timing metrics name. | ||
| 149 | |||
| 150 | =head2 param | ||
| 151 | |||
| 152 | Set or get an arbitrary name-value pair attribute. | ||
| 153 | |||
| 154 | =head2 params | ||
| 155 | |||
| 156 | Set or get multiple name-value parameters. | ||
| 157 | |||
| 158 | Calling this without any parameters, retrieves the associated L<hash object|Module::Generic::Hash> | ||
| 159 | |||
| 160 | =head1 THREAD-SAFETY | ||
| 161 | |||
| 162 | This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing> | ||
| 171 | |||
| 172 | 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> | ||
| 173 | |||
| 174 | =head1 COPYRIGHT & LICENSE | ||
| 175 | |||
| 176 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 177 | |||
| 178 | All rights reserved. | ||
| 179 | |||
| 180 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 181 | |||
| 182 | =cut |