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