From 73bbcebddb87db16fc6ba1be856df0fecf671c52 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 15 Jul 2023 01:48:36 +0000 Subject: [PATCH] Brushing off enough dust to be able to initiate HTTP requests from the server. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4345 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/http.js | 60 +++++++++++++++++++++++++++++++++ src/android/AndroidManifest.xml | 2 +- src/tls.c | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 core/http.js diff --git a/core/http.js b/core/http.js new file mode 100644 index 00000000..9bc02e3e --- /dev/null +++ b/core/http.js @@ -0,0 +1,60 @@ +function parseUrl(url) { + // XXX: Hack. + let match = url.match(new RegExp("(\\w+)://([^/]+)?(.*)")); + return { + protocol: match[1], + host: match[2], + path: match[3], + port: match[1] == "http" ? 80 : 443, + }; +} + +function parseResponse(data) { + let firstLine; + let headers = {}; + + while (true) { + let endLine = data.indexOf('\r\n'); + let line = data.substring(0, endLine); + if (!firstLine) { + firstLine = line; + } else if (!line.length) { + break; + } else { + let colon = line.indexOf(":"); + headers[line.substring(colon)] = line.substring(colon + 1); + } + data = data.substring(endLine + 2); + } + return {body: data}; +} + +export function fetch(url, options) { + let parsed = parseUrl(url); + return new Promise(function(resolve, reject) { + let socket = new Socket(); + let buffer = new Uint8Array(0) + + return socket.connect(parsed.host, parsed.port).then(function() { + 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(utf8Decode(buffer))); + } + }); + + if (parsed.port == 443) { + return socket.startTls(); + } + }).then(function() { + socket.write(`${options?.method ?? 'GET'} ${parsed.path} HTTP/1.0\r\nHost: ${parsed.host}\r\nConnection: close\r\n\r\n`); + socket.shutdown(); + }).catch(function(error) { + reject(error); + }); + }); +} diff --git a/src/android/AndroidManifest.xml b/src/android/AndroidManifest.xml index 46361df4..382d93d0 100644 --- a/src/android/AndroidManifest.xml +++ b/src/android/AndroidManifest.xml @@ -2,7 +2,7 @@ + versionName="0.0.9-wip"> diff --git a/src/tls.c b/src/tls.c index 587c2125..4ee4b55a 100644 --- a/src/tls.c +++ b/src/tls.c @@ -154,6 +154,7 @@ void tf_tls_session_start_connect(tf_tls_session_t* session) X509_VERIFY_PARAM* param = SSL_get0_param(session->ssl); X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); X509_VERIFY_PARAM_set1_host(param, session->hostname, 0); + SSL_set_tlsext_host_name(session->ssl, session->hostname); SSL_set_bio(session->ssl, session->bio_in, session->bio_out); SSL_connect(session->ssl); tf_tls_session_handshake(session);