diff --git a/lib/Mojolicious/Static.pm b/lib/Mojolicious/Static.pm index 9cfea9c1a2..3a5149a49f 100644 --- a/lib/Mojolicious/Static.pm +++ b/lib/Mojolicious/Static.pm @@ -20,6 +20,9 @@ has 'prefix'; sub asset_path { my ($self, $asset) = @_; + + return $asset if $self->_is_abs($asset); + $asset = "/$asset" unless $asset =~ /^\//; my $assets = $self->{assets} //= {}; return $self->file_path('/' . $self->asset_dir . ($assets->{$asset} // $asset)); @@ -64,7 +67,9 @@ sub file { } # Search DATA - if (my $asset = $self->_get_data_file($rel)) { return $asset } + if (my $asset = $self->_get_data_file($rel)) { + return $asset; + } # Search extra files my $extra = $self->extra; @@ -73,6 +78,9 @@ sub file { sub file_path { my ($self, $file) = @_; + + return $file if $self->_is_abs($file); + $file = "/$file" unless $file =~ /^\//; return $file unless my $prefix = $self->prefix; return "$prefix$file"; @@ -146,7 +154,9 @@ sub warmup { # DATA sections my $index = $self->{index} = {}; - for my $class (reverse @{$self->classes}) { $index->{$_} = $class for keys %{data_section $class} } + for my $class (reverse @{$self->classes}) { + $index->{$_} = $class for keys %{data_section $class}; + } # Static assets my $assets = $self->{assets} = {}; @@ -190,6 +200,11 @@ sub _get_file { return -f $path && -r _ ? Mojo::Asset::File->new(path => $path) : undef; } +sub _is_abs { + my ($self, $file) = @_; + return $file =~ m!^(?:[^:/?#]+:|//|#)!; +} + 1; =encoding utf8 diff --git a/t/mojolicious/static_prefix_lite_app.t b/t/mojolicious/static_prefix_lite_app.t index 6e47ea0821..611133c18a 100644 --- a/t/mojolicious/static_prefix_lite_app.t +++ b/t/mojolicious/static_prefix_lite_app.t @@ -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();