forked from leplatrem/node-get
/
node-get.js
275 lines (248 loc) · 8.82 KB
/
node-get.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
// node.js libraries
var http = require('http'),
https = require('https'),
util = require('util'),
fs = require('fs'),
events = require('events'),
Buffer = require('buffer').Buffer,
url = require('url'),
path = require('path');
// Local node-get libraries
var encodings = require('./encodings');
var default_headers = {
'Accept-Encoding': 'none',
'Connection': 'close',
'User-Agent': 'curl'
};
// Get a Get object. Takes an argument, that is either
// a plain string representing the URI, or an object:
//
// {
// uri: "string of uri", // required
// headers: {} // optional, default in source
// max_redirs: 5 // optional, default 10
// no_proxy: true // prevent automatic proxy usage when HTTP_PROXY is set.
// }
function Get(options) {
// Allow calling without new keyword.
if (!(this instanceof Get)) {
return new Get(options);
}
if (typeof options == 'string') {
this.uri = options;
this.headers = default_headers;
if (process.env.HTTP_PROXY) {
this.proxy = url.parse(process.env.HTTP_PROXY);
} else {
this.proxy = {};
}
} else {
if (!options.uri) {
throw Error('uri option required in get constructor');
}
this.uri = options.uri;
this.max_redirs = options.max_redirs || 10;
this.encoding = options.encoding;
this.headers = options.headers || default_headers;
if (!this.no_proxy && process.env.HTTP_PROXY) {
this.proxy = url.parse(process.env.HTTP_PROXY);
} else {
this.proxy = {};
}
}
}
util.inherits(Get, events.EventEmitter);
// Create a HTTP request. Just sanity-checks URLs and
// chooses an HTTP or HTTPS request.
//
// - @return {http.ClientRequest}
Get.prototype.request = function(callback) {
// TODO: handle non http/https protocols
this.uri_o = url.parse(this.uri);
// Validate the URI at this step so that invalid
// redirects are also caught.
if (!(this.uri_o.protocol &&
(this.uri_o.protocol == 'http:' || this.uri_o.protocol == 'https:') &&
this.uri_o.hostname)) {
return callback.call(this, null, new Error('Invalid URL'));
}
// TODO: should pronode-getxies support HTTPS?
if (this.uri_o.protocol == 'https:') {
return https.request({
host: this.uri_o.hostname,
port: 443,
path: this.proxy.hostname ?
this.uri :
((this.uri_o.pathname || '') +
(this.uri_o.search || '') +
(this.uri_o.hash || '')) || '/'
}, callback);
} else {
return http.request({
port: this.proxy.port || this.uri_o.port || 80,
host: this.proxy.hostname || this.uri_o.hostname,
path: this.proxy.hostname ?
this.uri :
((this.uri_o.pathname || '') +
(this.uri_o.search || '') +
(this.uri_o.hash || '')) || '/'
}, callback);
}
};
// Innermost API function of Get
//
// - @param {Function} callback
// - @param {Number} times number of times re-called.
Get.prototype.perform = function(callback, times) {
if (times > this.max_redirs) {
return callback(new Error('Redirect limit of ' +
this.max_redirs +
' reached'));
}
times = times || 1;
var clientrequest = this.request(function handleClientRequest(response, err) {
if (err) return callback.call(this, err, null);
if (response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location) {
// Redirection
// -----------
// Servers can send a full redirect location
// or a short form, like a hyperlink. Handle both.
if (url.parse(response.headers.location).protocol) {
this.uri = response.headers.location;
} else {
this.uri = url.resolve(this.uri, response.headers.location);
}
this.perform(callback, times + 1);
return;
} else if (response.statusCode >= 400) {
// failure
var err = new Error('Server returned HTTP ' + response.statusCode);
err.status = response.statusCode;
return callback.call(this, err, response);
} else {
// success
return callback.call(this, null, response);
}
}.bind(this));
// The client can fail if the url is invalid
if (clientrequest) {
// Handle DNS-level errors, like ECONNREFUSED
clientrequest.on('error', function(err) {
return callback.call(this, err);
}.bind(this));
// Enforce a timeout of 10 seconds.
clientrequest.setTimeout(10000, function() {
clientrequest.connection.end();
return callback.call(this, new Error('Timed out after 10 seconds'));
}.bind(this));
// TODO: fix when/if gzip is supported.
// If a proxy is defined, ask for the full requested URL,
// otherwise construct the URL without a hostname and protocol.
clientrequest.end();
}
};
Get.prototype.guessResponseExtension = function(response) {
if (response.headers['content-disposition']) {
var match = response.headers['content-disposition'].match(/filename=\"([^"]+)\"/);
if (match) {
var ext = path.extname(match[1]);
if (ext) {
return ext;
}
}
}
return false;
};
// Stream a file to disk
// ---------------------
// - @param {String} filename.
// - @param {Function} callback.
Get.prototype.toDisk = function(filename, callback) {
// TODO: catch all errors
this.perform(function(err, response) {
if (err) return callback(err);
// Don't set an encoding. Using an encoding messes up binary files.
// Pump contents from the response stream into a new writestream.
var file = fs.createWriteStream(filename);
file.on('error', callback);
file.on('close', function() {
return callback(null, filename, response, this);
}.bind(this));
response.pipe(file);
});
};
// Get the contents of a URL as a string
//
// - @param {Function} callback.
Get.prototype.asString = function(callback) {
// TODO: catch all errors
this.perform(function pipeResponseToString(err, response) {
if (err) return callback(err);
switch ((response.headers['content-type'] || '').toLowerCase().split('/')[0]) {
case 'binary':
case 'application':
case 'image':
case 'video':
return callback(new Error("Can't download binary file as string"));
default:
// TODO: respect Content-Transfer-Encoding header
response.setEncoding(this.guessEncoding(this.uri));
}
// Fill an array with chunks of data,
// and then join it into a string before calling `callback`
var out = [];
response.on('data', function(chunk) {
out.push(chunk);
});
response.on('error', function(err) {
return callback(err);
});
response.on('end', function() {
return callback(null, out.join(''), response.headers);
});
response.on('close', function() {
// TODO: should this be this way?
return callback(null, out.join(''), response.headers);
});
});
};
// Get the contents of a URL as a buffer
//
// - @param {Function} callback.
Get.prototype.asBuffer = function(callback) {
this.perform(function(err, response) {
if (err) return callback(err);
function returnBuffer() {
for (var length = 0, i = 0; i < out.length; i++) {
length += out[i].length;
}
var result = new Buffer(length);
for (var pos = 0, j = 0; j < out.length; j++) {
out[j].copy(result, pos);
pos += out[j].length;
}
return callback(null, result, response.headers);
}
// Fill an array with chunks of data,
// and then join it into a buffer before calling `callback`
var out = [];
response.on('data', function(chunk) {
out.push(chunk);
});
response.on('error', function(err) {
return callback(err);
});
response.on('end', returnBuffer);
response.on('close', returnBuffer);
});
};
Get.prototype.guessEncoding = function(location) {
// The 'most reliable' measure is probably the end of files, so
// start off with extname.
if (this.encoding) return this.encoding;
var ext = path.extname(location).toLowerCase();
if (encodings.ext[ext]) return encodings.ext[ext];
};
module.exports = Get;