Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add absolute check to url_for_asset and url_for_file to fix appending "/" to absolute file paths #2112

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
8 changes: 5 additions & 3 deletions lib/Mojolicious/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use Mojo::Util;
use Mojolicious::Routes::Match;
use Scalar::Util ();

my $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 +241,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 +276,12 @@ sub url_for {

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

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

sub write {
Expand Down
4 changes: 4 additions & 0 deletions t/mojolicious/static_prefix_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ 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('https://somesite.com/file.css')->to_string, 'https://somesite.com/file.css',
'right absolute file path?';
is $c->url_for_asset('https://somesite.com/file.css')->to_string, 'https://somesite.com/file.css',
'right absolute asset path?';
};

done_testing();
Expand Down
Loading