Skip to content

Commit

Permalink
fixed redirect support in get command (closes #493)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 21, 2013
1 parent c9b6f1e commit fe8411d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.03 2013-05-21
- Fixed redirect support in get command.

4.02 2013-05-20
- Fixed extends, layout and title helpers not to generate unnecessary log
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Server.pm
Expand Up @@ -35,7 +35,7 @@ sub load_app {
local $ENV{MOJO_EXE};

# Try to load application from script into sandbox
$self->app(my $app = eval sprintf <<'EOF', md5_sum($path . $$));
my $app = eval sprintf <<'EOF', md5_sum($path . $$);
package Mojo::Server::SandBox::%s;
my $app = do $path;
if (!$app && (my $e = $@ || $!)) { die $e }
Expand All @@ -44,6 +44,7 @@ EOF
die qq{Couldn't load application from file "$path": $@} if !$app && $@;
die qq{File "$path" did not return an application object.\n}
unless blessed $app && $app->isa('Mojo');
$self->app($app);
};
FindBin->again;

Expand Down
53 changes: 20 additions & 33 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -8,6 +8,7 @@ use Mojo::JSON;
use Mojo::JSON::Pointer;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Scalar::Util 'weaken';

has description => "Perform HTTP request.\n";
has usage => <<"EOF";
Expand Down Expand Up @@ -54,52 +55,38 @@ sub run {
my %headers;
/^\s*([^:]+)\s*:\s*(.+)$/ and $headers{$1} = $2 for @headers;

# Use global event loop singleton
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
$ua->max_redirects(10) if $redirect;

# Detect proxy for absolute URLs
if ($url !~ m!^/!) { $ua->detect_proxy }
else { $ua->app($self->app) }

# Do the real work with "start" event
my $v = my $buffer = '';
my $buffer = '';
$ua->on(
start => sub {
my ($ua, $tx) = @_;

# Verbose callback
my $v = $verbose;
my $cb = sub {
my $res = shift;

# Wait for headers
return unless $v && $res->headers->is_finished;
$v = undef;

# Show request
my $req = $tx->req;
my $startline = $req->build_start_line;
my $req_headers = $req->build_headers;
warn "$startline$req_headers";

# Show response
my $version = $res->version;
my $code = $res->code;
my $msg = $res->message;
my $res_headers = $res->headers->to_string;
warn "HTTP/$version $code $msg\n$res_headers\n\n";
};
$tx->res->on(progress => $cb);

# Stream content
# Verbose
weaken $tx;
$tx->res->content->on(
body => sub {

# Request
my $req = $tx->req;
warn $req->$_ for qw(build_start_line build_headers);

# Response
my $res = $tx->res;
warn $res->$_ for qw(build_start_line build_headers);
}
) if $verbose;

# Stream content (ignore redirects)
$tx->res->content->unsubscribe('read')->on(
read => sub {
$cb->(my $res = shift);

# Ignore intermediate content
return if $redirect && $res->is_status_class(300);
defined $selector ? ($buffer .= pop) : print(pop);
return if $redirect && $tx->res->is_status_class(300);
defined $selector ? ($buffer .= pop) : print pop;
}
);
}
Expand Down

0 comments on commit fe8411d

Please sign in to comment.