Check HTTP Byte Range Request Header With NodeJS And ExpressJS

I run – a podcast audio hosting service. Recently, I detailed an issue and solution for’s episode and podcast tracking reports. The gist of the problem is that mobile devices will make use of byte-range request headers to download files in very small chunks. This is useful for unreliable networks, as a device can request a small amount of data, record where it left off and pick up from there when the network comes back online. Unfortunately, it also caused a problem for me. In the worst case, I had more than 63,000 download tracking entries created by a mobile device that was making one request per second over an 11 hour period.

What should have been a single entry in the tracking for a podcast that averages 1,200 listens per episode showed up as 63,000 listens for that one episode!

Correcting The Problem

The solution to the problem is to only tracking 1 download entry no matter how many requests are made for the episode, by a given device. The easy way to make this happen is to just look at the byte range request header in the HTTP request for the media file. 

A byte range request generally looks like this:

There are variations for this that tell you if it’s requesting something other than bytes, or it may request a start with no end in the range, meaning give me everything after this. But either way, the code is the same and is fairly simple. Grab the “range” HTTP header from the request. If that header is not there (no “range” requested), or if the range is there and it starts with a 0 (zero) for the range, then track the download. Otherwise – if the range has something other than a 0 (zero) start – don’t track the download.

The code is then called, passing in the “range” http header:

More Details

If you’d like to know more about the problem that i ran into, see the SignalLeaf blog post on correcting download tracking for mobile devices. If you want to know more about byte range requests… well, it seems to be somewhat difficult to find good info for this, other than the HTTP spec. I managed to piece together the info I needed from various blog posts, stack overflow questions and use of curl -r for range requests, to test it out.

Get The Best JavaScript Secrets!

Get the best kept secrets of JavaScript, and the most important career advice you'll ever hear!
Don't miss out: join my list to get all the inside info!

  • Robbie Pallas

    Thanks for blogging about this issue :)nnOne question…nn”Just grab the u201crangeu201d HTTP header and check to see if itu2019s there or if it starts with a 0 (zero) for the range. If either of those are true, then track the download.”nnShould this actually say “if it’s NOT there”? nni.e. n if (!rangeRequest){ return true; }nnnnMaybe i’m wrong… its happened many times before :)

    • Derick Bailey

      hmm… i think you’re right.nnnshould be “if the range header is NOT there, OR the range header starts with zero”. i’ll update the wording to make that more clear. thanks, yo!

      • Robbie Pallas

        No worries, it’s nice not to be wrong for a change :) Oh, and thanks for the super speedy reply!