Permalink
Browse files

fix request bug

  • Loading branch information...
1 parent ce6c115 commit a2feae6b2d4e2e8a8e6f354a7c18e71dbf6a0fbb @btspoony committed Jan 26, 2013
Showing with 19 additions and 18 deletions.
  1. +19 −18 lib/base.js
View
@@ -133,60 +133,61 @@ SocialBase.prototype._performSecureRequest= function( method, url, extra_params,
}
if( callback ) {
- var dataBuffer = null;
- var datastr = "";
var self = this;
var allowEarlyClose= true;
var callbackCalled= false;
- function passBackControl( response ) {
+
+ function passBackControl( datastr, dataBuffers, response ) {
if(!callbackCalled) {
callbackCalled= true;
- if( dataBuffer ){
- datastr = dataBuffer.toString();
+ if( dataBuffers ){
+ datastr = Buffer.concat(dataBuffers).toString();
}
- if ( response.statusCode >= 200 && response.statusCode <= 299
- && datastr.indexOf('error_code') < 0 /* for wyx error_code*/) {
+ if ( response.statusCode >= 200 && response.statusCode <= 299 ) {
callback(null, datastr, response);
} else {
callback({ statusCode: response.statusCode, data: datastr }, datastr, response);
}
}
}
- request.on('response', function (response) {
+ var onResponse = function ( response) {
+ var dataBuffers = null;
+ var datastr = "";
+ if( response.headers['content-length'] ){
+ dataBuffers = [];
+ }
if (response.headers['content-encoding'] == 'gzip') {
var gunzip = zlib.createGunzip();
response.pipe(gunzip);
gunzip.on('data', function (chunk) {
- dataBuffer = chunk;
+ dataBuffers.push( chunk );
});
gunzip.on('end', function () {
- passBackControl( response );
+ passBackControl( null, dataBuffers, response );
});
}else{
- if( response.headers['content-length'] ){
- dataBuffer = new Buffer(Number(response.headers['content-length']));
- }
response.setEncoding('utf8');
response.on('data', function (chunk) {
- if( dataBuffer ){
- dataBuffer.write(chunk);
+ if( dataBuffers ){
+ dataBuffers.push( new Buffer(chunk) );
}else{
datastr += chunk;
}
});
response.on('end', function () {
- passBackControl( response );
+ passBackControl( datastr, dataBuffers, response );
});
}
response.on('close', function () {
if( allowEarlyClose ) {
- passBackControl( response );
+ passBackControl( datastr, dataBuffers, response );
}
});
- });
+ }
+ request.on('response', onResponse );
request.on("error", function(err) {
callbackCalled= true;
callback( err )

0 comments on commit a2feae6

Please sign in to comment.