comparison rlgwebd.js @ 31:7dd6becf9ce9

rlgwebd.js: improve MIME types. Respond with the proper MIME type for JSON, and identify HTML as using the UTF-8 charset.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 05 Jun 2012 09:31:49 -0700
parents b5570a594266
children c75fc4b1d13d
comparison
equal deleted inserted replaced
30:b5570a594266 31:7dd6becf9ce9
350 var nsession = new TermSession(gname, username, [lockfile, ttyrec], dims); 350 var nsession = new TermSession(gname, username, [lockfile, ttyrec], dims);
351 if (nsession) { 351 if (nsession) {
352 /* Technically there's a race condition for the "lock"file, but since 352 /* Technically there's a race condition for the "lock"file, but since
353 * it requires the user deliberately starting two games at similar times, 353 * it requires the user deliberately starting two games at similar times,
354 * it's not too serious. We can't get O_EXCL in Node anyway. */ 354 * it's not too serious. We can't get O_EXCL in Node anyway. */
355 res.writeHead(200, {'Content-Type': 'text/plain'}); 355 res.writeHead(200, {'Content-Type': 'application/json'});
356 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h": 356 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h":
357 nsession.h}; 357 nsession.h};
358 res.write(JSON.stringify(reply)); 358 res.write(JSON.stringify(reply));
359 res.end(); 359 res.end();
360 tslog("%s playing %s (key %s, pid %d)", username, gname, 360 tslog("%s playing %s (key %s, pid %d)", username, gname,
443 else if (code != 0) { 443 else if (code != 0) {
444 sendError(res, 0, null); 444 sendError(res, 0, null);
445 tslog("sqlickrypt register failed with code %d", code); 445 tslog("sqlickrypt register failed with code %d", code);
446 } 446 }
447 else { 447 else {
448 res.writeHead(200, {'Content-Type': 'text/plain'}); 448 res.writeHead(200, {'Content-Type': 'application/json'});
449 var reply = {"t": "r", "d": uname}; 449 var reply = {"t": "r", "d": uname};
450 res.write(JSON.stringify(reply)); 450 res.write(JSON.stringify(reply));
451 res.end(); 451 res.end();
452 tslog("Added new user: %s", uname); 452 tslog("Added new user: %s", uname);
453 regsetup(uname); 453 regsetup(uname);
463 if (!term.alive) { 463 if (!term.alive) {
464 sendError(res, 1, null); 464 sendError(res, 1, null);
465 return; 465 return;
466 } 466 }
467 term.close(); 467 term.close();
468 var resheaders = {'Content-Type': 'text/plain'}; 468 var resheaders = {'Content-Type': 'application/json'};
469 res.writeHead(200, resheaders); 469 res.writeHead(200, resheaders);
470 res.write(JSON.stringify({"t": "q"})); 470 res.write(JSON.stringify({"t": "q"}));
471 res.end(); 471 res.end();
472 return; 472 return;
473 } 473 }
493 var realname = path.join(serveStaticRoot, nname); 493 var realname = path.join(serveStaticRoot, nname);
494 var extension = path.extname(realname); 494 var extension = path.extname(realname);
495 path.exists(realname, function (exists) { 495 path.exists(realname, function (exists) {
496 var resheaders = {}; 496 var resheaders = {};
497 if (!exists || !extension || extension == ".html") 497 if (!exists || !extension || extension == ".html")
498 resheaders["Content-Type"] = "text/html"; 498 resheaders["Content-Type"] = "text/html; charset=utf-8";
499 else if (extension == ".png") 499 else if (extension == ".png")
500 resheaders["Content-Type"] = "image/png"; 500 resheaders["Content-Type"] = "image/png";
501 else if (extension == ".css") 501 else if (extension == ".css")
502 resheaders["Content-Type"] = "text/css"; 502 resheaders["Content-Type"] = "text/css";
503 else if (extension == ".js") 503 else if (extension == ".js")
547 else { 547 else {
548 reply.t = "d"; 548 reply.t = "d";
549 reply.n = term.nsend++; 549 reply.n = term.nsend++;
550 reply.d = result.toString("hex"); 550 reply.d = result.toString("hex");
551 } 551 }
552 res.writeHead(200, { "Content-Type": "text/plain" }); 552 res.writeHead(200, { "Content-Type": "application/json" });
553 res.write(JSON.stringify(reply)); 553 res.write(JSON.stringify(reply));
554 res.end(); 554 res.end();
555 } 555 }
556 else { 556 else {
557 sendError(res, 1, null); 557 sendError(res, 1, null);
561 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 561 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
562 "Login failed", "Already playing", "Game launch failed", 562 "Login failed", "Already playing", "Game launch failed",
563 "Server shutting down" ]; 563 "Server shutting down" ];
564 564
565 function sendError(res, ecode, msg) { 565 function sendError(res, ecode, msg) {
566 res.writeHead(200, { "Content-Type": "text/plain" }); 566 res.writeHead(200, { "Content-Type": "application/json" });
567 var edict = {"t": "E"}; 567 var edict = {"t": "E"};
568 if (!(ecode < errorcodes.length && ecode > 0)) 568 if (!(ecode < errorcodes.length && ecode > 0))
569 ecode = 0; 569 ecode = 0;
570 edict["c"] = ecode; 570 edict["c"] = ecode;
571 edict["s"] = errorcodes[ecode]; 571 edict["s"] = errorcodes[ecode];