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

Re: How to use Plack::Middleware::Static

by Anonymous Monk
on Mar 03, 2021 at 10:36 UTC ( [id://11129073]=note: print w/replies, xml ) Need Help??


in reply to How to use Plack::Middleware::Static

content_type => sub { ... },

https://metacpan.org/source/MIYAGAWA/Plack-1.0048/t%2FPlack-Middleware%2Fstatic.t

  • Comment on Re: How to use Plack::Middleware::Static

Replies are listed 'Best First'.
Re^2: How to use Plack::Middleware::Static
by vhein79 (Novice) on Mar 03, 2021 at 11:05 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'
        Thank you very much! That does what I want and is clear to me:
        use Plack::Builder; use Plack::MIME; my $expr = qr{\.c|\.cpp|\.csv|\.dat|\.dpl|\.gp|\.h|\.hpp|\.html| \.ini|\.java|\.js|makefile|\.m|\.mac| \.pl|\.pm|\.pod|\.py|readme|\.sch|\.sh|\.txt}xi; builder { # normal behaviour outside of downloads enable 'Plack::Middleware::Static', path => qr{^/html/(?!downloads/)}, root => '.'; # plain text for non binary download files enable 'Plack::Middleware::Static', path => qr{^/html/downloads/.*($expr)$}, root => '.', content_type => 'text/plain'; # download only for the other download files enable 'Plack::Middleware::Static', path => qr{^/html/downloads/.*(?!$expr)$}, root => '.', content_type => 'mime/type'; mount "/" => $app1; mount "/imggenerator.pl" => $app2; };

      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.

        I believe that was only the pestilence known as Internet Explorer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found