Some http client fixes.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3366 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
7e85e9fd11
commit
d75f276b86
@ -1,6 +1,11 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
//! {"permissions": ["network"]}
|
//! {
|
||||||
|
//! "permissions": ["network"],
|
||||||
|
//! "require": ["libencoding"]
|
||||||
|
//! }
|
||||||
|
|
||||||
|
require("libencoding");
|
||||||
|
|
||||||
function parseUrl(url) {
|
function parseUrl(url) {
|
||||||
// XXX: Hack.
|
// XXX: Hack.
|
||||||
@ -37,21 +42,24 @@ function parseResponse(data) {
|
|||||||
|
|
||||||
function get(url) {
|
function get(url) {
|
||||||
return new Promise(async function(resolve, reject) {
|
return new Promise(async function(resolve, reject) {
|
||||||
try {
|
|
||||||
let parsed = parseUrl(url);
|
let parsed = parseUrl(url);
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
throw new Error("Failed to parse: " + url);
|
throw new Error("Failed to parse: " + url);
|
||||||
}
|
}
|
||||||
let buffer = "";
|
let buffer = new Uint8Array(0);
|
||||||
|
|
||||||
let socket = await network.newConnection();
|
let socket = await network.newConnection();
|
||||||
|
|
||||||
await socket.connect(parsed.host, parsed.port);
|
await socket.connect(parsed.host, parsed.port);
|
||||||
socket.read(function(data) {
|
socket.read(function(data) {
|
||||||
if (data) {
|
if (data) {
|
||||||
buffer += data;
|
let newBuffer = new Uint8Array(buffer.length + data.length);
|
||||||
|
newBuffer.set(buffer, 0);
|
||||||
|
newBuffer.set(data, buffer.length);
|
||||||
|
buffer = newBuffer;
|
||||||
} else {
|
} else {
|
||||||
resolve(parseResponse(buffer));
|
resolve(parseResponse(new TextDecoder("UTF-8").decode(buffer)));
|
||||||
|
socket.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -60,10 +68,6 @@ function get(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socket.write(`GET ${parsed.path} HTTP/1.0\r\nHost: ${parsed.host}\r\nConnection: close\r\n\r\n`);
|
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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
//! {"category": "libraries", "require": ["libhttp", "libxml"], "permissions": ["network"]}
|
//! {"category": "libraries", "require": ["libencoding", "libhttp", "libxml"], "permissions": ["network"]}
|
||||||
|
|
||||||
let libxml = require("libxml");
|
let libxml = require("libxml");
|
||||||
let libhttp = require("libhttp");
|
let libhttp = require("libhttp");
|
||||||
|
Loading…
Reference in New Issue
Block a user