From 0a6c7e59e6ebd40e17e6df90790494f262553850 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 1 Jan 2020 20:39:34 -0600 Subject: [PATCH 1/3] http: some support for http upgrade, e.g. websockets If the headersComplete callback returns 'upgrade', return from the server callback immediately rather than continuing to the prepareResponse callback. --- modules/network/http/http.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/network/http/http.js b/modules/network/http/http.js index f2a68c1deb..e00e72493e 100644 --- a/modules/network/http/http.js +++ b/modules/network/http/http.js @@ -490,6 +490,10 @@ function server(message, value, etc) { delete this.line; let request = this.callback(Server.headersComplete); // headers complete... let's see what to do with the request body + if ('upgrade' === request) { + return; + } + if (false === request) delete this.total; // ignore request body and just send response From 6d143e44b74b3622526dc266cfd6f518897bb7fa Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 3 Jan 2020 11:35:04 -0600 Subject: [PATCH 2/3] http: support async replies --- modules/network/http/http.js | 85 ++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/modules/network/http/http.js b/modules/network/http/http.js index e00e72493e..337a52bd8e 100644 --- a/modules/network/http/http.js +++ b/modules/network/http/http.js @@ -596,53 +596,54 @@ function server(message, value, etc) { let first; if (7 === this.state) { - let response = this.callback(Server.prepareResponse); // prepare response - - let parts = []; - let status = (!response || (undefined === response.status)) ? 200 : response.status; - let message = (!response || (undefined === response.reason)) ? reason(status) : response.reason.toString(); - parts.push("HTTP/1.1 ", status.toString(), " ", message, "\r\n", - "connection: ", "close\r\n"); - - if (response) { - let byteLength; - - for (let i = 0, headers = response.headers; headers && (i < headers.length); i += 2) { - parts.push(headers[i], ": ", headers[i + 1].toString(), "\r\n"); - if ("content-length" == headers[i].toLowerCase()) - byteLength = parseInt(headers[i + 1]); - } + let responseP = Promise.resolve(this.callback(Server.prepareResponse)); // prepare response + responseP.then(response => { + let parts = []; + let status = (!response || (undefined === response.status)) ? 200 : response.status; + let message = (!response || (undefined === response.reason)) ? reason(status) : response.reason.toString(); + parts.push("HTTP/1.1 ", status.toString(), " ", message, "\r\n", + "connection: ", "close\r\n"); + + if (response) { + let byteLength; + + for (let i = 0, headers = response.headers; headers && (i < headers.length); i += 2) { + parts.push(headers[i], ": ", headers[i + 1].toString(), "\r\n"); + if ("content-length" == headers[i].toLowerCase()) + byteLength = parseInt(headers[i + 1]); + } - this.body = response.body; - if (true === response.body) { - if (undefined === byteLength) { - this.flags = 2; - parts.push("transfer-encoding: chunked\r\n"); + this.body = response.body; + if (true === response.body) { + if (undefined === byteLength) { + this.flags = 2; + parts.push("transfer-encoding: chunked\r\n"); + } + else + this.flags = 4; + } + else { + this.flags = 1; + let count = 0; + if (this.body) + count = ("string" === typeof this.body) ? this.body.length : this.body.byteLength; //@@ utf-8 hell + parts.push("content-length: ", count.toString(), "\r\n"); } - else - this.flags = 4; - } - else { - this.flags = 1; - let count = 0; - if (this.body) - count = ("string" === typeof this.body) ? this.body.length : this.body.byteLength; //@@ utf-8 hell - parts.push("content-length: ", count.toString(), "\r\n"); } - } - else - parts.push("content-length: 0\r\n"); - parts.push("\r\n"); - socket.write.apply(socket, parts); + else + parts.push("content-length: 0\r\n"); + parts.push("\r\n"); + socket.write.apply(socket, parts); - this.state = 8; - first = true; + this.state = 8; + first = true; - if (this.body && (true !== this.body)) { - let count = ("string" === typeof this.body) ? this.body.length : this.body.byteLength; - if (count > (socket.write() - ((2 & this.flags) ? 8 : 0))) - return; - } + if (this.body && (true !== this.body)) { + let count = ("string" === typeof this.body) ? this.body.length : this.body.byteLength; + if (count > (socket.write() - ((2 & this.flags) ? 8 : 0))) + return; + } + }); } if (8 === this.state) { let body = this.body; From 9ec77c2c6988e616253341389d63649bd1fbddfe Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Thu, 16 Jan 2020 01:32:09 -0600 Subject: [PATCH 3/3] websockets work-around: socket tx buffer 1k -> 4k websockets.js doesn't check socket.write() to see how much data it can send. The agoric pixel demo goes over 1k, so let's use 4k for now. --- modules/network/socket/lin/modSocket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/network/socket/lin/modSocket.c b/modules/network/socket/lin/modSocket.c index c199e4725b..08550fcd4c 100644 --- a/modules/network/socket/lin/modSocket.c +++ b/modules/network/socket/lin/modSocket.c @@ -43,7 +43,7 @@ #define kRAW (2) #define kRxBufferSize 1024 -#define kTxBufferSize 1024 +#define kTxBufferSize 4096 typedef struct xsSocketRecord xsSocketRecord; typedef xsSocketRecord *xsSocket;