| 1 | package OAuth::Lite2::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use Hash::MultiValue; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 18 | our %EXPORT_TAGS = ( all => [qw( | |||||||||||||||||||||||||||
| 12 | encode_param | ||||||||||||||||||||||||||||
| 13 | decode_param | ||||||||||||||||||||||||||||
| 14 | parse_content | ||||||||||||||||||||||||||||
| 15 | build_content | ||||||||||||||||||||||||||||
| 16 | )] ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 18 | our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | OAuth::Lite2::Util - utility methods for OAuth 2.0 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use OAuth::Lite2::Util qw(encode_param, decode_param); | ||||||||||||||||||||||||||||
| 27 | my $encoded = encode_param($str); | ||||||||||||||||||||||||||||
| 28 | my $origin = decode_param($encoded); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module exports utility methods for OAuth 2.0. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 METHODS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 encode_param ($str) | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 805 | ▶ | sub encode_param { | ||||||||||||||||||||||||||
|
Subroutine
encode_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 805 | my $param = shift; | |||||||||||||||||||||||||||
| 42 | 805 | return URI::Escape::uri_escape($param, '^\w.~-'); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 decode_param ($str) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 21 | ▶ | sub decode_param { | ||||||||||||||||||||||||||
|
Subroutine
decode_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 21 | my $param = shift; | |||||||||||||||||||||||||||
| 51 | 21 | return URI::Escape::uri_unescape($param); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 parse_content ($content) | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | sub parse_content { | ||||||||||||||||||||||||||
|
Subroutine
parse_content: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 2 | my $content = shift; | |||||||||||||||||||||||||||
| 60 | 2 | my $params = Hash::MultiValue->new; | |||||||||||||||||||||||||||
| 61 | 2 | for my $pair (split /\&/, $content) { | |||||||||||||||||||||||||||
| 62 | 8 | my ($key, $value) = split /\=/, $pair; | |||||||||||||||||||||||||||
| 63 | 8 | ▶ | $key = decode_param($key ||''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$key || ''
MC/DC0%0/1
$key || ''
$key
Truth table50%1/2
A
A= $key
| |||||||||||||||||||||||||||||
| 64 | 8 | ▶ | $value = decode_param($value||''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$value || ''
MC/DC0%0/1
$value || ''
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||
| 65 | 8 | $params->add($key, $value); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 2 | return $params; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 build_content ($params) | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 111 | ▶ | sub build_content { | ||||||||||||||||||||||||||
|
Subroutine
build_content: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 111 | my $params = shift; | |||||||||||||||||||||||||||
| 76 | 111 | ▶ | $params = $params->as_hashref_mixed | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($params) and $params->isa("Hash::MultiValue")
MC/DC50%1/2
builtin::blessed($params) and $params->isa("Hash::MultiValue")
builtin::blessed($params)
$params->isa("Hash::MultiValue")
Truth table66%2/3
A and B
A= builtin::blessed($params)B= $params->isa("Hash::MultiValue")
| |||||||||||||||||||||||||||||
| 77 | if blessed($params) && $params->isa('Hash::MultiValue'); | ||||||||||||||||||||||||||||
| 78 | 111 | my @pairs; | |||||||||||||||||||||||||||
| 79 | 111 | for my $key (keys %$params) { | |||||||||||||||||||||||||||
| 80 | 397 | my $k = encode_param($key); | |||||||||||||||||||||||||||
| 81 | 397 | my $v = $params->{$key}; | |||||||||||||||||||||||||||
| 82 | 397 | ▶ | if (ref($v) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | for my $av (@$v) { | |||||||||||||||||||||||||||
| 84 | 4 | push(@pairs, sprintf(q{%s=%s}, $k, encode_param($av))); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 395 | push(@pairs, sprintf(q{%s=%s}, $k, encode_param($v))); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 111 | return join("&", sort @pairs); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 18 | 1; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Lyo Kato, E<lt>lyo.kato@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Copyright (C) 2010 by Lyo Kato | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 104 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||
| 105 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||