forked from nodeca/probe-image-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
58 lines (43 loc) · 1.22 KB
/
http.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
'use strict';
var once = require('./lib/common').once;
var error = require('./lib/common').error;
var request = require('request').defaults({
timeout: 30000,
maxRedirects: 2,
rejectUnauthorized: false,
headers: {
'User-Agent': 'Image size prober https://github.com/nodeca/probe-image-size'
}
});
var probeStream = require('./stream');
module.exports = function probeHttp(options, _callback) {
var callback = once(_callback);
var req;
try {
req = request(options);
} catch (err) {
callback(err);
return;
}
req.on('response', function (res) {
if (res.statusCode === 200) {
probeStream(res, function (err, result) {
req.abort();
if (result) {
var length = res.headers['content-length'];
if (length && length.match(/^\d+$/)) {
result.length = +length;
}
result.url = res.request.uri.href;
}
callback(err, result);
});
} else {
var err = error('bad status code: ' + res.statusCode, null, res.statusCode);
req.abort();
callback(err);
}
});
req.on('error', function (err) { callback(err); });
};
module.exports.parsers = require('./lib/parsers_stream');