michael@0: function parseQueryString(str) michael@0: { michael@0: if (str == "") michael@0: return {}; michael@0: michael@0: var paramArray = str.split("&"); michael@0: var regex = /^([^=]+)=(.*)$/; michael@0: var params = {}; michael@0: for (var i = 0, sz = paramArray.length; i < sz; i++) michael@0: { michael@0: var match = regex.exec(paramArray[i]); michael@0: if (!match) michael@0: throw "Bad parameter in queryString! '" + paramArray[i] + "'"; michael@0: params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]); michael@0: } michael@0: michael@0: return params; michael@0: } michael@0: michael@0: function getPosition(action) michael@0: { michael@0: var response = { michael@0: status: "OK", michael@0: location: { michael@0: lat: 37.41857, michael@0: lng: -122.08769, michael@0: }, michael@0: accuracy: (action == "worse-accuracy") ? 100 : 42, michael@0: }; michael@0: michael@0: return JSON.stringify(response); michael@0: } michael@0: michael@0: var timer; michael@0: function handleRequest(request, response) michael@0: { michael@0: var params = parseQueryString(request.queryString); michael@0: michael@0: if (params.action == "stop-responding") { michael@0: response.processAsync(); michael@0: return; michael@0: } michael@0: michael@0: var position = getPosition(params.action); michael@0: michael@0: if (params.action == "respond-garbage") { michael@0: // better way? michael@0: var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; michael@0: position = ""; michael@0: var len = Math.floor(Math.random() * 5000); michael@0: michael@0: for (var i=0; i< len; i++) { michael@0: var c = Math.floor(Math.random() * chars.length); michael@0: position += chars.substring(c, c+1); michael@0: } michael@0: } michael@0: michael@0: var response; michael@0: response.processAsync(); michael@0: response.setStatusLine("1.0", 200, "OK"); michael@0: response.setHeader("Cache-Control", "no-cache", false); michael@0: response.setHeader("Content-Type", "aplication/x-javascript", false); michael@0: michael@0: var delay = 0; michael@0: if ('delay' in params) { michael@0: delay = params.delay; michael@0: } michael@0: if (params.action === "send404") { michael@0: response.setStatusLine("1.0", 404, "Not Found"); michael@0: position = ''; michael@0: } michael@0: timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer); michael@0: timer.initWithCallback(function() { michael@0: response.write(position); michael@0: response.finish(); michael@0: }, delay, timer.TYPE_ONE_SHOT); michael@0: } michael@0: