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