Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | function handleRequest(request, response) { |
michael@0 | 8 | switch (request.method) { |
michael@0 | 9 | case "POST": |
michael@0 | 10 | try { |
michael@0 | 11 | var optionsHost = request.getHeader("options-host"); |
michael@0 | 12 | } catch(e) { } |
michael@0 | 13 | |
michael@0 | 14 | if (optionsHost) { |
michael@0 | 15 | setState("postHost", request.host); |
michael@0 | 16 | setState("optionsHost", optionsHost); |
michael@0 | 17 | return; |
michael@0 | 18 | } |
michael@0 | 19 | break; |
michael@0 | 20 | |
michael@0 | 21 | case "OPTIONS": |
michael@0 | 22 | if (getState("optionsHost") == request.host) { |
michael@0 | 23 | try { |
michael@0 | 24 | var optionsHeader = |
michael@0 | 25 | request.getHeader("Access-Control-Request-Headers"); |
michael@0 | 26 | } catch(e) { } |
michael@0 | 27 | setState("optionsHeader", "'" + optionsHeader + "'"); |
michael@0 | 28 | } |
michael@0 | 29 | break; |
michael@0 | 30 | |
michael@0 | 31 | case "GET": |
michael@0 | 32 | response.setHeader("Cache-Control", "no-cache", false); |
michael@0 | 33 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 34 | |
michael@0 | 35 | if (getState("postHost") == request.host) { |
michael@0 | 36 | var result = getState("optionsHeader"); |
michael@0 | 37 | if (result) { |
michael@0 | 38 | response.write("Success: expected OPTIONS request with " + result + |
michael@0 | 39 | " header"); |
michael@0 | 40 | } else if (getState("badGet") == 1) { |
michael@0 | 41 | response.write("Error: unexpected GET request"); |
michael@0 | 42 | } |
michael@0 | 43 | } else { |
michael@0 | 44 | setState("badGet", "1"); |
michael@0 | 45 | response.write("Error: this response should never be seen"); |
michael@0 | 46 | } |
michael@0 | 47 | return; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | response.setStatusLine(request.httpVersion, 501, "Not Implemented"); |
michael@0 | 51 | } |