Skip to content

Commit

Permalink
added slurp_file method to Mojo::ByteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 25, 2012
1 parent 1ce74ed commit 14d9958
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -17,6 +17,8 @@
- Updated IO::Socket::SSL requirement to 1.75 for IO::Socket::IP support.
- Switched back from IO::Socket::INET6 to IO::Socket::IP for IPv6 support.
- Switched from HMAC-MD5 to HMAC-SHA1 for signed cookies.
- Added slurp_file function to Mojo::Util.
- Added slurp_file method to Mojo::ByteStream.
- Added j and r functions to ojo. (sharifulin, sri)
- Added accept_interval attribute to Mojo::IOLoop.
- Added support for new HTTP status code.
Expand Down
17 changes: 13 additions & 4 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -66,13 +66,16 @@ sub say {
say $handle $$self;
}

sub secure_compare {
my ($self, $check) = @_;
return Mojo::Util::secure_compare $$self, $check;
}
sub secure_compare { Mojo::Util::secure_compare ${shift()}, @_ }

sub size { length ${shift()} }

sub slurp_file {
my $self = shift;
$$self = Mojo::Util::slurp_file @_;
return $self;
}

sub split {
my ($self, $pattern) = @_;
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
Expand Down Expand Up @@ -273,6 +276,12 @@ Alias for L<Mojo::Util/"sha1_sum">.
Size of bytestream.
=head2 C<slurp_file>
$stream = $stream->slurp_file('/etc/passwd');
Alias for L<Mojo::Util/"slurp_file">.
=head2 C<split>
my $collection = $stream->split(',');
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojo/Home.pm
Expand Up @@ -10,8 +10,7 @@ use File::Basename 'dirname';
use File::Find 'find';
use File::Spec::Functions qw(abs2rel catdir catfile splitdir);
use FindBin;
use Mojo::Asset::File;
use Mojo::Util 'class_to_path';
use Mojo::Util qw(class_to_path slurp_file);

has app_class => 'Mojo::HelloWorld';

Expand Down Expand Up @@ -95,9 +94,7 @@ sub rel_dir { catdir(@{shift->{parts} || []}, split '/', shift) }

sub rel_file { catfile(@{shift->{parts} || []}, split '/', shift) }

sub slurp_rel_file {
Mojo::Asset::File->new(path => shift->rel_file(@_))->slurp;
}
sub slurp_rel_file { slurp_file shift->rel_file(@_) }

sub to_string { catdir(@{shift->{parts} || []}) }

Expand Down Expand Up @@ -192,7 +189,7 @@ Portably generate an absolute path for a file relative to the home directory.
=head2 C<slurp_rel_file>
my $string = $home->slurp_rel_file('foo/bar.html');
my $bytes = $home->slurp_rel_file('foo/bar.html');
Portably read all data at once from file relative to the home directory.
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojo/Template.pm
Expand Up @@ -2,10 +2,9 @@ package Mojo::Template;
use Mojo::Base -base;

use Carp 'croak';
use IO::Handle;
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Util qw(decode encode);
use Mojo::Util qw(decode encode slurp_file);

# "If for any reason you're not completely satisfied, I hate you."
has [qw(auto_escape compiled)];
Expand Down Expand Up @@ -295,9 +294,7 @@ sub render_file {

# Slurp file
$self->name($path) unless defined $self->{name};
croak qq{Can't open template "$path": $!} unless open my $file, '<', $path;
my $tmpl = '';
while ($file->sysread(my $buffer, 131072, 0)) { $tmpl .= $buffer }
my $tmpl = slurp_file $path;

# Decode and render
if (my $encoding = $self->encoding) {
Expand Down
20 changes: 18 additions & 2 deletions lib/Mojo/Util.pm
@@ -1,11 +1,13 @@
package Mojo::Util;
use Mojo::Base 'Exporter';

use Carp 'croak';
use Digest::MD5 qw(md5 md5_hex);
use Digest::SHA qw(sha1 sha1_hex);
use Encode ();
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use IO::Handle;
use MIME::Base64 qw(decode_base64 encode_base64);

# Punycode bootstring parameters
Expand Down Expand Up @@ -39,8 +41,8 @@ our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode),
qw(quote secure_compare sha1_bytes sha1_sum trim unquote url_escape),
qw(url_unescape xml_escape)
qw(quote secure_compare sha1_bytes sha1_sum slurp_file trim unquote),
qw(url_escape url_unescape xml_escape)
);

sub b64_decode { decode_base64(shift) }
Expand Down Expand Up @@ -262,6 +264,14 @@ sub secure_compare {
sub sha1_bytes { sha1(@_) }
sub sha1_sum { sha1_hex(@_) }

sub slurp_file {
my $path = shift;
croak qq{Can't open file "$path": $!} unless open my $file, '<', $path;
my $content = '';
while ($file->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
return $content;
}

sub trim {
my $string = shift;
for ($string) {
Expand Down Expand Up @@ -545,6 +555,12 @@ Generate binary SHA1 checksum for string.
Generate SHA1 checksum for string.
=head2 C<slurp_file>
my $bytes = slurp_file '/etc/passwd';
Read all data at once from file.
=head2 C<trim>
my $trimmed = trim $string;
Expand Down
25 changes: 17 additions & 8 deletions t/mojo/bytestream.t
Expand Up @@ -5,10 +5,12 @@ use utf8;
# "Homer, we're going to ask you a few simple yes or no questions.
# Do you understand?
# Yes. *lie dectector blows up*"
use Test::More tests => 144;
use Test::More tests => 146;

# Need to be loaded first to trigger edge case
# MIME::Base64 needs to be loaded first to trigger edge case
use MIME::Base64;
use File::Spec::Functions qw(catfile splitdir);
use FindBin;
use Mojo::Util 'md5_bytes';
use Mojo::ByteStream 'b';

Expand Down Expand Up @@ -400,13 +402,14 @@ b(1, 2, 3)->say;
*STDOUT = $stdout;
is $buffer, "test\n123\n", 'right output';

# Nested bytestreams
$stream = b(b('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
$stream = Mojo::ByteStream->new(Mojo::ByteStream->new('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
# slurp_file
my $file = catfile(splitdir($FindBin::Bin), qw(templates exception.mt));
$stream = b('whatever')->slurp_file($file);
is $stream, "test\n% die;\n123\n", 'right content';
$stream = b()->slurp_file($file)->b64_encode('');
is $stream, 'dGVzdAolIGRpZTsKMTIzCg==', 'right content';

# Secure compare
# secure_compare
ok b('hello')->secure_compare('hello'), 'values are equal';
ok !b('hell')->secure_compare('hello'), 'values are not equal';
ok !b('hallo')->secure_compare('hello'), 'values are not equal';
Expand All @@ -424,3 +427,9 @@ ok b('♥1')->secure_compare('♥1'), 'values are equal';
ok !b('')->secure_compare('♥0'), 'values are not equal';
ok !b('0♥')->secure_compare(''), 'values are not equal';
ok !b('0♥1')->secure_compare('1♥0'), 'values are not equal';

# Nested bytestreams
$stream = b(b('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
$stream = Mojo::ByteStream->new(Mojo::ByteStream->new('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
9 changes: 4 additions & 5 deletions t/mojo/template.t
Expand Up @@ -22,7 +22,6 @@ use Test::More tests => 200;
# "When I held that gun in my hand, I felt a surge of power...
# like God must feel when he's holding a gun."
use File::Spec::Functions qw(catfile splitdir);
use File::Temp;
use FindBin;
use Mojo::Template;

Expand Down Expand Up @@ -1030,13 +1029,13 @@ EOF

# File
$mt = Mojo::Template->new;
my $file = catfile(splitdir($FindBin::Bin), qw(lib test.mt));
my $file = catfile(splitdir($FindBin::Bin), qw(templates test.mt));
$output = $mt->render_file($file, 3);
like $output, qr/23\nHello World!/, 'file';

# Exception in file
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(lib exception.mt));
$file = catfile(splitdir($FindBin::Bin), qw(templates exception.mt));
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
Expand All @@ -1063,7 +1062,7 @@ like "$output", qr/foo\.mt line 2/, 'right result';

# Exception with utf8 context
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(lib utf8_exception.mt));
$file = catfile(splitdir($FindBin::Bin), qw(templates utf8_exception.mt));
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
is $output->lines_before->[0]->[1], '', 'right line';
Expand All @@ -1075,6 +1074,6 @@ is utf8::is_utf8($output->lines_after->[0]->[1]), 1, 'context has utf8 flag';

# Different encodings
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = catfile(splitdir($FindBin::Bin), qw(lib utf8_exception.mt));
$file = catfile(splitdir($FindBin::Bin), qw(templates utf8_exception.mt));
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 14d9958

Please sign in to comment.