HTTP::Body - small performance tweaks to urlencoded parser
[catagits/HTTP-Body.git] / lib / HTTP / Body / UrlEncoded.pm
1 package HTTP::Body::UrlEncoded;
2
3 use strict;
4 use base 'HTTP::Body';
5 use bytes;
6
7 our $DECODE = qr/%([0-9a-fA-F]{2})/;
8
9 our %hex_chr;
10
11 BEGIN {
12     for my $num ( 0 .. 255 ) {
13         my $h = sprintf "%02X", $num;
14         $hex_chr{ lc $h } = $hex_chr{ uc $h } = chr $num;
15     }
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.
29
30 =head1 METHODS
31
32 =over 4
33
34 =item spin
35
36 =cut
37
38 sub spin {
39     my $self = shift;
40
41     return unless $self->length == $self->content_length;
42     
43     $self->{buffer} =~ tr/+/ /;
44
45     for my $pair ( split( /[&;]/, $self->{buffer} ) ) {
46
47         my ( $name, $value ) = split( /=/, $pair );
48
49         next unless defined $name;
50         next unless defined $value;
51         
52         $name  =~ s/$DECODE/$hex_chr{$1}/gs;
53         $value =~ s/$DECODE/$hex_chr{$1}/gs;
54
55         $self->param( $name, $value );
56     }
57
58     $self->{buffer} = '';
59     $self->{state}  = 'done';
60 }
61
62 =back
63
64 =head1 AUTHOR
65
66 Christian Hansen, C<ch@ngmedia.com>
67
68 =head1 LICENSE
69
70 This library is free software . You can redistribute it and/or modify 
71 it under the same terms as perl itself.
72
73 =cut
74
75 1;