m0603va 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Laba6/file1.js +23 -0
- package/Laba6/file2.js +23 -0
- package/Laba6/index.html +63 -0
- package/Laba6/m0603.js +29 -0
- package/Laba6/node_modules/.package-lock.json +83 -0
- package/Laba6/node_modules/httpntlm/.jshintrc +4 -0
- package/Laba6/node_modules/httpntlm/LICENSE +20 -0
- package/Laba6/node_modules/httpntlm/README.md +148 -0
- package/Laba6/node_modules/httpntlm/httpntlm.js +104 -0
- package/Laba6/node_modules/httpntlm/ntlm.js +390 -0
- package/Laba6/node_modules/httpntlm/package.json +33 -0
- package/Laba6/node_modules/httpreq/LICENSE +19 -0
- package/Laba6/node_modules/httpreq/README.md +383 -0
- package/Laba6/node_modules/httpreq/contributors.md +26 -0
- package/Laba6/node_modules/httpreq/lib/httpreq.js +681 -0
- package/Laba6/node_modules/httpreq/package.json +102 -0
- package/Laba6/node_modules/m0603sol/m0603.js +29 -0
- package/Laba6/node_modules/m0603sol/package.json +12 -0
- package/Laba6/node_modules/nodemailer/.gitattributes +6 -0
- package/Laba6/node_modules/nodemailer/.prettierrc.js +8 -0
- package/Laba6/node_modules/nodemailer/CHANGELOG.md +725 -0
- package/Laba6/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
- package/Laba6/node_modules/nodemailer/CONTRIBUTING.md +67 -0
- package/Laba6/node_modules/nodemailer/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer/README.md +97 -0
- package/Laba6/node_modules/nodemailer/SECURITY.txt +22 -0
- package/Laba6/node_modules/nodemailer/lib/addressparser/index.js +313 -0
- package/Laba6/node_modules/nodemailer/lib/base64/index.js +142 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/index.js +251 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/sign.js +117 -0
- package/Laba6/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
- package/Laba6/node_modules/nodemailer/lib/fetch/index.js +274 -0
- package/Laba6/node_modules/nodemailer/lib/json-transport/index.js +82 -0
- package/Laba6/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
- package/Laba6/node_modules/nodemailer/lib/mailer/index.js +427 -0
- package/Laba6/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
- package/Laba6/node_modules/nodemailer/lib/mime-funcs/index.js +625 -0
- package/Laba6/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
- package/Laba6/node_modules/nodemailer/lib/nodemailer.js +143 -0
- package/Laba6/node_modules/nodemailer/lib/qp/index.js +219 -0
- package/Laba6/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
- package/Laba6/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
- package/Laba6/node_modules/nodemailer/lib/shared/index.js +638 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/index.js +1796 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
- package/Laba6/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
- package/Laba6/node_modules/nodemailer/lib/well-known/index.js +47 -0
- package/Laba6/node_modules/nodemailer/lib/well-known/services.json +286 -0
- package/Laba6/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/Laba6/node_modules/nodemailer/package.json +46 -0
- package/Laba6/node_modules/nodemailer/postinstall.js +101 -0
- package/Laba6/node_modules/nodemailer-fetch/.eslintrc.js +56 -0
- package/Laba6/node_modules/nodemailer-fetch/.travis.yml +19 -0
- package/Laba6/node_modules/nodemailer-fetch/CHANGELOG.md +30 -0
- package/Laba6/node_modules/nodemailer-fetch/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-fetch/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer-fetch/README.md +55 -0
- package/Laba6/node_modules/nodemailer-fetch/lib/cookies.js +275 -0
- package/Laba6/node_modules/nodemailer-fetch/lib/fetch.js +224 -0
- package/Laba6/node_modules/nodemailer-fetch/package.json +30 -0
- package/Laba6/node_modules/nodemailer-fetch/test/cookies-test.js +391 -0
- package/Laba6/node_modules/nodemailer-fetch/test/fetch-test.js +486 -0
- package/Laba6/node_modules/nodemailer-shared/.eslintrc.js +59 -0
- package/Laba6/node_modules/nodemailer-shared/.travis.yml +18 -0
- package/Laba6/node_modules/nodemailer-shared/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-shared/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer-shared/README.md +14 -0
- package/Laba6/node_modules/nodemailer-shared/lib/shared.js +282 -0
- package/Laba6/node_modules/nodemailer-shared/package.json +36 -0
- package/Laba6/node_modules/nodemailer-shared/test/fixtures/message.html +1 -0
- package/Laba6/node_modules/nodemailer-shared/test/shared-test.js +291 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/.eslintrc.js +59 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/LICENSE +19 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/README.md +7 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/lib/smtp-transport.js +281 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/package.json +37 -0
- package/Laba6/node_modules/nodemailer-wellknown/.travis.yml +17 -0
- package/Laba6/node_modules/nodemailer-wellknown/LICENSE +19 -0
- package/Laba6/node_modules/nodemailer-wellknown/README.md +80 -0
- package/Laba6/node_modules/nodemailer-wellknown/index.js +47 -0
- package/Laba6/node_modules/nodemailer-wellknown/package.json +26 -0
- package/Laba6/node_modules/nodemailer-wellknown/services.json +255 -0
- package/Laba6/node_modules/nodemailer-wellknown/test.js +23 -0
- package/Laba6/node_modules/smtp-connection/.eslintrc.js +56 -0
- package/Laba6/node_modules/smtp-connection/CHANGELOG.md +164 -0
- package/Laba6/node_modules/smtp-connection/Gruntfile.js +27 -0
- package/Laba6/node_modules/smtp-connection/LICENSE +19 -0
- package/Laba6/node_modules/smtp-connection/README.md +200 -0
- package/Laba6/node_modules/smtp-connection/lib/data-stream.js +111 -0
- package/Laba6/node_modules/smtp-connection/lib/smtp-connection.js +1443 -0
- package/Laba6/node_modules/smtp-connection/package.json +41 -0
- package/Laba6/node_modules/underscore/LICENSE +23 -0
- package/Laba6/node_modules/underscore/README.md +22 -0
- package/Laba6/node_modules/underscore/package.json +41 -0
- package/Laba6/node_modules/underscore/underscore-min.js +6 -0
- package/Laba6/node_modules/underscore/underscore.js +1415 -0
- package/Laba6/package-lock.json +94 -0
- package/Laba6/package.json +17 -0
- package/Laba6.txt +31 -0
- package/file1.js +23 -0
- package/file2.js +23 -0
- package/index.html +63 -0
- package/m0603.js +29 -0
- package/m0603Sol/m0603.js +29 -0
- package/m0603Sol/package.json +12 -0
- package/package.json +17 -0
@@ -0,0 +1,681 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com>
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
*/
|
24
|
+
|
25
|
+
var querystring = require ('querystring');
|
26
|
+
var https = require ('https');
|
27
|
+
var http = require ('http');
|
28
|
+
var url = require ('url');
|
29
|
+
var fs = require ('fs');
|
30
|
+
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Generate multipart boundary
|
34
|
+
*
|
35
|
+
* @returns {string}
|
36
|
+
*/
|
37
|
+
|
38
|
+
function generateBoundary () {
|
39
|
+
var boundary = '---------------------------';
|
40
|
+
var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
41
|
+
|
42
|
+
for (var i = 0; i < 29; i++) {
|
43
|
+
boundary += charset.charAt (Math.floor (Math.random () * charset.length));
|
44
|
+
}
|
45
|
+
|
46
|
+
return boundary;
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Extract cookies from headers
|
52
|
+
*
|
53
|
+
* @param headers {object} - Response headers
|
54
|
+
* @returns {array} - Extracted cookie strings
|
55
|
+
*/
|
56
|
+
|
57
|
+
function extractCookies (headers) {
|
58
|
+
var rawcookies = headers['set-cookie'];
|
59
|
+
|
60
|
+
if (!rawcookies) {
|
61
|
+
return [];
|
62
|
+
}
|
63
|
+
|
64
|
+
if (rawcookies == []) {
|
65
|
+
return [];
|
66
|
+
}
|
67
|
+
|
68
|
+
var cookies = [];
|
69
|
+
for (var i = 0; i < rawcookies.length; i++) {
|
70
|
+
var rawcookie = rawcookies[i].split (';');
|
71
|
+
if (rawcookie[0]) {
|
72
|
+
cookies.push (rawcookie[0]);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
return cookies;
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Custom HTTP request
|
81
|
+
*
|
82
|
+
* @callback callback
|
83
|
+
* @param o {object} - Request options
|
84
|
+
* @param callback [function] - Process response
|
85
|
+
* @returns {void}
|
86
|
+
*/
|
87
|
+
|
88
|
+
function doRequest (o, callback) {
|
89
|
+
// support promises and async/await:
|
90
|
+
if (callback === undefined) {
|
91
|
+
return new Promise((resolve, reject) => {
|
92
|
+
doRequest(o, (err, res) => {
|
93
|
+
err ? reject(err) : resolve(res);
|
94
|
+
});
|
95
|
+
});
|
96
|
+
}
|
97
|
+
|
98
|
+
// prevent multiple callbacks
|
99
|
+
var finalCallbackDone = false;
|
100
|
+
function finalCallback (err, res) {
|
101
|
+
if (!finalCallbackDone) {
|
102
|
+
finalCallbackDone = true;
|
103
|
+
callback (err, res);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
if (o.maxRedirects === undefined) {
|
108
|
+
o.maxRedirects = 10;
|
109
|
+
}
|
110
|
+
|
111
|
+
if (o.encodePostParameters === undefined) {
|
112
|
+
o.encodePostParameters = true;
|
113
|
+
}
|
114
|
+
|
115
|
+
var chunks = [];
|
116
|
+
var body; // Buffer
|
117
|
+
var contentType;
|
118
|
+
|
119
|
+
var port;
|
120
|
+
var host;
|
121
|
+
var path;
|
122
|
+
var isHttps = false;
|
123
|
+
|
124
|
+
if (o.proxy) {
|
125
|
+
port = o.proxy.port;
|
126
|
+
host = o.proxy.host;
|
127
|
+
path = o.url; // complete url
|
128
|
+
|
129
|
+
if (o.proxy.protocol && o.proxy.protocol.match (/https/)) {
|
130
|
+
isHttps = true;
|
131
|
+
}
|
132
|
+
} else {
|
133
|
+
var reqUrl = url.parse (o.url);
|
134
|
+
host = reqUrl.hostname;
|
135
|
+
path = reqUrl.path;
|
136
|
+
|
137
|
+
if (reqUrl.protocol === 'https:') {
|
138
|
+
isHttps = true;
|
139
|
+
}
|
140
|
+
|
141
|
+
if (reqUrl.port) {
|
142
|
+
port = reqUrl.port;
|
143
|
+
} else if (isHttps) {
|
144
|
+
port = 443;
|
145
|
+
} else {
|
146
|
+
port = 80;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
if (o.files && o.files.length > 0 && o.method === 'GET') {
|
151
|
+
var err = new Error ('Can\'t send files using GET');
|
152
|
+
err.code = 'CANT_SEND_FILES_USING_GET';
|
153
|
+
return finalCallback (err);
|
154
|
+
}
|
155
|
+
|
156
|
+
if (o.parameters) {
|
157
|
+
if (o.method === 'GET') {
|
158
|
+
path += '?' + querystring.stringify (o.parameters);
|
159
|
+
} else {
|
160
|
+
body = new Buffer (querystring.stringify (o.parameters), 'utf8');
|
161
|
+
contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
if (o.json) {
|
166
|
+
body = new Buffer (JSON.stringify (o.json), 'utf8');
|
167
|
+
contentType = 'application/json';
|
168
|
+
}
|
169
|
+
|
170
|
+
if (o.files) {
|
171
|
+
var crlf = '\r\n';
|
172
|
+
var boundary = generateBoundary ();
|
173
|
+
var separator = '--' + boundary;
|
174
|
+
var bodyArray = new Array (); // temporary body array
|
175
|
+
|
176
|
+
// if the user wants to POST/PUT files, other parameters need to be encoded using 'Content-Disposition'
|
177
|
+
for (var key in o.parameters) {
|
178
|
+
// According to RFC 2388 (https://www.ietf.org/rfc/rfc2388.txt)
|
179
|
+
// "Field names originally in non-ASCII character sets MAY be encoded
|
180
|
+
// within the value of the "name" parameter using the standard method
|
181
|
+
// described in RFC 2047."
|
182
|
+
// -- encodePostParameters -- true by default and MAY be changed by the user
|
183
|
+
var headerKey = o.encodePostParameters ? encodeURIComponent (key) : key;
|
184
|
+
var encodedParameter = separator + crlf
|
185
|
+
+ 'Content-Disposition: form-data; name="' + headerKey + '"' + crlf
|
186
|
+
+ crlf
|
187
|
+
+ o.parameters[key] + crlf;
|
188
|
+
bodyArray.push (new Buffer (encodedParameter));
|
189
|
+
}
|
190
|
+
|
191
|
+
// now for the files:
|
192
|
+
var haveAlreadyAddedAFile = false;
|
193
|
+
|
194
|
+
for (var file in o.files) {
|
195
|
+
var filepath = o.files[file];
|
196
|
+
var filename = filepath.replace (/\\/g, '/').replace (/.*\//, '');
|
197
|
+
|
198
|
+
var encodedFile = separator + crlf
|
199
|
+
+ 'Content-Disposition: form-data; name="' + file + '"; filename="' + filename + '"' + crlf
|
200
|
+
+ 'Content-Type: application/octet-stream' + crlf
|
201
|
+
+ crlf;
|
202
|
+
|
203
|
+
// add crlf before separator if we have already added a file
|
204
|
+
if (haveAlreadyAddedAFile) {
|
205
|
+
encodedFile = crlf + encodedFile;
|
206
|
+
}
|
207
|
+
|
208
|
+
bodyArray.push (new Buffer (encodedFile));
|
209
|
+
|
210
|
+
// add binary file:
|
211
|
+
bodyArray.push (require ('fs').readFileSync (filepath));
|
212
|
+
|
213
|
+
haveAlreadyAddedAFile = true;
|
214
|
+
}
|
215
|
+
|
216
|
+
var footer = crlf + separator + '--' + crlf;
|
217
|
+
bodyArray.push (new Buffer (footer));
|
218
|
+
|
219
|
+
// set body and contentType:
|
220
|
+
body = Buffer.concat (bodyArray);
|
221
|
+
contentType = 'multipart/form-data; boundary=' + boundary;
|
222
|
+
}
|
223
|
+
|
224
|
+
// overwrites the body if the user passes a body:
|
225
|
+
// clears the content-type
|
226
|
+
if (o.body) {
|
227
|
+
body = new Buffer (o.body, 'utf8');
|
228
|
+
contentType = null;
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
var requestoptions = {
|
233
|
+
host: host,
|
234
|
+
port: port,
|
235
|
+
path: path,
|
236
|
+
method: o.method,
|
237
|
+
headers: {}
|
238
|
+
};
|
239
|
+
|
240
|
+
if (!o.redirectCount) {
|
241
|
+
o.redirectCount = 0;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (body) {
|
245
|
+
requestoptions.headers['Content-Length'] = body.length;
|
246
|
+
}
|
247
|
+
|
248
|
+
if (contentType) {
|
249
|
+
requestoptions.headers['Content-Type'] = contentType;
|
250
|
+
}
|
251
|
+
|
252
|
+
if (o.cookies) {
|
253
|
+
requestoptions.headers.Cookie = o.cookies.join ('; ');
|
254
|
+
}
|
255
|
+
|
256
|
+
if (o.rejectUnauthorized !== undefined && isHttps) {
|
257
|
+
requestoptions.rejectUnauthorized = o.rejectUnauthorized;
|
258
|
+
}
|
259
|
+
|
260
|
+
if (isHttps && o.key) {
|
261
|
+
requestoptions.key = o.key;
|
262
|
+
}
|
263
|
+
|
264
|
+
if (isHttps && o.cert) {
|
265
|
+
requestoptions.cert = o.cert;
|
266
|
+
}
|
267
|
+
|
268
|
+
if (isHttps && o.secureProtocol) {
|
269
|
+
requestoptions.secureProtocol = o.secureProtocol;
|
270
|
+
}
|
271
|
+
|
272
|
+
if (isHttps && o.ciphers) {
|
273
|
+
requestoptions.ciphers = o.ciphers;
|
274
|
+
}
|
275
|
+
|
276
|
+
if (isHttps && o.passphrase) {
|
277
|
+
requestoptions.passphrase = o.passphrase;
|
278
|
+
}
|
279
|
+
|
280
|
+
if (isHttps && o.pfx) {
|
281
|
+
requestoptions.pfx = o.pfx;
|
282
|
+
}
|
283
|
+
|
284
|
+
if (isHttps && o.ca) {
|
285
|
+
requestoptions.ca = o.ca;
|
286
|
+
}
|
287
|
+
|
288
|
+
// add custom headers:
|
289
|
+
if (o.headers) {
|
290
|
+
for (var headerkey in o.headers) {
|
291
|
+
requestoptions.headers[headerkey] = o.headers[headerkey];
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
if (o.agent) {
|
296
|
+
requestoptions.agent = o.agent;
|
297
|
+
}
|
298
|
+
|
299
|
+
if (o.auth) {
|
300
|
+
requestoptions.auth = o.auth;
|
301
|
+
}
|
302
|
+
|
303
|
+
if (o.localAddress) {
|
304
|
+
requestoptions.localAddress = o.localAddress;
|
305
|
+
}
|
306
|
+
|
307
|
+
if (o.secureOptions) {
|
308
|
+
requestoptions.secureOptions = o.secureOptions;
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
/**
|
313
|
+
* Process request response
|
314
|
+
*
|
315
|
+
* @param res {object} - Response details
|
316
|
+
* @returns {void}
|
317
|
+
*/
|
318
|
+
|
319
|
+
function requestResponse (res) {
|
320
|
+
var ended = false;
|
321
|
+
var currentsize = 0;
|
322
|
+
|
323
|
+
var downloadstream = null;
|
324
|
+
if (o.downloadlocation) {
|
325
|
+
downloadstream = fs.createWriteStream (o.downloadlocation);
|
326
|
+
}
|
327
|
+
|
328
|
+
res.on ('data', function (chunk) {
|
329
|
+
if (o.downloadlocation) {
|
330
|
+
downloadstream.write (chunk); //write it to disk, not to memory
|
331
|
+
} else {
|
332
|
+
chunks.push (chunk);
|
333
|
+
}
|
334
|
+
|
335
|
+
if (o.progressCallback) {
|
336
|
+
var totalsize = res.headers['content-length'];
|
337
|
+
if (totalsize) {
|
338
|
+
currentsize += chunk.length;
|
339
|
+
|
340
|
+
o.progressCallback (null, {
|
341
|
+
url: o.url,
|
342
|
+
totalsize: totalsize,
|
343
|
+
currentsize: currentsize,
|
344
|
+
percentage: currentsize * 100 / totalsize
|
345
|
+
});
|
346
|
+
} else {
|
347
|
+
o.progressCallback (new Error ('no content-length specified for file, so no progress monitoring possible'));
|
348
|
+
}
|
349
|
+
}
|
350
|
+
});
|
351
|
+
|
352
|
+
res.on ('end', function (err) {
|
353
|
+
ended = true;
|
354
|
+
|
355
|
+
// check for redirects
|
356
|
+
if (res.headers.location && o.allowRedirects) {
|
357
|
+
// Close any open file
|
358
|
+
if (o.downloadlocation) {
|
359
|
+
downloadstream.end ();
|
360
|
+
}
|
361
|
+
|
362
|
+
if (o.redirectCount < o.maxRedirects) {
|
363
|
+
o.redirectCount++;
|
364
|
+
o.url = (new URL(res.headers.location, o.url)).href; // location can be the path only (no base url present)
|
365
|
+
o.cookies = extractCookies (res.headers);
|
366
|
+
return doRequest (o, finalCallback);
|
367
|
+
} else {
|
368
|
+
var err = new Error ('Too many redirects (> ' + o.maxRedirects + ')');
|
369
|
+
err.code = 'TOOMANYREDIRECTS';
|
370
|
+
err.redirects = o.maxRedirects;
|
371
|
+
return finalCallback (err);
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
if (!o.downloadlocation) {
|
376
|
+
var responsebody = Buffer.concat (chunks);
|
377
|
+
if (!o.binary) {
|
378
|
+
responsebody = responsebody.toString ('utf8');
|
379
|
+
}
|
380
|
+
|
381
|
+
finalCallback (null, {
|
382
|
+
headers: res.headers,
|
383
|
+
statusCode: res.statusCode,
|
384
|
+
body: responsebody,
|
385
|
+
cookies: extractCookies (res.headers)
|
386
|
+
});
|
387
|
+
} else {
|
388
|
+
downloadstream.end (null, null, function () {
|
389
|
+
finalCallback (null, {
|
390
|
+
headers: res.headers,
|
391
|
+
statusCode: res.statusCode,
|
392
|
+
downloadlocation: o.downloadlocation,
|
393
|
+
cookies: extractCookies (res.headers)
|
394
|
+
});
|
395
|
+
});
|
396
|
+
}
|
397
|
+
});
|
398
|
+
|
399
|
+
res.on ('close', function () {
|
400
|
+
if (!ended) {
|
401
|
+
finalCallback (new Error ('Request aborted'));
|
402
|
+
}
|
403
|
+
});
|
404
|
+
}
|
405
|
+
|
406
|
+
var request;
|
407
|
+
|
408
|
+
// remove headers with undefined keys or values
|
409
|
+
// else we get an error in Node 0.12.0 about "setHeader ()"
|
410
|
+
for (var headerName in requestoptions.headers) {
|
411
|
+
var headerValue = requestoptions.headers[headerName];
|
412
|
+
if (!headerName || !headerValue) {
|
413
|
+
delete requestoptions.headers[headerName];
|
414
|
+
}
|
415
|
+
}
|
416
|
+
|
417
|
+
if (isHttps) {
|
418
|
+
request = https.request (requestoptions, requestResponse);
|
419
|
+
} else {
|
420
|
+
request = http.request (requestoptions, requestResponse);
|
421
|
+
}
|
422
|
+
|
423
|
+
if (o.timeout) {
|
424
|
+
request.setTimeout (parseInt (o.timeout, 10), function () {
|
425
|
+
var err = new Error ('request timed out');
|
426
|
+
err.code = 'TIMEOUT';
|
427
|
+
finalCallback (err);
|
428
|
+
request.abort ();
|
429
|
+
});
|
430
|
+
}
|
431
|
+
|
432
|
+
request.on ('error', function (err) {
|
433
|
+
finalCallback (err);
|
434
|
+
});
|
435
|
+
|
436
|
+
if (body) {
|
437
|
+
request.write (body);
|
438
|
+
}
|
439
|
+
|
440
|
+
request.end ();
|
441
|
+
}
|
442
|
+
|
443
|
+
exports.doRequest = doRequest;
|
444
|
+
|
445
|
+
|
446
|
+
/**
|
447
|
+
* HTTP GET method
|
448
|
+
*
|
449
|
+
* @callback callback
|
450
|
+
* @param url {string} - Request URL
|
451
|
+
* @param [options] {object} - Request options
|
452
|
+
* @param callback [function] - Process response
|
453
|
+
* @returns {void}
|
454
|
+
*/
|
455
|
+
|
456
|
+
exports.get = function (url, options, callback) {
|
457
|
+
if (callback === undefined && options && typeof options === 'function') {
|
458
|
+
callback = options;
|
459
|
+
}
|
460
|
+
|
461
|
+
if (options === undefined) {
|
462
|
+
options = {};
|
463
|
+
}
|
464
|
+
|
465
|
+
var moreOptions = options;
|
466
|
+
moreOptions.url = url;
|
467
|
+
moreOptions.method = 'GET';
|
468
|
+
|
469
|
+
if (moreOptions.allowRedirects === undefined) {
|
470
|
+
moreOptions.allowRedirects = true;
|
471
|
+
}
|
472
|
+
|
473
|
+
return doRequest (moreOptions, callback);
|
474
|
+
};
|
475
|
+
|
476
|
+
|
477
|
+
/**
|
478
|
+
* HTTP OPTIONS method
|
479
|
+
*
|
480
|
+
* @callback callback
|
481
|
+
* @param url {string} - Request URL
|
482
|
+
* @param [options] {object} - Request options
|
483
|
+
* @param callback [function] - Process response
|
484
|
+
* @returns {void}
|
485
|
+
*/
|
486
|
+
|
487
|
+
exports.options = function (url, options, callback) {
|
488
|
+
if (callback === undefined && options && typeof options === 'function') {
|
489
|
+
callback = options;
|
490
|
+
}
|
491
|
+
|
492
|
+
if (options === undefined) {
|
493
|
+
options = {};
|
494
|
+
}
|
495
|
+
|
496
|
+
var moreOptions = options;
|
497
|
+
moreOptions.url = url;
|
498
|
+
moreOptions.method = 'OPTIONS';
|
499
|
+
return doRequest (moreOptions, callback);
|
500
|
+
};
|
501
|
+
|
502
|
+
|
503
|
+
/**
|
504
|
+
* HTTP POST method
|
505
|
+
*
|
506
|
+
* @callback callback
|
507
|
+
* @param url {string} - Request URL
|
508
|
+
* @param [options] {object} - Request options
|
509
|
+
* @param callback [function] - Process response
|
510
|
+
* @returns {void}
|
511
|
+
*/
|
512
|
+
|
513
|
+
exports.post = function (url, options, callback) {
|
514
|
+
if (callback === undefined && options && typeof options === 'function') {
|
515
|
+
callback = options;
|
516
|
+
}
|
517
|
+
|
518
|
+
if (options === undefined) {
|
519
|
+
options = {};
|
520
|
+
}
|
521
|
+
|
522
|
+
var moreOptions = options;
|
523
|
+
moreOptions.url = url;
|
524
|
+
moreOptions.method = 'POST';
|
525
|
+
return doRequest (moreOptions, callback);
|
526
|
+
};
|
527
|
+
|
528
|
+
|
529
|
+
/**
|
530
|
+
* HTTP PUT method
|
531
|
+
*
|
532
|
+
* @callback callback
|
533
|
+
* @param url {string} - Request URL
|
534
|
+
* @param [options] {object} - Request options
|
535
|
+
* @param callback [function] - Process response
|
536
|
+
* @returns {void}
|
537
|
+
*/
|
538
|
+
|
539
|
+
exports.put = function (url, options, callback) {
|
540
|
+
if (callback === undefined && options && typeof options === 'function') {
|
541
|
+
callback = options;
|
542
|
+
}
|
543
|
+
|
544
|
+
if (options === undefined) {
|
545
|
+
options = {};
|
546
|
+
}
|
547
|
+
|
548
|
+
var moreOptions = options;
|
549
|
+
moreOptions.url = url;
|
550
|
+
moreOptions.method = 'PUT';
|
551
|
+
return doRequest (moreOptions, callback);
|
552
|
+
};
|
553
|
+
|
554
|
+
|
555
|
+
/**
|
556
|
+
* HTTP PATCH method
|
557
|
+
*
|
558
|
+
* @callback callback
|
559
|
+
* @param url {string} - Request URL
|
560
|
+
* @param [options] {object} - Request options
|
561
|
+
* @param callback [function] - Process response
|
562
|
+
* @returns {void}
|
563
|
+
*/
|
564
|
+
|
565
|
+
exports.patch = function (url, options, callback) {
|
566
|
+
if (callback === undefined && options && typeof options === 'function') {
|
567
|
+
callback = options;
|
568
|
+
}
|
569
|
+
|
570
|
+
if (options === undefined) {
|
571
|
+
options = {};
|
572
|
+
}
|
573
|
+
|
574
|
+
var moreOptions = options;
|
575
|
+
moreOptions.url = url;
|
576
|
+
moreOptions.method = 'PATCH';
|
577
|
+
return doRequest (moreOptions, callback);
|
578
|
+
};
|
579
|
+
|
580
|
+
|
581
|
+
/**
|
582
|
+
* HTTP DELETE method
|
583
|
+
*
|
584
|
+
* @callback callback
|
585
|
+
* @param url {string} - Request URL
|
586
|
+
* @param [options] {object} - Request options
|
587
|
+
* @param callback [function] - Process response
|
588
|
+
* @returns {void}
|
589
|
+
*/
|
590
|
+
|
591
|
+
exports.delete = function (url, options, callback) {
|
592
|
+
if (callback === undefined && options && typeof options === 'function') {
|
593
|
+
callback = options;
|
594
|
+
}
|
595
|
+
|
596
|
+
if (options === undefined) {
|
597
|
+
options = {};
|
598
|
+
}
|
599
|
+
|
600
|
+
var moreOptions = options;
|
601
|
+
moreOptions.url = url;
|
602
|
+
moreOptions.method = 'DELETE';
|
603
|
+
return doRequest (moreOptions, callback);
|
604
|
+
};
|
605
|
+
|
606
|
+
|
607
|
+
/**
|
608
|
+
* HTTP HEAD method
|
609
|
+
*
|
610
|
+
* @callback callback
|
611
|
+
* @param url {string} - Request URL
|
612
|
+
* @param [options] {object} - Request options
|
613
|
+
* @param callback [function] - Process response
|
614
|
+
* @returns {void}
|
615
|
+
*/
|
616
|
+
|
617
|
+
exports.head = function (url, options, callback) {
|
618
|
+
if (callback === undefined && options && typeof options === 'function') {
|
619
|
+
callback = options;
|
620
|
+
}
|
621
|
+
|
622
|
+
if (options === undefined) {
|
623
|
+
options = {};
|
624
|
+
}
|
625
|
+
|
626
|
+
var moreOptions = options;
|
627
|
+
moreOptions.url = url;
|
628
|
+
moreOptions.method = 'HEAD';
|
629
|
+
|
630
|
+
if (moreOptions.allowRedirects === undefined) {
|
631
|
+
moreOptions.allowRedirects = true;
|
632
|
+
}
|
633
|
+
|
634
|
+
return doRequest (moreOptions, callback);
|
635
|
+
};
|
636
|
+
|
637
|
+
|
638
|
+
/**
|
639
|
+
* Download a file
|
640
|
+
*
|
641
|
+
* @callback callback
|
642
|
+
* @param url {string} - Request URL
|
643
|
+
* @param downloadlocation {string} - Path where to store file
|
644
|
+
* @param [progressCallback] {function} - Called multiple times during download
|
645
|
+
* @param callback {function} - Called once when download ends
|
646
|
+
* @returns {void}
|
647
|
+
*/
|
648
|
+
|
649
|
+
exports.download = function (url, downloadlocation, progressCallback, callback) {
|
650
|
+
var options = {};
|
651
|
+
options.url = url;
|
652
|
+
options.method = 'GET';
|
653
|
+
options.downloadlocation = downloadlocation;
|
654
|
+
options.allowRedirects = true;
|
655
|
+
|
656
|
+
// if only 3 args are provided, we assume no progressCallback
|
657
|
+
if (callback === undefined && progressCallback && typeof progressCallback === 'function') {
|
658
|
+
callback = progressCallback;
|
659
|
+
} else {
|
660
|
+
options.progressCallback = progressCallback;
|
661
|
+
}
|
662
|
+
|
663
|
+
return doRequest (options, callback);
|
664
|
+
};
|
665
|
+
|
666
|
+
|
667
|
+
/**
|
668
|
+
* Upload files
|
669
|
+
* old function, can still be used
|
670
|
+
*
|
671
|
+
* @callback callback
|
672
|
+
* @param options {object} - Request options
|
673
|
+
* @param callback [function] - Process response
|
674
|
+
* @returns {void}
|
675
|
+
*/
|
676
|
+
|
677
|
+
exports.uploadFiles = function (options, callback) {
|
678
|
+
var moreOptions = options;
|
679
|
+
moreOptions.method = 'POST';
|
680
|
+
return doRequest (moreOptions, callback);
|
681
|
+
};
|