Intercepting HTTP requests with JavaScript

If you are building an offline capable application, then I would posit that at some level in your client architecture you are going to need a Proxy. On one side of the Proxy will sit your presentation logic, on the other side will sit your business logic. When operating in online mode this Proxy will pass requests on to the remote server, when operating in offline mode the Proxy will redirect requests to some local implementation of business logic that provides a facsimile of the remote server’s behaviour. For browser based applications, I feel a desirable place to position such a Proxy is at the browser’s HTTP interface. Implementing a Proxy at this level means that all means of initiating HTTP requests from within a browser can be proxied.  Indeed Google Gears takes exactly this approach, providing a ResourceStore into which resources can be cached, Gears then intercepts all HTTP requests made by the browser and if it detects a matching HTTP GET request for a stored resource, serves the cached version of the resource from the ResourceStore. This provides a means for an application to provide the static data it needs to ‘read’ when offline, but what about data the application needs to ‘write’ when offline? What about ‘read’ data that is constructed on the fly, for example the results of a query? My colleague, Nikunj Mehta has just released a draft specification (I helped review this specification and I also helped build an implementation of an earlier incarnation of the specification) that aims to address these scenarios. BITSY specifies a means for browser based applications to provide JavaScript based callbacks to be called whenever a HTTP request is made by the browser. Applications can implement whatever logic they require in these callbacks, for example to mimic the behaviour of a remote server, or store data for later propagation to the server (when connectivity is resumed), or some blend of these behaviours. Similar to Gear’s ResourceStore, BITSY also specifies a DataCache to provide offline resource storage.

Ⓗ Home   Ⓑ Blog   Ⓐ About