forked from coolinker/tako
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplehttp.js
78 lines (66 loc) · 2.02 KB
/
simplehttp.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
var request = require("request");
var pixelsUtil = require("get-pixels");
var logutil = require("./logutil");
exports.GET = sendGet;
function sendGet(url, options, callback) {
options.uri = url;
options.method = "GET";
sendRequest(options, callback);
}
exports.POST = sendPost;
function sendPost(url, options, callback) {
options.uri = url;
options.method = "POST";
sendRequest(options, callback);
}
exports.image = image;
function image(url, options, callback) {
options.encoding = null;
sendGet(url, options, function(err, response, body) {
if (err) {
callback(err)
return
}
// console.log("response.headers", response.headers, body)
var type = options.type;
if (!type) {
if (response.getHeader !== undefined) {
type = response.getHeader('content-type');
} else if (response.headers !== undefined) {
type = response.headers['content-type'];
}
}
//var type = "image/jpeg"//response.headers['content-type'];
if (!type) {
callback(new Error('Invalid content-type'))
return
}
pixelsUtil(body, type, callback)
})
}
// function sendRequest(url, method, options, callback) {
// var timeout = options["../timeout"];
// delete options["../timeout"];
// var jar = options["../cookieJar"];
// delete options["../cookieJar"];
// var form = options;
// request({
// uri: url,
// method: method,
// form: form,
// jar: jar,
// timeout: timeout
// }, function(error, response, body) {
// callback(error, response, body);
// });
// }
function sendRequest(options, callback) {
if (options.cookieJar) {
options.jar = options.cookieJar;
delete options.cookieJar;
}
var req = request(options, function(error, response, body) {
callback(error, response, body);
});
req.__options = options;
}