Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to use Plack::Middleware::Static

by vhein79 (Novice)
on Mar 03, 2021 at 10:11 UTC ( [id://11129070]=perlquestion: print w/replies, xml ) Need Help??

vhein79 has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I run a website, changing now from FCGI to PSGI using Plack as server. The behaviour should be the same as with the old Apache website, (which I configured with .htaccess files for each directory) so I want to use js when it is needed, and show it as text/plain when someone clicks such a non binary file in my download directory.

I don't want to use dancer etc now, because I had to make big changes to the website then (FCGI and PSGI are quite similar to use). I tested a bit and the behaviour seems to be version dependent (on Windows it works, on CentOS not), but I want a safe way using different Plack::MIME instances for different directories.

What I have now uses the same Plack::MIME instance and doesn't work correctly when I include .html, .css etc there:

use Plack::Builder; use Plack::MIME; # normal behaviour /html/ # special behaviour /html/downloads/ for my $expr ( ".c", ".cpp",".csv", ".dat", ".dpl" , ".gp", ".h", ".hpp", ".ini" , ".java", "makefile", ".m", ".mac" , ".pl", ".pm", ".pod", ".py" , "README", ".sch", ".sh", ".txt") { Plack::MIME->add_type($expr => 'text/plain'); } builder { enable 'Plack::Middleware::Static', path => qr{/html/}, root => '.'; # normal behaviour: # needs a new content_type instance! how doing this? # enable 'Plack::Middleware::Static', # path => qr{^/html/(?!downloads).*\. # (css|gif|jpg|js|pdf|png)$}, root => '.'; mount "/" => $app1; mount "/imggenerator.pl" => $app2; };
I could treat every single file in my $app1, but I think configuring that with Plack should be possible.

Replies are listed 'Best First'.
Re: How to use Plack::Middleware::Static
by Anonymous Monk on Mar 03, 2021 at 10:36 UTC

      Thank you for the really fast answer!

      This works:

      use Plack::Builder; use Plack::MIME; for my $expr ( ".c", ".cpp", ".css", ".csv", ".dat", ".dpl" , ".gp", ".h", ".hpp", ".html", ".ini" , ".java", ".js", "makefile", "Makefile", ".m", ".mac" , ".pl", ".pm", ".pod", ".py", "README", ".sch", ".sh", ".txt") { Plack::MIME->add_type($expr => 'text/plain'); } builder { enable 'Plack::Middleware::Static', path => qr{^/html/downloads/}, root => '.'; enable 'Plack::Middleware::Static', path => qr{^/html/(?!downloads).*\.(css|gif|jpg|js|pdf|png)$}, root => '.', content_type => sub { Plack::MIME->mime_type($_[0]); }; mount "/" => $app1; mount "/imggenerator.pl" => $app2; };
      But I'm far from really understanding it and there still is one issue: .html in downloads are still treated as html files, not as plain text.

        Per default, Plack::Middleware::Static will serve your files depending on their extension by using Plack::MIME. So I suggest that you just drop your own invocation of Plack::MIME for the "normal" behaviour and override it for your download directory like this: content_type => sub { 'text/plain' }

        Edited to add: After a glance at the code, you can also provide the content type directly:

        content_type => 'text/plain'

        Have you confirmed that the server is actually sending Content-Type: text/plain? I know at least some browsers once would interpret text/plain as text/html if the contents "look like" HTML and I do not know if modern browsers still do that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11129070]
Approved by hippo
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found