Body/UrlEncoded.pm

filter: Statementstmt 100% 32 / 32 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 94.0% 47 / 50 CC 7 SCAR 16.5
CC
7
Cov
95%
spin
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
« Body/MultiPart.pm ↑ Summary Body/OctetStream.pm »
1 package HTTP::Body::UrlEncoded;
2 25 $HTTP::Body::UrlEncoded::VERSION = '1.24_02'; # TRIAL
3 25 $HTTP::Body::UrlEncoded::VERSION = '1.2402';
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use base 'HTTP::Body';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 our $DECODE = qr/%([0-9a-fA-F]{2})/;
9  
10 25 our $max_params = 1000;
11  
12 25 our %hex_chr;
13  
14 25 for my $num ( 0 .. 255 ) {
15 6400 my $h = sprintf "%02X", $num;
16 6400 $hex_chr{ lc $h } = $hex_chr{ uc $h } = chr $num;
17 }
18  
19 =head1 NAME
20  
21 HTTP::Body::UrlEncoded - HTTP Body UrlEncoded Parser
22  
23 =head1 SYNOPSIS
24  
25 use HTTP::Body::UrlEncoded;
26  
27 =head1 DESCRIPTION
28  
29 HTTP Body UrlEncoded Parser for application/x-www-form-urlencoded bodies.
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item spin
36  
37 Waits until the whole body has arrived, then splits it into fields on C<&>
38 and C<;>, decodes C<+> and percent escapes in names and values, and stores
39 each field with L<HTTP::Body/param>. A field without C<=> is dropped. Croaks
40 when the body has more than C<$HTTP::Body::UrlEncoded::max_params> fields (see
41 L<HTTP::Body/Limits>).
42  
43 =cut
44  
45 68 ▶sub spin {
Subroutine spin: called CC=7 SCAR=19.5
Pod documented
46 68 my $self = shift;
47  
48 68 ▶ return unless $self->length == $self->content_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->length == $self->content_length 42 26 68
49
50 # I tested parsing this using APR::Request, but perl is faster
51 # Pure-Perl 2560/s
52 # APR::Request 2305/s
53
54 # Note: s/// appears faster than tr///
55 26 $self->{buffer} =~ s/\+/ /g;
56  
57 26 ▶ if ( $max_params && ( $self->{buffer} =~ tr/&;// ) >= $max_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTTP::Body::UrlEncoded::max_params and $self->{"buffer"} =~ tr/&;// >= $HTTP::Body::UrlEncoded::max_params) 1 25 26
Condition66%2/3
$HTTP::Body::UrlEncoded::max_params and $self->{"buffer"} =~ tr/&;// >= $HTTP::Body::UrlEncoded::max_params
!ll&&!rl&&r
0251
MC/DC50%1/2
$HTTP::Body::UrlEncoded::max_params and $self->{"buffer"} =~ tr/&;// >= $HTTP::Body::UrlEncoded::max_params
$HTTP::Body::UrlEncoded::max_params $self->{"buffer"} =~ tr/&;// >= $HTTP::Body::UrlEncoded::max_params
Truth table66%2/3
A and B
A= $HTTP::Body::UrlEncoded::max_paramsB= $self->{"buffer"} =~ tr/&;// >= $HTTP::Body::UrlEncoded::max_params
ABresult
0X0
100
111
58 1 Carp::croak("Too many parameters in request body (limit $max_params)");
59 }
60  
61 25 for my $pair ( split( /[&;](?:\s+)?/, $self->{buffer} ) ) {
62  
63 1044 my ( $name, $value ) = split( /=/, $pair , 2 );
64  
65 1044 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1044 1044
66 1044 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1043 1044
67
68 1043 $name =~ s/$DECODE/$hex_chr{ uc $1 }/gs;
69 1043 $value =~ s/$DECODE/$hex_chr{ uc $1 }/gs;
70  
71 1043 $self->param( $name, $value );
72 }
73  
74 25 $self->{buffer} = '';
75 25 $self->{state} = 'done';
76 }
77  
78 =back
79  
80 =head1 SUPPORT
81  
82 See L<HTTP::Body>
83  
84 =head1 AUTHORS
85  
86 Christian Hansen, C<ch@ngmedia.com>
87  
88 Andy Grundman, C<andy@hybridized.org>
89  
90 =head1 LICENSE
91  
92 This library is free software . You can redistribute it and/or modify
93 it under the same terms as perl itself.
94  
95 =cut
96  
97 25 1;
« Body/MultiPart.pm ↑ Summary Body/OctetStream.pm »