From d75f276b86ef6b4cb07bdec2f3a965077429578f Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Fri, 23 Dec 2016 22:28:33 +0000 Subject: [PATCH] Some http client fixes. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3366 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- packages/cory/libhttp/libhttp.js | 58 +++++++++++++++------------- packages/cory/libunfurl/libunfurl.js | 2 +- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/cory/libhttp/libhttp.js b/packages/cory/libhttp/libhttp.js index 7c17bd83..12afec2d 100644 --- a/packages/cory/libhttp/libhttp.js +++ b/packages/cory/libhttp/libhttp.js @@ -1,6 +1,11 @@ "use strict"; -//! {"permissions": ["network"]} +//! { +//! "permissions": ["network"], +//! "require": ["libencoding"] +//! } + +require("libencoding"); function parseUrl(url) { // XXX: Hack. @@ -37,33 +42,32 @@ function parseResponse(data) { function get(url) { return new Promise(async function(resolve, reject) { - try { - let parsed = parseUrl(url); - if (!parsed) { - throw new Error("Failed to parse: " + url); - } - let buffer = ""; - - let socket = await network.newConnection(); - - await socket.connect(parsed.host, parsed.port); - socket.read(function(data) { - if (data) { - buffer += data; - } else { - resolve(parseResponse(buffer)); - } - }); - - if (parsed.port == 443) { - await socket.startTls(); - } - - socket.write(`GET ${parsed.path} HTTP/1.0\r\nHost: ${parsed.host}\r\nConnection: close\r\n\r\n`); - //socket.close(); - } catch(error) { - reject(error); + let parsed = parseUrl(url); + if (!parsed) { + throw new Error("Failed to parse: " + url); } + let buffer = new Uint8Array(0); + + let socket = await network.newConnection(); + + await socket.connect(parsed.host, parsed.port); + socket.read(function(data) { + if (data) { + let newBuffer = new Uint8Array(buffer.length + data.length); + newBuffer.set(buffer, 0); + newBuffer.set(data, buffer.length); + buffer = newBuffer; + } else { + resolve(parseResponse(new TextDecoder("UTF-8").decode(buffer))); + socket.close(); + } + }); + + if (parsed.port == 443) { + await socket.startTls(); + } + + socket.write(`GET ${parsed.path} HTTP/1.0\r\nHost: ${parsed.host}\r\nConnection: close\r\n\r\n`); }); } diff --git a/packages/cory/libunfurl/libunfurl.js b/packages/cory/libunfurl/libunfurl.js index b0b9f898..9d2dee2b 100644 --- a/packages/cory/libunfurl/libunfurl.js +++ b/packages/cory/libunfurl/libunfurl.js @@ -1,6 +1,6 @@ "use strict"; -//! {"category": "libraries", "require": ["libhttp", "libxml"], "permissions": ["network"]} +//! {"category": "libraries", "require": ["libencoding", "libhttp", "libxml"], "permissions": ["network"]} let libxml = require("libxml"); let libhttp = require("libhttp");