-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathrequest.js
67 lines (56 loc) · 1.59 KB
/
request.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
/**
* request.js
*
* Request class contains server only options
*/
var parse_url = require('url').parse;
module.exports = Request;
/**
* Request class
*
* @param Mixed input Url or Request instance
* @param Object init Custom options
* @return Void
*/
function Request(input, init) {
var url, url_parsed;
// normalize input
if (!(input instanceof Request)) {
url = input;
url_parsed = parse_url(url);
input = {};
} else {
url = input.url;
url_parsed = parse_url(url);
}
if (!url_parsed.protocol || !url_parsed.hostname) {
throw new Error('only absolute urls are supported');
}
if (url_parsed.protocol !== 'http:' && url_parsed.protocol !== 'https:') {
throw new Error('only http(s) protocols are supported');
}
// normalize init
init = init || {};
// fetch spec options
this.method = init.method || input.method || 'GET';
this.headers = init.headers || input.headers || {};
this.body = init.body || input.body;
this.url = url;
// server only options
this.follow = init.follow !== undefined ?
init.follow : input.follow !== undefined ?
input.follow : 20;
this.counter = init.counter || input.follow || 0;
this.timeout = init.timeout || input.timeout || 0;
this.compress = init.compress !== undefined ?
init.compress : input.compress !== undefined ?
input.compress : true;
this.size = init.size || input.size || 0;
this.agent = init.agent || input.agent;
// server request options
this.protocol = url_parsed.protocol;
this.hostname = url_parsed.hostname;
this.port = url_parsed.port;
this.path = url_parsed.path;
this.auth = url_parsed.auth;
}