xeronuro
11/19/2012 - 3:38 AM

gistfile1.txt

#!/usr/bin/env perl

# put.pl

# This works :
# ./put.pl daemon
# mojo get --method PUT --content 'foo' --header 'X-Disk: /tmp' http://localhost:3000/here

# This does not :
# ./put.pl get --method PUT --content 'foo' --header 'X-Disk: /tmp' /here

# This prints "after_build_tx" in the log, unlike --method PUT
# ./put.pl get /

use Mojolicious::Lite;

$ENV{MOJO_MAX_MEMORY_SIZE} = 2;   # Force temp files.
$ENV{MOJO_TMPDIR} = "/nosuchdir"; # test setting tempdir dynamically

app->hook(
    after_build_tx => sub {
        my ( $tx, $app ) = @_;
        app->log->warn("after_build_tx");
        $tx->req->content->on( body => sub {
            my $content = shift;
            my $disk = $content->headers->header('X-Disk');
            $content->asset->on(
                upgrade => sub {
                    my ( $mem, $file ) = @_;
                    $file->tmpdir($disk);
                }
            );
        })
    }
);

get '/' => sub {
    shift->render_text('hi');
};

put '/here' => sub {
    shift->render_text('ok');
};

app->start;