Dancer

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

   Simões)
 - Hard deprecate lots of stuff (Alberto Simões)

1.3079_02 2011-08-28
 [BUG FIXES]
 - Remove hard-coded version from 404.html and 500.html (Alberto Simões)
 - Fix logging of UTF8-encoded strings (jamhed)
 - Do not clean 'vars' during forward (Alberto Simões)

 [ENHANCEMENTS]
 - Add streaming support to send_file. (Sawyer X)

1.3079_01 2011-08-17
 [BUG FIXES]
 - Fix prefix behavior with load_app (alexrj)
 - send_file() shouldn't clobber previously-set response status (David
   Precious, reported by tylerdu - thanks!)
 - Depend on URI 1.59 - Fixes problems when redirecting with UTF-8 strings
   (Alberto Simões)
 - Fix before_serializer POD fix (Yanick Champoux)

README  view on Meta::CPAN


        get '/some/route' => sub {
            if (...) {
                # we want to let the next matching route handler process this one
                send_file(...);
                # This code will be ignored
                do_stuff();
            }
        };

    Send file supports streaming possibility using PSGI streaming. The
    server should support it but normal streaming is supported on most, if
    not all.

        get '/download/:file' => sub {
            send_file( params->{file}, streaming => 1 );
        }

    You can control what happens using callbacks.

    First, around_content allows you to get the writer object and the chunk
    of content read, and then decide what to do with each chunk:

        get '/download/:file' => sub {
            send_file(
                params->{file},
                streaming => 1,
                callbacks => {
                    around_content => sub {
                        my ( $writer, $chunk ) = @_;
                        $writer->write("* $chunk");
                    },
                },
            );
        }

    You can use around to all get all the content (whether a filehandle if
    it's a regular file or a full string if it's a scalar ref) and decide
    what to do with it:

        get '/download/:file' => sub {
            send_file(
                params->{file},
                streaming => 1,
                callbacks => {
                    around => sub {
                        my ( $writer, $content ) = @_;
    
                        # we know it's a text file, so we'll just stream
                        # line by line
                        while ( my $line = <$content> ) {
                            $writer->write($line);
                        }
                    },
                },
            );
        }

    Or you could use override to control the entire streaming callback
    request:

        get '/download/:file' => sub {
            send_file(
                params->{file},
                streaming => 1,
                callbacks => {
                    override => sub {
                        my ( $respond, $response ) = @_;
    
                        my $writer = $respond->( [ $newstatus, $newheaders ] );
                        $writer->write("some line");
                    },
                },
            );
        }

    You can also set the number of bytes that will be read at a time
    (default being 42K bytes) using bytes:

        get '/download/:file' => sub {
            send_file(
                params->{file},
                streaming => 1,
                bytes     => 524288, # 512K
            );
        };

    The content-type will be set depending on the current MIME types
    definition (see mime if you want to define your own).

    If your filename does not have an extension, or you need to force a
    specific mime type, you can pass it to send_file as follows:

lib/Dancer.pm  view on Meta::CPAN

    :           Dancer::Session->write(@_);
}

sub _send_file {
    my ($path, %options) = @_;
    my $env = Dancer::SharedData->request->env;

    my $request = Dancer::Request->new_for_request('GET' => $path);
    Dancer::SharedData->request($request);

    # if you asked for streaming but it's not supported in PSGI
    if ( $options{'streaming'} && ! $env->{'psgi.streaming'} ) {
        # TODO: throw a fit (AKA "exception") or a Dancer::Error?
        raise core => 'Sorry, streaming is not supported on this server.';
    }

    if (exists($options{content_type})) {
        $request->content_type($options{content_type});
    }

    # If we're given an IO::Scalar object, DTRT (take the scalar ref from it)
    if (Scalar::Util::blessed($path) && $path->isa('IO::Scalar')) {
        $path = $path->sref;
    }

lib/Dancer/Handler.pm  view on Meta::CPAN

    if ($response->status =~ (/^[23]04$/ )) {
        $content = [''];
        $response->header('Content-Length' => 0);
    }

    Dancer::Logger::core("response: " . $response->status);

    my $status  = $response->status();
    my $headers = $response->headers_to_array();

    # reverse streaming
    if ( ref $response->streamed and ref $response->streamed eq 'CODE' ) {
        return $response->streamed->(
            $status, $headers
        );
    }

    return [ $status, $headers, $content ];
}

sub _is_text {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.906 second using v1.00-cache-2.02-grep-82fe00e-cpan-503542c4f10 )