|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // The timer never fires if it's not declared and set to this variable outside |
|
5 // handleRequest, as if it's getting GC'ed when handleRequest's scope goes away. |
|
6 // Shouldn't the timer thread hold a strong reference to it? |
|
7 var timer; |
|
8 |
|
9 function handleRequest(req, resp) { |
|
10 resp.processAsync(); |
|
11 resp.setHeader("Cache-Control", "no-cache, no-store", false); |
|
12 resp.setHeader("Content-Type", "text/html;charset=utf-8", false); |
|
13 |
|
14 let opts = {}; |
|
15 try { |
|
16 opts = JSON.parse(decodeURIComponent(req.queryString)); |
|
17 } |
|
18 catch (err) {} |
|
19 |
|
20 if (opts.setRedCookie) |
|
21 resp.setHeader("Set-Cookie", "red", false); |
|
22 |
|
23 if (opts.setGreenCookie) |
|
24 resp.setHeader("Set-Cookie", "green", false); |
|
25 |
|
26 if (req.hasHeader("Cookie") && |
|
27 req.getHeader("Cookie").split(";").indexOf("red") >= 0) { |
|
28 resp.write('<html style="background: #f00;"></html>'); |
|
29 resp.finish(); |
|
30 return; |
|
31 } |
|
32 |
|
33 if (req.hasHeader("Cookie") && |
|
34 req.getHeader("Cookie").split(";").indexOf("green") >= 0) { |
|
35 resp.write('<html style="background: #0f0;"></html>'); |
|
36 resp.finish(); |
|
37 return; |
|
38 } |
|
39 |
|
40 if (opts.redirect) { |
|
41 resp.setHeader("Location", opts.redirect); |
|
42 resp.setStatusLine(null, 303, null); |
|
43 resp.finish(); |
|
44 return; |
|
45 } |
|
46 |
|
47 if (opts.wait) { |
|
48 resp.write("Waiting " + opts.wait + " ms... "); |
|
49 timer = Components.classes["@mozilla.org/timer;1"]. |
|
50 createInstance(Components.interfaces.nsITimer); |
|
51 timer.init(function ding() { |
|
52 resp.write("OK!"); |
|
53 resp.finish(); |
|
54 }, opts.wait, Components.interfaces.nsITimer.TYPE_ONE_SHOT); |
|
55 return; |
|
56 } |
|
57 |
|
58 resp.write("<pre>" + JSON.stringify(opts, undefined, 2) + "</pre>"); |
|
59 resp.finish(); |
|
60 } |