summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/quicksuggest/browser/searchSuggestionEngine.sjs
blob: 145392fcf2ee99742b2513593b04e1aeb261dfe0 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

let gTimer;

function handleRequest(req, resp) {
  // Parse the query params.  If the params aren't in the form "foo=bar", then
  // treat the entire query string as a search string.
  let params = req.queryString.split("&").reduce((memo, pair) => {
    let [key, val] = pair.split("=");
    if (!val) {
      // This part isn't in the form "foo=bar".  Treat it as the search string
      // (the "query").
      val = key;
      key = "query";
    }
    memo[decode(key)] = decode(val);
    return memo;
  }, {});

  let timeout = parseInt(params.timeout);
  if (timeout) {
    // Write the response after a timeout.
    resp.processAsync();
    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    gTimer.init(
      () => {
        writeResponse(params, resp);
        resp.finish();
      },
      timeout,
      Ci.nsITimer.TYPE_ONE_SHOT
    );
    return;
  }

  writeResponse(params, resp);
}

function writeResponse(params, resp) {
  // Echo back the search string with "foo" and "bar" appended.
  let suffixes = ["foo", "bar"];
  if (params.count) {
    // Add more suffixes.
    let serial = 0;
    while (suffixes.length < params.count) {
      suffixes.push(++serial);
    }
  }
  let data = [params.query, suffixes.map(s => params.query + s)];
  resp.setHeader("Content-Type", "application/json", false);
  resp.write(JSON.stringify(data));
}

function decode(str) {
  return decodeURIComponent(str.replace(/\+/g, encodeURIComponent(" ")));
}