annotate webtty.js @ 58:7a50b4412fea

Move credentials into the drivers. The session id, or whether the session is currently active on the server, are better handled in the drivers than in termemu.js.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 19 Jun 2012 13:40:30 -0700
parents 7466927c17a5
children e4773ac5d4d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2 var http = require('http');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var fs = require('fs');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var child_process = require("child_process");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 var serveStaticRoot = "/home/elwin/hk/nodejs/rlg/s/";
7
d1b3c3af34d6 WebTTY: switch terminal size to 80x25.
John "Elwin" Edwards <elwin@sdf.org>
parents: 3
diff changeset
9 var ptyhelp = "/home/elwin/hk/nodejs/rlg/ptyhelper";
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 var sessions = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
12 var env_dontuse = {"TMUX": true, "TMUX_PANE": true};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
13
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 /* Constructor for TermSessions. Note that it opens the terminal and
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 * adds itself to the sessions dict.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 */
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
17 function TermSession(sessid, h, w) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
18 /* Set up the sizes. */
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
19 w = Math.floor(Number(w));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
20 if (!(w > 0 && w < 256))
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
21 w = 80;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
22 this.w = w;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
23 h = Math.floor(Number(h));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
24 if (!(h > 0 && h < 256))
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
25 h = 25;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
26 this.h = h;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
27 /* Customize the environment. */
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
28 var childenv = {};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
29 for (var key in process.env) {
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
30 if (!(key in env_dontuse))
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
31 childenv[key] = process.env[key];
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
32 }
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
33 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
34 // Should setsid get set?
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
35 var spawnopts = {"env": childenv, "cwd": process.env["HOME"]};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
36 this.child = child_process.spawn(ptyhelp, ["bash"], spawnopts);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 var ss = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 /* Eventually we'll need to make sure the sessid isn't in use yet. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 this.sessid = sessid;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 this.alive = true;
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
41 this.data = []; // Buffer for the process' output.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
42 this.nsend = 0; // Number to use for the next message sent.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
43 this.nrecv = 0; // Number expected on the next message received.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
44 this.msgQ = []; // Queue for messages that arrived out of order.
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 this.child.stdout.on("data", function (buf) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
46 ss.data.push(buf);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 this.child.stderr.on("data", function (buf) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 ss.data.push(buf);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51 this.child.on("exit", function (code, signal) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 ss.exitcode = (code != null ? code : 255);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 ss.alive = false;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 /* Wait for all the data to get collected */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 setTimeout(ss.cleanup, 1000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 });
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
57 this.write = function (data, n) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
58 if (!this.alive) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
59 /* Throw some kind of exception? */
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
60 return;
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
61 }
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
62 if (n !== this.nrecv) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
63 console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n);
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
64 }
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
65 this.nrecv = n + 1;
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
66 this.child.stdin.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
67 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 this.read = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 if (this.data.length == 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 var pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72 var i = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
73 for (i = 0; i < this.data.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
74 pos += this.data[i].length;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
75 var nbuf = new Buffer(pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
76 var tptr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
78 while (this.data.length > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 tptr = this.data.shift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
80 tptr.copy(nbuf, pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
81 pos += tptr.length;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
82 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83 return nbuf;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
84 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
85 this.close = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
86 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 this.child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
89 this.cleanup = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
90 /* Call this when the child is dead. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
91 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
92 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 /* Give the client a chance to read any leftover data. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
94 if (ss.data.length > 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95 setTimeout(ss.remove, 8000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 ss.remove();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
98 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 this.remove = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 delete sessions[ss.sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 console.log("Session " + this.sessid + " removed.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 sessions[sessid] = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 function randkey() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 rnum = Math.floor(Math.random() * 65536 * 65536);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 hexstr = rnum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 while (hexstr.length < 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 hexstr = "0" + hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 return hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
126 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
127 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
128 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
130 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 function urlDec(encstr) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 var decstr = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 var tnum;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
136 for (var i = 0; i < encstr.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
138 if (encstr.charAt(i) == "+")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 decstr += " ";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
140 else if (encstr.charAt(i) == "%")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 tnum = Number("0x" + encstr.slice(i + 1, 2));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 if (!isNaN(tnum) && tnum >= 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 decstr += String.fromCharCode(tnum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 i += 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 decstr += encstr.charAt(i);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 return decstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 /* Returns the contents of a form */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
154 function getFormValues(formtext) {
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
155 var jsonobj;
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
156 try {
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
157 jsonobj = JSON.parse(formtext);
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
158 } catch (e) {
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
159 if (e instanceof SyntaxError)
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
160 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
162 return jsonobj;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
165 function login(req, res, formdata) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
166 var resheaders = {'Content-Type': 'text/plain'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167 var sessid = randkey();
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
168 /* The TermSession constructor will check these thoroughly too, but
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
169 * you can't be too suspicious of client-supplied data. */
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
170 var w = 80;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
171 var h = 25;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
172 var t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
173 if ("w" in formdata) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
174 t = Math.floor(Number(formdata["w"]));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
175 if (t > 0 && t < 256)
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
176 w = t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
177 }
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
178 if ("h" in formdata) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
179 t = Math.floor(Number(formdata["h"]));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
180 if (t > 0 && t < 256)
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
181 h = t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
182 }
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
183 var nsession = new TermSession(sessid, h, w);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
184 resheaders["Set-Cookie"] = "ID=" + sessid;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
185 res.writeHead(200, resheaders);
10
d051aad3e95f webtty: begin experimenting with JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
186 var logindict = {"login": true, "id": sessid, "w": w, "h": h};
d051aad3e95f webtty: begin experimenting with JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
187 res.write(JSON.stringify(logindict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
188 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 console.log("Started new session with key " + sessid + ", pid " + nsession.child.pid);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
190 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
191 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
192
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
193 function findTermSession(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
194 var cookies = getCookies(req);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
195 if ("id" in cookies) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
196 var sessid = cookies["id"];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
197 if (sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
198 return sessions[sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
199 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
200 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
201 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
202 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
203
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
204 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
205 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
206 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
207 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
208 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
209 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
211 var extension = path.extname(realname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
212 path.exists(realname, function (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 if (!exists || !extension || extension == ".html")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
215 resheaders["Content-Type"] = "text/html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
216 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
217 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
218 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
219 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
220 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
221 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
222 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
223 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
224 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
226 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
227 /* Not nice, not sensible. First see if it's readable, then respond
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
228 * 200 or 500. Don't throw nasty errors. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
229 res.writeHead(200, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
230 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
231 if (error) throw error;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
232 res.write(data);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
233 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
234 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
235 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
236 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
237 res.writeHead(404, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
238 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" + nname + " Not Found</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
239 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
240 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
241 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
242 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
243 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
244
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
245 function readFeed(res, term) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
246 res.writeHead(200, { "Content-Type": "text/plain" });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
247 if (term) {
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
248 var answer = {};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
249 var result = term.read();
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
250 if (result == null) {
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
251 answer["t"] = "n";
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
252 }
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
253 else {
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
254 answer["t"] = "d";
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
255 answer["d"] = result.toString("hex");
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
256 answer["n"] = term.nsend++;
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
257 }
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
258 res.write(JSON.stringify(answer));
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
259 res.end();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
260 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
261 else {
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
262 sendError(res, 1);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 function sendError(res, ecode) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269 res.writeHead(200, { "Content-Type": "text/plain" });
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
270 if (!(ecode >= 0 && ecode < errorcodes.length))
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
271 ecode = 0;
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
272 res.write(JSON.stringify({"t": "E", "c": ecode, "s": errorcodes[ecode]}));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
273 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
276 function handler(req, res) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
277 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
283 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
284 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
285 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
286 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
287 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
289 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 function respond() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
291 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
292 var cterm = findTermSession(req);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
294 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 formdata = getFormValues(reqbody);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
296 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
297 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 sendError(res, 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
299 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
300 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
301 if (formdata["t"] == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
302 /* The client wants to quit. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 // FIXME need to send a message back to the client
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
304 cterm.close();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
305 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
306 else if (formdata["t"] == "d" && typeof(formdata["d"]) == "string") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 /* process the keys */
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
308 hexstr = formdata["d"].replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 if (hexstr.length % 2 != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 sendError(res, 2);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 keybuf = new Buffer(hexstr, "hex");
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
314 cterm.write(keybuf, formdata["n"]);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
316 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
317 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
318 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
319 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
320 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
321 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
322 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
323 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
324 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
325 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
326 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
327 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
328 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
329 sendError(res, 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
330 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
331 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
332 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
333 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
334 /* Default page, create a new term */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
335 /* FIXME New term not created anymore, is a special case still needed? */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
336 else if (target == '/') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 serveStatic(req, res, "/");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
338 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
339 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
343 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
344 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
345 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
346 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
347 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
348 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
349 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
352
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354 for (var sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
355 if (sessions[sessid].alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 sessions[sessid].child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
357 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 console.log("Quitting...");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
359 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
362 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 http.createServer(handler).listen(8080, "127.0.0.1");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 console.log('Server running at http://127.0.0.1:8080/');