Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 5 additions & 57 deletions lib/httplib.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,12 @@ exports.proxy = function proxy(params, callback) {

params.redirect_urls = params.redirect_urls || [];

/*
// apparently url.parse is useless.
//urlParts = url.parse(params.url);
var urlSearch = (/^(?:([a-z][a-z0-9]{1,6}\:)\/\/([^\:\@\/\?\#]+(?:\:[^\:\@\/\?\#]+)?\@)?([^\:\@\/\?\#]+))?([^\#\?]+)?(\?[^\#]*)?(\#.*)?$/gim).exec(params.url);
//var urlSearch = params.url.match(/^([a-z0-9]{2,6}\:)\/\/([^\/\?\#]+)(\/[^\#\?]+)?(\?[^\#]*)?(\#.*)?/gim);
//var urlSearch = (/^([a-z0-9]{2,6}\:)(\/\/)/gim).exec(params.url);
urlParts = {
protocol : urlSearch[1] || '',
auth : urlSearch[2] || '',
hostname : urlSearch[3] || '',
pathname : urlSearch[4] || '',
search : urlSearch[5] || '',
hash : urlSearch[6] || ''
};
urlParts.host = urlParts.auth + urlParts.hostname;
*/

urlParts = require('url').parse(params.url);

function prop(obj, key) {
return obj[key] || '';
}

/*
callback({
url : params.url,
urlSearch : Array.prototype.slice.call(urlSearch, 0, urlSearch.length),
urlParts : JSON.stringify(urlParts)
});
return true;
*/

method = params.method && params.method.toUpperCase() || "GET";

resolved_url = urlParts.protocol + '//' + prop(urlParts, 'auth') + prop(urlParts, 'host') + prop(urlParts, 'pathname') + prop(urlParts, 'search') + prop(urlParts, 'hash');
Expand Down Expand Up @@ -93,21 +67,6 @@ exports.proxy = function proxy(params, callback) {
urlParts.pathname = "/" + urlParts.pathname;
}

/*
if (urlParts.protocol==='https') {
client = https.createClient(urlParts.port || 443, urlParts.hostname);
}
else {
client = http.createClient(urlParts.port || 80, urlParts.hostname);
}

request = client.request(
method,
(urlParts.pathname || "") + (urlParts.search || "") + (urlParts.hash || ""),
requestHeaders
);
*/

request = ( urlParts.protocol==='https:' ? https : http ).request({
hostname : urlParts.hostname,
port : urlParts.port || (urlParts.protocol==='https:' ? 443 : 80),
Expand All @@ -124,12 +83,11 @@ exports.proxy = function proxy(params, callback) {
// automatically follow location redirects
if (response.headers && response.headers['location']) {
if (redirectCount<20) {
//response.end(); // there is no response.end, so just let the request die :(
//response.end(); // @todo: kill response

var redirUrl = response.headers['location'];
//sys.puts("Redirect #"+redirectCount+" = " + redirUrl);

// I guess we can be lenient with relative Location: redirects... but it's still wrong!

// relative Location redirects (bad)
if (!redirUrl.match(/^[a-z0-9]{2,9}\:\/\//gim)) {
if (redirUrl.substring(0,1)!=="/") {
redirUrl = urlParts.pathname.replace(/\/[^\/]*?$/gim,'/') + redirUrl;
Expand All @@ -139,7 +97,7 @@ exports.proxy = function proxy(params, callback) {

params.redirect_urls.push(redirUrl);

// don't forward the request body (or length). The spec doesn't allow for rediction of POST/PUT/etc.
// Don't forward request body. Only allow redirection of GET
if (!params.headers) {
params.headers = {};
}
Expand All @@ -157,8 +115,6 @@ exports.proxy = function proxy(params, callback) {
cookieStr += cookies[x].substring(0, cookies[x].indexOf(';')>-1?cookies[x].indexOf(';'):cookies[x].length) + "; ";
}
cookieStr = cookieStr.replace(/\;\s$/gim,'');
//sys.puts("URL :: " + redirUrl + "\r\nCOOKIE :: " + cookieStr);
//console.log(cookieStr);
params.headers['cookie'] = cookieStr;
}

Expand Down Expand Up @@ -201,18 +157,10 @@ exports.proxy = function proxy(params, callback) {
url : params.url,
proxyCookies : params.proxyCookies
};

var bodyBuffers = [];
//response.setEncoding(params.encoding || 'utf8');
//response.setEncoding('hex');
response.on('data', function(chunk) {
bodyBuffers.push(chunk);
//bodyBuffer.write(chunk.toString('base64'), buflen, );
//bodyBuffer = Buffer.concat([bodyBuffer, chunk]);
//try {
// clientResponse.body += chunk;
//} catch(err) {
// clientResponse.parseError = true;
//}
});
response.on('end', function() {
clientResponse.body = Buffer.concat(bodyBuffers).toString(params.encoding || 'utf8');
Expand Down