diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm index 473e362e18..71488e5340 100644 --- a/lib/Mojolicious/Controller.pm +++ b/lib/Mojolicious/Controller.pm @@ -11,6 +11,8 @@ 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) }; @@ -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; @@ -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 { diff --git a/t/mojolicious/static_prefix_lite_app.t b/t/mojolicious/static_prefix_lite_app.t index 6e47ea0821..47474b19ca 100644 --- a/t/mojolicious/static_prefix_lite_app.t +++ b/t/mojolicious/static_prefix_lite_app.t @@ -75,8 +75,10 @@ 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?'; + is $c->url_for_asset('https://somesite.com/file.css'), 'https://somesite.com/file.css', 'right abosolute asset path?'; }; done_testing();