All pastes #895969 Raw Edit

Untitled

public javascript v1 · immutable
·published 2008-02-08 06:00 UTC
rendered paste body
// accepted HTTP status codes var redirect = {300: null, 301: null, 302: null, 303: null, 307: null}; var listener = {     onStartRequest: function(request, context) {},     onStopRequest: function(request, context, status) {         if (status == 0x804B001F) { // NS_ERROR_REDIRECT_LOOP             var channel = request.QueryInterface(Components.interfaces.nsIHttpChannel);             if (channel.responseStatus in redirect) {                 var location = null;                 try {                     location = channel.getResponseHeader('Location');                 } catch (exc) {                     // no 'Location' header sent with the redirect;                 }                 if (location != null) {                     // here you go, do with 'location' whatever you want 					LOG('location '+location);                }             }         }     },     onDataAvailable: function(request, context, stream, offset, length) {         // abort the request in case we didn't get redirected         request.cancel(Components.results.NS_ERROR_UNEXPECTED);     },     QueryInterface: function(iid) {         if (iid.equals(Components.interfaces.nsISupports) ||             iid.equals(Components.interfaces.nsIStreamListener) ||             iid.equals(Components.interfaces.nsIRequestObserver))             return this;         else             throw Components.results.NS_NOINTERFACE;     }, }; var channel = null; try { 	channel = Components.classes["@mozilla.org/network/io-service;1"] 		.getService(Components.interfaces.nsIIOService) 		.newChannel(srcWithT, null, null) 		.QueryInterface(Components.interfaces.nsIHttpChannel) } catch (exc) { 	// something went wrong when creating a channel instance } if (channel) { 	channel.redirectionLimit = 0; 	channel.asyncOpen(listener, context); }