-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbc-http-restclient.js
158 lines (134 loc) · 4.75 KB
/
bbc-http-restclient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var http = require('http');
var https = require('https');
var url = require('url');
var extend = require('util')._extend;
http.globalAgent.maxSockets = 100;
function RestClient(options) {
this.defaults = options || {};
this.cacheManager = require('./lib/cacheManager');
}
RestClient.prototype.request = function (_options) {
var options = extend({}, this.defaults);
for(var i in _options) {
options[i] = _options[i];
}
var parsedUrl = url.parse(options.url);
var isSSL = parsedUrl.protocol == "https:";
var contents = "";
try {
var http_options = {
host: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port || ((isSSL) ? 443 : 80),
agent: false,
strictSSL: false,
http_module: http,
method: options.method,
headers: options.headers
}
if (isSSL) {
if (options.ca) http_options.ca = options.ca;
if (options.cert) http_options.cert = options.cert;
if (options.key) http_options.key = options.key;
if (options.pfx) http_options.pfx = options.pfx;
if (options.passphrase) http_options.passphrase = options.passphrase;
http_options.http_module = https;
}
this.checkCache(options.cache, http_options, function (obj) {
if (obj) {
options.success(null, obj);
return;
}
if (options.cache) {
this.cacheManager.reserveCacheSpace(http_options);
}
var body = options.body || null;
if(isSSL) {
if (this.defaults.proxy || options.proxy) {
var _proxy = this.defaults.proxy || options.proxy;
var connectReq = http.request({
host: _proxy.host,
port: _proxy.port,
method: 'CONNECT',
path: parsedUrl.hostname
}).on('connect', function (res, socket, head) {
http_options.socket = socket;
var req = this._makeRequest(http_options, body, options, this);
}.bind(this)).end();
}
else {
this._makeRequest(http_options, body, options, this);
}
}
else {
if (this.defaults.proxy || options.proxy) {
http_options.path = options.url;
http_options.host = options.proxy.host;
http_options.port = options.proxy.port;
}
this._makeRequest(http_options, body, options, this);
}
}.bind(this));
}
catch (e) {
console.log(e);
}
}
RestClient.prototype.checkCache = function (enableCache, http_options, callback) {
if (enableCache) {
this.cacheManager.getItemFromCache(http_options, function (obj) {
callback(obj);
}.bind(this));
}
else {
callback(null);
}
}
RestClient.prototype._makeRequest = function (http_options, body, options, restClient) {
if (!http_options.method) {
http_options.method = "GET";
}
http_options.headers = http_options.headers || {};
if (body) {
var length = typeof(body) == 'string' ? Buffer.byteLength(body) : body.length;
http_options.headers['Content-Length'] = length;
}
var req = http_options.http_module.request(http_options);
if (body) {
req.write(body);
}
this._processResponse(req, options, http_options, restClient);
return req;
}
RestClient.prototype._processResponse = function (request, options, http_options) {
var contents = new Buffer(0);
var self = this;
request.on('response', function (response) {
response.on('data', function (data) {
contents = Buffer.concat([contents, data]);
});
response.on("end", function () {
if (response.statusCode < 200 || response.statusCode >= 300) {
if (options.error) {
options.error(response, contents);
}
}
else {
var obj = self.cacheManager.addItemToCache(contents, http_options);
if (options.success) {
options.success(response, contents);
}
}
});
});
request.on('error', function (e) {
console.log("Error in bbc-http-restclient. Message follows: ");
console.log(e);
self.cacheManager.unReserveCacheSpace(http_options);
if (options.error) {
options.error(request, e);
}
});
request.end();
}
module.exports = RestClient;