Skip to content

Commit

Permalink
Merge branch 'release/1.3014'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Sukrieh committed Mar 4, 2011
2 parents cbc8069 + 8ba7a86 commit bd7be1a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
14 changes: 14 additions & 0 deletions CHANGES
@@ -1,3 +1,17 @@
1.3014 04.03.2011

[ BUG FIXES ]
* YAML Session UTF-8 Fix
(Roman Galeev)
* Tests and documentations for Dancer::Request::Upload + type method in
Dancer::Request::Upload
(Michael G. Schwern)
* Dancer::Test::dancer_response handles correctly its 'body' parameter
We can now pass a hash ref as the body of dancer_response, it will
automatically be serialized as an URL-encoded string with the appropriate
content_type header.
(Alexis Sukrieh)

1.3013 01.03.2011 1.3013 01.03.2011


[ ENHANCEMENTS ] [ ENHANCEMENTS ]
Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer.pm
Expand Up @@ -7,7 +7,7 @@ use Cwd 'realpath';


use vars qw($VERSION $AUTHORITY @EXPORT); use vars qw($VERSION $AUTHORITY @EXPORT);


$VERSION = '1.3013'; $VERSION = '1.3014';
$AUTHORITY = 'SUKRIA'; $AUTHORITY = 'SUKRIA';


use Dancer::App; use Dancer::App;
Expand Down
19 changes: 13 additions & 6 deletions lib/Dancer/FileUtils.pm
Expand Up @@ -11,7 +11,7 @@ use Cwd 'realpath';
use base 'Exporter'; use base 'Exporter';
use vars '@EXPORT_OK'; use vars '@EXPORT_OK';


@EXPORT_OK = qw(path dirname read_file_content read_glob_content open_file); @EXPORT_OK = qw(path dirname read_file_content read_glob_content open_file set_file_mode);


# Undo UNC special-casing catfile-voodoo on cygwin # Undo UNC special-casing catfile-voodoo on cygwin
sub _trim_UNC { sub _trim_UNC {
Expand Down Expand Up @@ -54,15 +54,22 @@ sub path_no_verify {


sub dirname { File::Basename::dirname(@_) } sub dirname { File::Basename::dirname(@_) }


sub set_file_mode {
my ($fh) = @_;
require Dancer::Config;
my $charset = Dancer::Config::setting('charset') || 'utf-8';

if($charset) {
binmode($fh, ":encoding($charset)");
}
return $fh;
}

sub open_file { sub open_file {
my ($mode, $filename) = @_; my ($mode, $filename) = @_;
require Dancer::Config;
my $charset = Dancer::Config::setting('charset');
length($charset || '')
and $mode .= ":encoding($charset)";
open(my $fh, $mode, $filename) open(my $fh, $mode, $filename)
or croak "$! while opening '$filename' using mode '$mode'"; or croak "$! while opening '$filename' using mode '$mode'";
return $fh; return set_file_mode($fh);
} }


sub read_file_content { sub read_file_content {
Expand Down
20 changes: 20 additions & 0 deletions lib/Dancer/Request/Upload.pm
Expand Up @@ -58,6 +58,14 @@ sub basename {
File::Basename::basename($self->filename); File::Basename::basename($self->filename);
} }


sub type {
my $self = shift;

return $self->headers->{'Content-Type'};
}



# private # private


=pod =pod
Expand Down Expand Up @@ -112,6 +120,18 @@ false for failure.
$upload->copy_to('/path/to/target') $upload->copy_to('/path/to/target')
=item size
The size of the upload, in bytes.
=item headers
Returns a hash ref of the headers associated with this upload.
=item type
The Content-Type of this upload.
=back =back
=head1 AUTHORS =head1 AUTHORS
Expand Down
3 changes: 2 additions & 1 deletion lib/Dancer/Session/YAML.pm
Expand Up @@ -8,7 +8,7 @@ use base 'Dancer::Session::Abstract';
use Dancer::Logger; use Dancer::Logger;
use Dancer::ModuleLoader; use Dancer::ModuleLoader;
use Dancer::Config 'setting'; use Dancer::Config 'setting';
use Dancer::FileUtils qw(path open_file); use Dancer::FileUtils qw(path set_file_mode);
use File::Copy; use File::Copy;
use File::Temp qw(tempfile); use File::Temp qw(tempfile);


Expand Down Expand Up @@ -76,6 +76,7 @@ sub flush {
my $self = shift; my $self = shift;
my ( $fh, $tmpname ) = my ( $fh, $tmpname ) =
tempfile( $self->id . '.XXXXXXXX', DIR => setting('session_dir') ); tempfile( $self->id . '.XXXXXXXX', DIR => setting('session_dir') );
set_file_mode($fh);
print {$fh} YAML::Dump($self); print {$fh} YAML::Dump($self);
close $fh; close $fh;
move($tmpname, yaml_file($self->id)); move($tmpname, yaml_file($self->id));
Expand Down
21 changes: 20 additions & 1 deletion lib/Dancer/Test.pm
Expand Up @@ -183,9 +183,22 @@ sub dancer_response {


if ($method =~ /^(?:PUT|POST)$/ && $args->{body}) { if ($method =~ /^(?:PUT|POST)$/ && $args->{body}) {
my $body = $args->{body}; my $body = $args->{body};
my $l = length $body;
# coerce hashref into an url-encoded string
if (ref($body) && (ref($body) eq 'HASH')) {
my @tokens;
while (my ($name, $value) = each %{$body}) {
$name = _url_encode($name);
$value = _url_encode($value);
push @tokens, "${name}=${value}";
}
$body = join('&', @tokens);
}

my $l = length $body;
open my $in, '<', \$body; open my $in, '<', \$body;
$ENV{'CONTENT_LENGTH'} = $l; $ENV{'CONTENT_LENGTH'} = $l;
$ENV{'CONTENT_TYPE'} = 'application/x-www-form-urlencoded';
$ENV{'psgi.input'} = $in; $ENV{'psgi.input'} = $in;
} }


Expand Down Expand Up @@ -224,6 +237,12 @@ sub get_response {


# private # private


sub _url_encode {
my $string = shift;
$string =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
return $string;
}

sub _get_file_response { sub _get_file_response {
my ($req) = @_; my ($req) = @_;
my ($method, $path, $params) = @$req; my ($method, $path, $params) = @$req;
Expand Down
13 changes: 11 additions & 2 deletions t/02_request/14_uploads.t
Expand Up @@ -13,7 +13,6 @@ sub test_path {
is dirname($file), $dir, "dir of $file is $dir"; is dirname($file), $dir, "dir of $file is $dir";
} }


plan tests => 15;


my $content = qq{------BOUNDARY my $content = qq{------BOUNDARY
Content-Disposition: form-data; name="test_upload_file"; filename="yappo.txt" Content-Disposition: form-data; name="test_upload_file"; filename="yappo.txt"
Expand Down Expand Up @@ -50,6 +49,8 @@ SHOGUN6
$content =~ s/\r\n/\n/g; $content =~ s/\r\n/\n/g;
$content =~ s/\n/\r\n/g; $content =~ s/\n/\r\n/g;


plan tests => 17;

do { do {
open my $in, '<', \$content; open my $in, '<', \$content;
my $req = Dancer::Request->new( my $req = Dancer::Request->new(
Expand All @@ -76,6 +77,15 @@ do {
is $req->uploads->{'test_upload_file4'}[0]->content, 'SHOGUN4', is $req->uploads->{'test_upload_file4'}[0]->content, 'SHOGUN4',
"... content for other also good"; "... content for other also good";


note "headers";
is_deeply $uploads[0]->headers, {
'Content-Disposition' => q[form-data; name="test_upload_file"; filename="yappo.txt"],
'Content-Type' => 'text/plain',
};

note "type";
is $uploads[0]->type, 'text/plain';

my $test_upload_file3 = $req->upload('test_upload_file3'); my $test_upload_file3 = $req->upload('test_upload_file3');
is $test_upload_file3->content, 'SHOGUN3', is $test_upload_file3->content, 'SHOGUN3',
"content for upload #3 as a scalar is good, via req->upload"; "content for upload #3 as a scalar is good, via req->upload";
Expand Down Expand Up @@ -112,4 +122,3 @@ do {


unlink($file) if ($^O eq 'MSWin32'); unlink($file) if ($^O eq 'MSWin32');
}; };

0 comments on commit bd7be1a

Please sign in to comment.