HTTPD/Util.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 6 totaltotal 87.3% 76 / 87 CC 5 SCAR 4.0
CC
5
Cov
94%
__ANON__
2.1
parse_urlencoded
2.0
__ANON__
2.0
CRAP 1.5
« HTTPD/Request.pm ↑ Summary HTTPD/HTTPServer.pm »
1 package AnyEvent::HTTPD::Util;
2 12 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 require Exporter;
7 12 our @ISA = qw/Exporter/;
8  
9 12 our @EXPORT = qw/parse_urlencoded url_unescape header_set
10 header_get header_exists/;
11  
12 =head1 NAME
13  
14 AnyEvent::HTTPD::Util - Utility functions for AnyEvent::HTTPD
15  
16 =head1 SYNOPSIS
17  
18 =head1 DESCRIPTION
19  
20 The functions in this package are not public.
21  
22 =over 4
23  
24 =cut
25  
26 8 ▶sub url_unescape {
Subroutine url_unescape: called CC=1 SCAR=0.0
Pod undocumented
27 8 my ($val) = @_;
28 8 $val =~ s/\+/\040/g;
29 8 $val =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr (hex ($1))/eg;
30 8 $val
31 }
32  
33 12 ▶sub parse_urlencoded {
Subroutine parse_urlencoded: called CC=2 SCAR=6.9
Pod undocumented
34 12 my ($cont) = @_;
35 12 my (@pars) = split /[\&\;]/, $cont;
36 12 $cont = {};
37  
38 12 for (@pars) {
39 4 my ($name, $val) = split /=/, $_;
40 4 $name = url_unescape ($name);
41 4 $val = url_unescape ($val);
42  
43 4 push @{$cont->{$name}}, [$val, ''];
44 }
45 $cont
46 12 }
47  
48 8 ▶sub test_connect {
Subroutine test_connect: called CC=1 SCAR=0.1
Pod undocumented
49 8 my ($host, $port, $data) = @_;
50  
51 8 my $c = AE::cv;
52  
53 8 ▶ my $t; $t = AnyEvent->timer (after => 0.1, cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
54 8 my $hdl;
55 my $buf;
56 8 undef $t;
57 8 ▶ tcp_connect $host, $port, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
58 8 ▶ my ($fh) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or die "couldn't connect: $!" 8 0 8
59 or die "couldn't connect: $!";
60  
61 $hdl =
62 AnyEvent::Handle->new (
63 fh => $fh,
64 timeout => 15,
65 8 ▶ on_eof => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 8 $c->send ($buf);
67 8 undef $hdl;
68 },
69 0 ▶ on_timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 warn "test_connect timed out";
71 0 $c->send ($buf);
72 0 undef $hdl;
73 },
74 8 ▶ on_read => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 8 $buf .= $hdl->rbuf;
76 8 $hdl->rbuf = '';
77 8 });
78 8 $hdl->push_write ($data);
79 8 };
80 8 });
81  
82 8 $c
83 }
84  
85 ###
86 # these functions set/get/check existence of a header name:value pair while
87 # ignoring the case of the name
88 #
89 # quick hack, does not scale to large hashes. however, it's not expected to be
90 # run on large hashes.
91 #
92 # a more performant alternative would be to keep two hashes for each set of
93 # headers, one for the headers in the case they like, and one a mapping of
94 # names from some consistent form (say, all lowercase) to the name in the other
95 # hash, including capitalization. (this style is used in HTTP::Headers)
96  
97 270 ▶sub _header_transform_case_insens {
Subroutine _header_transform_case_insens: called CC=2 SCAR=6.9
98 270 my $lname = lc $_[1];
99 270 my (@names) = grep { $lname eq lc ($_) } keys %{$_[0]};
100 270 ▶ @names ? $names[0] : $_[1]
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 55 215 270
101 }
102  
103 121 ▶sub header_set {
Subroutine header_set: called CC=1 SCAR=0.0
Pod undocumented
104 121 my ($hdrs, $name, $value) = @_;
105 121 $name = _header_transform_case_insens ($hdrs, $name);
106 121 $hdrs->{$name} = $value;
107 }
108  
109 49 ▶sub header_get {
Subroutine header_get: called CC=2 SCAR=6.9
Pod undocumented
110 49 my ($hdrs, $name) = @_;
111 49 $name = _header_transform_case_insens ($hdrs, $name);
112 49 ▶ exists $hdrs->{$name} ? $hdrs->{$name} : undef
Branch: 1 branch
BranchTrueFalseTotal
exists $hdrs->{$name} ? : 48 1 49
113 }
114  
115 100 ▶sub header_exists {
Subroutine header_exists: called CC=1 SCAR=0.0
Pod undocumented
116 100 my ($hdrs, $name) = @_;
117 100 $name = _header_transform_case_insens ($hdrs, $name);
118 # NB: even if the value is undefined, return true
119 100 return exists $hdrs->{$name}
120 }
121  
122 =back
123  
124 =head1 AUTHOR
125  
126 Robin Redeker, C<< <elmex@ta-sa.org> >>
127  
128 =head1 SEE ALSO
129  
130 =head1 COPYRIGHT & LICENSE
131  
132 Copyright 2009-2011 Robin Redeker, all rights reserved.
133  
134 This program is free software; you can redistribute it and/or modify it
135 under the same terms as Perl itself.
136  
137 =cut
138  
139 12 1;
140  
« HTTPD/Request.pm ↑ Summary HTTPD/HTTPServer.pm »