Saturday, December 19, 2015

The HTTP dance required make Chrome play video tags

Sometimes in an app you need to use an embedded HTTP server to make HTML content work properly.  In theory you could serve up content from the FileSystem directly: but then HTML5 localstorage, media, cookies, requests for resources from other directories misbehave.  What does cross-domain mean on a filesystem?

In our app we run the excellent NanoHTTPD to serve up content from epub (zipped) files.  Chrome has some (seemingly undocumented) requirements:


  1. You must support HTTP range requests.  You don't need to support multiple ranges: but you must implement returning of 206 partial content in response to a range request.  Interestingly on Android 5.1 Chrome seems to test this by sending a range request for the first byte (0-1).
  2. You must give chrome a means of validating the file remains the same and hasn't changed: A strong (normal) etag works for this.  Setting the last-modified header might work as well.

When using Java inputstreams this of course means using the skip method: be careful that needs to go in a loop because the skip method often does not skill all the bytes requested.  We implemented range support in our EmbeddedHTTPD and RangeInputStream .