services/common/tests/unit/head_http.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:fc3a09d40921
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 Cu.import("resource://services-common/utils.js");
5
6 function basic_auth_header(user, password) {
7 return "Basic " + btoa(user + ":" + CommonUtils.encodeUTF8(password));
8 }
9
10 function basic_auth_matches(req, user, password) {
11 if (!req.hasHeader("Authorization")) {
12 return false;
13 }
14
15 let expected = basic_auth_header(user, CommonUtils.encodeUTF8(password));
16 return req.getHeader("Authorization") == expected;
17 }
18
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