Skip to content

Commit

Permalink
Add absolute check to file_path and asset_path
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler committed Sep 20, 2023
1 parent 2c8d1b6 commit e26ce8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/Mojolicious/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Mojo::URL;
use Mojo::Util;
use Mojolicious::Routes::Match;
use Scalar::Util ();
use constant ABSOLUTE => qr!^(?:[^:/?#]+:|//|#)!;

has [qw(app tx)] => undef, weak => 1;
has match => sub { Mojolicious::Routes::Match->new(root => shift->app->routes) };
Expand Down Expand Up @@ -239,7 +240,7 @@ sub url_for {

# Absolute URL
return $target if Scalar::Util::blessed $target && $target->isa('Mojo::URL');
return Mojo::URL->new($target) if $target =~ m!^(?:[^:/?#]+:|//|#)!;
return Mojo::URL->new($target) if $target =~ ABSOLUTE;

# Base
my $url = Mojo::URL->new;
Expand Down Expand Up @@ -274,12 +275,12 @@ sub url_for {

sub url_for_asset {
my ($self, $asset) = @_;
return $self->url_for($self->app->static->asset_path($asset));
return $asset =~ ABSOLUTE ? $asset : $self->url_for($self->app->static->asset_path($asset));
}

sub url_for_file {
my ($self, $file) = @_;
return $self->url_for($self->app->static->file_path($file));
return $file =~ ABSOLUTE ? $file : $self->url_for($self->app->static->file_path($file));
}

sub write {
Expand Down
5 changes: 3 additions & 2 deletions t/mojolicious/static_prefix_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ subtest 'UTF-8 encoded inline file' => sub {

subtest 'File' => sub {
my $c = $t->app->build_controller;
is $c->url_for_file('/unknown.css')->path, '/static/unknown.css', 'right file path';
is $c->url_for_file('/foo/bar.css')->path, '/static/foo/bar.css', 'right file path';
is $c->url_for_file('/unknown.css')->path, '/static/unknown.css', 'right file path';
is $c->url_for_file('/foo/bar.css')->path, '/static/foo/bar.css', 'right file path';
is $c->url_for_file('https://somesite.com/file.css'), 'https://somesite.com/file.css', 'right absolute file path?';
};

done_testing();
Expand Down

0 comments on commit e26ce8e

Please sign in to comment.