Plack::Middleware::BufferedStreaming - Enable buffering for non-streaming aware servers


Plack documentation Contained in the Plack distribution.

Index


Code Index:

NAME

Top

Plack::Middleware::BufferedStreaming - Enable buffering for non-streaming aware servers

SYNOPSIS

Top

  enable "BufferedStreaming";

DESCRIPTION

Top

Plack::Middleware::BufferedStreaming is a PSGI middleware component that wraps the application that uses psgi.streaming interface to run on the servers that do not support the interface, by buffering the writer output to a temporary buffer.

This middleware doesn't do anything and bypass the application if the server supports psgi.streaming interface.

AUTHOR

Top

Yuval Kogman

Tatsuhiko Miyagawa


Plack documentation Contained in the Plack distribution.

package Plack::Middleware::BufferedStreaming;
use strict;
no warnings;
use Carp;
use Plack::Util;
use Scalar::Util qw(weaken);
use parent qw(Plack::Middleware);

sub call {
    my ( $self, $env ) = @_;

    my $caller_supports_streaming = $env->{'psgi.streaming'};
    $env->{'psgi.streaming'} = Plack::Util::TRUE;

    my $res = $self->app->($env);
    return $res if $caller_supports_streaming;

    if ( ref($res) eq 'CODE' ) {
        my $ret;

        $res->(sub {
            my $write = shift;

            if ( @$write == 2 ) {
                my @body;

                $ret = [ @$write, \@body ];

                return Plack::Util::inline_object(
                    write => sub { push @body, $_[0] },
                    close => sub { },
                );
            } else {
                $ret = $write;
                return;
            }
        });

        return $ret;
    } else {
        return $res;
    }
}

1;

__END__