Tomcat 7 Default Servlet and History.pushState()

It is very common these days to have a website load all of its content via AJAX and use the HTML5 History.pushState() method to control the URL and the browser back button. This is done manually or through some kind of JavaScript MVC framework (AngularJS with $locationProvider.html5Mode(true), or Backbone.Router with {pushState: true}). In order for these techniques to function properly after the page is refreshed, the server must redirect requests for non-existent files to index.html silently (return the content of index.html immediately without a redirect header).

This can be achieved easily enough in Apache2 with mod_rewrite, but what about in Apache Tomcat 7? That turned out to be incredibly difficult to do without writing a special Java servlet dedicated to static files, but many StackOverflow searches later I have come up with a recipe.

In web.xml:


Then create a new file in WebContent called rewrite-404.jsp:

<% response.setStatus(HttpServletResponse.SC_OK); %>
<%@ include file="index.html" %>

This will cause existing files in WebContent to be served as usual, and all requests that would normally return a 404 error to return index.html instead with a 200 (OK) response code.

Downloading Directly From Torrentz

2017 Update: now works with Torrentz2

The popular torrent search engine, does not provide direct links to download torrents. Normally, one would have to pick one of the available torrent websites at the top of the page in order to get a .torrent file or magnet link. However, I recently noticed that all the information needed to generate a .torrent file or magnet link is actually available on the page itself, namely the info hash and a list of trackers. As such I wrote some JavaScript to generate a magnet link.

var infoHash = document.querySelector("div.trackers h2").childNodes[1].textContent.substring(6),
displayName = document.querySelector("div.downlinks h2 span").textContent,
trackers = document.querySelectorAll("div.trackers dl dt"),
a = document.createElement("a"),
magnet = "magnet:?xt=urn:btih:" + infoHash + "&dn=" + displayName;
for (var i = 0; i < trackers.length; i++) {
    magnet += "&tr=" + trackers[i].textContent;
window.location = magnet;

The last line is optional and performs the equivalent of clicking on the link. To convert the torrent hash above the trackers list into a clickable link instead of automatically opening the magnet handler, replace window.location = magnet with:

var hashNode = document.querySelector("div.trackers h2").childNodes[1],
hashLink = document.createElement('a');
hashLink.textContent = hashNode.textContent;
hashLink.setAttribute('href', magnet);
hashNode.parentNode.replaceChild(hashLink, hashNode);

For the sake of convenience, I have also made a bookmarklet that will automatically start a torrent download when clicked (assuming that you are currently on a torrent page). Drag this button onto your bookmarks toolbar and you will be good to go:

Torrentz Download

And here is one that “linkifies” the torrent hash instead of automatically downloading:

Torrentz Linkify