Mercurial > hg > rlgwebd
comparison webtty.js @ 13:bf7c26d0b66d
webtty: switch upward protocol to JSON
Switch the client-to-server messages from the HTML forms format to
JSON (for the webtty app). Message numbers are sent but not yet used.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Sun, 13 May 2012 20:50:13 -0700 |
parents | 481dcee353c9 |
children | 7466927c17a5 |
comparison
equal
deleted
inserted
replaced
12:9e1d83f50c9e | 13:bf7c26d0b66d |
---|---|
142 return decstr; | 142 return decstr; |
143 } | 143 } |
144 | 144 |
145 /* Returns the contents of a form */ | 145 /* Returns the contents of a form */ |
146 function getFormValues(formtext) { | 146 function getFormValues(formtext) { |
147 var pairstrs = formtext.split("&"); | 147 var jsonobj; |
148 var data = {}; | 148 try { |
149 for (var i = 0; i < pairstrs.length; i++) | 149 jsonobj = JSON.parse(formtext); |
150 { | 150 } catch (e) { |
151 var eqsign = pairstrs[i].indexOf("="); | 151 if (e instanceof SyntaxError) |
152 if (eqsign > 0) { | 152 return null; |
153 rawname = pairstrs[i].slice(0, eqsign); | 153 } |
154 rawval = pairstrs[i].slice(eqsign + 1); | 154 return jsonobj; |
155 name = urlDec(rawname); | |
156 val = urlDec(rawval); | |
157 if (!(name in data)) | |
158 data[name] = []; | |
159 data[name].push(val); | |
160 } | |
161 } | |
162 return data; | |
163 } | 155 } |
164 | 156 |
165 function login(req, res, formdata) { | 157 function login(req, res, formdata) { |
166 var resheaders = {'Content-Type': 'text/plain'}; | 158 var resheaders = {'Content-Type': 'text/plain'}; |
167 var sessid = randkey(); | 159 var sessid = randkey(); |
296 if (target == '/feed') { | 288 if (target == '/feed') { |
297 if (!cterm) { | 289 if (!cterm) { |
298 sendError(res, 1); | 290 sendError(res, 1); |
299 return; | 291 return; |
300 } | 292 } |
301 if (formdata["quit"] == "quit") { | 293 if (formdata["t"] == "q") { |
302 /* The client wants to quit. */ | 294 /* The client wants to quit. */ |
303 // FIXME need to send a message back to the client | 295 // FIXME need to send a message back to the client |
304 cterm.close(); | 296 cterm.close(); |
305 } | 297 } |
306 else if (formdata["keys"]) { | 298 else if (formdata["t"] == "d" && typeof(formdata["d"]) == "string") { |
307 /* process the keys */ | 299 /* process the keys */ |
308 hexstr = formdata["keys"][0].replace(/[^0-9a-f]/gi, ""); | 300 hexstr = formdata["d"].replace(/[^0-9a-f]/gi, ""); |
309 if (hexstr.length % 2 != 0) { | 301 if (hexstr.length % 2 != 0) { |
310 sendError(res, 2); | 302 sendError(res, 2); |
311 return; | 303 return; |
312 } | 304 } |
313 keybuf = new Buffer(hexstr, "hex"); | 305 keybuf = new Buffer(hexstr, "hex"); |