diff rlgwebd.js @ 125:5ad15380f851

Improve the /uinfo interface. Change the URL scheme to /uinfo/<property>?key=<key> because that makes more sense.
author John "Elwin" Edwards <elwin@sdf.org>
date Sat, 25 Aug 2012 19:33:31 -0700
parents fbeb0bf2b51d
children 3e3824711791
line wrap: on
line diff
--- a/rlgwebd.js	Mon Aug 13 09:07:28 2012 -0700
+++ b/rlgwebd.js	Sat Aug 25 19:33:31 2012 -0700
@@ -991,12 +991,7 @@
       });
     }
     else {
-      res.writeHead(404, resheaders);
-      if (req.method != 'HEAD') {
-        res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
-                  + nname + " Not Found</h1></body></html>\n");
-      }
-      res.end();
+      send404(res, nname, req.method == 'HEAD');
     }
   });
   return;
@@ -1093,11 +1088,18 @@
 }
 
 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)) {
     sendError(res, 1);
     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 reply = { "u": name };
   function send() {
@@ -1105,11 +1107,12 @@
     res.write(JSON.stringify(reply));
     res.end();
   }
-  if ("pw" in query) {
+  if (which == "pw") {
     /* Don't actually divulge passwords. */
     reply["pw"] = "";
+    send();
   }
-  if ("email" in query) {
+  else if (which == "email") {
     var address;
     function finish(code, signal) {
       if (code != 0) {
@@ -1128,8 +1131,10 @@
     subproc.on("exit", finish);
     subproc.stdin.end(name + '\n', "utf8");
   }
-  else
-    send();
+  else {
+    send404(res, urlobj.pathname, req.method == 'HEAD');
+    return;
+  }
 }
 
 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 
@@ -1152,6 +1157,15 @@
   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) {
   /* default headers for the response */
   var resheaders = {'Content-Type': 'text/html'};
@@ -1231,7 +1245,7 @@
       else if (target == '/status') {
         statusmsg(req, res);
       }
-      else if (target == '/uinfo') {
+      else if (target.match(/^\/uinfo\//)) {
         getuinfo(req, res);
       }
       else if (target.match(/^\/pstatus\//)) {