dom/tests/mochitest/geolocation/network_geolocation.sjs

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 function parseQueryString(str)
michael@0 2 {
michael@0 3 if (str == "")
michael@0 4 return {};
michael@0 5
michael@0 6 var paramArray = str.split("&");
michael@0 7 var regex = /^([^=]+)=(.*)$/;
michael@0 8 var params = {};
michael@0 9 for (var i = 0, sz = paramArray.length; i < sz; i++)
michael@0 10 {
michael@0 11 var match = regex.exec(paramArray[i]);
michael@0 12 if (!match)
michael@0 13 throw "Bad parameter in queryString! '" + paramArray[i] + "'";
michael@0 14 params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
michael@0 15 }
michael@0 16
michael@0 17 return params;
michael@0 18 }
michael@0 19
michael@0 20 function getPosition(action)
michael@0 21 {
michael@0 22 var response = {
michael@0 23 status: "OK",
michael@0 24 location: {
michael@0 25 lat: 37.41857,
michael@0 26 lng: -122.08769,
michael@0 27 },
michael@0 28 accuracy: (action == "worse-accuracy") ? 100 : 42,
michael@0 29 };
michael@0 30
michael@0 31 return JSON.stringify(response);
michael@0 32 }
michael@0 33
michael@0 34 var timer;
michael@0 35 function handleRequest(request, response)
michael@0 36 {
michael@0 37 var params = parseQueryString(request.queryString);
michael@0 38
michael@0 39 if (params.action == "stop-responding") {
michael@0 40 response.processAsync();
michael@0 41 return;
michael@0 42 }
michael@0 43
michael@0 44 var position = getPosition(params.action);
michael@0 45
michael@0 46 if (params.action == "respond-garbage") {
michael@0 47 // better way?
michael@0 48 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
michael@0 49 position = "";
michael@0 50 var len = Math.floor(Math.random() * 5000);
michael@0 51
michael@0 52 for (var i=0; i< len; i++) {
michael@0 53 var c = Math.floor(Math.random() * chars.length);
michael@0 54 position += chars.substring(c, c+1);
michael@0 55 }
michael@0 56 }
michael@0 57
michael@0 58 var response;
michael@0 59 response.processAsync();
michael@0 60 response.setStatusLine("1.0", 200, "OK");
michael@0 61 response.setHeader("Cache-Control", "no-cache", false);
michael@0 62 response.setHeader("Content-Type", "aplication/x-javascript", false);
michael@0 63
michael@0 64 var delay = 0;
michael@0 65 if ('delay' in params) {
michael@0 66 delay = params.delay;
michael@0 67 }
michael@0 68 if (params.action === "send404") {
michael@0 69 response.setStatusLine("1.0", 404, "Not Found");
michael@0 70 position = '';
michael@0 71 }
michael@0 72 timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
michael@0 73 timer.initWithCallback(function() {
michael@0 74 response.write(position);
michael@0 75 response.finish();
michael@0 76 }, delay, timer.TYPE_ONE_SHOT);
michael@0 77 }
michael@0 78

mercurial