HTTP::Body - small performance tweaks to urlencoded parser
Andy Grundman [Sat, 24 Mar 2007 03:47:33 +0000 (03:47 +0000)]
Changes
lib/HTTP/Body.pm
lib/HTTP/Body/UrlEncoded.pm
t/data/urlencoded/001-content.dat
t/data/urlencoded/001-headers.yml
t/data/urlencoded/001-results.yml

diff --git a/Changes b/Changes
index d7cf2dd..2bc8eff 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 This file documents the revision history for Perl extension HTTP::Body.
 
+0.9
+        - Small performance tweaks to urlencoded parser.
+
 0.8   2007-03-23 18:40:00
         - Some browsers such as MSIE send extra data after the body content.  We now
           properly ignore anything beyond Content-Length.
index 27addcc..e915bfa 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 
 use Carp       qw[ ];
 
-our $VERSION = 0.8;
+our $VERSION = 0.9;
 
 our $TYPES = {
     'application/octet-stream'          => 'HTTP::Body::OctetStream',
index 4cc6d59..9545688 100644 (file)
@@ -6,6 +6,15 @@ use bytes;
 
 our $DECODE = qr/%([0-9a-fA-F]{2})/;
 
+our %hex_chr;
+
+BEGIN {
+    for my $num ( 0 .. 255 ) {
+        my $h = sprintf "%02X", $num;
+        $hex_chr{ lc $h } = $hex_chr{ uc $h } = chr $num;
+    }
+}
+
 =head1 NAME
 
 HTTP::Body::UrlEncoded - HTTP Body UrlEncoded Parser
@@ -30,6 +39,8 @@ sub spin {
     my $self = shift;
 
     return unless $self->length == $self->content_length;
+    
+    $self->{buffer} =~ tr/+/ /;
 
     for my $pair ( split( /[&;]/, $self->{buffer} ) ) {
 
@@ -37,11 +48,9 @@ sub spin {
 
         next unless defined $name;
         next unless defined $value;
-
-        $name  =~ tr/+/ /;
-        $name  =~ s/$DECODE/chr(hex($1))/eg;
-        $value =~ tr/+/ /;
-        $value =~ s/$DECODE/chr(hex($1))/eg;
+        
+        $name  =~ s/$DECODE/$hex_chr{$1}/gs;
+        $value =~ s/$DECODE/$hex_chr{$1}/gs;
 
         $self->param( $name, $value );
     }
index 865341a..ea25aba 100644 (file)
@@ -1 +1 @@
-text1=Ratione+accusamus+aspernatur+aliquam&text2=%C3%A5%C3%A4%C3%B6%C3%A5%C3%A4%C3%B6&select=A&select=B&textarea=Voluptatem+cumque+voluptate+sit+recusandae+at.+Et+quas+facere+rerum+unde+esse.+Sit+est+et+voluptatem.+Vel+temporibus+velit+neque+odio+non.%0D%0A%0D%0AMolestias+rerum+ut+sapiente+facere+repellendus+illo.+Eum+nulla+quis+aut.+Quidem+voluptas+vitae+ipsam+officia+voluptatibus+eveniet.+Aspernatur+cupiditate+ratione+aliquam+quidem+corrupti.+Eos+sunt+rerum+non+optio+culpa.
\ No newline at end of file
+text1=Ratione+accusamus+aspernatur+aliquam&text2=%C3%A5%C3%A4%C3%B6%C3%A5%C3%A4%C3%B6&select=A&select=B&textarea=Voluptatem+cumque+voluptate+sit+recusandae+at.+Et+quas+facere+rerum+unde+esse.+Sit+est+et+voluptatem.+Vel+temporibus+velit+neque+odio+non.%0D%0A%0D%0AMolestias+rerum+ut+sapiente+facere+repellendus+illo.+Eum+nulla+quis+aut.+Quidem+voluptas+vitae+ipsam+officia+voluptatibus+eveniet.+Aspernatur+cupiditate+ratione+aliquam+quidem+corrupti.+Eos+sunt+rerum+non+optio+culpa.&encoding=foo%3Dbar
\ No newline at end of file
index ba334a7..e041c87 100644 (file)
@@ -1,4 +1,4 @@
 ---
-Content-Length: 480
+Content-Length: 499
 Content-Type: application/x-www-form-urlencoded
 User-Agent: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'
index 430caf3..1571078 100644 (file)
@@ -7,4 +7,5 @@ param:
   text1: Ratione accusamus aspernatur aliquam
   text2: åäöåäö
   textarea: "Voluptatem cumque voluptate sit recusandae at. Et quas facere rerum unde esse. Sit est et voluptatem. Vel temporibus velit neque odio non.\r\n\r\nMolestias rerum ut sapiente facere repellendus illo. Eum nulla quis aut. Quidem voluptas vitae ipsam officia voluptatibus eveniet. Aspernatur cupiditate ratione aliquam quidem corrupti. Eos sunt rerum non optio culpa."
+  encoding: foo=bar
 upload: {}