| 1 | package OAuthomatic::Internal::Util; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: internal helper routines (form parsing and filling) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 27 | use Exporter::Shiny qw(fill_httpmsg_form parse_http_msg_form fill_httpmsg_text | |||||||||||||||||||||||||||
| 8 | 27 | ▶ | serialize_json fill_httpmsg_json parse_http_msg_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 27 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 27 | ▶ | use HTTP::Body; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 27 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 27 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 27 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 27 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 27 | ▶ | use JSON qw/decode_json encode_json from_json to_json/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 27 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 27 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 27 | ▶ | use namespace::sweep; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # FIXME: throw on errors | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub fill_httpmsg_form { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($http_message, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fill_httpmsg_form: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my $body_form = URI->new('http:'); | |||||||||||||||||||||||||||
| 29 | 0 | $body_form->query_form_hash($params); | |||||||||||||||||||||||||||
| 30 | 0 | $http_message->content($body_form->query()); | |||||||||||||||||||||||||||
| 31 | 0 | $http_message->content_type("application/x-www-form-urlencoded; charset=utf-8"); | |||||||||||||||||||||||||||
| 32 | 0 | return; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub parse_http_msg_form { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my ($http_message, $force_form) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_http_msg_form: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my $content_type = $http_message->content_type; | |||||||||||||||||||||||||||
| 40 | 0 | my $charset = $http_message->content_type_charset; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if($http_message->content_is_text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if($force_form) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | $content_type = 'application/x-www-form-urlencoded'; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $body = HTTP::Body->new( | |||||||||||||||||||||||||||
| 49 | $content_type, | ||||||||||||||||||||||||||||
| 50 | $http_message->content_length); | ||||||||||||||||||||||||||||
| 51 | 0 | $body->add($http_message->content); | |||||||||||||||||||||||||||
| 52 | 0 | my $params = $body->param; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # HTTP::Body does not decode | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if($charset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if($charset =~ /^UTF-?8$/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | for my $value (values %$params) { | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | unless ( ref $value && ref $value ne 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $value and ref $value ne "ARRAY"
MC/DC0%0/2
ref $value and ref $value ne "ARRAY"
ref $value
ref $value ne "ARRAY"
Truth table0%0/3
A and B
A= ref $valueB= ref $value ne "ARRAY"
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | utf8::decode($_) for ( ref($value) ? @{$value} : $value ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | foreach my $key (keys %$params) { | |||||||||||||||||||||||||||
| 64 | 0 | my $value = $params->{$key}; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless( ref($value) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 0 | $params->{$key} = decode($charset, $value); | |||||||||||||||||||||||||||
| 67 | 0 | } elsif( ref($value) eq 'ARRAY') { | |||||||||||||||||||||||||||
| 68 | 0 | my @fixed = map { decode($charset, $_) } @$value; | |||||||||||||||||||||||||||
| 69 | 0 | $params->{$key} = \@fixed; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return $params; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # For comparison: this usually works OK too (albeit is too magic for my taste) | ||||||||||||||||||||||||||||
| 78 | # use CGI qw(); | ||||||||||||||||||||||||||||
| 79 | # my %vars = CGI->new($http_message->content)->Vars; | ||||||||||||||||||||||||||||
| 80 | # return \%vars; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub fill_httpmsg_text { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my ($http_message, $text, $content_type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fill_httpmsg_text: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $text_ref = ref($text) ? $text : \$text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $http_message->content_type($content_type); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if(utf8::is_utf8($$text_ref)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $charset = $http_message->content_type_charset; | |||||||||||||||||||||||||||
| 92 | # For UTF-8 we may leave things as-is, binary encoding matches | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | unless($charset eq 'UTF-8') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $text = encode($charset, $$text_ref, Encode::FB_WARN); # FIXME: maybe throw... | |||||||||||||||||||||||||||
| 95 | 0 | $text_ref = \$text; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $http_message->content($$text_ref); | |||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub serialize_json { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $json = shift; | ||||||||||||||||||||||||||
|
Subroutine
serialize_json: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if(reftype($json) =~ /^(?:HASH|ARRAY)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 0 | return encode_json($json); # FIXME rethrow exception as sth better | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | ▶ | elsif(! ref($json) || reftype($json) eq 'SCALAR') { | |||||||||||||||||||||||||||
|
Condition0%0/3
not ref $json or builtin::reftype($json) eq "SCALAR"
MC/DC0%0/2
not ref $json or builtin::reftype($json) eq "SCALAR"
not ref $json
builtin::reftype($json) eq "SCALAR"
Truth table0%0/3
not(A) or B
A= not ref $jsonB= builtin::reftype($json) eq "SCALAR"
| |||||||||||||||||||||||||||||
| 112 | 0 | return $json; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 0 | OAuthomatic::Error::Generic->throw( | |||||||||||||||||||||||||||
| 116 | ident => "Can not serialize to JSON", | ||||||||||||||||||||||||||||
| 117 | extra => "Provided type is neither hash/array ref, nor already serialized string"); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub fill_httpmsg_json { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my ($http_message, $json) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fill_httpmsg_json: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | fill_httpmsg_text($http_message, serialize_json($json), "application/json; charset=utf-8"); | |||||||||||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub parse_http_msg_json { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my ($http_message, $force) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_http_msg_json: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my $content_type = $http_message->content_type; | |||||||||||||||||||||||||||
| 135 | # my $charset = $http_message->content_type_charset; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | unless( $force || $content_type =~ m{^(application/(?:x-)?json|text/plain)$}x ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$force or $content_type =~ m[^(application/(?:x-)?json|text/plain)$]x
MC/DC0%0/2
$force or $content_type =~ m[^(application/(?:x-)?json|text/plain)$]x
$force
$content_type =~ m[^(application/(?:x-)?json|text/plain)$]x
Truth table0%0/3
A or B
A= $forceB= $content_type =~ m[^(application/(?:x-)?json|text/plain)$]x
| |||||||||||||||||||||||||||||
| 138 | 0 | return; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # FIXME: throw sensible exceptions on errors (preserve object...) | ||||||||||||||||||||||||||||
| 142 | # FIXME: isn't charset needed here? | ||||||||||||||||||||||||||||
| 143 | 0 | return from_json($http_message->decoded_content); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 27 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =pod | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 NAME | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | OAuthomatic::Internal::Util - internal helper routines (form parsing and filling) | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 VERSION | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | version 0.0201 | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Internally used by L<OAuthomatic> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 EXPORTS FUNCTIONS | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 fill_httpmsg_form($http_message, $params) | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Serializes $params (dict ref) as form data and sets $http_message (HTTP::Request or HTTP::Response) | ||||||||||||||||||||||||||||
| 172 | content with that data. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 parse_http_msg_form($http_message, $:force_form) | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Parses content as message, returns hashref (empty if parsing failed, | ||||||||||||||||||||||||||||
| 177 | content type is not parseable etc). Supports a few content types (as | ||||||||||||||||||||||||||||
| 178 | HTTP::Body). | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | With $force_form parses also things with incorrect content type. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 fill_httpmsg_text($http_message, $text, $content_type) | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Fills given HTTP::Message content with given text, using encoding | ||||||||||||||||||||||||||||
| 185 | specified inside content type to serialize if text is provided as perl | ||||||||||||||||||||||||||||
| 186 | unicode string (and appending text as is if it is binary string). | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Set's also content_type (here it should be full, with charset). | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | $text can also be specified as reference to string. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 serialize_json($json) | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Serializes JSON to utf-8 encoded string. If $json is already string or string-ref, leaves it as is. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Function defined to keep conventions in one place. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 fill_httpmsg_json($http_message, $json) | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Serializes $params (dict ref) as json data and sets $http_message | ||||||||||||||||||||||||||||
| 201 | (HTTP::Request or HTTP::Response) content with that data. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | In case $json is already scalar or scalar ref, passes it on assuming | ||||||||||||||||||||||||||||
| 204 | it is already serialized. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head2 parse_http_msg_json($http_message, $:force) | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Parses content as message, returns hashref (empty if parsing failed, | ||||||||||||||||||||||||||||
| 209 | content type is not parseable etc). | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | With $force parses also things with incorrect content type. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | This software is copyright (c) 2015 by Marcin Kasperski. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 222 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||