var http = require('http');
var url = require('url');
var config = require('../../lib/config');
var util = require('../../../../util');
var HTTPS_FLAG = require('../../../../package.json').whistleSsl + '.';

function parseHeaders(headers) {
	if (!headers || typeof headers != 'string') {
		return {};
	}
	
	try {
		return util.lowerCaseify(JSON.parse(headers));
	} catch(e) {}
	
	var _headers = {};
	headers.split(/\n|\r\n|\r/g)
		.forEach(function(line) {
			line = line.trim().split(/\s*:\s*/);
			if (line[0] && line[1]) {
				_headers[line[0]] = line[1];
			}
		});
	
	return util.lowerCaseify(_headers);
}

module.exports = function(req, res) {
	var _url = req.body.url;
	
	if (_url && typeof _url == 'string') {
		_url = _url.replace(/#.*$/, '');
		var options = url.parse(util.setProtocol(_url));
		var headers = parseHeaders(req.body.headers);
		if (!headers['user-agent']) {
			headers['user-agent'] = 'whistle/' + config.version;
		}
		
		if (!headers.host) {
			headers.host = options.hostname;
		}
		
		if (options.protocol == 'https:') {
			headers[util.HTTPS_FIELD] = 1;
		}
		
		options.protocol = null;
		options.hostname = null;
		options.method = req.body.method;
		options.host = '127.0.0.1';
		options.port = config.port;
		options.headers = headers;
		http.request(options, function(res) {
			res.on('error', util.noop);
			util.drain(res);
		})
		.on('error', util.noop)
		.end(req.body.body);
	}
	
	
	res.json({ec: 0, em: 'success'});
};
@@ -950,70 +950,141 @@
 if (
-!headers.host) %7B%0A%09%09%09headers.host = options.hostname
+options.protocol == 'https:') %7B%0A%09%09%09headers%5Butil.HTTPS_FIELD%5D = 1;%0A%09%09%09if (options.port == 443) %7B%0A%09%09%09%09options.port = ''
 ;%0A%09%09
+%09
 %7D%0A%09%09
-%0A%09%09
+%7D else 
 if (
@@ -1096,72 +1096,150 @@
 ns.p
-rotocol == 'https:') %7B%0A%09%09%09headers%5Butil.HTTPS_FIELD%5D = 1
+ort == 80) %7B%0A%09%09%09options.port = '';%0A%09%09%7D%0A%09%09%0A%09%09headers.host = options.hostname;%0A%09%09if (options.port) %7B%0A%09%09%09headers.host += ':' + options.port
 ;%0A%09%09%7D%0A
-%09%09%0A
 %09%09op

remove host name