-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
168 lines (156 loc) · 4.7 KB
/
index.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
/*
Mimicry
A simple HTTP request library that mimics a browser.
*/
var http = require('http'),
url = require('url'),
zlib = require('zlib'),
tough = require('tough-cookie'),
Cookie = tough.Cookie,
querystring = require('querystring'),
https = require('https');
function Mimicry(useragent)
{
// default to Chrome
if(!useragent)
useragent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
var defaultHeaders = {
'User-Agent': useragent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
};
var cookieJar = {};
this.get = function(urlStr, customHeaders, callback) { makeRequest(urlStr, null, customHeaders, callback); };
this.post = function(urlStr, data, customHeaders, callback) { makeRequest(urlStr, data, customHeaders, callback); };
function makeRequest(urlStr, postData, customHeaders, callback) {
if(customHeaders instanceof Function)
{
callback = customHeaders;
customHeaders = null;
}
var uri = url.parse(urlStr);
headers = JSON.parse(JSON.stringify(defaultHeaders)); // copy
var cookies = getCookies(uri.host);
if(cookies != '')
headers['Cookie'] = getCookies(uri.host);
if(customHeaders)
for(var h in customHeaders)
headers[h] = customHeaders[h];
function reqCallback(err, data, headers) {
if(err)
callback(err, null);
else
{
if(headers['set-cookie'])
parseCookies(headers['set-cookie']);
if(headers['location'])
makeRequest(headers['location'], customHeaders, callback);
else
callback(null, data.toString('utf8'), headers);
}
}
if(postData)
{
var data = querystring.stringify(postData);
headers['Content-Type'] = 'application/x-www-form-urlencoded';
headers['Content-Length'] = data.length;
makePostRequest(data, {
host: uri.host,
port: (uri.protocol == 'https:' ? 443 : 80),
path: uri.path,
headers: headers,
method: 'POST'
}, reqCallback);
}
else
makeGetRequest({
host: uri.host,
port: (uri.protocol == 'https:' ? 443 : 80),
path: uri.path,
headers: headers
}, reqCallback);
}
// Internal function, make an HTTP request and return the response
function makeGetRequest(options, callback) {
var response = new Buffer(0);
(options.port == 443 ? https : http).get(options, function(resp) {
resp.on('data', function(chunk) {
response = Buffer.concat([response, chunk], response.length + chunk.length);
});
resp.on('end', function() {
if(resp.headers['content-encoding'] == 'gzip')
zlib.unzip(response, function(err, buffer) {
if(err)
callback(err, null, null);
else
callback(null, buffer, resp.headers);
});
else
callback(null, response, resp.headers);
});
}).on('error', function(e) {
callback(e.message, null, null);
});
}
function makePostRequest(data, options, callback) {
var response = new Buffer(0);
var post = (options.port == 443 ? https : http).request(options, function(resp) {
resp.on('data', function(chunk) {
response = Buffer.concat([response, chunk], response.length + chunk.length);
});
resp.on('end', function() {
if(resp.headers['content-encoding'] == 'gzip')
zlib.unzip(response, function(err, buffer) {
if(err)
callback(err, null, null);
else
callback(null, buffer, resp.headers);
});
else
callback(null, response, resp.headers);
});
}).on('error', function(e) {
callback(e.message, null, null);
});
post.write(data);
post.end();
}
function parseCookies(setcookie) {
if (setcookie instanceof Array)
cookies = setcookie.map(function (c) { return (Cookie.parse(c)); });
else
cookies = [Cookie.parse(setcookie)];
for(var c in cookies)
{
var cookie = cookies[c];
cookie.domain = cookie.domain || 'none';
cookieJar[cookie.domain] = cookieJar[cookie.domain] || [];
// update cookie if it exists
if(cookieJar[cookie.domain].filter(function(v) {
return v.key == cookie.key;
}).length == 0)
cookieJar[cookie.domain].push(cookie);
else
cookieJar[cookie.domain].filter(function(v) {
return v.key == cookie.key;
})[0].value = cookie.value;
}
}
function getCookies(domain) {
var cookieString = '';
for(var d in cookieJar)
{
if(tough.getPublicSuffix(d) == tough.getPublicSuffix(domain) || d == 'none')
for(var c in cookieJar[d])
{
var cookie = cookieJar[d][c];
cookieString += cookie.key + '=' + cookie.value + '; ';
}
}
return cookieString.replace(/; $/, '');
}
}
module.exports = Mimicry;