dom/tests/mochitest/beacon/beacon-handler.sjs

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/beacon/beacon-handler.sjs	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const CC = Components.Constructor;
    1.11 +const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
    1.12 +                             "nsIBinaryInputStream",
    1.13 +                             "setInputStream");
    1.14 +
    1.15 +function DEBUG(str)
    1.16 +{
    1.17 +  // dump("********** " + str + "\n");
    1.18 +}
    1.19 +
    1.20 +function setOurState(data) {
    1.21 +  x = { data: data, QueryInterface: function(iid) { return this } };
    1.22 +  x.wrappedJSObject = x;
    1.23 +  setObjectState("beacon-handler", x);
    1.24 +  DEBUG("our state is " + data);
    1.25 +}
    1.26 +
    1.27 +function getOurState() {
    1.28 +  var data;
    1.29 +  getObjectState("beacon-handler", function(x) {
    1.30 +    // x can be null if no one has set any state yet
    1.31 +    if (x) {
    1.32 +      data = x.wrappedJSObject.data;
    1.33 +    }
    1.34 +  });
    1.35 +  return data;
    1.36 +}
    1.37 +
    1.38 +function handleRequest(request, response) {
    1.39 +  DEBUG("Entered request handler");
    1.40 +  response.setHeader("Cache-Control", "no-cache", false);
    1.41 +
    1.42 +  function finishControlResponse(response) {
    1.43 +    DEBUG("********* sending out the control GET response");
    1.44 +    var data = getState("beaconData");
    1.45 +    var mimetype = getState("beaconMimetype");
    1.46 +    DEBUG("GET was sending : " + data + "\n");
    1.47 +    DEBUG("GET was sending : " + mimetype + "\n");
    1.48 +    var result = {
    1.49 +      "data": data,
    1.50 +      "mimetype": mimetype,
    1.51 +    };
    1.52 +    response.write(JSON.stringify(result));
    1.53 +    setOurState(null);
    1.54 +  }
    1.55 +
    1.56 +  if (request.method == "GET") {
    1.57 +    DEBUG(" ------------ GET --------------- ");
    1.58 +    response.setHeader("Content-Type", "application/json", false);
    1.59 +    switch (request.queryString) {
    1.60 +    case "getLastBeacon":
    1.61 +      var state = getOurState();
    1.62 +      if (state === "unblocked") {
    1.63 +        finishControlResponse(response);
    1.64 +      } else {
    1.65 +        DEBUG("GET has  arrived, but POST has not, blocking response!");
    1.66 +        setOurState(response);
    1.67 +        response.processAsync();
    1.68 +      }
    1.69 +      break;
    1.70 +    default:
    1.71 +      response.setStatusLine(request.httpVersion, 400, "Bad Request");
    1.72 +      break;
    1.73 +    }
    1.74 +    return;
    1.75 +  }
    1.76 +
    1.77 +  if (request.method == "POST") {
    1.78 +    DEBUG(" ------------ POST --------------- ");
    1.79 +    var body = new BinaryInputStream(request.bodyInputStream);
    1.80 +    var avail;
    1.81 +    var bytes = [];
    1.82 +
    1.83 +    while ((avail = body.available()) > 0) {
    1.84 +      Array.prototype.push.apply(bytes, body.readByteArray(avail));
    1.85 +    }
    1.86 +
    1.87 +    var data = "";
    1.88 +    for (var i=0; i < bytes.length; i++) {
    1.89 +      // We are only passing strings at this point.
    1.90 +      if (bytes[i] < 32) continue;
    1.91 +      var charcode = String.fromCharCode(bytes[i]);
    1.92 +      data += charcode;
    1.93 +    }
    1.94 +
    1.95 +    var mimetype = request.getHeader("Content-Type");
    1.96 +
    1.97 +    // check to see if this is form data.
    1.98 +    if (mimetype.indexOf("multipart/form-data") != -1) {
    1.99 +
   1.100 +      // trim the mime type to make testing easier.
   1.101 +      mimetype = "multipart/form-data";
   1.102 +      // Extract only the form-data name.
   1.103 +
   1.104 +      var pattern = /; name=\"(.+)\";/;
   1.105 +      data = data.split(pattern)[1];
   1.106 +    }
   1.107 +
   1.108 +    DEBUG("**********   POST was sending : " + data + "\n");
   1.109 +    DEBUG("**********   POST was sending : " + mimetype + "\n");
   1.110 +    setState("beaconData", data);
   1.111 +    setState("beaconMimetype", mimetype);
   1.112 +
   1.113 +    response.setHeader("Content-Type", "text/plain", false);
   1.114 +    response.write('ok');
   1.115 +
   1.116 +    var blockedResponse = getOurState();
   1.117 +    if (typeof(blockedResponse) == "object" && blockedResponse) {
   1.118 +      DEBUG("GET is already pending, finishing!");
   1.119 +      finishControlResponse(blockedResponse);
   1.120 +      blockedResponse.finish();
   1.121 +    } else {
   1.122 +      DEBUG("GET has not arrived, marking it as unblocked");
   1.123 +      setOurState("unblocked");
   1.124 +    }
   1.125 +
   1.126 +    return;
   1.127 +  }
   1.128 +
   1.129 +  response.setStatusLine(request.httpVersion, 402, "Bad Request");
   1.130 +}

mercurial