comparison 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
comparison
equal deleted inserted replaced
124:fbeb0bf2b51d 125:5ad15380f851
989 res.end(); 989 res.end();
990 } 990 }
991 }); 991 });
992 } 992 }
993 else { 993 else {
994 res.writeHead(404, resheaders); 994 send404(res, nname, req.method == 'HEAD');
995 if (req.method != 'HEAD') {
996 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
997 + nname + " Not Found</h1></body></html>\n");
998 }
999 res.end();
1000 } 995 }
1001 }); 996 });
1002 return; 997 return;
1003 } 998 }
1004 999
1091 res.end(); 1086 res.end();
1092 }); 1087 });
1093 } 1088 }
1094 1089
1095 function getuinfo(req, res) { 1090 function getuinfo(req, res) {
1096 var query = url.parse(req.url, true).query; 1091 var urlobj = url.parse(req.url, true);
1092 var query = urlobj.query;
1097 if (!("key" in query) || !(query["key"] in logins)) { 1093 if (!("key" in query) || !(query["key"] in logins)) {
1098 sendError(res, 1); 1094 sendError(res, 1);
1099 return; 1095 return;
1100 } 1096 }
1097 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/);
1098 if (!match || !match[1]) {
1099 send404(res, urlobj.pathname, req.method == 'HEAD');
1100 return;
1101 }
1102 var which = match[1];
1101 var name = logins[query["key"]].name; 1103 var name = logins[query["key"]].name;
1102 var reply = { "u": name }; 1104 var reply = { "u": name };
1103 function send() { 1105 function send() {
1104 res.writeHead(200, { "Content-Type": "application/json" }); 1106 res.writeHead(200, { "Content-Type": "application/json" });
1105 res.write(JSON.stringify(reply)); 1107 res.write(JSON.stringify(reply));
1106 res.end(); 1108 res.end();
1107 } 1109 }
1108 if ("pw" in query) { 1110 if (which == "pw") {
1109 /* Don't actually divulge passwords. */ 1111 /* Don't actually divulge passwords. */
1110 reply["pw"] = ""; 1112 reply["pw"] = "";
1111 } 1113 send();
1112 if ("email" in query) { 1114 }
1115 else if (which == "email") {
1113 var address; 1116 var address;
1114 function finish(code, signal) { 1117 function finish(code, signal) {
1115 if (code != 0) { 1118 if (code != 0) {
1116 tslog("sqlickrypt: %d with name %s", code, name); 1119 tslog("sqlickrypt: %d with name %s", code, name);
1117 sendError(res, 2); 1120 sendError(res, 2);
1126 address = data.toString().replace(/\n/g, ""); 1129 address = data.toString().replace(/\n/g, "");
1127 }); 1130 });
1128 subproc.on("exit", finish); 1131 subproc.on("exit", finish);
1129 subproc.stdin.end(name + '\n', "utf8"); 1132 subproc.stdin.end(name + '\n', "utf8");
1130 } 1133 }
1131 else 1134 else {
1132 send(); 1135 send404(res, urlobj.pathname, req.method == 'HEAD');
1136 return;
1137 }
1133 } 1138 }
1134 1139
1135 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 1140 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
1136 "Login failed", "Already playing", "Game launch failed", 1141 "Login failed", "Already playing", "Game launch failed",
1137 "Server shutting down", "Game not in progress" ]; 1142 "Server shutting down", "Game not in progress" ];
1147 edict["s"] += ": " + msg; 1152 edict["s"] += ": " + msg;
1148 if (box) 1153 if (box)
1149 res.write(JSON.stringify([edict])); 1154 res.write(JSON.stringify([edict]));
1150 else 1155 else
1151 res.write(JSON.stringify(edict)); 1156 res.write(JSON.stringify(edict));
1157 res.end();
1158 }
1159
1160 function send404(res, path, nopage) {
1161 res.writeHead(404, {"Content-Type": "text/html; charset=utf-8"});
1162 if (!nopage) {
1163 res.write("<html><head><title>" + path + "</title></head>\n<body><h1>"
1164 + path + " Not Found</h1></body></html>\n");
1165 }
1152 res.end(); 1166 res.end();
1153 } 1167 }
1154 1168
1155 function webHandler(req, res) { 1169 function webHandler(req, res) {
1156 /* default headers for the response */ 1170 /* default headers for the response */
1229 return; 1243 return;
1230 } 1244 }
1231 else if (target == '/status') { 1245 else if (target == '/status') {
1232 statusmsg(req, res); 1246 statusmsg(req, res);
1233 } 1247 }
1234 else if (target == '/uinfo') { 1248 else if (target.match(/^\/uinfo\//)) {
1235 getuinfo(req, res); 1249 getuinfo(req, res);
1236 } 1250 }
1237 else if (target.match(/^\/pstatus\//)) { 1251 else if (target.match(/^\/pstatus\//)) {
1238 pstatusmsg(req, res); 1252 pstatusmsg(req, res);
1239 } 1253 }