# HG changeset patch # User John "Elwin" Edwards # Date 1336967413 25200 # Node ID bf7c26d0b66d2c2b7a7d702e92f4df3b5cd8ceb4 # Parent 9e1d83f50c9e5a2bffd9087ab3c13864b10c1462 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. diff -r 9e1d83f50c9e -r bf7c26d0b66d shterm.js --- a/shterm.js Sun May 13 08:47:35 2012 -0700 +++ b/shterm.js Sun May 13 20:50:13 2012 -0700 @@ -2,6 +2,9 @@ * is running a shell via the webtty.js server. */ +/* The number of the next packet to send. */ +var nsend = 0; + // A state machine that keeps track of polling the server. var ajaxstate = { state: 0, @@ -169,10 +172,11 @@ function sendback(str) { /* For responding to terminal queries. */ + var formdata = {"t": "d", "n": nsend++, "d": str}; var datareq = new XMLHttpRequest(); datareq.onreadystatechange = postResponseHandler; datareq.open('POST', '/feed', true); - datareq.send("keys=" + str); + datareq.send(JSON.stringify(formdata)); return; } @@ -214,10 +218,11 @@ } if (termemu.alive) ev.preventDefault(); + var formdata = {"t": "d", "n": nsend++, "d": code}; var datareq = new XMLHttpRequest(); datareq.onreadystatechange = postResponseHandler; datareq.open('POST', '/feed', true); - datareq.send("keys=" + code); + datareq.send(JSON.stringify(formdata)); //dkey(code); return; } @@ -263,10 +268,11 @@ else return; //writeData("Sending " + keystr); + var formdata = {"t": "d", "n": nsend++, "d": code}; var datareq = new XMLHttpRequest(); datareq.onreadystatechange = postResponseHandler; datareq.open('POST', '/feed', true); - datareq.send("keys=" + keystr); + datareq.send(JSON.stringify(formdata)); return; } @@ -300,6 +306,7 @@ function login(h, w) { if (termemu.alive) return; + params = {"login": true, "h": h, "w": w}; var req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { @@ -308,6 +315,7 @@ /* Success */ termemu.resize(logindict.h, logindict.w); termemu.alive = true; + nsend = 0; setTitle("Logged in"); debug(1, "Logged in with id " + logindict.id); getData(); @@ -317,7 +325,8 @@ } }; req.open('POST', '/login', true); - req.send("login=login&h=" + String(h) + "&w=" + String(w)); + req.send(JSON.stringify(params)); + //req.send("login=login&h=" + String(h) + "&w=" + String(w)); return; } @@ -332,7 +341,7 @@ } }; req.open('POST', '/feed', true); - req.send("quit=quit"); + req.send(JSON.stringify({"t": "q", "n": nsend++})); return; } diff -r 9e1d83f50c9e -r bf7c26d0b66d webtty.js --- a/webtty.js Sun May 13 08:47:35 2012 -0700 +++ b/webtty.js Sun May 13 20:50:13 2012 -0700 @@ -144,22 +144,14 @@ /* Returns the contents of a form */ function getFormValues(formtext) { - var pairstrs = formtext.split("&"); - var data = {}; - for (var i = 0; i < pairstrs.length; i++) - { - var eqsign = pairstrs[i].indexOf("="); - if (eqsign > 0) { - rawname = pairstrs[i].slice(0, eqsign); - rawval = pairstrs[i].slice(eqsign + 1); - name = urlDec(rawname); - val = urlDec(rawval); - if (!(name in data)) - data[name] = []; - data[name].push(val); - } + var jsonobj; + try { + jsonobj = JSON.parse(formtext); + } catch (e) { + if (e instanceof SyntaxError) + return null; } - return data; + return jsonobj; } function login(req, res, formdata) { @@ -298,14 +290,14 @@ sendError(res, 1); return; } - if (formdata["quit"] == "quit") { + if (formdata["t"] == "q") { /* The client wants to quit. */ // FIXME need to send a message back to the client cterm.close(); } - else if (formdata["keys"]) { + else if (formdata["t"] == "d" && typeof(formdata["d"]) == "string") { /* process the keys */ - hexstr = formdata["keys"][0].replace(/[^0-9a-f]/gi, ""); + hexstr = formdata["d"].replace(/[^0-9a-f]/gi, ""); if (hexstr.length % 2 != 0) { sendError(res, 2); return;