Dancer
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
Returns a Dancer::Request object representing the current request.
See the Dancer::Request documentation for the methods you can call, for
example:
request->referer; # value of the HTTP referer header
request->remote_address; # user's IP address
request->user_agent; # User-Agent header value
send_error
Returns an HTTP error. By default the HTTP code returned is 500:
get '/photo/:id' => sub {
if (...) {
send_error("Not allowed", 403);
} else {
# return content
}
}
WARNING : Issuing a send_error immediately exits the current route, and
perform the send_error. Thus, any code after a send_error is ignored,
until the end of the route. So it's not necessary anymore to use return
with send_error.
get '/some/route' => sub {
if (...) {
# we want to let the next matching route handler process this one
send_error(..);
# This code will be ignored
do_stuff();
}
};
send_file
Lets the current route handler send a file to the client. Note that the
path of the file must be relative to the public directory unless you
use the system_path option (see below).
get '/download/:file' => sub {
send_file(params->{file});
}
WARNING : Issuing a send_file immediately exits the current route, and
performs the send_file. Thus, any code after a send_file is ignored
until the end of the route. So it's not necessary any more to use
return with send_file.
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:
send_file(params->{file}, content_type => 'image/png');
Also, you can use your aliases or file extension names on content_type,
like this:
send_file(params->{file}, content_type => 'png');
For files outside your public folder, you can use the system_path
switch. Just bear in mind that its use needs caution as it can be
dangerous.
send_file('/etc/passwd', system_path => 1);
If you have your data in a scalar variable, send_file can be useful as
well. Pass a reference to that scalar, and send_file will behave as if
there were a file with that contents:
send_file( \$data, content_type => 'image/png' );
Note that Dancer is unable to guess the content type from the data
contents. Therefore you might need to set the content_type properly.
For this kind of usage an attribute named filename can be useful. It is
used as the Content-Disposition header, to hint the browser about the
filename it should use.
send_file( \$data, content_type => 'image/png'
filename => 'onion.png' );
set
Defines a setting:
set something => 'value';
You can set more than one value at once:
set something => 'value', otherthing => 'othervalue';
setting
Returns the value of a given setting:
setting('something'); # 'value'
set_cookie
Creates or updates cookie values:
get '/some_action' => sub {
set_cookie name => 'value',
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.516 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )