Dancer

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

 - If YAML does not load, Dancer::Config now reports why (Ovid)

1.3095_01 2012-06-22
 [BUG FIXES]
 - Don't assume returned references are blessed when considering
   continuations (Neil Hooey, GH-778)
 - Malformed/missing cookies caused warnings  (James Aitken/LoonyPandora,
   GH-782 and GH-783)
 - Avoid potential crash in t/14_serializer/06_api.t if tmp dir is replaced
   when %ENV gets cleared (Adam Kennedy)
 - Properly initialize %callbacks to default empty hashref  in _send_file
   if  not provided (Gary Mullen)

 [DOCUMENTATION]
 - Update Ubic service example (Vyacheslav Matyukhin)
 - Silly typo fixing (Paul Fenwick)
 - Typo in Dancer::Test file upload example (Jonathan "Duke" Leto)
 - UTF-8 fixes in POD (ambs)

 [ENHANCEMENTS]
 - Add UTC timestamp options for logger_format (Alex C - perlpong).

README  view on Meta::CPAN

        };

    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");
                    },
                },
            );
        }

lib/Dancer.pm  view on Meta::CPAN

        if (exists($options{filename})) {
            $resp->push_header('Content-Disposition' => 
                "attachment; filename=\"$options{filename}\""
            );
        }

        if ( $options{'streaming'} ) {
            # handle streaming
            $resp->streamed( sub {
                my ( $status, $headers ) = @_;
                my %callbacks = defined $options{'callbacks'} ?
                                %{ $options{'callbacks'} }    :
                                ();

                return sub {
                    my $respond = shift;
                    exists $callbacks{'override'}
                        and return $callbacks{'override'}->( $respond, $resp );

                    # get respond callback and set headers, get writer in return
                    my $writer = $respond->( [
                        $status,
                        $headers,
                    ] );

                    # get content from original response
                    my $content = $resp->content;

                    exists $callbacks{'around'}
                        and return $callbacks{'around'}->( $writer, $content );

                    if ( ref $content ) {
                        my $bytes = $options{'bytes'} || '43008'; # 42K (dams)
                        my $buf;
                        while ( ( my $read = sysread $content, $buf, $bytes ) != 0 ) {
                            if ( exists $callbacks{'around_content'} ) {
                                $callbacks{'around_content'}->( $writer, $buf );
                            } else {
                                $writer->write($buf);
                            }
                        }
                    } else {
                        $writer->write($content);
                    }
                };
            } );
        }

lib/Dancer.pm  view on Meta::CPAN

        }
    };

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, C<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 C<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 C<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");
                },
            },
        );
    }



( run in 0.397 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )