1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var parse_url = require('url').parse;
|
9 |
|
10 | module.exports = Request;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function Request(input, init) {
|
20 | var url, url_parsed;
|
21 |
|
22 |
|
23 | if (!(input instanceof Request)) {
|
24 | url = input;
|
25 | url_parsed = parse_url(url);
|
26 | input = {};
|
27 | } else {
|
28 | url = input.url;
|
29 | url_parsed = parse_url(url);
|
30 | }
|
31 |
|
32 | if (!url_parsed.protocol || !url_parsed.hostname) {
|
33 | throw new Error('only absolute urls are supported');
|
34 | }
|
35 |
|
36 | if (url_parsed.protocol !== 'http:' && url_parsed.protocol !== 'https:') {
|
37 | throw new Error('only http(s) protocols are supported');
|
38 | }
|
39 |
|
40 |
|
41 | init = init || {};
|
42 |
|
43 |
|
44 | this.method = init.method || input.method || 'GET';
|
45 | this.headers = init.headers || input.headers || {};
|
46 | this.body = init.body || input.body;
|
47 | this.url = url;
|
48 |
|
49 |
|
50 | this.follow = init.follow !== undefined ?
|
51 | init.follow : input.follow !== undefined ?
|
52 | input.follow : 20;
|
53 | this.counter = init.counter || input.follow || 0;
|
54 | this.timeout = init.timeout || input.timeout || 0;
|
55 | this.compress = init.compress !== undefined ?
|
56 | init.compress : input.compress !== undefined ?
|
57 | input.compress : true;
|
58 | this.size = init.size || input.size || 0;
|
59 | this.agent = init.agent || input.agent;
|
60 |
|
61 |
|
62 | this.protocol = url_parsed.protocol;
|
63 | this.hostname = url_parsed.hostname;
|
64 | this.port = url_parsed.port;
|
65 | this.path = url_parsed.path;
|
66 | this.auth = url_parsed.auth;
|
67 | }
|