Mercurial > hg > rlgwebd
comparison webtty.js @ 11:481dcee353c9
webtty: switch all server responses to JSON.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Fri, 11 May 2012 13:33:48 -0700 |
parents | d051aad3e95f |
children | bf7c26d0b66d |
comparison
equal
deleted
inserted
replaced
10:d051aad3e95f | 11:481dcee353c9 |
---|---|
37 var ss = this; | 37 var ss = this; |
38 /* Eventually we'll need to make sure the sessid isn't in use yet. */ | 38 /* Eventually we'll need to make sure the sessid isn't in use yet. */ |
39 this.sessid = sessid; | 39 this.sessid = sessid; |
40 this.alive = true; | 40 this.alive = true; |
41 this.data = []; | 41 this.data = []; |
42 this.nsend = 0; | |
42 this.child.stdout.on("data", function (buf) { | 43 this.child.stdout.on("data", function (buf) { |
43 ss.data.push(buf); | 44 ss.data.push(buf); |
44 }); | 45 }); |
45 this.child.stderr.on("data", function (buf) { | 46 this.child.stderr.on("data", function (buf) { |
46 ss.data.push(buf); | 47 ss.data.push(buf); |
242 } | 243 } |
243 | 244 |
244 function readFeed(res, term) { | 245 function readFeed(res, term) { |
245 res.writeHead(200, { "Content-Type": "text/plain" }); | 246 res.writeHead(200, { "Content-Type": "text/plain" }); |
246 if (term) { | 247 if (term) { |
248 var answer = {}; | |
247 var result = term.read(); | 249 var result = term.read(); |
248 if (result == null) | 250 if (result == null) { |
249 resultstr = ""; | 251 answer["t"] = "n"; |
250 else | 252 } |
251 resultstr = result.toString("hex"); | 253 else { |
252 res.write("d" + resultstr.length.toString() + "\n" + resultstr + "\n"); | 254 answer["t"] = "d"; |
255 answer["d"] = result.toString("hex"); | |
256 answer["n"] = term.nsend++; | |
257 } | |
258 res.write(JSON.stringify(answer)); | |
259 res.end(); | |
253 } | 260 } |
254 else { | 261 else { |
255 //console.log("Where's the term?"); | 262 sendError(res, 1); |
256 res.write("d0\n\n"); | |
257 } | 263 } |
258 } | 264 } |
259 | 265 |
260 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ]; | 266 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ]; |
261 | 267 |
262 function sendError(res, ecode) { | 268 function sendError(res, ecode) { |
263 res.writeHead(200, { "Content-Type": "text/plain" }); | 269 res.writeHead(200, { "Content-Type": "text/plain" }); |
264 if (ecode < errorcodes.length && ecode > 0) | 270 if (!(ecode >= 0 && ecode < errorcodes.length)) |
265 res.write("E" + ecode + '\n' + errorcodes[ecode] + '\n'); | 271 ecode = 0; |
266 else | 272 res.write(JSON.stringify({"t": "E", "c": ecode, "s": errorcodes[ecode]})); |
267 res.write("E0\nGeneric Error\n"); | |
268 res.end(); | 273 res.end(); |
269 } | 274 } |
270 | 275 |
271 function handler(req, res) { | 276 function handler(req, res) { |
272 /* default headers for the response */ | 277 /* default headers for the response */ |
307 } | 312 } |
308 keybuf = new Buffer(hexstr, "hex"); | 313 keybuf = new Buffer(hexstr, "hex"); |
309 cterm.write(keybuf); | 314 cterm.write(keybuf); |
310 } | 315 } |
311 readFeed(res, cterm); | 316 readFeed(res, cterm); |
312 res.end(); | |
313 } | 317 } |
314 else if (target == "/login") { | 318 else if (target == "/login") { |
315 login(req, res, formdata); | 319 login(req, res, formdata); |
316 } | 320 } |
317 else { | 321 else { |
324 if (!cterm) { | 328 if (!cterm) { |
325 sendError(res, 1); | 329 sendError(res, 1); |
326 return; | 330 return; |
327 } | 331 } |
328 readFeed(res, cterm); | 332 readFeed(res, cterm); |
329 res.end(); | |
330 } | 333 } |
331 /* Default page, create a new term */ | 334 /* Default page, create a new term */ |
332 /* FIXME New term not created anymore, is a special case still needed? */ | 335 /* FIXME New term not created anymore, is a special case still needed? */ |
333 else if (target == '/') { | 336 else if (target == '/') { |
334 serveStatic(req, res, "/"); | 337 serveStatic(req, res, "/"); |