Dancer

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
- 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

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
    };
 
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

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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

1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
        }
    };
 
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.282 second using v1.01-cache-2.11-cpan-00829025b61 )