1 function handleRequest(request, response)
4 reallyHandleRequest(request, response);
6 response.setStatusLine("1.0", 200, "AlmostOK");
7 response.write("Error handling request: " + e);
12 function reallyHandleRequest(request, response) {
14 var requestAuth = true;
16 // Allow the caller to drive how authentication is processed via the query.
17 // Eg, http://localhost:8888/authenticate.sjs?user=foo&realm=bar
18 var query = request.queryString;
20 var expected_user = "", expected_pass = "", realm = "mochitest";
22 match = /user=([^&]*)/.exec(query);
24 expected_user = match[1];
27 match = /pass=([^&]*)/.exec(query);
29 expected_pass = match[1];
32 match = /realm=([^&]*)/.exec(query);
37 // Look for an authentication header, if any, in the request.
39 // EG: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
41 // This test only supports Basic auth. The value sent by the client is
42 // "username:password", obscured with base64 encoding.
44 var actual_user = "", actual_pass = "", authHeader;
45 if (request.hasHeader("Authorization")) {
46 authHeader = request.getHeader("Authorization");
47 match = /Basic (.+)/.exec(authHeader);
48 if (match.length != 2)
49 throw "Couldn't parse auth header: " + authHeader;
51 var userpass = base64ToString(match[1]); // no atob() :-(
52 match = /(.*):(.*)/.exec(userpass);
53 if (match.length != 3)
54 throw "Couldn't decode auth header: " + userpass;
55 actual_user = match[1];
56 actual_pass = match[2];
59 // Don't request authentication if the credentials we got were what we
61 if (expected_user == actual_user &&
62 expected_pass == actual_pass) {
67 response.setStatusLine("1.0", 401, "Authentication required");
68 response.setHeader("WWW-Authenticate", "basic realm=\"" + realm + "\"", false);
70 response.setStatusLine("1.0", 200, "OK");
73 response.setHeader("Content-Type", "application/xhtml+xml", false);
74 response.write("<html xmlns='http://www.w3.org/1999/xhtml'>");
75 response.write("<p>Login: <span id='ok'>" + (requestAuth ? "FAIL" : "PASS") + "</span></p>\n");
76 response.write("<p>Auth: <span id='auth'>" + authHeader + "</span></p>\n");
77 response.write("<p>User: <span id='user'>" + actual_user + "</span></p>\n");
78 response.write("<p>Pass: <span id='pass'>" + actual_pass + "</span></p>\n");
79 response.write("</html>");
85 // Yoinked from extensions/xml-rpc/src/nsXmlRpcClient.js because btoa()
86 // doesn't seem to exist. :-(
87 /* Convert Base64 data to a string */
88 const toBinaryTable = [
89 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
90 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
91 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
92 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
93 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
94 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
95 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
96 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
98 const base64Pad = '=';
100 function base64ToString(data) {
103 var leftbits = 0; // number of bits decoded, but yet to be appended
104 var leftdata = 0; // bits decoded, but yet to be appended
106 // Convert one by one.
107 for (var i = 0; i < data.length; i++) {
108 var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
109 var padding = (data[i] == base64Pad);
110 // Skip illegal characters and whitespace
111 if (c == -1) continue;
113 // Collect data into leftdata, update bitcount
114 leftdata = (leftdata << 6) | c;
117 // If we have 8 or more bits, append 8 bits to the result
120 // Append if not padding.
122 result += String.fromCharCode((leftdata >> leftbits) & 0xff);
123 leftdata &= (1 << leftbits) - 1;
127 // If there are any bits left, the base64 string was corrupted
129 throw Components.Exception('Corrupted base64 string');