Skip to content

Commit

Permalink
more comment tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 20, 2013
1 parent 38fd46d commit 894cf80
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 42 deletions.
3 changes: 1 addition & 2 deletions lib/Mojo.pm
Expand Up @@ -15,7 +15,6 @@ has log => sub { Mojo::Log->new };
has ua => sub {
my $self = shift;

# Fresh user agent
my $ua = Mojo::UserAgent->new->app($self);
weaken $self;
$ua->on(error => sub { $self->log->error($_[1]) });
Expand All @@ -30,7 +29,7 @@ sub new {
# Detect home directory
my $home = $self->home->detect(ref $self);

# Log directory
# Check if we have a log directory
$self->log->path($home->rel_file('log/mojo.log'))
if -w $home->rel_file('log');

Expand Down
6 changes: 1 addition & 5 deletions lib/Mojo/Content.pm
Expand Up @@ -293,12 +293,8 @@ sub _parse_until_body {

# Parser started
unless ($self->{state}) {

# Update size
$self->{header_size} = $self->{raw_size} - length $self->{pre_buffer};

# Headers
$self->{state} = 'headers';
$self->{state} = 'headers';
}

# Parse headers
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/Exception.pm
Expand Up @@ -21,7 +21,6 @@ sub throw { die shift->new->trace(2)->_detect(@_) }
sub to_string {
my $self = shift;

# Message
return $self->message unless $self->verbose;
my $string = $self->message ? $self->message : '';

Expand Down Expand Up @@ -88,7 +87,6 @@ sub _context {
sub _detect {
my ($self, $msg, $files) = @_;

# Message
return $msg if blessed $msg && $msg->isa('Mojo::Exception');
$self->message($msg);

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -318,7 +318,7 @@ sub _parse_formdata {
push @parts, $content;
while (my $part = shift @parts) {

# Multipart
# Nested multipart content
if ($part->is_multipart) {
unshift @parts, @{$part->parts};
next;
Expand All @@ -334,7 +334,7 @@ sub _parse_formdata {
$filename = decode($charset, $filename) // $filename if $filename;
}

# Upload
# Check for file upload
my $value = $part;
unless (defined $filename) {
$value = $part->asset->slurp;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -78,7 +78,7 @@ sub new {
$r->hide(qw(rendered req res respond_to send session signed_cookie stash));
$r->hide(qw(tx ua url_for write write_chunk));

# Prepare log
# Check if we have a log directory
my $mode = $self->mode;
$self->log->path($home->rel_file("log/$mode.log"))
if -w $home->rel_file('log');
Expand Down Expand Up @@ -145,12 +145,12 @@ sub handler {
= $self->controller_class->new(app => $self, stash => $stash, tx => $tx);
weaken $c->{$_} for qw(app tx);

# Dispatcher
# Dispatcher has to be last in the chain
++$self->{dispatch}
and $self->hook(around_dispatch => sub { $_[1]->app->dispatch($_[1]) })
unless $self->{dispatch};

# Process
# Process with chain
unless (eval { $self->plugins->emit_chain(around_dispatch => $c) }) {
$self->log->fatal("Processing request failed: $@");
$tx->res->code(500);
Expand Down
32 changes: 12 additions & 20 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -45,33 +45,28 @@ sub run {
'M|method=s' => \(my $method = 'GET'),
'r|redirect' => \my $redirect,
'v|verbose' => \my $verbose;
$verbose = 1 if $method eq 'HEAD';

# Headers
my %headers;
/^\s*([^:]+)\s*:\s*(.+)$/ and $headers{$1} = $2 for @headers;

# URL and selector
die $self->usage unless my $url = decode 'UTF-8', shift @args // '';
my $selector = shift @args;

# Fresh user agent
# Parse header pairs
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;

# Absolute URL
if ($url !~ m!/!) { $ua->detect_proxy }

# Application
else { $ua->app($self->app) }
# 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 = '';
$ua->on(
start => sub {
my $tx = pop;

# Prepare request information
my $req = $tx->req;
my $startline = $req->build_start_line;
my $req_headers = $req->build_headers;
Expand All @@ -85,18 +80,16 @@ sub run {
return unless $v && $res->headers->is_finished;
$v = undef;

# Request
# Show request
warn "$startline$req_headers";

# Response
# 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";
};

# Progress
$tx->res->on(progress => $cb);

# Stream content
Expand All @@ -112,11 +105,10 @@ sub run {
}
);

# Perform request
# Perform request and handle errors
$verbose = 1 if $method eq 'HEAD';
STDOUT->autoflush(1);
my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));

# Error
my ($err, $code) = $tx->error;
$url = encode 'UTF-8', $url;
warn qq{Problem loading URL "$url". ($err)\n} if $err && !$code;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -22,18 +22,18 @@ sub import {
push @{"${caller}::ISA"}, 'Mojo';
my $app = shift->new;

# Moniker
# Generate moniker based on filename
my $moniker = basename $ENV{MOJO_EXE};
$moniker =~ s/\.(?:pl|pm|t)$//i;
$app->moniker($moniker);

# Initialize routes
# Initialize routes without namespaces
my $routes = $app->routes->namespaces([]);

# Default static and template class
$app->static->classes->[0] = $app->renderer->classes->[0] = $caller;

# Functions
# The Mojolicious::Lite DSL
my $root = $routes;
for my $name (qw(any get options patch post put websocket)) {
monkey_patch $caller, $name, sub { $routes->$name(@_) };
Expand Down
7 changes: 2 additions & 5 deletions lib/ojo.pm
Expand Up @@ -11,7 +11,7 @@ use Mojo::Util 'monkey_patch';
# Silent oneliners
$ENV{MOJO_LOG_LEVEL} ||= 'fatal';

# User agent
# Singleton user agent for oneliners
my $UA = Mojo::UserAgent->new;

sub import {
Expand All @@ -21,13 +21,10 @@ sub import {
eval "package $caller; use Mojolicious::Lite;";
$UA->app($caller->app);

# Allow redirects
$UA->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS};

# Detect proxy
$UA->detect_proxy unless defined $ENV{MOJO_PROXY};

# Functions
# The ojo DSL
monkey_patch $caller,
a => sub { $caller->can('any')->(@_) and return $UA->app },
b => \&b,
Expand Down

0 comments on commit 894cf80

Please sign in to comment.