DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d8847129d134)

VCS Links

Line Code
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const { classes: Cc, interfaces: Ci } = Components;

function handleRequest(request, response) {
  response.processAsync();

  let params = request.queryString.split("&");
  let format = (params.filter((s) => s.contains("fmt="))[0] || "").split("=")[1];
  let status = (params.filter((s) => s.contains("sts="))[0] || "").split("=")[1] || 200;

  let cachedCount = 0;
  let cacheExpire = 60; // seconds

  function setCacheHeaders() {
    if (status != 304) {
      response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
      response.setHeader("Pragma", "no-cache");
      response.setHeader("Expires", "0");
      return;
    }
    // Spice things up a little!
    if (cachedCount % 2) {
      response.setHeader("Cache-Control", "max-age=" + cacheExpire, false);
    } else {
      response.setHeader("Expires", Date(Date.now() + cacheExpire * 1000), false);
    }
    cachedCount++;
  }

  let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  timer.initWithCallback(() => {
    // to avoid garbage collection
    timer = null;
    switch (format) {
      case "txt": {
        response.setStatusLine(request.httpVersion, status, "DA DA DA");
        response.setHeader("Content-Type", "text/plain", false);
        setCacheHeaders();
        response.write("Братан, ты вообще качаешься?");
        response.finish();
        break;
      }
      case "xml": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/xml; charset=utf-8", false);
        setCacheHeaders();
        response.write("<label value='greeting'>Hello XML!</label>");
        response.finish();
        break;
      }
      case "html": {
        let content = params.filter((s) => s.contains("res="))[0].split("=")[1];
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/html; charset=utf-8", false);
        setCacheHeaders();
        response.write(content || "<p>Hello HTML!</p>");
        response.finish();
        break;
      }
      case "html-long": {
        let str = new Array(102400 /* 100 KB in bytes */).join(".");
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/html; charset=utf-8", false);
        setCacheHeaders();
        response.write("<p>" + str + "</p>");
        response.finish();
        break;
      }
      case "css": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/css; charset=utf-8", false);
        setCacheHeaders();
        response.write("body:pre { content: 'Hello CSS!' }");
        response.finish();
        break;
      }
      case "js": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "application/javascript; charset=utf-8", false);
        setCacheHeaders();
        response.write("function() { return 'Hello JS!'; }");
        response.finish();
        break;
      }
      case "json": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "application/json; charset=utf-8", false);
        setCacheHeaders();
        response.write("{ \"greeting\": \"Hello JSON!\" }");
        response.finish();
        break;
      }
      case "jsonp": {
        let fun = params.filter((s) => s.contains("jsonp="))[0].split("=")[1];
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/json; charset=utf-8", false);
        setCacheHeaders();
        response.write(fun + "({ \"greeting\": \"Hello JSONP!\" })");
        response.finish();
        break;
      }
      case "jsonp2": {
        let fun = params.filter((s) => s.contains("jsonp="))[0].split("=")[1];
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/json; charset=utf-8", false);
        setCacheHeaders();
        response.write(" " + fun + " ( { \"greeting\": \"Hello weird JSONP!\" } ) ; ");
        response.finish();
        break;
      }
      case "json-long": {
        let str = "{ \"greeting\": \"Hello long string JSON!\" },";
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/json; charset=utf-8", false);
        setCacheHeaders();
        response.write("[" + new Array(2048).join(str).slice(0, -1) + "]");
        response.finish();
        break;
      }
      case "json-malformed": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/json; charset=utf-8", false);
        setCacheHeaders();
        response.write("{ \"greeting\": \"Hello malformed JSON!\" },");
        response.finish();
        break;
      }
      case "json-text-mime": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/plain; charset=utf-8", false);
        setCacheHeaders();
        response.write("{ \"greeting\": \"Hello third-party JSON!\" }");
        response.finish();
        break;
      }
      case "json-custom-mime": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "text/x-bigcorp-json; charset=utf-8", false);
        setCacheHeaders();
        response.write("{ \"greeting\": \"Hello oddly-named JSON!\" }");
        response.finish();
        break;
      }
      case "font": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "font/woff", false);
        setCacheHeaders();
        response.finish();
        break;
      }
      case "image": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "image/png", false);
        setCacheHeaders();
        response.finish();
        break;
      }
      case "audio": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "audio/ogg", false);
        setCacheHeaders();
        response.finish();
        break;
      }
      case "video": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "video/webm", false);
        setCacheHeaders();
        response.finish();
        break;
      }
      case "flash": {
        response.setStatusLine(request.httpVersion, status, "OK");
        response.setHeader("Content-Type", "application/x-shockwave-flash", false);
        setCacheHeaders();
        response.finish();
        break;
      }
      default: {
        response.setStatusLine(request.httpVersion, 404, "Not Found");
        response.setHeader("Content-Type", "text/html; charset=utf-8", false);
        setCacheHeaders();
        response.write("<blink>Not Found</blink>");
        response.finish();
        break;
      }
    }
  }, 10, Ci.nsITimer.TYPE_ONE_SHOT); // Make sure this request takes a few ms.
}