forked from RafaelVidaurre/yakuza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
229 lines (185 loc) · 4.76 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
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
/**
* A wrapper for mikeal's request module for Yakuza
* @module Http
* @author Rafael Vidaurre
*/
'use strict';
var _, needle, defaults;
needle = require('needle');
_ = require('lodash');
defaults = {
follow_max: 0,
user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko)' +
'Chrome/41.0.2272.76 Safari/537.36'
};
needle.defaults(defaults);
/**
* @class
* @param {object} defaultCookies cookie jar for the class instance to be initialized with, defaults
* to a brand new cookie jar
*/
function Http (defaultCookies) {
/**
* Cookie jar for the Http instance
* @private
*/
this._cookieJar = defaultCookies || {};
/**
* Array of requests logged
* @private
*/
this._log = [];
}
/**
* Pushes a response object to the request log and responds to passed callback
* @param {object} err Error object, is null if no error is present
* @param {object} res Needle's response
* @param {string} body Needle's response body only, is null if an error is present
* @param {function} callback Callback to be executed
* @private
*/
Http.prototype._interceptResponse = function (err, res, body, url, callback) {
var entry, resCookieString, _this, cb, noop;
_this = this;
noop = function () {};
cb = callback || noop;
if (err) {
callback(err, null, null);
return;
}
resCookieString = '';
_.each(res.cookies, function (value, key) {
// Update our cookie jar
_this._cookieJar[key] = value;
// Build cookie string for logging
if (resCookieString) {
resCookieString += ' ';
}
resCookieString += key + '=' + value + ';';
});
entry = {
request: {
headers: res.req._headers,
cookies: res.req._headers.cookie || '',
url: url
},
response: {
cookies: resCookieString,
headers: res.headers,
statusCode: res.statusCode,
body: body
}
};
this._pushToLog(entry);
cb(err, res, body);
};
/**
* Pushes an entry to the class log
* @param {object} logEntry entry that represents a unit of the log
*/
Http.prototype._pushToLog = function (logEntry) {
this._log.push(logEntry);
};
Http.prototype._buildParams = function (param1, param2) {
var params;
params = {
opts: {},
callback: undefined
};
params.callback = param2;
if (_.isString(param1)) {
params.opts.url = param1;
} else {
params.opts = param1;
}
return params;
};
Http.prototype.request = function (method, opts, callback) {
var _this, data, url, finalOpts;
_this = this;
if (!opts.url) {
throw new Error('Url is not set');
}
url = opts.url;
data = opts.data || null;
finalOpts = _.omit(opts, ['data', 'url']);
finalOpts.cookies = _.extend(this._cookieJar, finalOpts.cookies);
needle.request(method, url, data, finalOpts, function (err, res, body) {
_this._interceptResponse(err, res, body, opts.url, callback);
});
};
/**
* Delegate to request's `del` method
*/
Http.prototype.del = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('delete', opts, callback);
};
/**
* Delegate to request's `get` method
*/
Http.prototype.get = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('get', opts, callback);
};
/**
* Delegate to request's `head` method
*/
Http.prototype.head = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('head', opts, callback);
};
/**
* Delegate to request's `patch` method
*/
Http.prototype.patch = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('patch', opts, callback);
};
/**
* Delegate to request's `post` method
*/
Http.prototype.post = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('post', opts, callback);
};
/**
* Delegate to request's `put` method
*/
Http.prototype.put = function (param1, param2) {
var opts, params, callback;
params = this._buildParams(param1, param2);
opts = params.opts;
callback = params.callback;
this.request('put', opts, callback);
};
/**
* Returns the current request log as an array
* @return {array} current request log
*/
Http.prototype.getLog = function () {
return this._log;
};
/**
* Returns a clone of the current cookie jar
* @return current cookie jar clone
*/
Http.prototype.getCookieJar = function () {
return _.cloneDeep(this._cookieJar);
};
module.exports = Http;