services/common/tests/unit/head_http.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     1  /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://services-common/utils.js");
     6 function basic_auth_header(user, password) {
     7   return "Basic " + btoa(user + ":" + CommonUtils.encodeUTF8(password));
     8 }
    10 function basic_auth_matches(req, user, password) {
    11   if (!req.hasHeader("Authorization")) {
    12     return false;
    13   }
    15   let expected = basic_auth_header(user, CommonUtils.encodeUTF8(password));
    16   return req.getHeader("Authorization") == expected;
    17 }
    19 function httpd_basic_auth_handler(body, metadata, response) {
    20   if (basic_auth_matches(metadata, "guest", "guest")) {
    21     response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
    22     response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
    23   } else {
    24     body = "This path exists and is protected - failed";
    25     response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
    26     response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
    27   }
    28   response.bodyOutputStream.write(body, body.length);
    29 }

mercurial