Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
v3.89 2013-03-14 00:00:00
- Rebased on Mojolicious v3.89
v3.84 2013-02-04 00:00:00
- Rebased on Mojolicious v3.84
v3.70 2012-12-24 00:00:00
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.SKIP
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
^\.(?!perltidyrc)
.*\.old$
\.tar\.gz$
^Makefile$
^MYMETA\.
^blib
^pm_to_blib
4 changes: 2 additions & 2 deletions examples/connect-proxy.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mojo::IOLoop;

# Minimal connect proxy server to test TLS tunneling
# Minimal CONNECT proxy server to test TLS tunneling
my %buffer;
Mojo::IOLoop->server(
{port => 3000} => sub {
Expand Down Expand Up @@ -79,7 +79,7 @@
) or die "Couldn't create listen socket!\n";

print <<'EOF';
Starting connect proxy on port 3000.
Starting CONNECT proxy on port 3000.
For testing use something like "HTTPS_PROXY=http://127.0.0.1:3000".
EOF

Expand Down
4 changes: 2 additions & 2 deletions examples/microhttpd.pl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Write a minimal HTTP response
# (the "Hello World!" message has been optimized away!)
$stream->write("HTTP/1.1 200 OK\x0d\x0a"
$stream->write("HTTP/1.1 200 OK\x0d\x0aContent-Length: 0\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
}
}
Expand All @@ -36,7 +36,7 @@

print <<'EOF';
Starting server on port 8080.
Try something like "ab -c 30 -n 100000 -k http://127.0.0.1:8080/" for testing.
Try something like "wrk -c 100 -d 10s http://127.0.0.1:8080/" for testing.
On a MacBook Air this results in about 18k req/s.
EOF

Expand Down
35 changes: 14 additions & 21 deletions examples/websocket.pl
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojolicious::Lite;
use Mojo::JSON 'j';

websocket '/' => sub {
websocket '/test' => sub {
my $self = shift;
$self->on(
text => sub {
my ($self, $data) = @_;
my $hash = j($data);
json => sub {
my ($self, $hash) = @_;
$hash->{test} = "♥ $hash->{test}";
$self->send({text => j($hash)});
$self->send({json => $hash});
}
);
};
Expand All @@ -25,29 +23,24 @@
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
% my $url = url_for->to_abs->scheme('ws');
<title>WebSocket Test</title>
%= javascript begin
var ws;
if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
ws = new WebSocket('<%= url_for('test')->to_abs %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
alert(JSON.parse(event.data).test);
}
function wsopen(event) {
ws.send(JSON.stringify({test: "WebSocket support works! ♥"}));
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
ws.onmessage = function (event) {
document.body.innerHTML += JSON.parse(event.data).test;
};
ws.onopen = function (event) {
ws.send(JSON.stringify({test: 'WebSocket support works! ♥'}));
};
}
else {
alert("Sorry, your browser does not support WebSockets.");
document.body.innerHTML += 'Browser does not support WebSockets.';
}
% end
</head>
<body>
Testing WebSockets, please make sure you have JavaScript enabled.
</body>
<body>Testing WebSockets: </body>
</html>
18 changes: 9 additions & 9 deletions lib/Mojo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ sub _dict {

1;

=encoding utf8

=head1 NAME

Mojo - Duct tape for the HTML5 web!
Expand Down Expand Up @@ -130,7 +132,7 @@ plugins, since non-blocking requests that are already in progress will
interfere with new blocking ones.

# Perform blocking request
my $body = $app->ua->get('mojolicio.us')->res->body;
my $body = $app->ua->get('example.com')->res->body;

=head1 METHODS

Expand All @@ -153,17 +155,15 @@ object.

=head2 config

my $config = $app->config;
my $foo = $app->config('foo');
$app = $app->config({foo => 'bar'});
$app = $app->config(foo => 'bar');
my $hash = $app->config;
my $foo = $app->config('foo');
$app = $app->config({foo => 'bar'});
$app = $app->config(foo => 'bar');

Application configuration.

# Manipulate configuration
$app->config->{foo} = 'bar';
my $foo = $app->config->{foo};
delete $app->config->{foo};
# Remove value
my $foo = delete $app->config->{foo};

=head2 handler

Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/Asset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ sub slurp { croak 'Method "slurp" not implemented by subclass' }

1;

=encoding utf8

=head1 NAME

Mojo::Asset - HTTP content storage base class
Expand Down Expand Up @@ -83,9 +85,10 @@ subclass.
=head2 get_chunk

my $bytes = $asset->get_chunk($offset);
my $bytes = $asset->get_chunk($offset, $max);

Get chunk of data starting from a specific position. Meant to be overloaded
in a subclass.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes. Meant to be overloaded in a subclass.

=head2 is_file

Expand Down
35 changes: 20 additions & 15 deletions lib/Mojo/Asset/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ has handle => sub {
}

# Open new or temporary file
my $base = catfile File::Spec::Functions::tmpdir, 'mojo.tmp';
my $base = catfile $self->tmpdir, 'mojo.tmp';
my $name = defined $path ? $path : $base;
until ($handle->open($name, O_CREAT | O_EXCL | O_RDWR)) {
croak qq{Can't open file "$name": $!} if defined $path || $! != $!{EEXIST};
$name = "$base." . md5_sum(time . $$ . rand 9999999);
$name = "$base." . md5_sum(time . $$ . rand 9 x 7);
}
$self->path($name);

Expand Down Expand Up @@ -57,14 +57,14 @@ sub add_chunk {
}

sub contains {
my ($self, $string) = @_;
my ($self, $str) = @_;

my $handle = $self->handle;
$handle->sysseek($self->start_range, SEEK_SET);

# Calculate window size
my $end = defined $self->end_range ? $self->end_range : $self->size;
my $len = length $string;
my $len = length $str;
my $size = $len > 131072 ? $len : 131072;
$size = $end - $self->start_range if $size > $end - $self->start_range;

Expand All @@ -79,7 +79,7 @@ sub contains {
$window .= $buffer;

# Search window
my $pos = index $window, $string;
my $pos = index $window, $str;
return $offset + $pos if $pos >= 0;
$offset += $read;
return -1 if $read == 0 || $offset == $end;
Expand All @@ -92,19 +92,20 @@ sub contains {
}

sub get_chunk {
my ($self, $start) = @_;
my ($self, $offset, $max) = @_;
$max = defined $max ? $max : 131072;

$start += $self->start_range;
$offset += $self->start_range;
my $handle = $self->handle;
$handle->sysseek($start, SEEK_SET);
$handle->sysseek($offset, SEEK_SET);

my $buffer;
if (defined(my $end = $self->end_range)) {
my $chunk = $end + 1 - $start;
my $chunk = $end + 1 - $offset;
return '' if $chunk <= 0;
$handle->sysread($buffer, $chunk > 131072 ? 131072 : $chunk);
$handle->sysread($buffer, $chunk > $max ? $max : $chunk);
}
else { $handle->sysread($buffer, 131072) }
else { $handle->sysread($buffer, $max) }

return $buffer;
}
Expand Down Expand Up @@ -139,6 +140,8 @@ sub slurp {

1;

=encoding utf8

=head1 NAME

Mojo::Asset::File - File storage for HTTP content
Expand Down Expand Up @@ -183,7 +186,7 @@ Delete file automatically once it's not used anymore.
my $handle = $file->handle;
$file = $file->handle(IO::File->new);

File handle, created on demand.
Filehandle, created on demand.

=head2 path

Expand All @@ -199,7 +202,7 @@ necessary.
$file = $file->tmpdir('/tmp');

Temporary directory used to generate C<path>, defaults to the value of the
C<MOJO_TMPDIR> environment variable or auto detection.
MOJO_TMPDIR environment variable or auto detection.

=head1 METHODS

Expand All @@ -220,9 +223,11 @@ Check if asset contains a specific string.

=head2 get_chunk

my $bytes = $file->get_chunk($start);
my $bytes = $file->get_chunk($offset);
my $bytes = $file->get_chunk($offset, $max);

Get chunk of data starting from a specific position.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes.

=head2 is_file

Expand Down
24 changes: 14 additions & 10 deletions lib/Mojo/Asset/Memory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ sub add_chunk {
}

sub contains {
my ($self, $string) = @_;
my ($self, $str) = @_;

my $start = $self->start_range;
my $pos = index $self->{content}, $string, $start;
my $pos = index $self->{content}, $str, $start;
$pos -= $start if $start && $pos >= 0;
my $end = $self->end_range;

return $end && ($pos + length $string) >= $end ? -1 : $pos;
return $end && ($pos + length $str) >= $end ? -1 : $pos;
}

sub get_chunk {
my ($self, $start) = @_;
my ($self, $offset, $max) = @_;
$max = defined $max ? $max : 131072;

$start += $self->start_range;
my $size = 131072;
$offset += $self->start_range;
if (my $end = $self->end_range) {
$size = $end + 1 - $start if ($start + $size) > $end;
$max = $end + 1 - $offset if ($offset + $max) > $end;
}

return substr shift->{content}, $start, $size;
return substr shift->{content}, $offset, $max;
}

sub move_to {
Expand All @@ -55,6 +55,8 @@ sub slurp { shift->{content} }

1;

=encoding utf8

=head1 NAME

Mojo::Asset::Memory - In-memory storage for HTTP content
Expand Down Expand Up @@ -110,7 +112,7 @@ automatically upgrade to a L<Mojo::Asset::File> object.

Maximum size in bytes of data to keep in memory before automatically upgrading
to a L<Mojo::Asset::File> object, defaults to the value of the
C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144>.
MOJO_MAX_MEMORY_SIZE environment variable or C<262144>.

=head1 METHODS

Expand Down Expand Up @@ -139,8 +141,10 @@ Check if asset contains a specific string.
=head2 get_chunk

my $bytes = $mem->get_chunk($offset);
my $bytes = $mem->get_chunk($offset, $max);

Get chunk of data starting from a specific position.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes.

=head2 move_to

Expand Down
Loading