Plack::Middleware::NoMultipleSlashes

Ignited by comments on IRC and in the Israeli Perl Mongers mailing list, I've sat (for 10 minutes), wrote and published a new Plack middleware: Plack::Middleware::NoMultipleSlashes. Let me explain what it is.

Apparently you cannot count (by RFC, even) that the paths http://mysite/ and http://mysite// will lead to the same place. This is very tricky, because it means that the framework you're using (Dancer, right? :) shouldn't clean those multiple slashes for you.

So, I figured "why not let Plack take care of it?". I wrote Plack::Middleware::NoMultipleSlashes (a single line of code, really), that cleans those multiple slashes for you, wherever they are in the path.

To use it with Dancer, just add the following to your config.yml file:

plack_middlewares:
- [ NoMultipleSlashes ]

Done!

Thank you, Plack! :)

BTW, if you still haven't gotten around to reading Franck Cuny's latest post, it's right here (PSGIChrome).

6 Comments

Well... If you end up with multiple slashes, the problem is in somewhere else. This is no better than blindly silencing the warnings in Perl code instead of actually fixing the cause.

builder {
    enable 'Rewrite', rules => sub { s!/+!/!g };
    $app;
}

:-)

I wouldn’t care to use any framework which doesn’t! It would have to go far out of its way to make itself less useful like that. And the Builder DSL is very concise and readable anyway.

Before I wrote Rewrite, I never realised how many tiny middlewares it would obviate. It killed off two of mine (that I hadn’t released) and I’ve seen several other examples, like yours. And though it’s not much ceremony to write a middleware, having a way that takes care of many small desires with no more setup than a one-liner is still nice.

Oh I don’t fault you! Plack makes it easy to think “I’ll just write a little middleware for that” anyway, so it’s understandable that one wouldn’t think of looking for something like Rewrite for this sort of thing. And even if you did – I only released Rewrite a few days ago anyway. Though already I keep getting surprised at how nicely the API works. The basic idea was obvious immediately, but I had to try a number of different things for return values and wasn’t entirely sure that the design makes good sense. In practice it worked out handsomely. And since this is “just Perl”, you can easily do some of the simple things that are complicated to achieve with mod_rewrite. I’m very happy with it.

So I feel it deserves to be widely known, and I plan to write a post or a couple about it to help with that, and generally promote it as opportunity arises. (These comments are part of that.) It’s a pity I didn’t write it about 7 weeks ago: I could have pestered Miyagawa to include it in the Plack advent calendar. But maybe next Christmas. By then I could also have a parser for Apache mod_rewrite rules that spits out a Plack::Middleware::Rewrite rules coderef, plus integrate with Plack::App::Proxy to emulate the proxy support of mod_rewrite. That would be full of win.

Leave a comment

About Sawyer X

user-pic Gots to do the bloggingz