Permalink
Browse files

NODEJS-998: acs-node needs to indicate server to compress response

  • Loading branch information...
1 parent 076c738 commit ad80dac85bd93198a9d983d28e2c1bc7c0c158a5 @ypjin ypjin committed Jul 24, 2013
Showing with 88 additions and 77 deletions.
  1. +88 −77 lib/util/utils.js
View
@@ -1,5 +1,6 @@
var querystring = require('querystring');
var OAuth = require('../oauth/oauth');
+var zlib = require('zlib');
//URL
exports.baseURL = 'api.cloud.appcelerator.com';
@@ -91,6 +92,7 @@ exports.sendRequest = function(host, port, path, method, data, header, useSecure
}
header['Content-Length'] = request_data.length;
header['Content-Type'] = 'application/x-www-form-urlencoded';
+ header['Accept-Encoding'] = 'gzip';
var options = {
'host': host,
'port': port,
@@ -99,48 +101,15 @@ exports.sendRequest = function(host, port, path, method, data, header, useSecure
'headers': header
};
- var protocal = null;
+ var protocol = null;
if(useSecure) {
- protocal = require('https');
+ protocol = require('https');
} else {
- protocal = require('http');
+ protocol = require('http');
}
- var req = protocal.request(options);
- req.on('response', function(response) {
-
- var statusCode = response.statusCode;
-
- function performOnload(body) {
- if(body && body.trim().length > 0) {
-
- try {
- var data = JSON.parse(body);
- } catch(E) {
- if(statusCode == 200) statusCode = 500;
- return callback(buildErrResponse(statusCode, body));
- }
-
- if(data && data.meta) {
- if(data.meta.session_id) {
- var sessionId = data.meta.session_id;
- sdk.session_id = sessionId;
- }
- }
- callback(data);
- } else {
- callback(errServerReturn);
- }
- }
-
- var data = '';
- response.on('data',function(chunk) {
- data += chunk;
- });
- response.on('end',function() {
- performOnload(data);
- });
- });
+ var req = protocol.request(options);
+ req.on('response', APIResponseHandler(callback));
req.on("error",function(err) {
callback(errServerReturn);
@@ -161,7 +130,8 @@ exports.sendRequestWithFile = function(host, port, path, method, data, header, u
}
header['Content-Type'] = 'multipart/form-data; boundary=' + boundary;
header['Content-Length'] = length;
-
+ header['Accept-Encoding'] = 'gzip';
+
var options = {
'host': host,
'port': port,
@@ -170,49 +140,16 @@ exports.sendRequestWithFile = function(host, port, path, method, data, header, u
'headers': header
};
- var protocal = null;
+ var protocol = null;
if(useSecure) {
- protocal = require('https');
+ protocol = require('https');
} else {
- protocal = require('http');
+ protocol = require('http');
}
- var req = protocal.request(options);
-
- req.on('response', function(response) {
-
- var statusCode = response.statusCode;
-
- function performOnload(body) {
- if(body && body.trim().length > 0) {
-
- try {
- var data = JSON.parse(body);
- } catch(E) {
- if(statusCode == 200) statusCode = 500;
- return callback(buildErrResponse(statusCode, body));
- }
+ var req = protocol.request(options);
- if(data && data.meta) {
- if(data.meta.session_id) {
- var sessionId = data.meta.session_id;
- sdk.session_id = sessionId;
- }
- }
- callback(data);
- } else {
- callback(errServerReturn);
- }
- }
-
- var data = '';
- response.on('data',function(chunk) {
- data += chunk;
- });
- response.on('end',function() {
- performOnload(data);
- });
- });
+ req.on('response', APIResponseHandler(callback));
req.on("error",function(err) {
callback(errServerReturn);
@@ -259,4 +196,78 @@ function prepareRequestBody(params, filePropName, fileName, fileBinary, mimeType
function buildErrResponse(code, message) {
return {'meta' : {'status': 'fail', 'code': code, 'message': message}};
+}
+
+function APIResponseHandler(callback) {
+
+ return function(response) {
+
+ function performOnload(body) {
+
+ if(!body)
+ return callback(errServerReturn);
+
+ var statusCode = response.statusCode;
+ if(statusCode !== 200) {
+ return callback(buildErrResponse(statusCode, body.toString()));
+ }
+
+ var contentEncoding = response.headers['content-encoding'];
+ if(contentEncoding && contentEncoding.indexOf('gzip') !== -1) {
+ zlib.gunzip(body, parseResult);
+ return;
+ } else {
+ body = body.toString();
+ if(body.trim().length > 0) {
+ parseResult(null, body);
+ } else {
+ callback(errServerReturn);
+ }
+ }
+ }
+
+ function parseResult(err, resbody) {
+ if(err) {
+ return callback(errServerReturn);
+ }
+
+ try {
+ var data = JSON.parse(resbody);
+ } catch(E) {
+ if(statusCode == 200) statusCode = 500;
+ return callback(buildErrResponse(statusCode, resbody));
+ }
+
+ if(data && data.meta) {
+ if(data.meta.session_id) {
+ var sessionId = data.meta.session_id;
+ sdk.session_id = sessionId;
+ }
+ }
+ callback(data);
+ }
+
+ var buffer = [];
+ var bodyLen = 0
+ response.on('data',function(chunk) {
+ buffer.push(chunk)
+ bodyLen += chunk.length
+ });
+ response.on('end',function() {
+ var body = null;
+ if (buffer.length && Buffer.isBuffer(buffer[0])) {
+ body = new Buffer(bodyLen)
+ var i = 0
+ buffer.forEach(function (chunk) {
+ chunk.copy(body, i, 0, chunk.length);
+ i += chunk.length;
+ });
+ } else if (buffer.length) {
+ body = buffer.join('')
+ }
+
+ performOnload(body);
+ });
+
+ }
}

0 comments on commit ad80dac

Please sign in to comment.