Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http async replies, upgrade hook #324

Open
wants to merge 3 commits into
base: public
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions modules/network/http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -592,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;
Expand Down
2 changes: 1 addition & 1 deletion modules/network/socket/lin/modSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define kRAW (2)

#define kRxBufferSize 1024
#define kTxBufferSize 1024
#define kTxBufferSize 4096

typedef struct xsSocketRecord xsSocketRecord;
typedef xsSocketRecord *xsSocket;
Expand Down