khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 31 forks source link

[#14] Avoid "dot in path" hickup #108

Closed hidden-primary-net closed 3 years ago

hidden-primary-net commented 3 years ago

Do not interpret "." in path names as a file extension.

khrt commented 3 years ago

Thanks for your merge request @hidden-primary-net, but I have to reject it.

This would be a patch which, in my opinion, does the job slightly better:

index f9b8bef..08d5da0 100644
--- a/lib/Raisin/Middleware/Formatter.pm
+++ b/lib/Raisin/Middleware/Formatter.pm
@@ -9,6 +9,7 @@ package Raisin::Middleware::Formatter;

 use parent 'Plack::Middleware';

+use File::Basename qw(fileparse);
 use HTTP::Status qw(:constants);
 use Plack::Request;
 use Plack::Response;
@@ -71,8 +72,8 @@ sub call {
 sub _accept_header_set { length(shift || '') }
 sub _path_has_extension {
     my $path = shift;
-    my @chunks = split /\./, $path;
-    scalar(@chunks) > 1;
+    my (undef, undef, $suffix) = fileparse($path, qr"\..[^.]*$");
+    $suffix;
 }

 sub negotiate_format {
diff --git a/t/unit/middleware/formatter.t b/t/unit/middleware/formatter.t
index c112062..51b7a8b 100644
--- a/t/unit/middleware/formatter.t
+++ b/t/unit/middleware/formatter.t
@@ -291,4 +291,33 @@ subtest 'format_from_header' => sub {
     }
 };

+subtest '_path_has_extension' => sub {
+    my @CASES = (
+        {
+            path => '/a/b/c.exe',
+            expected => '.exe',
+            message => 'extension',
+        },
+        {
+            path => '/a/b.at/c',
+            expected => '',
+            message => 'no extension',
+        },
+        {
+            path => '/',
+            expected => '',
+            message => 'slash',
+        },
+        {
+            path => '',
+            expected => '',
+            message => 'empty',
+        },
+    );
+
+    for my $c (@CASES) {
+        is Raisin::Middleware::Formatter::_path_has_extension($c->{path}), $c->{expected}, $c->{message};
+    }
+};
+
 done_testing;

I'll try to make a fix and a new release somewhere today, that might include a fix for https://github.com/khrt/Raisin/issues/14 as well.

Thanks for your contribution either way!

hidden-primary-net commented 3 years ago

Thank you very much :+1:

khrt commented 3 years ago

Related https://github.com/khrt/Raisin/pull/109

khrt commented 3 years ago

I've released version 0.91 which includes the fix for your particular problem. Must be available on CPAN shortly.