services/common/tests/unit/head_http.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial