| 1 | package Furl::Request; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 13 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 13 | ▶ | use Class::Accessor::Lite; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 13 | ▶ | use Furl::Headers; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 13 | ▶ | use Furl::HTTP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 13 | Class::Accessor::Lite->mk_accessors(qw/ method uri protocol headers content /); | |||||||||||||
| 11 | |||||||||||||||
| 12 | 6 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||||||
| 13 | 6 | my $class = shift; | |||||||||||||
| 14 | 6 | my ($method, $uri, $headers, $content) = @_; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 6 | ▶ | unless (defined $headers) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | 0 | $headers = +{}; | |||||||||||||
| 18 | } | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 6 | ▶ | unless (defined $content) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | 0 | $content = ''; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | bless +{ | ||||||||||||||
| 25 | 6 | method => $method, | |||||||||||||
| 26 | uri => $uri, | ||||||||||||||
| 27 | headers => Furl::Headers->new($headers), | ||||||||||||||
| 28 | content => $content, | ||||||||||||||
| 29 | }, $class; | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 2 | ▶ | sub parse { | ||||||||||||
|
Subroutine
parse: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 33 | 2 | my $class = shift; | |||||||||||||
| 34 | 2 | my $raw_request = shift; | |||||||||||||
| 35 | |||||||||||||||
| 36 | # I didn't use HTTP::Parser::XS for following reasons: | ||||||||||||||
| 37 | # 1. parse_http_request() function omits request content, but need to deal it. | ||||||||||||||
| 38 | # 2. this function parses header to PSGI env, but env/header mapping is troublesome. | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 2 | ▶ | return unless $raw_request =~ s!^(.+) (.+) (HTTP/1.\d+)\s*!!; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 2 | my ($method, $uri, $protocol) = ($1, $2, $3); | |||||||||||||
| 42 | |||||||||||||||
| 43 | 2 | my ($header_str, $content) = split /\015?\012\015?\012/, $raw_request, 2; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 2 | my $headers = +{}; | |||||||||||||
| 46 | 2 | for (split /\015?\012/, $header_str) { | |||||||||||||
| 47 | 8 | tr/\015\012//d; | |||||||||||||
| 48 | 8 | my ($k, $v) = split /\s*:\s*/, $_, 2; | |||||||||||||
| 49 | 8 | $headers->{lc $k} = $v; | |||||||||||||
| 50 | |||||||||||||||
| 51 | # complete host_port | ||||||||||||||
| 52 | 8 | ▶ | if (lc $k eq 'host') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 2 | $uri = $v . $uri; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 2 | ▶ | unless ($uri =~ /^http/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 2 | $uri = "http://$uri"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 2 | my $req = $class->new($method, $uri, $headers, $content); | |||||||||||||
| 62 | 2 | $req->protocol($protocol); | |||||||||||||
| 63 | 2 | return $req; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | # alias | ||||||||||||||
| 67 | 13 | *body = \&content; | |||||||||||||
| 68 | |||||||||||||||
| 69 | # shorthand | ||||||||||||||
| 70 | 2 | ▶ | sub content_length { shift->headers->content_length } | ||||||||||||
|
Subroutine
content_length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 71 | 2 | ▶ | sub content_type { shift->headers->content_type } | ||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 72 | 1 | ▶ | sub header { shift->headers->header(@_) } | ||||||||||||
|
Subroutine
header: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 2 | ▶ | sub request_line { | ||||||||||||
|
Subroutine
request_line: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 75 | 2 | my $self = shift; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 2 | my $path_query = $self->uri . ''; # for URI.pm | |||||||||||||
| 78 | 2 | $path_query =~ s!^https?://[^/]+!!; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 2 | ▶ | my $method = $self->method || ''; | ||||||||||||
|
Condition50%1/2
$self->method || ''
MC/DC0%0/1
$self->method || ''
$self->method
Truth table50%1/2
A
A= $self->method
| |||||||||||||||
| 81 | 2 | ▶ | my $protocol = $self->protocol || ''; | ||||||||||||
|
Condition50%1/2
$self->protocol || ''
MC/DC0%0/1
$self->protocol || ''
$self->protocol
Truth table50%1/2
A
A= $self->protocol
| |||||||||||||||
| 82 | |||||||||||||||
| 83 | 2 | return "$method $path_query $protocol"; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 2 | ▶ | sub as_http_request { | ||||||||||||
|
Subroutine
as_http_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 87 | 2 | my $self = shift; | |||||||||||||
| 88 | |||||||||||||||
| 89 | 2 | require HTTP::Request; | |||||||||||||
| 90 | 2 | my $req = HTTP::Request->new( | |||||||||||||
| 91 | $self->method, | ||||||||||||||
| 92 | $self->uri, | ||||||||||||||
| 93 | [ $self->headers->flatten ], | ||||||||||||||
| 94 | $self->content, | ||||||||||||||
| 95 | ); | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 2 | $req->protocol($self->protocol); | |||||||||||||
| 98 | 2 | return $req; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 1 | ▶ | sub as_hashref { | ||||||||||||
|
Subroutine
as_hashref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 102 | 1 | my $self = shift; | |||||||||||||
| 103 | |||||||||||||||
| 104 | return +{ | ||||||||||||||
| 105 | 1 | method => $self->method, | |||||||||||||
| 106 | uri => $self->uri, | ||||||||||||||
| 107 | protocol => $self->protocol, | ||||||||||||||
| 108 | headers => [ $self->headers->flatten ], | ||||||||||||||
| 109 | content => $self->content, | ||||||||||||||
| 110 | }; | ||||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 2 | ▶ | sub as_string { | ||||||||||||
|
Subroutine
as_string: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 114 | 2 | my $self = shift; | |||||||||||||
| 115 | |||||||||||||||
| 116 | 2 | ▶ | join("\015\012", | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 117 | $self->method . ' ' . $self->uri . (defined($self->protocol) ? ' ' . $self->protocol : ''), | ||||||||||||||
| 118 | $self->headers->as_string, | ||||||||||||||
| 119 | ref($self->content) =~ qr{\A(?:ARRAY|HASH)\z} ? Furl::HTTP->make_x_www_form_urlencoded($self->content) : $self->content, | ||||||||||||||
| 120 | ); | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 13 | 1; | |||||||||||||
| 124 | __END__ | ||||||||||||||