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