| 1 | package WebSource::Query; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | use WebSource::Module | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | our @ISA = ('WebSource::Module'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | WebSource::Query - Build a query (HTTP request) given a hash | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | A Query operator builds an HTTP query for each input. | ||||||||||||||||||||||||||||
| 17 | The declaration of a query operator is a DOM Node with the following format : | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | <ws:query name="opname" method="POST|GET" forward-to="ops"> | ||||||||||||||||||||||||||||
| 20 | <base>http://somewhere/query.cgi</base> | ||||||||||||||||||||||||||||
| 21 | <parameters> | ||||||||||||||||||||||||||||
| 22 | <param name="p1" default="v1" /> | ||||||||||||||||||||||||||||
| 23 | ... | ||||||||||||||||||||||||||||
| 24 | <param name="pn" default="vn" /> | ||||||||||||||||||||||||||||
| 25 | </parameters> | ||||||||||||||||||||||||||||
| 26 | </ws:query> | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | See WebSource::Module | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub _init_ { | ||||||||||||||||||||||||||
|
Subroutine
_init_: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | 0 | $self->SUPER::_init_; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $wsd = $self->{wsdnode} or croak("No description node given\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $self->{base_uri} = $wsd->findvalue('base'); | |||||||||||||||||||||||||||
| 39 | 0 | $self->{method} = $wsd->getAttribute('method'); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 0 | my $env = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my ($query, $missing) = $self->build_query($env); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if(@$missing) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | croak "Missing query parameters :\n\t- '" . join("'\n\t- '",@$missing) . "'\n"; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $req = $self->make_request($query,$env); | |||||||||||||||||||||||||||
| 52 | 0 | return WebSource::Envelope->new( | |||||||||||||||||||||||||||
| 53 | type => "object/http-request", | ||||||||||||||||||||||||||||
| 54 | data => $req); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub build_query { | ||||||||||||||||||||||||||
|
Subroutine
build_query: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($self,$env) = @_; | |||||||||||||||||||||||||||
| 59 | # $env->type eq "hash" or croak("Envelope didn't contain hash\n"); | ||||||||||||||||||||||||||||
| 60 | # my $qref = $env->data; | ||||||||||||||||||||||||||||
| 61 | 0 | my $qref = $env; | |||||||||||||||||||||||||||
| 62 | 0 | my @params = $self->{wsdnode}->findnodes('parameters/param'); | |||||||||||||||||||||||||||
| 63 | 0 | my @missing; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my %query; | ||||||||||||||||||||||||||||
| 66 | 0 | foreach $_ (@params) { | |||||||||||||||||||||||||||
| 67 | 0 | my $name = $_->getAttribute("name"); | |||||||||||||||||||||||||||
| 68 | 0 | my $value; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $_->hasAttribute("rename") and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | croak("Deprecated use of attribute rename in query parameters"); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if($_->hasAttribute("value-of")){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | my $key = $_->getAttribute("value-of"); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (exists($qref->{$key})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if($qref->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | $value = ($key eq 'data') ? | ||||||||||||||||||||||||||||
| 78 | $env->dataString : | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $qref->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 0 | $value = ""; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if(!defined($value) && $_->hasAttribute("default")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $value and $_->hasAttribute("default")
MC/DC0%0/2
not defined $value and $_->hasAttribute("default")
not defined $value
$_->hasAttribute("default")
Truth table0%0/3
not(A) and B
A= not defined $valueB= $_->hasAttribute("default")
| |||||||||||||||||||||||||||||
| 86 | 0 | $value = $_->getAttribute("default"); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (defined($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $query{$name} = $value; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 0 | push(@missing, ($name)); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return (\%query,\@missing); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub make_request { | ||||||||||||||||||||||||||
|
Subroutine
make_request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self,$query,$env) = @_; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $uri = URI->new($self->{base_uri} ? $self->{base_uri} : $env->dataAsURI); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my $qstr; | |||||||||||||||||||||||||||
| 103 | map { | ||||||||||||||||||||||||||||
| 104 | 0 | $qstr .= "\t". $_ . " => " . $query->{$_} ."\n"; | |||||||||||||||||||||||||||
| 105 | } keys(%$query); | ||||||||||||||||||||||||||||
| 106 | 0 | $self->log(5,"Built query : \n",$qstr); | |||||||||||||||||||||||||||
| 107 | 0 | $uri->query_form(%$query); | |||||||||||||||||||||||||||
| 108 | 0 | my $method = $self->{method}; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if($method ne "GET") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | my $query = $uri->query; | |||||||||||||||||||||||||||
| 111 | 0 | $uri->query(undef); | |||||||||||||||||||||||||||
| 112 | 0 | my $req = HTTP::Request->new('POST',$uri); | |||||||||||||||||||||||||||
| 113 | 0 | $req->content_type("application/x-www-form-urlencoded"); | |||||||||||||||||||||||||||
| 114 | 0 | $req->content_length(length($query)); | |||||||||||||||||||||||||||
| 115 | 0 | $req->content($query); | |||||||||||||||||||||||||||
| 116 | 0 | return $req; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 0 | return HTTP::Request->new('GET',$uri); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub parameters { | ||||||||||||||||||||||||||
|
Subroutine
parameters: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 124 | return map { | ||||||||||||||||||||||||||||
| 125 | 0 | $_->getAttribute("name") => $_->getAttribute("default") | |||||||||||||||||||||||||||
| 126 | 0 | } $self->{wsdnode}->findnodes('parameters/param'); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | WebSource | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | 1; | |||||||||||||||||||||||||||