Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

remove Plack::TempBuffer in favor of Stream::Buffered #335

Merged
merged 1 commit into from

2 participants

@doy
Owner

I've released Stream::Buffered to CPAN, does this seem reasonable? I'd like to start the process of splitting Plack out into separate dists. Also, should I move the stream-buffered github repo to the plack organization?

@miyagawa
Owner

Looks great :)

If i understand correctly if a current user sets $Plack::TempBuffer::MaxMemoryBufferSize they will get ignored and fall back to the default value with this change? I wonder if somehow we can rescue/detect that and give warnings or something.

@doy
Owner
doy commented
@doy
Owner
doy commented

Okay, fixed.

@miyagawa miyagawa merged commit 46b1c56 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 1, 2012
  1. @doy
This page is out of date. Refresh to see the latest.
View
3  MANIFEST
@@ -108,9 +108,6 @@ lib/Plack/Request/Upload.pm
lib/Plack/Response.pm
lib/Plack/Runner.pm
lib/Plack/TempBuffer.pm
-lib/Plack/TempBuffer/Auto.pm
-lib/Plack/TempBuffer/File.pm
-lib/Plack/TempBuffer/PerlIO.pm
lib/Plack/Test.pm
lib/Plack/Test/MockHTTP.pm
lib/Plack/Test/Server.pm
View
1  Makefile.PL
@@ -13,6 +13,7 @@ requires 'Pod::Usage', 1.36; # plackup
requires 'File::ShareDir', '1.00'; # Plack::Test::Suite
requires 'Try::Tiny';
requires 'parent';
+requires 'Stream::Buffered', '0.02';
requires 'Devel::StackTrace', 1.23; # Middleware::StackTrace
requires 'Devel::StackTrace::AsHTML', 0.11; # Middleware::StackTrace
View
4 lib/HTTP/Server/PSGI.pm
@@ -10,7 +10,7 @@ use HTTP::Date;
use HTTP::Status;
use List::Util qw(max sum);
use Plack::Util;
-use Plack::TempBuffer;
+use Stream::Buffered;
use Plack::Middleware::ContentLength;
use POSIX qw(EINTR);
use Socket qw(IPPROTO_TCP TCP_NODELAY);
@@ -148,7 +148,7 @@ sub handle_connection {
if ($reqlen >= 0) {
$buf = substr $buf, $reqlen;
if (my $cl = $env->{CONTENT_LENGTH}) {
- my $buffer = Plack::TempBuffer->new($cl);
+ my $buffer = Stream::Buffered->new($cl);
while ($cl > 0) {
my $chunk;
if (length $buf) {
View
4 lib/Plack/Request.pm
@@ -11,7 +11,7 @@ use Hash::MultiValue;
use HTTP::Body;
use Plack::Request::Upload;
-use Plack::TempBuffer;
+use Stream::Buffered;
use URI;
use URI::Escape ();
@@ -251,7 +251,7 @@ sub _parse_request_body {
# Just in case if input is read by middleware/apps beforehand
$input->seek(0, 0);
} else {
- $buffer = Plack::TempBuffer->new($cl);
+ $buffer = Stream::Buffered->new($cl);
}
my $spin = 0;
View
65 lib/Plack/TempBuffer.pm
@@ -1,70 +1,19 @@
package Plack::TempBuffer;
use strict;
use warnings;
-use FileHandle; # for seek etc.
-use Plack::TempBuffer::PerlIO ();
-use Plack::TempBuffer::File ();
-use Plack::TempBuffer::Auto ();
-our $MaxMemoryBufferSize = 1024 * 1024;
+use parent 'Stream::Buffered';
sub new {
- my($class, $length) = @_;
+ my $class = shift;
- # $MaxMemoryBufferSize = 0 -> Always temp file
- # $MaxMemoryBufferSize = -1 -> Always PerlIO
- my $backend;
- if ($MaxMemoryBufferSize < 0) {
- $backend = "PerlIO";
- } elsif ($MaxMemoryBufferSize == 0) {
- $backend = "File";
- } elsif (!$length) {
- $backend = "Auto";
- } elsif ($length > $MaxMemoryBufferSize) {
- $backend = "File";
- } else {
- $backend = "PerlIO";
+ if (defined $Plack::TempBuffer::MaxMemoryBufferSize) {
+ warn "Setting \$Plack::TempBuffer::MaxMemoryBufferSize is deprecated. "
+ . "You should set \$Stream::Buffered::MaxMemoryBufferSize instead.";
+ $Stream::Buffered::MaxMemoryBufferSize = $Plack::TempBuffer::MaxMemoryBufferSize;
}
- $class->create($backend, $length, $MaxMemoryBufferSize);
+ return $class->SUPER::new(@_);
}
-sub create {
- my($class, $backend, $length, $max) = @_;
- my $package = "$class\::$backend";
- $package->new($length, $max);
-}
-
-sub print;
-sub rewind;
-sub size;
-
1;
-
-__END__
-
-=head1 NAME
-
-Plack::TempBuffer - temporary buffer to save bytes
-
-=head1 SYNOPSIS
-
- my $buf = Plack::TempBuffer->new($length);
- $buf->print($bytes);
-
- my $size = $buf->size;
- my $fh = $buf->rewind;
-
-=head1 DESCRIPTION
-
-Plack::TempBuffer is a buffer class to store arbitrary length of byte
-strings and then get a seekable filehandle once everything is
-buffered. It uses PerlIO and/or temporary file to save the buffer
-depending on the length of the size.
-
-=head1 SEE ALSO
-
-L<Plack> L<Plack::Request>
-
-=cut
-
View
35 lib/Plack/TempBuffer/Auto.pm
@@ -1,35 +0,0 @@
-package Plack::TempBuffer::Auto;
-use strict;
-use parent 'Plack::TempBuffer';
-
-sub new {
- my($class, undef, $max_memory_size) = @_;
- bless {
- _buffer => Plack::TempBuffer->create('PerlIO'),
- _max => $max_memory_size,
- }, $class;
-}
-
-sub print {
- my $self = shift;
- $self->{_buffer}->print(@_);
-
- if ($self->{_max} && $self->{_buffer}->size > $self->{_max}) {
- my $buf = $self->{_buffer}->{buffer};
- $self->{_buffer} = Plack::TempBuffer->create('File'),
- $self->{_buffer}->print($buf);
- delete $self->{_max};
- }
-}
-
-sub size {
- my $self = shift;
- $self->{_buffer}->size;
-}
-
-sub rewind {
- my $self = shift;
- $self->{_buffer}->rewind;
-}
-
-1;
View
33 lib/Plack/TempBuffer/File.pm
@@ -1,33 +0,0 @@
-package Plack::TempBuffer::File;
-use strict;
-use parent 'Plack::TempBuffer';
-
-use IO::File;
-
-sub new {
- my $class = shift;
-
- my $fh = IO::File->new_tmpfile;
- $fh->binmode;
-
- bless { fh => $fh }, $class;
-}
-
-sub print {
- my $self = shift;
- $self->{fh}->print(@_);
-}
-
-sub size {
- my $self = shift;
- $self->{fh}->flush;
- -s $self->{fh};
-}
-
-sub rewind {
- my $self = shift;
- $self->{fh}->seek(0, 0);
- $self->{fh};
-}
-
-1;
View
28 lib/Plack/TempBuffer/PerlIO.pm
@@ -1,28 +0,0 @@
-package Plack::TempBuffer::PerlIO;
-use strict;
-use parent 'Plack::TempBuffer';
-
-sub new {
- my $class = shift;
- bless { buffer => '' }, $class;
-}
-
-sub print {
- my $self = shift;
- $self->{buffer} .= "@_";
-}
-
-sub size {
- my $self = shift;
- length $self->{buffer};
-}
-
-sub rewind {
- my $self = shift;
- my $buffer = $self->{buffer};
- open my $io, "<", \$buffer;
- bless $io, 'FileHandle'; # This makes $io work as FileHandle under 5.8, .10 and .11 :/
- return $io;
-}
-
-1;
View
7 t/Plack-TempBuffer/print.t
@@ -2,6 +2,9 @@ use strict;
use Test::More;
use Plack::TempBuffer;
+my $warn = '';
+$SIG{__WARN__} = sub { $warn .= $_[0] };
+
{
my $b = Plack::TempBuffer->new(-1);
$b->print("foo");
@@ -20,6 +23,8 @@ use Plack::TempBuffer;
my $fh = $b->rewind;
isa_ok $fh, 'IO::File';
is do { local $/; <$fh> }, ('foo' x 5);
+ like $warn, qr/MaxMemoryBufferSize.*deprecated/;
+ $warn = '';
}
{
@@ -30,6 +35,8 @@ use Plack::TempBuffer;
my $fh = $b->rewind;
isa_ok $fh, 'IO::File';
is do { local $/; <$fh> }, "foo\n";
+ like $warn, qr/MaxMemoryBufferSize.*deprecated/;
+ $warn = '';
}
done_testing;
Something went wrong with that request. Please try again.