Skip to content

Commit

Permalink
describe request rewriting examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 13, 2012
1 parent 4d6555a commit 4d52d28
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 88 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Content.pm
Expand Up @@ -16,7 +16,7 @@ sub body_size { croak 'Method "body_size" not implemented by subclass' }

sub boundary {
(shift->headers->content_type || '')
=~ m#multipart.*boundary="*([a-zA-Z0-9'(),.:?\-_+/]+)#i
=~ m!multipart.*boundary="*([a-zA-Z0-9'(),.:?\-_+/]+)!i
and return $1;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -77,7 +77,7 @@ sub build_boundary {
}

# Add boundary to Content-Type header
$type =~ m#^(.*multipart/[^;]+)(.*)$#;
$type =~ m!^(.*multipart/[^;]+)(.*)$!;
my $before = $1 || 'multipart/mixed';
my $after = $2 || '';
$headers->content_type("$before; boundary=$boundary$after");
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -22,7 +22,7 @@ my $ATTR_RE = qr/
)?
\s*
/x;
my $END_RE = qr#^\s*/\s*(.+)\s*#;
my $END_RE = qr!^\s*/\s*(.+)\s*!;
my $TOKEN_RE = qr/
([^<]*) # Text
(?:
Expand Down Expand Up @@ -115,7 +115,7 @@ sub parse {
if ($tag =~ $END_RE) { $self->_end($cs ? $1 : lc($1), \$current) }

# Start
elsif ($tag =~ m#([^\s/]+)([\s\S]*)#) {
elsif ($tag =~ m!([^\s/]+)([\s\S]*)!) {
my ($start, $attr) = ($cs ? $1 : lc($1), $2);

# Attributes
Expand All @@ -137,11 +137,11 @@ sub parse {

# Empty element
$self->_end($start, \$current)
if (!$self->xml && $VOID{$start}) || $attr =~ m#/\s*$#;
if (!$self->xml && $VOID{$start}) || $attr =~ m!/\s*$!;

# Relaxed "script" or "style"
if ($start ~~ [qw(script style)]) {
if ($html =~ m#\G(.*?)<\s*/\s*$start\s*>#gcsi) {
if ($html =~ m!\G(.*?)<\s*/\s*$start\s*>!gcsi) {
$self->_raw($1, \$current);
$self->_end($start, \$current);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -170,7 +170,7 @@ sub _decode_string {

# Unescape popular characters
if (index($str, '\\u') < 0) {
$str =~ s|\\(["\\/bfnrt])|$ESCAPE{$1}|gs;
$str =~ s!\\(["\\/bfnrt])!$ESCAPE{$1}!gs;
return $str;
}

Expand Down Expand Up @@ -262,7 +262,7 @@ sub _encode_string {
my $string = shift;

# Escape string
$string =~ s|([\x00-\x1F\x7F\x{2028}\x{2029}\\"/\b\f\n\r\t])|$REVERSE{$1}|gs;
$string =~ s!([\x00-\x1F\x7F\x{2028}\x{2029}\\"/\b\f\n\r\t])!$REVERSE{$1}!gs;

# Stringify
return "\"$string\"";
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -13,7 +13,7 @@ sub _pointer {
my ($self, $contains, $data, $pointer) = @_;

# Parse pointer and walk data structure
return unless $pointer =~ s|^/||;
return unless $pointer =~ s!^/!!;
for my $p (split '/', $pointer) {
$p = decode('UTF-8', url_unescape $p);

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -70,12 +70,12 @@ sub body_params {

# "x-application-urlencoded" and "application/x-www-form-urlencoded"
my $type = $self->headers->content_type || '';
if ($type =~ m#(?:x-application|application/x-www-form)-urlencoded#i) {
if ($type =~ m!(?:x-application|application/x-www-form)-urlencoded!i) {
$p->parse($self->content->asset->slurp);
}

# "multipart/formdata"
elsif ($type =~ m#multipart/form-data#i) {
elsif ($type =~ m!multipart/form-data!i) {
my $formdata = $self->_parse_formdata;

# Formdata
Expand Down
14 changes: 7 additions & 7 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -172,7 +172,7 @@ sub _build_start_line {
my $path = $url->path->to_string;
my $query = $url->query->to_string;
$path .= "?$query" if $query;
$path = "/$path" unless $path =~ m#^/#;
$path = "/$path" unless $path =~ m!^/!;

# CONNECT
my $method = uc $self->method;
Expand Down Expand Up @@ -245,7 +245,7 @@ sub _parse_env {
$self->method($env->{REQUEST_METHOD}) if $env->{REQUEST_METHOD};

# Scheme/Version
if (($env->{SERVER_PROTOCOL} || '') =~ m#^([^/]+)/([^/]+)$#) {
if (($env->{SERVER_PROTOCOL} || '') =~ m!^([^/]+)/([^/]+)$!) {
$base->scheme($1);
$self->version($2);
}
Expand All @@ -262,14 +262,14 @@ sub _parse_env {
if (my $value = $env->{SCRIPT_NAME}) {

# Make sure there is a trailing slash (important for merging)
$base->path->parse($value =~ m#/$# ? $value : "$value/");
$base->path->parse($value =~ m!/$! ? $value : "$value/");

# Remove SCRIPT_NAME prefix if necessary
my $buffer = $path->to_string;
$value =~ s|^/||;
$value =~ s|/$||;
$buffer =~ s|^/?$value/?||;
$buffer =~ s|^/||;
$value =~ s!^/!!;
$value =~ s!/$!!;
$buffer =~ s!^/?$value/?!!;
$buffer =~ s!^/!!;
$path->parse($buffer);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -127,7 +127,7 @@ sub _parse_start_line {
my $self = shift;

# Try to detect HTTP 0.9
if ($self->{buffer} =~ /^\s*(\S.{4})/ && $1 !~ m#^HTTP/#) {
if ($self->{buffer} =~ /^\s*(\S.{4})/ && $1 !~ m!^HTTP/!) {
$self->version('0.9');
$self->content->relaxed(1);
return $self->{state} = 'content';
Expand All @@ -136,7 +136,7 @@ sub _parse_start_line {
# We have a full HTTP 1.0+ response line
return unless defined(my $line = get_line \$self->{buffer});
return $self->error('Bad response start line.')
unless $line =~ m|^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$|;
unless $line =~ m!^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$!;
$self->version($1)->code($2)->message($3);
$self->content->auto_relax(1);
$self->{state} = 'content';
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Path.pm
Expand Up @@ -65,7 +65,7 @@ sub merge {
my ($self, $path) = @_;

# Replace
return $self->parse($path) if $path =~ m|^/|;
return $self->parse($path) if $path =~ m!^/!;

# Merge
pop @{$self->parts} unless $self->trailing_slash;
Expand All @@ -79,8 +79,8 @@ sub parse {

$path = url_unescape $path // '';
utf8::decode $path;
$path =~ s|^/|| ? $self->leading_slash(1) : $self->leading_slash(undef);
$path =~ s|/$|| ? $self->trailing_slash(1) : $self->trailing_slash(undef);
$path =~ s!^/!! ? $self->leading_slash(1) : $self->leading_slash(undef);
$path =~ s!/$!! ? $self->trailing_slash(1) : $self->trailing_slash(undef);

return $self->parts([split '/', $path, -1]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Daemon.pm
Expand Up @@ -205,7 +205,7 @@ sub _listen {
# Friendly message
return if $self->silent;
$self->app->log->info(qq{Listening at "$listen".});
$listen =~ s|//\*|//127.0.0.1|i;
$listen =~ s!//\*!//127.0.0.1!i;
say "Server available at $listen.";
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/URL.pm
Expand Up @@ -95,7 +95,7 @@ sub parse {
return $self unless $url;

# Official regex
$url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
$url =~ m!(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?!;
$self->scheme($1);
$self->authority($2);
$self->path->parse($3);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -161,7 +161,7 @@ sub tx {
my $req = $tx->req;
$req->method(shift);
my $url = shift;
$url = "http://$url" unless $url =~ m#^/|\://#;
$url = "http://$url" unless $url =~ m!^/|\://!;
ref $url ? $req->url($url) : $req->url->parse($url);

# Body
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -74,7 +74,7 @@ sub run {
$ua->max_redirects(10) if $redirect;

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

# Application
else { $ua->app($ENV{MOJO_APP} || 'Mojo::HelloWorld') }
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -468,7 +468,7 @@ sub url_for {

# Absolute URL
return $target if (Scalar::Util::blessed($target) || '') eq 'Mojo::URL';
return Mojo::URL->new($target) if $target =~ m#^\w+\://#;
return Mojo::URL->new($target) if $target =~ m!^\w+\://!;

# Base
my $url = Mojo::URL->new;
Expand All @@ -477,11 +477,11 @@ sub url_for {

# Relative URL
my $path = $url->path;
if ($target =~ m#^/#) {
if ($target =~ m!^/!) {
if (my $prefix = $self->stash->{path}) {
my $real = Mojo::Util::url_unescape($req->url->path->to_abs_string);
$real = Mojo::Util::decode('UTF-8', $real) // $real;
$real =~ s|/?$prefix$|$target|;
$real =~ s!/?$prefix$!$target!;
$target = $real;
}
$url->parse($target);
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -215,6 +215,7 @@ where you can't just change the server configuration or behind a reverse proxy
that passes along additional information with C<X-*> headers. In such cases
you can use a C<before_dispatch> hook to rewrite incoming requests.

# Change scheme if "X-Forwarded-Protocol" header is set to "https"
app->hook(before_dispatch => sub {
my $self = shift;
$self->req->url->base->scheme('https')
Expand All @@ -225,6 +226,7 @@ Since reverse proxies generally don't pass along information about path
prefixes your application might be deployed under, rewriting the base path of
incoming requests is also quite common.

# Move first part from path to base path in production mode
app->hook(before_dispatch => sub {
my $self = shift;
push @{$self->req->url->base->path->parts},
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -34,7 +34,7 @@ While it is very well possible to make all these connections static, it is
also rather inefficient. That's why regular expressions are commonly used to
make the dispatch process more dynamic.

qr|/user/show/(\d+)| -> $self->render(text => $users{$1});
qr!/user/show/(\d+)! -> $self->render(text => $users{$1});

Modern dispatchers have pretty much everything HTTP has to offer at their
disposal and can use many more variables than just the request path, such as
Expand All @@ -50,7 +50,7 @@ request method and headers like C<Host>, C<User-Agent> and C<Accept>.
While regular expressions are quite powerful they also tend to be unpleasant
to look at and are generally overkill for ordinary path matching.

qr|/user/show/(\d+)| -> $self->render(text => $users{$1});
qr!/user/show/(\d+)! -> $self->render(text => $users{$1});

This is where routes come into play, they have been designed from the ground
up to represent paths with placeholders.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/Mount.pm
Expand Up @@ -12,7 +12,7 @@ sub register {

# Extract host
my $host;
if ($path =~ m#^(\*\.)?([^/]+)(/.*)?$#) {
if ($path =~ m!^(\*\.)?([^/]+)(/.*)?$!) {
$host = $1 ? qr/^(?:.*\.)?\Q$2\E$/i : qr/^\Q$2\E$/i;
$path = $3;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -42,7 +42,7 @@ sub register {

# Find module
my $module = $self->param('module');
$module =~ s|/|\:\:|g;
$module =~ s!/!\:\:!g;
my $path = Pod::Simple::Search->new->find($module, @PATHS);

# Redirect to CPAN
Expand All @@ -59,9 +59,9 @@ sub register {
$dom->find('a[href]')->each(
sub {
my $attrs = shift->attrs;
$attrs->{href} =~ s|%3A%3A|/|gi
$attrs->{href} =~ s!%3A%3A!/!gi
if $attrs->{href}
=~ s|^http\://search\.cpan\.org/perldoc\?|$perldoc|;
=~ s!^http\://search\.cpan\.org/perldoc\?!$perldoc!;
}
);

Expand Down Expand Up @@ -129,8 +129,8 @@ sub _pod_to_html {
return $@ unless eval { $parser->parse_string_document("$pod"); 1 };

# Filter
$output =~ s|<a name='___top' class='dummyTopAnchor'\s*?></a>\n||g;
$output =~ s|<a class='u'.*?name=".*?"\s*>(.*?)</a>|$1|sg;
$output =~ s!<a name='___top' class='dummyTopAnchor'\s*?></a>\n!!g;
$output =~ s!<a class='u'.*?name=".*?"\s*>(.*?)</a>!$1!sg;

return $output;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -52,7 +52,7 @@ sub dispatch {
# Prepare path
my $req = $c->req;
my $path = $c->stash->{path};
if (defined $path) { $path = "/$path" if $path !~ m#^/# }
if (defined $path) { $path = "/$path" if $path !~ m!^/! }
else { $path = $req->url->path->to_abs_string }

# Prepare match
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -24,7 +24,7 @@ sub parse {

# Make sure we have a viable pattern
my $pattern = @_ % 2 ? (shift || '/') : '/';
$pattern = "/$pattern" unless $pattern =~ m#^/#;
$pattern = "/$pattern" unless $pattern =~ m!^/!;

# Requirements
$self->reqs({@_});
Expand Down Expand Up @@ -95,7 +95,7 @@ sub shape_match {
# Format
my $req = $self->reqs->{format};
return $result if !$detect || defined $req && !$req;
if ($$pathref =~ s|^/?$format||) { $result->{format} = $1 }
if ($$pathref =~ s!^/?$format!!) { $result->{format} = $1 }
elsif ($req) { return unless $result->{format} }

return $result;
Expand Down Expand Up @@ -168,13 +168,13 @@ sub _compile_format {

# Default regex
my $reqs = $self->reqs;
return $self->format(qr#\.([^/]+)$#)->format
return $self->format(qr!\.([^/]+)$!)->format
if !exists $reqs->{format} && $reqs->{format};

# Compile custom regex
my $regex
= defined $reqs->{format} ? _compile_req($reqs->{format}) : '([^/]+)';
return $self->format(qr#\.$regex$#)->format;
return $self->format(qr!\.$regex$!)->format;
}

# "Interesting... Oh no wait, the other thing, tedious."
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/app.t
Expand Up @@ -114,7 +114,7 @@ $id = Mojo::IOLoop->client(
Mojo::IOLoop->remove($id) and Mojo::IOLoop->stop
if $buffer =~ s/ is working!$//;
$stream->write('4321')
if $buffer =~ m#HTTP/1.1 100 Continue.*\x0d\x0a\x0d\x0a#gs;
if $buffer =~ m!HTTP/1.1 100 Continue.*\x0d\x0a\x0d\x0a!gs;
}
);
$stream->write("GET /1/ HTTP/1.1\x0d\x0a"
Expand All @@ -123,7 +123,7 @@ $id = Mojo::IOLoop->client(
}
);
Mojo::IOLoop->start;
like $buffer, qr#HTTP/1.1 100 Continue.*Mojo$#s, 'request was continued';
like $buffer, qr!HTTP/1.1 100 Continue.*Mojo$!s, 'request was continued';

# Pipelined
$buffer = '';
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/request.t
Expand Up @@ -1839,7 +1839,7 @@ is $req->url, '/', 'right URL';
is $req->cookie('mojolicious')->value,
'BAcIMTIzNDU2NzgECAgIAwIAAAAXDGFsZXgudm9yb25vdgQAAAB1c2VyBp6FjksAAAAABwA'
. 'AAGV4cGlyZXM=--1641adddfe885276cda0deb7475f153a', 'right value';
like $req->headers->content_type, qr#multipart/form-data#,
like $req->headers->content_type, qr!multipart/form-data!,
'right "Content-Type" value';
is $req->param('fname'), 'Иван', 'right value';
is $req->param('sname'), 'Иванов', 'right value';
Expand Down Expand Up @@ -1911,7 +1911,7 @@ is $req->url, '/', 'right URL';
is $req->cookie('mojolicious')->value,
'BAcIMTIzNDU2NzgECAgIAwIAAAAXDGFsZXgudm9yb25vdgQAAAB1c2VyBiWFjksAAAAABwA'
. 'AAGV4cGlyZXM=--cd933a37999e0fa8d7804205e89193a7', 'right value';
like $req->headers->content_type, qr#multipart/form-data#,
like $req->headers->content_type, qr!multipart/form-data!,
'right "Content-Type" value';
is $req->param('fname'), 'Иван', 'right value';
is $req->param('sname'), 'Иванов', 'right value';
Expand Down Expand Up @@ -1978,7 +1978,7 @@ is $req->url, '/', 'right URL';
is $req->cookie('mojolicious')->value,
'BAcIMTIzNDU2NzgECAgIAwIAAAAXDGFsZXgudm9yb25vdgQAAAB1c2VyBhaIjksAAAAABwA'
. 'AAGV4cGlyZXM=--78a58a94f98ae5b75a489be1189f2672', 'right value';
like $req->headers->content_type, qr#multipart/form-data#,
like $req->headers->content_type, qr!multipart/form-data!,
'right "Content-Type" value';
is $req->param('fname'), 'Иван', 'right value';
is $req->param('sname'), 'Иванов', 'right value';
Expand Down

0 comments on commit 4d52d28

Please sign in to comment.