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

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.

Replies are listed 'Best First'.
Re^3: How to use Plack::Middleware::Static
by haj (Vicar) on Mar 04, 2021 at 07:43 UTC
    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; };
Re^3: How to use Plack::Middleware::Static
by jcb (Parson) on Mar 04, 2021 at 03:21 UTC

    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.

        The web developers best friend, so helpful, not at all a pain to work with....