GX/HTTP/Util.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 90.7% 69 / 76 CC 8 SCAR 7.7
CC
8
Cov
89%
url_decode_utf8
3.2
url_encode
3.0
url_decode
2.0
CRAP 2.2
« GX/Callback/Queue.pm ↑ Summary GX/Callback/Method.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Util.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Util;
7  
8 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Data
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 58 my %URL_ENCODE_MAP = map { chr( $_ ) => sprintf( "%%%02X", $_ ) } 0 .. 255;
19  
20 # See "Unreserved Characters" in RFC 3986, Section 2.3
21 58 my $REGEX_RESERVED_CHARS_RFC3986 = qr/[^a-zA-Z0-9\-\._~]/;
22  
23  
24 # ----------------------------------------------------------------------------------------------------------------------
25 # Export mechanism
26 # ----------------------------------------------------------------------------------------------------------------------
27  
28 58 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 58 our @EXPORT_OK = qw(
31 quote
32 url_decode
33 url_decode_utf8
34 url_encode
35 url_encode_utf8
36 );
37  
38  
39 # ----------------------------------------------------------------------------------------------------------------------
40 # Public functions
41 # ----------------------------------------------------------------------------------------------------------------------
42  
43 8 ▶sub quote {
Subroutine quote: called CC=2 SCAR=7.0
Pod documented
44  
45 8 my $string = $_[0];
46  
47 8 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 8 8
48  
49 8 $string =~ s/\\/\\\\/g;
50 8 $string =~ s/"/\\"/g;
51 8 $string = '"' . $string . '"';
52  
53 8 return $string;
54  
55 }
56  
57 33 ▶sub url_decode {
Subroutine url_decode: called CC=2 SCAR=7.0
Pod documented
58  
59 33 my $string = shift;
60  
61 33 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 33 33
62  
63 33 $string =~ s/%([0-9a-fA-F]{2})/chr( hex $1 )/eg;
64  
65 33 return $string;
66  
67 }
68  
69 1 ▶sub url_decode_utf8 {
Subroutine url_decode_utf8: called CC=3 SCAR=11.6
Pod documented
70  
71 1 my $string = shift;
72  
73 1 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1 1
74  
75 1 $string =~ s/%([0-9a-fA-F]{2})/chr( hex $1 )/eg;
76  
77 1 ▶ if ( ! utf8::decode( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($string)) 0 1 1
78 0 complain "Cannot decode string";
79 }
80  
81 1 return $string;
82  
83 }
84  
85 313 ▶sub url_encode {
Subroutine url_encode: called CC=3 SCAR=11.0
Pod documented
86  
87 313 my $string = shift;
88  
89 313 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 313 313
90  
91 313 ▶ if ( utf8::is_utf8( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($string)) 3 310 313
92 3 complain "Cannot URL-encode wide characters";
93 }
94  
95 310 $string =~ s/($REGEX_RESERVED_CHARS_RFC3986)/$URL_ENCODE_MAP{$1}/eg;
96  
97 310 return $string;
98  
99 }
100  
101 1 ▶sub url_encode_utf8 {
Subroutine url_encode_utf8: called CC=2 SCAR=7.0
Pod documented
102  
103 1 my $string = shift;
104  
105 1 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1 1
106  
107 1 utf8::encode( $string );
108  
109 1 $string =~ s/($REGEX_RESERVED_CHARS_RFC3986)/$URL_ENCODE_MAP{$1}/eg;
110  
111 1 return $string;
112  
113 }
114  
115  
116 58 1;
117  
118 __END__
« GX/Callback/Queue.pm ↑ Summary GX/Callback/Method.pm »