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).
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.
:-)
@Burak: I half agree there. Indeed the problem is somewhere else. That is also partly why the framework shouldn't fix it. However, I think that sometimes we need to monkeypatch issues that rise which we can't control in core.
This specific issue was raised by someone who used a proxy he couldn't control. You can either provide a way for him to fix it, or tell him he should have the proxy fixed. Which one is more likely to happen? :)
@Aristotle: nailed it! :)
In Dancer, you can edit the builder in the dispatch script, but I'm not sure every framework allows you to do that.
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.
Quite the contrary, Aristotle: I think your solution is better than mine. :)
I should have looked for it, but I didn't.
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.