| 1 | # vim: set ts=2 sts=2 sw=2 expandtab smarttab: | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # This file is part of AnyEvent-HTTP-Message | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # This software is copyright (c) 2012 by Randy Stauner. | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 9 | # | ||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | package AnyEvent::HTTP::Request; | ||||||||||||
| 14 | { | ||||||||||||
| 15 | 2 | $AnyEvent::HTTP::Request::VERSION = '0.302'; | |||||||||||
| 16 | } | ||||||||||||
| 17 | 2 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 2 | $AnyEvent::HTTP::Request::AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||
| 19 | } | ||||||||||||
| 20 | # ABSTRACT: HTTP Request object for AnyEvent::HTTP | ||||||||||||
| 21 | |||||||||||||
| 22 | 2 | ▶ | use parent 'AnyEvent::HTTP::Message'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | 7 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | 7 | my $class = shift; | |||||||||||
| 27 | 7 | my $self = $class->SUPER::new(@_); | |||||||||||
| 28 | |||||||||||||
| 29 | 7 | $self->{method} = uc $self->{method}; | |||||||||||
| 30 | |||||||||||||
| 31 | 7 | return $self; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | 8 | ▶ | sub parse_args { | ||||||||||
|
Subroutine
parse_args: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 36 | 8 | my $self = shift; | |||||||||||
| 37 | |||||||||||||
| 38 | 8 | ▶ | $self->_error( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | q[expects an odd number of arguments:], | ||||||||||||
| 40 | q[($method, $uri, (key => value, ...)*, \&callback)] | ||||||||||||
| 41 | ) | ||||||||||||
| 42 | unless @_ & 1; ## no critic BitwiseOperators | ||||||||||||
| 43 | |||||||||||||
| 44 | 5 | my $args = { | |||||||||||
| 45 | method => shift, | ||||||||||||
| 46 | uri => shift, | ||||||||||||
| 47 | cb => pop, | ||||||||||||
| 48 | params => { @_ }, | ||||||||||||
| 49 | }; | ||||||||||||
| 50 | |||||||||||||
| 51 | # remove these from params | ||||||||||||
| 52 | $args->{$_} = delete $args->{params}{$_} | ||||||||||||
| 53 | 5 | for qw( body headers ); | |||||||||||
| 54 | |||||||||||||
| 55 | 5 | return $args; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | |||||||||||||
| 59 | 1 | ▶ | sub from_http_message { | ||||||||||
|
Subroutine
from_http_message: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 60 | 1 | my ($self, $req, $extra) = @_; | |||||||||||
| 61 | 1 | ▶ | my $args = { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | method => $req->method, | ||||||||||||
| 63 | uri => $req->uri, | ||||||||||||
| 64 | headers => $self->_hash_http_headers($req->headers), | ||||||||||||
| 65 | body => $req->content, | ||||||||||||
| 66 | (ref($extra) eq 'HASH' ? %$extra : ()), | ||||||||||||
| 67 | }; | ||||||||||||
| 68 | |||||||||||||
| 69 | # rt-85665: AE:H will provide it's own content-length. | ||||||||||||
| 70 | # If you provide your own it may persist incorrectly across redirects. | ||||||||||||
| 71 | delete $args->{headers}{$_} | ||||||||||||
| 72 | 1 | for qw( content-length ); | |||||||||||
| 73 | |||||||||||||
| 74 | 1 | return $args; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | |||||||||||||
| 78 | 5 | ▶ | sub args { | ||||||||||
|
Subroutine
args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 79 | 5 | my ($self) = @_; | |||||||||||
| 80 | return ( | ||||||||||||
| 81 | $self->method => $self->uri, | ||||||||||||
| 82 | body => $self->body, | ||||||||||||
| 83 | headers => $self->headers, | ||||||||||||
| 84 | 5 | %{ $self->params }, | |||||||||||
| 85 | $self->cb, | ||||||||||||
| 86 | ); | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | |||||||||||||
| 90 | 11 | ▶ | sub method { $_[0]->{method} } | ||||||||||
|
Subroutine
method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 91 | 11 | ▶ | sub uri { $_[0]->{uri} } | ||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 92 | 14 | ▶ | sub cb { | ||||||||||
|
Subroutine
cb: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 93 | 14 | my $self = shift; | |||||||||||
| 94 | 14 | ▶ | $self->_error( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | q[cb() is a read-only accessor (for consistency and to avoid confusion).], | ||||||||||||
| 96 | q[To execute the callback dereference it or use respond_with().] | ||||||||||||
| 97 | ) | ||||||||||||
| 98 | if @_; | ||||||||||||
| 99 | 13 | return $self->{cb}; | |||||||||||
| 100 | } | ||||||||||||
| 101 | 8 | ▶ | sub params { $_[0]->{params} ||= {} } | ||||||||||
|
Subroutine
params: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[0]{"params"} ||= {}
MC/DC0%0/1
$_[0]{"params"} ||= {}
$_[0]{"params"}
Truth table50%1/2
A
A= $_[0]{"params"}
| |||||||||||||
| 102 | |||||||||||||
| 103 | |||||||||||||
| 104 | 4 | ▶ | sub respond_with { | ||||||||||
|
Subroutine
respond_with: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 105 | 4 | my $self = shift; | |||||||||||
| 106 | 4 | my ($res) = @_; # don't shift | |||||||||||
| 107 | 4 | require AnyEvent::HTTP::Response; | |||||||||||
| 108 | $res = AnyEvent::HTTP::Response->new(@_) | ||||||||||||
| 109 | 4 | ▶ | unless do { local $@; eval { $res->isa('AnyEvent::HTTP::Response') } }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 3 | return $self->cb->($res->args); | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | 3 | ▶ | sub send { | ||||||||||
|
Subroutine
send: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 115 | 3 | my ($self) = @_; | |||||||||||
| 116 | 3 | require AnyEvent::HTTP; | |||||||||||
| 117 | # circumvent the sub's prototype | ||||||||||||
| 118 | 3 | &AnyEvent::HTTP::http_request( $self->args ); | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | |||||||||||||
| 122 | 3 | ▶ | sub to_http_message { | ||||||||||
|
Subroutine
to_http_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 123 | 3 | my ($self) = @_; | |||||||||||
| 124 | 3 | require HTTP::Request; | |||||||||||
| 125 | |||||||||||||
| 126 | my $res = HTTP::Request->new( | ||||||||||||
| 127 | $self->method, | ||||||||||||
| 128 | $self->uri, | ||||||||||||
| 129 | 3 | [ %{ $self->headers } ], | |||||||||||
| 130 | $self->body | ||||||||||||
| 131 | ); | ||||||||||||
| 132 | 3 | return $res; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | 2 | 1; | |||||||||||
| 136 | |||||||||||||
| 137 | __END__ | ||||||||||||