forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
379 lines (335 loc) · 12.5 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2010-2011 Mikeal Rogers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var http = require('http')
, https = false
, tls = false
, url = require('url')
, util = require('util')
, stream = require('stream')
, qs = require('querystring')
;
try {
https = require('https');
} catch (e) {}
try {
tls = require('tls');
} catch (e) {}
var toBase64 = function(str) {
return (new Buffer(str || "", "ascii")).toString("base64");
};
// Hacky fix for pre-0.4.4 https
if (https && !https.Agent) {
https.Agent = function (options) {
http.Agent.call(this, options);
}
util.inherits(https.Agent, http.Agent);
https.Agent.prototype._getConnection = function(host, port, cb) {
var s = tls.connect(port, host, this.options, function() {
// do other checks here?
if (cb) cb();
});
return s;
};
}
var isUrl = /^https?:/;
var globalPool = {};
var Request = function (options) {
stream.Stream.call(this);
this.readable = true;
this.writable = true;
if (typeof options === 'string') {
options = {uri:options};
}
for (i in options) {
this[i] = options[i];
}
if (!this.pool) this.pool = globalPool;
this.dests = [];
this.__isRequestRequest = true;
}
util.inherits(Request, stream.Stream);
Request.prototype.getAgent = function (host, port) {
if (!this.pool[host+':'+port]) {
this.pool[host+':'+port] = new this.httpModule.Agent({host:host, port:port});
}
return this.pool[host+':'+port];
}
Request.prototype.request = function () {
var options = this;
if (options.url) {
// People use this property instead all the time so why not just support it.
options.uri = options.url;
delete options.url;
}
if (!options.uri) {
throw new Error("options.uri is a required argument");
} else {
if (typeof options.uri == "string") options.uri = url.parse(options.uri);
}
if (options.proxy) {
if (typeof options.proxy == 'string') options.proxy = url.parse(options.proxy);
}
options._redirectsFollowed = options._redirectsFollowed || 0;
options.maxRedirects = (options.maxRedirects !== undefined) ? options.maxRedirects : 10;
options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true;
options.method = options.method || 'GET';
options.headers = options.headers || {};
var setHost = false;
if (!options.headers.host) {
options.headers.host = options.uri.hostname;
if (options.uri.port) {
if ( !(options.uri.port === 80 && options.uri.protocol === 'http:') &&
!(options.uri.port === 443 && options.uri.protocol === 'https:') )
options.headers.host += (':'+options.uri.port);
}
setHost = true;
}
if (!options.uri.pathname) {options.uri.pathname = '/';}
if (!options.uri.port) {
if (options.uri.protocol == 'http:') {options.uri.port = 80;}
else if (options.uri.protocol == 'https:') {options.uri.port = 443;}
}
if (options.bodyStream || options.responseBodyStream) {
console.error('options.bodyStream and options.responseBodyStream is deprecated. You should now send the request object to stream.pipe()');
this.pipe(options.responseBodyStream || options.bodyStream)
}
if (options.proxy) {
options.port = options.proxy.port;
options.host = options.proxy.hostname;
} else {
options.port = options.uri.port;
options.host = options.uri.hostname;
}
if (options.onResponse === true) {
options.onResponse = options.callback;
delete options.callback;
}
var clientErrorHandler = function (error) {
if (setHost) delete options.headers.host;
options.emit('error', error);
};
if (options.onResponse) options.on('error', function (e) {options.onResponse(e)});
if (options.callback) options.on('error', function (e) {options.callback(e)});
if (options.uri.auth && !options.headers.authorization) {
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'));
}
if (options.proxy && options.proxy.auth && !options.headers['proxy-authorization']) {
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'));
}
options.path = options.uri.href.replace(options.uri.protocol + '//' + options.uri.host, '');
if (options.path.length === 0) options.path = '/';
if (options.proxy) options.path = (options.uri.protocol + '//' + options.uri.host + options.path);
if (options.json) {
options.headers['content-type'] = 'application/json';
options.body = JSON.stringify(options.json);
} else if (options.multipart) {
options.body = '';
options.headers['content-type'] = 'multipart/related;boundary="frontier"';
if (!options.multipart.forEach) throw new Error('Argument error, options.multipart.');
options.multipart.forEach(function (part) {
var body = part.body;
if(!body) throw Error('Body attribute missing in multipart.');
delete part.body;
options.body += '--frontier\r\n';
Object.keys(part).forEach(function(key){
options.body += key + ': ' + part[key] + '\r\n'
})
options.body += '\r\n' + body + '\r\n';
})
options.body += '--frontier--'
}
if (options.body) {
if (!Buffer.isBuffer(options.body)) {
options.body = new Buffer(options.body);
}
if (options.body.length) {
options.headers['content-length'] = options.body.length;
} else {
throw new Error('Argument error, options.body.');
}
}
options.httpModule =
{"http:":http, "https:":https}[options.proxy ? options.proxy.protocol : options.uri.protocol]
if (!options.httpModule) throw new Error("Invalid protocol");
if (options.pool === false) {
options.agent = false;
} else {
if (options.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.maxSockets;
}
if (options.pool.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.pool.maxSockets;
}
}
options.start = function () {
options._started = true;
options.req = options.httpModule.request(options, function (response) {
options.response = response;
if (setHost) delete options.headers.host;
if (response.statusCode >= 300 &&
response.statusCode < 400 &&
options.followRedirect &&
options.method !== 'PUT' &&
options.method !== 'POST' &&
response.headers.location) {
if (options._redirectsFollowed >= options.maxRedirects) {
options.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."));
}
options._redirectsFollowed += 1;
if (!isUrl.test(response.headers.location)) {
response.headers.location = url.resolve(options.uri.href, response.headers.location);
}
options.uri = response.headers.location;
delete options.req;
delete options.agent;
if (options.headers) {
delete options.headers.host;
}
request(options, options.callback);
return; // Ignore the rest of the response
} else {
options._redirectsFollowed = 0;
// Be a good stream and emit end when the response is finished.
// Hack to emit end on close becuase of a core bug that never fires end
response.on('close', function () {options.emit('end')})
if (options.encoding) {
if (options.dests.length !== 0) {
console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.");
} else {
response.setEncoding(options.encoding);
}
}
options.dests.forEach(function (dest) {
if (dest.headers) {
dest.headers['content-type'] = response.headers['content-type'];
if (response.headers['content-length']) {
dest.headers['content-length'] = response.headers['content-length'];
}
}
})
response.on("data", function (chunk) {options.emit("data", chunk)});
response.on("end", function (chunk) {options.emit("end", chunk)});
response.on("close", function () {options.emit("close")});
if (options.onResponse) {
options.onResponse(null, response);
}
if (options.callback) {
var buffer = '';
options.on("data", function (chunk) {
buffer += chunk;
})
options.on("end", function () {
options.callback(null, response, buffer);
})
;
}
}
})
options.req.on('error', clientErrorHandler);
}
options.once('pipe', function (src) {
if (options.ntick) throw new Error("You cannot pipe to this stream after the first nextTick() after creation of the request stream.")
options.src = src;
options.on('pipe', function () {
console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
})
})
process.nextTick(function () {
if (options.body) {
options.write(options.body);
options.end();
} else if (options.requestBodyStream) {
console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.")
options.requestBodyStream.pipe(options);
} else if (!options.src) {
options.end();
}
options.ntick = true;
})
}
Request.prototype.pipe = function (dest) {
if (this.response) throw new Error("You cannot pipe after the response event.")
this.dests.push(dest);
stream.Stream.prototype.pipe.call(this, dest)
}
Request.prototype.write = function () {
if (!this._started) this.start();
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.write.apply(this.req, arguments);
}
Request.prototype.end = function () {
if (!this._started) this.start();
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.end.apply(this.req, arguments);
}
Request.prototype.pause = function () {
if (!this.response) throw new Error("This request has been piped before http.request() was called.");
this.response.pause.apply(this.response, arguments);
}
Request.prototype.resume = function () {
if (!this.response) throw new Error("This request has been piped before http.request() was called.");
this.response.resume.apply(this.response, arguments);
}
function request (options, callback) {
if (typeof options === 'string') options = {uri:options};
if (callback) options.callback = callback;
var r = new Request(options);
r.request();
return r;
}
module.exports = request;
request.defaults = function (options) {
var def = function (method) {
var d = function (opts, callback) {
for (i in options) {
if (opts[i] === undefined) opts[i] = options[i];
}
return method(opts, callback);
}
return d;
}
de = def(request);
de.get = def(request.get);
de.post = def(request.post);
de.put = def(request.put);
de.head = def(request.head);
de.del = def(request.del);
return de;
}
request.get = request;
request.post = function (options, callback) {
if (typeof options === 'string') options = {uri:options};
options.method = 'POST';
return request(options, callback);
};
request.put = function (options, callback) {
if (typeof options === 'string') options = {uri:options};
options.method = 'PUT';
return request(options, callback);
};
request.head = function (options, callback) {
if (typeof options === 'string') options = {uri:options};
options.method = 'HEAD';
if (options.body || options.requestBodyStream || options.json || options.multipart) {
throw new Error("HTTP HEAD requests MUST NOT include a request body.");
}
return request(options, callback);
};
request.del = function (options, callback) {
if (typeof options === 'string') options = {uri:options};
options.method = 'DELETE';
return request(options, callback);
}