Supports OAuth Providers that don't end connections neatly (Google!)
This commit is contained in:
37
lib/oauth.js
37
lib/oauth.js
@ -3,7 +3,8 @@ var crypto= require('crypto'),
|
||||
http= require('http'),
|
||||
https= require('https'),
|
||||
URL= require('url'),
|
||||
querystring= require('querystring');
|
||||
querystring= require('querystring'),
|
||||
OAuthUtils= require('./_utils');
|
||||
|
||||
exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
|
||||
this._isEcho = false;
|
||||
@ -344,12 +345,14 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
||||
if( callback ) {
|
||||
var data="";
|
||||
var self= this;
|
||||
request.on('response', function (response) {
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', function (chunk) {
|
||||
data+=chunk;
|
||||
});
|
||||
response.on('end', function () {
|
||||
|
||||
// Some hosts *cough* google appear to close the connection early / send no content-length header
|
||||
// allow this behaviour.
|
||||
var allowEarlyClose= OAuthUtils.isAnEarlyCloseHost( parsedUrl.hostname );
|
||||
var callbackCalled= false;
|
||||
function passBackControl( response ) {
|
||||
if(!callbackCalled) {
|
||||
callbackCalled= true;
|
||||
if ( response.statusCode >= 200 && response.statusCode <= 299 ) {
|
||||
callback(null, data, response);
|
||||
} else {
|
||||
@ -361,10 +364,28 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
||||
callback({ statusCode: response.statusCode, data: data }, data, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request.on('response', function (response) {
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', function (chunk) {
|
||||
data+=chunk;
|
||||
});
|
||||
response.on('end', function () {
|
||||
passBackControl( response );
|
||||
});
|
||||
response.on('close', function () {
|
||||
if( allowEarlyClose ) {
|
||||
passBackControl( response );
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
request.on("error", callback);
|
||||
request.on("error", function(err) {
|
||||
callbackCalled= true;
|
||||
callback( err )
|
||||
});
|
||||
|
||||
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
|
||||
request.write(post_body);
|
||||
|
||||
Reference in New Issue
Block a user