[libhttp-entity-parser-perl] 01/03: make buffer length configurable per instance #3

gregor herrmann gregoa at debian.org
Sun Oct 23 00:23:45 UTC 2016


This is an automated email from the git hooks/post-receive script.

gregoa pushed a commit to tag 0.15
in repository libhttp-entity-parser-perl.

commit 4bdc89afb2ed66ce68e1f32f4cf0d15a8239a7da
Author: Masahiro Nagano <kazeburo at gmail.com>
Date:   Tue Nov 17 14:09:03 2015 +0900

    make buffer length configurable per instance #3
---
 README.md                 |  6 +++++-
 lib/HTTP/Entity/Parser.pm | 24 +++++++++++++++++++-----
 t/00_compile.t            |  4 ++++
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 2a84c07..9939274 100644
--- a/README.md
+++ b/README.md
@@ -25,10 +25,14 @@ This module support application/x-www-form-urlencoded, multipart/form-data and a
 
 # METHODS
 
-- new()
+- new( buffer\_length => $length:Intger)
 
     Create the instance.
 
+    - buffer\_length
+
+        buffer length, HTTP::Entity::Parser read from psgi.input. 16384 by default.
+
 - register($content\_type:String, $class:String, $opts:HashRef)
 
     Register parser class.
diff --git a/lib/HTTP/Entity/Parser.pm b/lib/HTTP/Entity/Parser.pm
index a995ad2..aeec03c 100644
--- a/lib/HTTP/Entity/Parser.pm
+++ b/lib/HTTP/Entity/Parser.pm
@@ -8,7 +8,7 @@ use Module::Load;
 
 our $VERSION = "0.14";
 
-our $READ_BUF_SIZE = 16384;
+our $BUFFER_LENGTH = 16384;
 
 our %LOADED;
 our @DEFAULT_PARSER = qw/
@@ -23,7 +23,12 @@ for my $parser ( @DEFAULT_PARSER ) {
 }
 
 sub new {
-    bless [ [] ], $_[0];
+    my $class = shift;
+    my %args = (
+        buffer_length => $BUFFER_LENGTH,
+        @_,
+    );
+    bless [ [], $args{buffer_length} ], $class;
 }
 
 sub register {
@@ -38,6 +43,7 @@ sub register {
 sub parse {
     my ($self, $env) = @_;
 
+    my $buffer_length = $self->[1];
     my $ct = $env->{CONTENT_TYPE};
     if (!$ct) {
         # No Content-Type
@@ -71,7 +77,7 @@ sub parse {
     if ( my $cl = $env->{CONTENT_LENGTH} ) {
         my $spin = 0;
         while ($cl > 0) {
-            $input->read(my $chunk, $cl < $READ_BUF_SIZE ? $cl : $READ_BUF_SIZE);
+            $input->read(my $chunk, $cl < $buffer_length ? $cl : $buffer_length);
             my $read = length $chunk;
             $cl -= $read;
             $parser->add($chunk);
@@ -85,7 +91,7 @@ sub parse {
         my $chunk_buffer = '';
         my $length;
         DECHUNK: while(1) {
-            $input->read(my $chunk, $READ_BUF_SIZE);
+            $input->read(my $chunk, $buffer_length);
             $chunk_buffer .= $chunk;
             while ( $chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) {
                 my $trailer   = $1;
@@ -151,10 +157,18 @@ This module support application/x-www-form-urlencoded, multipart/form-data and a
 
 =over 4
 
-=item new()
+=item new( buffer_length => $length:Intger)
 
 Create the instance.
 
+=over 4
+
+=item buffer_length
+
+buffer length, HTTP::Entity::Parser read from psgi.input. 16384 by default.
+
+=back
+
 =item register($content_type:String, $class:String, $opts:HashRef)
 
 Register parser class.
diff --git a/t/00_compile.t b/t/00_compile.t
index 1f198b0..7aa33ac 100644
--- a/t/00_compile.t
+++ b/t/00_compile.t
@@ -9,5 +9,9 @@ use_ok $_ for qw(
     HTTP::Entity::Parser::UrlEncoded
 );
 
+my $parser = HTTP::Entity::Parser->new(buffer_length => 100);
+ok($parser);
+is($parser->[1],100);
+
 done_testing;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libhttp-entity-parser-perl.git



More information about the Pkg-perl-cvs-commits mailing list