paypro: use an http proxy to handle self-signed certs - probably not a good idea.

This commit is contained in:
Christopher Jeffrey 2014-08-04 10:11:12 -07:00 committed by Manuel Araoz
commit abf71a81ff
3 changed files with 176 additions and 50 deletions

View file

@ -1600,61 +1600,119 @@ G.$http = G.$http || function $http(options, callback) {
var req = options;
req.headers = req.headers || {};
req.body = req.body || {};
req.body = req.body || req.data || {};
if (typeof XMLHttpRequest !== 'undefined') {
var xhr = new XMLHttpRequest();
xhr.open(method, uri, true);
var xhr = new XMLHttpRequest();
xhr.open(method, uri, true);
Object.keys(options.headers).forEach(function(key) {
var val = options.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
Object.keys(req.headers).forEach(function(key) {
var val = req.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
// For older browsers (binary data):
// xhr.overrideMimeType('text/plain; charset=x-user-defined');
// For older browsers (binary data):
// xhr.overrideMimeType('text/plain; charset=x-user-defined');
// Newer browsers (binary data):
// xhr.responseType = 'arraybuffer';
// Newer browsers (binary data):
// xhr.responseType = 'arraybuffer';
if (options.responseType) {
xhr.responseType = options.responseType;
}
// xhr.onreadystatechange = function() {
// if (xhr.readyState == 4) {
// ;
// }
// };
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};
xhr.onerror = function(event) {
return ret._error(null, new Error(event.message), null, options);
};
if (options.data || options.body) {
xhr.send(options.data || options.body);
} else {
xhr.send(null);
}
return ret;
if (req.responseType) {
xhr.responseType = req.responseType;
}
// xhr.onreadystatechange = function() {
// if (xhr.readyState == 4) {
// ;
// }
// };
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, req);
};
xhr.onerror = function(event) {
return ret._error(null, new Error(event.message), null, req);
};
if (req.body) {
xhr.send(req.body);
} else {
xhr.send(null);
}
return ret;
return ret;
};
G.$http = G.$http || function $http(options, callback) {
if (typeof options === 'string') {
options = { uri: options };
}
var ret = {
success: function(cb) {
this._success = cb;
return this;
},
error: function(cb) {
this._error = cb;
return this;
},
_success: function() {
;
},
_error: function(_, err) {
throw err;
}
};
var xhr = new XMLHttpRequest();
xhr.open('POST', '/_request', true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.responseType = 'arraybuffer';
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, req);
};
xhr.onerror = function(event) {
return ret._error(null, new Error(event.message), null, req);
};
options.body = options.body || options.data;
if (options.body) {
if (!Buffer.isBuffer(options.body)) {
options.body = new Buffer(options.body);
}
options.body = options.body.toString('hex');
}
options.encoding = null;
xhr.send(JSON.stringify(options));
return ret;
};