forked from cory/tildefriends
		
	git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3187 ed5197a5-7fde-0310-b194-c3ffbd925b24
		
			
				
	
	
		
			62 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "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;
 |