… only this time it's Owen's fault.
LiveJournal has the terribly annoying bug that not everybody is forced to use my sensible journal style, and instead the system allows everyone to use the illegible and eye-torturing monstrosity of their choice. Fortunately, I can render any individual LJ page sane-looking simply by adding ?style=mine
(or &style=mine
) at the appropriate point in its URL. Unfortunately, doing this by hand every time I find a page too hard to read is a total pain.
So I've been searching, for some time, for a means to make my web browser automatically rewrite some kinds of LiveJournal URL to add this vital ‘make it legible’ clause. I've looked into writing a Mozilla/Firefox plugin, but couldn't find enough documentation; I've even grubbed about in Firefox's own JavaScript internals, in the hope of finding just the right file to tweak. No luck there either.
On Monday, Owen suggested a much higher-level solution: create a custom web proxy which does the rewrite and returns a 302 Moved Temporarily response containing the altered URL. Then set up a proxy_config.pac
file which directs URLs to that proxy if and only if they need a rewrite, and otherwise fetches them in the normal way.
Three days later, I have just such a thing running on my Linux boxes both at work and at home (I haven't ported it to my home Windows box yet, but might give it a go at some point), and so far I haven't been able to detect it not working perfectly. Woo!
The fun bit, though, was arranging synchronisation of configuration. The .pac
file needs to know precisely what my URL rewriting requirements are, so that it can reliably direct things to the rewriting proxy if and only if they need a rewrite. (I can't have the proxy receive URLs that don't need rewriting, since then it would have to retrieve them itself, and that would mean implementing a real HTTP client in it.) And of course the proxy needs to know my URL rewriting requirements too, since it has to implement them. But maintaining two sets of configuration in parallel is just asking for an editing error to cause them to get out of sync.
My solution – and here's where I suppose the blame has to stop being Owen's – was to embed a JavaScript interpreter library in my custom web proxy. (Debian helpfully provides such a thing in the libjs0
package.) So my .pac
file now doubles as the web proxy's URL rewriting script: it declares a function called RewriteURL()
, and then implements FindProxyForURL()
in terms of that:
if (RewriteURL(url) != url)
return "PROXY localhost:XXXXX";
So Mozilla loads this file as its proxy configuration, calls FindProxyForURL()
, and everything works there. My custom proxy (which I'm currently calling ick-proxy
, for obvious reasons!) loads the same JavaScript file, and calls RewriteURL()
every time it gets an HTTP request. And if I want to change my URL rewriting configuration, I edit it in just one place and both halves of the mechanism adapt.
I'm rather pleased with that, in a disgusting sort of way. And it's nice to be able to blame someone else for the basic idea for once :-)