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