1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var parse_url = require('url').parse;
|
9 | var Headers = require('./headers');
|
10 | var Body = require('./body');
|
11 |
|
12 | module.exports = Request;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function Request(input, init) {
|
22 | var url, url_parsed;
|
23 |
|
24 |
|
25 | if (!(input instanceof Request)) {
|
26 | url = input;
|
27 | url_parsed = parse_url(url);
|
28 | input = {};
|
29 | } else {
|
30 | url = input.url;
|
31 | url_parsed = parse_url(url);
|
32 | }
|
33 |
|
34 |
|
35 | init = init || {};
|
36 |
|
37 |
|
38 | this.method = init.method || input.method || 'GET';
|
39 | this.redirect = init.redirect || input.redirect || 'follow';
|
40 | this.headers = new Headers(init.headers || input.headers || {});
|
41 | this.url = url;
|
42 |
|
43 |
|
44 | this.follow = init.follow !== undefined ?
|
45 | init.follow : input.follow !== undefined ?
|
46 | input.follow : 20;
|
47 | this.compress = init.compress !== undefined ?
|
48 | init.compress : input.compress !== undefined ?
|
49 | input.compress : true;
|
50 | this.counter = init.counter || input.counter || 0;
|
51 | this.agent = init.agent || input.agent;
|
52 |
|
53 | Body.call(this, init.body || this._clone(input), {
|
54 | timeout: init.timeout || input.timeout || 0,
|
55 | size: init.size || input.size || 0
|
56 | });
|
57 |
|
58 |
|
59 | this.protocol = url_parsed.protocol;
|
60 | this.hostname = url_parsed.hostname;
|
61 | this.port = url_parsed.port;
|
62 | this.path = url_parsed.path;
|
63 | this.auth = url_parsed.auth;
|
64 | }
|
65 |
|
66 | Request.prototype = Object.create(Body.prototype);
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | Request.prototype.clone = function() {
|
74 | return new Request(this);
|
75 | };
|