| 1 | package Archive::HAR::Writer; | ||
| 2 | |||
| 3 | # ABSTRACT: Interface for writing HTTP Archive (HAR) files. | ||
| 4 | our $VERSION = '0.01'; # VERSION | ||
| 5 | |||
| 6 | 2 | ▶ | use 5.042; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Path::Tiny (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use JSON::MaybeXS (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use POSIX (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use MIME::Base64 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use Ref::Util qw( is_plain_arrayref is_plain_scalarref ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | |||
| 16 | 2 | ▶ | use Class::Tiny { |
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
| |||
| 17 | 0 | ▶ | api => sub { 1 }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 18 | 0 | ▶ | _entries => sub { [] }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 19 | 0 | }; | |
| 20 | |||
| 21 | |||
| 22 | sub add_http_transaction ( $self, $transaction ) { | ||
| 23 | my $entry; | ||
| 24 | |||
| 25 | if ( $transaction isa Mojo::Transaction::HTTP ) { | ||
| 26 | $entry = $self->_entry_from_mojo( $transaction->req, $transaction->res ); | ||
| 27 | } elsif ( is_plain_arrayref $transaction ) { | ||
| 28 | my ( $request, $response ) = @$transaction; | ||
| 29 | |||
| 30 | Carp::croak('request must be an HTTP::Request') | ||
| 31 | unless $request isa HTTP::Request; | ||
| 32 | Carp::croak('response must be an HTTP::Response') | ||
| 33 | unless $response isa HTTP::Response; | ||
| 34 | |||
| 35 | $entry = $self->_entry_from_http( $request, $response ); | ||
| 36 | } else { | ||
| 37 | Carp::croak( 'unsupported transaction type: ' . ( ref($transaction) || $transaction ) ); | ||
| 38 | } | ||
| 39 | |||
| 40 | push $self->_entries->@*, $entry; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub write_file ( $self, $path ) { | ||
| 47 | $path = Path::Tiny->new($path) unless $path isa Path::Tiny; | ||
| 48 | |||
| 49 | my $fh = $path->openw_raw; | ||
| 50 | $self->write($fh); | ||
| 51 | close $fh or Carp::croak("error closing $path: $!"); | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub write ( $self, $fh ) { | ||
| 58 | my $json = $self->_encode_har; | ||
| 59 | |||
| 60 | if ( is_plain_scalarref $fh ) { | ||
| 61 | $$fh .= $json; | ||
| 62 | } else { | ||
| 63 | print {$fh} $json or Carp::croak("error writing HAR: $!"); | ||
| 64 | } | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _encode_har ($self) { | ||
| 70 | return JSON::MaybeXS->new( utf8 => 1, pretty => 1, canonical => 1 )->encode( | ||
| 71 | { | ||
| 72 | log => { | ||
| 73 | version => '1.2', | ||
| 74 | creator => { | ||
| 75 | name => 'Archive::HAR::Writer', | ||
| 76 | version => __PACKAGE__->VERSION // 'dev', | ||
| 77 | }, | ||
| 78 | entries => $self->_entries, | ||
| 79 | }, | ||
| 80 | } | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _entry_from_mojo ( $self, $req, $res ) { | ||
| 85 | return $self->_build_entry( | ||
| 86 | method => $req->method, | ||
| 87 | url => $req->url->to_string, | ||
| 88 | request_version => 'HTTP/' . ( $req->version // '1.1' ), | ||
| 89 | request_headers => [ _mojo_header_pairs( $req->headers ) ], | ||
| 90 | request_body => $req->body, | ||
| 91 | query_pairs => [ _flat_to_pairs( $req->url->query->pairs->@* ) ], | ||
| 92 | status => $res->code, | ||
| 93 | status_text => $res->message, | ||
| 94 | response_version => 'HTTP/' . ( $res->version // '1.1' ), | ||
| 95 | response_headers => [ _mojo_header_pairs( $res->headers ) ], | ||
| 96 | response_body => $res->body, | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _entry_from_http ( $self, $req, $res ) { | ||
| 101 | return $self->_build_entry( | ||
| 102 | method => $req->method, | ||
| 103 | url => $req->uri->as_string, | ||
| 104 | request_version => $req->protocol // 'HTTP/1.1', | ||
| 105 | request_headers => [ _http_header_pairs( $req->headers ) ], | ||
| 106 | request_body => scalar $req->content, | ||
| 107 | query_pairs => [ _flat_to_pairs( $req->uri->query_form ) ], | ||
| 108 | status => $res->code, | ||
| 109 | status_text => $res->message, | ||
| 110 | response_version => $res->protocol // 'HTTP/1.1', | ||
| 111 | response_headers => [ _http_header_pairs( $res->headers ) ], | ||
| 112 | response_body => scalar $res->content, | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _build_entry ( $self, %args ) { | ||
| 117 | my $req_headers = $args{request_headers}; | ||
| 118 | my $res_headers = $args{response_headers}; | ||
| 119 | |||
| 120 | my $request_body = $args{request_body} // ''; | ||
| 121 | |||
| 122 | my $request = { | ||
| 123 | method => $args{method}, | ||
| 124 | url => $args{url}, | ||
| 125 | httpVersion => $args{request_version}, | ||
| 126 | cookies => _request_cookies($req_headers), | ||
| 127 | headers => _headers_array($req_headers), | ||
| 128 | queryString => [ map { { name => $_->[0], value => $_->[1] } } $args{query_pairs}->@* ], | ||
| 129 | headersSize => -1, | ||
| 130 | bodySize => length($request_body), | ||
| 131 | }; | ||
| 132 | |||
| 133 | if ( length $request_body ) { | ||
| 134 | $request->{postData} = { | ||
| 135 | mimeType => _header_value( $req_headers, 'Content-Type' ) // '', | ||
| 136 | text => $request_body, | ||
| 137 | }; | ||
| 138 | } | ||
| 139 | |||
| 140 | my $response_body = $args{response_body} // ''; | ||
| 141 | my $mime_type = _header_value( $res_headers, 'Content-Type' ) // ''; | ||
| 142 | |||
| 143 | my $content = { | ||
| 144 | size => length($response_body), | ||
| 145 | mimeType => $mime_type, | ||
| 146 | }; | ||
| 147 | |||
| 148 | if ( length $response_body ) { | ||
| 149 | if ( _mime_is_text($mime_type) ) { | ||
| 150 | $content->{text} = $response_body; | ||
| 151 | } else { | ||
| 152 | $content->{text} = MIME::Base64::encode_base64( $response_body, '' ); | ||
| 153 | $content->{encoding} = 'base64'; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | my $response = { | ||
| 158 | status => $args{status}, | ||
| 159 | statusText => $args{status_text} // '', | ||
| 160 | httpVersion => $args{response_version}, | ||
| 161 | cookies => _response_cookies($res_headers), | ||
| 162 | headers => _headers_array($res_headers), | ||
| 163 | content => $content, | ||
| 164 | redirectURL => _header_value( $res_headers, 'Location' ) // '', | ||
| 165 | headersSize => -1, | ||
| 166 | bodySize => length($response_body), | ||
| 167 | }; | ||
| 168 | |||
| 169 | return { | ||
| 170 | startedDateTime => _now_iso8601(), | ||
| 171 | time => 0, | ||
| 172 | request => $request, | ||
| 173 | response => $response, | ||
| 174 | cache => {}, | ||
| 175 | timings => { send => 0, wait => 0, receive => 0 }, | ||
| 176 | }; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub _mojo_header_pairs ($headers) { | ||
| 180 | my @pairs; | ||
| 181 | for my $name ( $headers->names->@* ) { | ||
| 182 | push @pairs, [ $name, $_ ] for $headers->every_header($name)->@*; | ||
| 183 | } | ||
| 184 | return @pairs; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _http_header_pairs ($headers) { | ||
| 188 | my @pairs; | ||
| 189 | $headers->scan( sub { push @pairs, [ $_[0], $_[1] ] } ); | ||
| 190 | return @pairs; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _flat_to_pairs (@flat) { | ||
| 194 | my @pairs; | ||
| 195 | while (@flat) { | ||
| 196 | my $name = shift @flat; | ||
| 197 | my $value = shift @flat; | ||
| 198 | push @pairs, [ $name, $value ]; | ||
| 199 | } | ||
| 200 | return @pairs; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _header_value ( $pairs, $name ) { | ||
| 204 | for my $pair (@$pairs) { | ||
| 205 | return $pair->[1] if lc( $pair->[0] ) eq lc($name); | ||
| 206 | } | ||
| 207 | return undef; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _headers_array ($pairs) { | ||
| 211 | return [ map { { name => $_->[0], value => $_->[1] } } @$pairs ]; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _request_cookies ($pairs) { | ||
| 215 | my @cookies; | ||
| 216 | |||
| 217 | for my $pair (@$pairs) { | ||
| 218 | next unless lc( $pair->[0] ) eq 'cookie'; | ||
| 219 | |||
| 220 | for my $part ( split /;\s*/, $pair->[1] ) { | ||
| 221 | next unless length $part; | ||
| 222 | my ( $name, $value ) = split /=/, $part, 2; | ||
| 223 | push @cookies, { name => $name, value => $value // '' }; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | return \@cookies; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _response_cookies ($pairs) { | ||
| 231 | my @cookies; | ||
| 232 | |||
| 233 | for my $pair (@$pairs) { | ||
| 234 | next unless lc( $pair->[0] ) eq 'set-cookie'; | ||
| 235 | |||
| 236 | my @parts = split /;\s*/, $pair->[1]; | ||
| 237 | my $name_value = shift @parts; | ||
| 238 | next unless defined $name_value; | ||
| 239 | |||
| 240 | my ( $name, $value ) = split /=/, $name_value, 2; | ||
| 241 | my $cookie = { name => $name, value => $value // '' }; | ||
| 242 | |||
| 243 | for my $attr (@parts) { | ||
| 244 | my ( $key, $val ) = split /=/, $attr, 2; | ||
| 245 | my $lkey = lc($key); | ||
| 246 | |||
| 247 | if ( $lkey eq 'path' ) { | ||
| 248 | $cookie->{path} = $val; | ||
| 249 | } elsif ( $lkey eq 'domain' ) { | ||
| 250 | $cookie->{domain} = $val; | ||
| 251 | } elsif ( $lkey eq 'expires' ) { | ||
| 252 | $cookie->{expires} = $val; | ||
| 253 | } elsif ( $lkey eq 'httponly' ) { | ||
| 254 | $cookie->{httpOnly} = JSON::MaybeXS::true(); | ||
| 255 | } elsif ( $lkey eq 'secure' ) { | ||
| 256 | $cookie->{secure} = JSON::MaybeXS::true(); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | push @cookies, $cookie; | ||
| 261 | } | ||
| 262 | |||
| 263 | return \@cookies; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub _mime_is_text ($mime) { | ||
| 267 | return 1 if $mime =~ m{^text/}i; | ||
| 268 | return 1 if $mime =~ m{^application/(?:json|xml|javascript|x-www-form-urlencoded)}i; | ||
| 269 | return 1 if $mime =~ m{[+](?:json|xml)}i; | ||
| 270 | return 0; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _now_iso8601 () { | ||
| 274 | return POSIX::strftime( '%Y-%m-%dT%H:%M:%S.000Z', gmtime ); | ||
| 275 | } | ||
| 276 | |||
| 277 | __END__ | ||