Improve the /uinfo interface.

Change the URL scheme to /uinfo/<property>?key=<key> because that makes
more sense.
This commit is contained in:
John "Elwin" Edwards 2012-08-25 19:33:31 -07:00
parent 81e772545a
commit 48b5b5d32a

View file

@ -991,12 +991,7 @@ function serveStatic(req, res, fname) {
}); });
} }
else { else {
res.writeHead(404, resheaders); send404(res, nname, req.method == 'HEAD');
if (req.method != 'HEAD') {
res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
+ nname + " Not Found</h1></body></html>\n");
}
res.end();
} }
}); });
return; return;
@ -1093,11 +1088,18 @@ function pstatusmsg(req, res) {
} }
function getuinfo(req, res) { function getuinfo(req, res) {
var query = url.parse(req.url, true).query; var urlobj = url.parse(req.url, true);
var query = urlobj.query;
if (!("key" in query) || !(query["key"] in logins)) { if (!("key" in query) || !(query["key"] in logins)) {
sendError(res, 1); sendError(res, 1);
return; return;
} }
var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
if (!match || !match[1]) {
send404(res, urlobj.pathname, req.method == 'HEAD');
return;
}
var which = match[1];
var name = logins[query["key"]].name; var name = logins[query["key"]].name;
var reply = { "u": name }; var reply = { "u": name };
function send() { function send() {
@ -1105,11 +1107,12 @@ function getuinfo(req, res) {
res.write(JSON.stringify(reply)); res.write(JSON.stringify(reply));
res.end(); res.end();
} }
if ("pw" in query) { if (which == "pw") {
/* Don't actually divulge passwords. */ /* Don't actually divulge passwords. */
reply["pw"] = ""; reply["pw"] = "";
send();
} }
if ("email" in query) { else if (which == "email") {
var address; var address;
function finish(code, signal) { function finish(code, signal) {
if (code != 0) { if (code != 0) {
@ -1128,8 +1131,10 @@ function getuinfo(req, res) {
subproc.on("exit", finish); subproc.on("exit", finish);
subproc.stdin.end(name + '\n', "utf8"); subproc.stdin.end(name + '\n', "utf8");
} }
else else {
send(); send404(res, urlobj.pathname, req.method == 'HEAD');
return;
}
} }
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
@ -1152,6 +1157,15 @@ function sendError(res, ecode, msg, box) {
res.end(); res.end();
} }
function send404(res, path, nopage) {
res.writeHead(404, {"Content-Type": "text/html; charset=utf-8"});
if (!nopage) {
res.write("<html><head><title>" + path + "</title></head>\n<body><h1>"
+ path + " Not Found</h1></body></html>\n");
}
res.end();
}
function webHandler(req, res) { function webHandler(req, res) {
/* default headers for the response */ /* default headers for the response */
var resheaders = {'Content-Type': 'text/html'}; var resheaders = {'Content-Type': 'text/html'};
@ -1231,7 +1245,7 @@ function webHandler(req, res) {
else if (target == '/status') { else if (target == '/status') {
statusmsg(req, res); statusmsg(req, res);
} }
else if (target == '/uinfo') { else if (target.match(/^\/uinfo\//)) {
getuinfo(req, res); getuinfo(req, res);
} }
else if (target.match(/^\/pstatus\//)) { else if (target.match(/^\/pstatus\//)) {