From 39f9d2c3b38c8a9d09d890927a0b0ff7c2f53a33 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 13 Jun 2006 14:53:38 +0000 Subject: [PATCH] Wrapper functions for XMLHTTPRequest in scholar.js, stolen and adapted slightly from Simon's ingester code (those override the mime type to text/plain and only use responseText, I assume to conform to the Piggy Bank API, so unfortunately we can't use the same ones elsewhere) Scholar.HTTP.doGet(url, onStatus, onDone) and Scholar.HTTP.doPost(url, body, onStatus, onDone) -- onStatus and onDone are callbacks to call on non-200 responses and the response body, respectively --- .../content/scholar/xpcom/scholar.js | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/chrome/chromeFiles/content/scholar/xpcom/scholar.js b/chrome/chromeFiles/content/scholar/xpcom/scholar.js index a892c3950..c7b8b9ffe 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/scholar.js +++ b/chrome/chromeFiles/content/scholar/xpcom/scholar.js @@ -3,7 +3,8 @@ const SCHOLAR_CONFIG = { DB_FILE: 'scholar.sqlite', DB_REBUILD: false, // erase DB and recreate from schema DEBUG_LOGGING: true, - DEBUG_TO_CONSOLE: true // dump debug messages to console rather than (much slower) Debug Logger + DEBUG_TO_CONSOLE: true, // dump debug messages to console rather than (much slower) Debug Logger + REPOSITORY_URL: 'http://chnm.gmu.edu/firefoxscholar/dev/repo' }; /* @@ -304,4 +305,83 @@ Scholar.Hash.prototype.remove = function(in_key){ Scholar.Hash.prototype.has = function(in_key){ return typeof(this.items[in_key]) != 'undefined'; +} + + + +Scholar.HTTP = new function(){ + + this.doGet = doGet; + this.doPost = doPost; + + function doGet(url, onStatus, onDone){ + var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] + .createInstance(); + + xmlhttp.open('GET', url, true); + + xmlhttp.onreadystatechange = function(){ + _stateChange(xmlhttp, onStatus, onDone); + }; + xmlhttp.send(null); + } + + + function doPost(url, body, onStatus, onDone){ + var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] + .createInstance(); + + xmlhttp.open('POST', url, true); + + xmlhttp.onreadystatechange = function(){ + _stateChange(xmlhttp, onStatus, onDone); + }; + xmlhttp.send(body); + } + + + function _stateChange(xmlhttp, onStatus, onDone){ + switch (xmlhttp.readyState){ + + // Request not yet made + case 1: + break; + + // Contact established with server but nothing downloaded yet + case 2: + try { + // Check for HTTP status 200 + if (xmlhttp.status != 200){ + if (onStatus) { + onStatus( + xmlhttp.status, + xmlhttp.statusText, + xmlhttp + ); + xmlhttp.abort(); + } + } + } + catch (e){ + Scholar.debug(e, 2); + } + break; + + // Called multiple while downloading in progress + case 3: + break; + + // Download complete + case 4: + try { + if (onDone){ + onDone(xmlhttp); + } + } + catch (e){ + Scholar.debug(e, 2); + } + break; + } + } } \ No newline at end of file