Added support for Google Sign-In, optional in every way.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3187 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2016-04-03 19:31:03 +00:00
parent 7b112c5376
commit cbf54eaa17
9 changed files with 199 additions and 18 deletions

61
core/http.js Normal file
View File

@ -0,0 +1,61 @@
"use strict";
function parseUrl(url) {
// XXX: Hack.
var 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) {
var firstLine;
var headers = {};
while (true) {
var endLine = data.indexOf("\r\n");
var line = data.substring(0, endLine);
if (!firstLine) {
firstLine = line;
} else if (!line.length) {
break;
} else {
var colon = line.indexOf(":");
headers[line.substring(colon)] = line.substring(colon + 1);
}
data = data.substring(endLine + 2);
}
return {body: data};
}
function get(url) {
var parsed = parseUrl(url);
return new Promise(function(resolve, reject) {
var socket = new Socket();
var buffer = "";
return socket.connect(parsed.host, parsed.port).then(function() {
socket.read(function(data) {
if (data) {
buffer += data;
} else {
resolve(parseResponse(buffer));
}
});
if (parsed.port == 443) {
return socket.startTls();
}
}).then(function() {
socket.write(`GET ${parsed.path} HTTP/1.0\r\nHost: ${parsed.host}\r\nConnection: close\r\n\r\n`);
socket.shutdown();
}).catch(function(error) {
reject(error);
});
});
}
exports.get = get;