summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/fetch/context/beacon.sjs
blob: 8401bc29bbbd77a1fa69b2c257fe62ef28c86951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * This is based on dom/tests/mochitest/beacon/beacon-originheader-handler.sjs.
 */

function handleRequest(request, response)
{
  response.setHeader("Cache-Control", "no-cache", false);
  response.setHeader("Content-Type", "text/plain", false);

  // case XHR-REQUEST: the xhr-request tries to query the
  // stored context from the beacon request.
  if (request.queryString == "queryContext") {
    var context = getState("interceptContext");
    // if the beacon already stored the context - return.
    if (context) {
      response.write(context);
      setState("interceptContext", "");
      return;
    }
    // otherwise wait for the beacon request
    response.processAsync();
    setObjectState("sw-xhr-response", response);
    return;
  }

  // case BEACON-REQUEST: get the beacon context and
  // store the context on the server.
  var context = request.queryString;
  setState("interceptContext", context);

  // if there is an xhr-request waiting, return the context now.
  try{
    getObjectState("sw-xhr-response", function(xhrResponse) {
      if (!xhrResponse) {
        return;
      }
      setState("interceptContext", "");
      xhrResponse.write(context);
      xhrResponse.finish();
    });
  } catch(e) {
  }
}