mm_https 1.6.3 → 1.6.5
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/README.md +244 -220
- package/index.js +204 -171
- package/npm-publish-checklist.md +99 -0
- package/package.json +15 -13
- package/test.js +162 -209
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileOverview
|
|
2
|
+
* @fileOverview HTTP请求模块
|
|
3
3
|
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
4
|
* @version 1.2
|
|
5
5
|
*/
|
|
@@ -8,37 +8,38 @@ const https = require('https');
|
|
|
8
8
|
const Url = require('url');
|
|
9
9
|
const HttpProxyAgent = require('https-proxy-agent');
|
|
10
10
|
const fs = require("fs");
|
|
11
|
-
const Cookie = require('./cookie');
|
|
12
11
|
const {
|
|
13
12
|
unzipSync
|
|
14
13
|
} = require('zlib');
|
|
15
14
|
const iconv = require('iconv-lite');
|
|
16
|
-
|
|
17
|
-
hostname
|
|
18
|
-
} = require('os');
|
|
19
|
-
const {
|
|
20
|
-
HttpsProxyAgent
|
|
21
|
-
} = require('https-proxy-agent');
|
|
15
|
+
require('mm_expand');
|
|
22
16
|
|
|
17
|
+
/**
|
|
18
|
+
* HTTP客户端类
|
|
19
|
+
* @class
|
|
20
|
+
*/
|
|
23
21
|
class Http {
|
|
22
|
+
/**
|
|
23
|
+
* 创建HTTP客户端实例
|
|
24
|
+
* @param {Object} config - 配置选项
|
|
25
|
+
*/
|
|
24
26
|
constructor(config) {
|
|
25
|
-
this.
|
|
27
|
+
this._config = Object.assign({
|
|
26
28
|
headers: "",
|
|
27
29
|
rejectUnauthorized: false,
|
|
28
30
|
cookie: "",
|
|
29
31
|
proxy: null
|
|
30
|
-
// proxy: "http://127.0.0.1:10809"
|
|
31
32
|
}, config);
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
|
-
* cookies
|
|
35
|
+
* cookies缓存
|
|
35
36
|
*/
|
|
36
|
-
this.
|
|
37
|
+
this._cookies = {};
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
|
-
* headers
|
|
40
|
+
* headers协议头
|
|
40
41
|
*/
|
|
41
|
-
this.
|
|
42
|
+
this._headers = {
|
|
42
43
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
43
44
|
'Accept-Encoding': 'gzip, deflate',
|
|
44
45
|
'Accept-Language': 'zh-CN,zh;q=0.9',
|
|
@@ -47,79 +48,102 @@ class Http {
|
|
|
47
48
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
if (this.
|
|
51
|
-
this.
|
|
51
|
+
if (this._config.cookie) {
|
|
52
|
+
this.setCookie(this._config.cookie);
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
if (this.
|
|
55
|
-
var headers = this.
|
|
56
|
-
Object.assign(this.
|
|
55
|
+
if (this._config.headers) {
|
|
56
|
+
var headers = this.changeHeaders(config.headers);
|
|
57
|
+
Object.assign(this._headers, headers);
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
/**
|
|
63
|
+
* 设置Cookie
|
|
64
|
+
* @param {Object|String} cookies - Cookie字符串
|
|
65
|
+
*/
|
|
66
|
+
Http.prototype.setCookie = function (cookies) {
|
|
67
|
+
if (typeof (cookies) == "string") {
|
|
68
|
+
var arr = cookies.split('; ');
|
|
69
|
+
var name;
|
|
70
|
+
if (arr.length > 0) {
|
|
71
|
+
for (var i = 0; i < arr.length; i++) {
|
|
72
|
+
var kv = arr[i];
|
|
73
|
+
var kvArr = kv.split("=");
|
|
74
|
+
name = kvArr[0];
|
|
75
|
+
this._cookies[name] = encodeURIComponent(kvArr[1]);
|
|
76
|
+
}
|
|
70
77
|
}
|
|
71
78
|
}
|
|
79
|
+
else if (typeof (cookies) == "object") {
|
|
80
|
+
Object.assign(this._cookies, cookies);
|
|
81
|
+
}
|
|
72
82
|
}
|
|
73
83
|
|
|
74
|
-
|
|
75
|
-
|
|
84
|
+
/**
|
|
85
|
+
* 获取Cookie字符串
|
|
86
|
+
* @returns {string} Cookie字符串
|
|
87
|
+
*/
|
|
88
|
+
Http.prototype.getCookie = function () {
|
|
89
|
+
var cookies = this._cookies;
|
|
76
90
|
var str = "";
|
|
77
|
-
for (var
|
|
78
|
-
str += "; " +
|
|
91
|
+
for (var key in cookies) {
|
|
92
|
+
str += "; " + key + "=" + cookies[key];
|
|
79
93
|
}
|
|
80
|
-
var
|
|
81
|
-
return
|
|
94
|
+
var cookieStr = str.replace("; ", "");
|
|
95
|
+
return cookieStr;
|
|
82
96
|
}
|
|
83
97
|
|
|
84
98
|
/**
|
|
85
99
|
* 转换协议头
|
|
86
|
-
* @param {Object|
|
|
100
|
+
* @param {Object|string} headers - 协议头
|
|
101
|
+
* @returns {Object} 转换后的协议头对象
|
|
87
102
|
*/
|
|
88
|
-
Http.prototype.
|
|
103
|
+
Http.prototype.changeHeaders = function (headers) {
|
|
89
104
|
if (headers) {
|
|
90
|
-
if (typeof(headers) == "string") {
|
|
105
|
+
if (typeof (headers) == "string") {
|
|
91
106
|
if (headers.indexOf("{") === 0) {
|
|
92
107
|
return headers.toJson();
|
|
93
108
|
} else {
|
|
94
|
-
var
|
|
109
|
+
var headerDict = {};
|
|
95
110
|
var lines = headers.split('\n');
|
|
96
111
|
for (var i = 0; i < lines.length; i++) {
|
|
97
112
|
var line = lines[i].trim();
|
|
98
113
|
if (line) {
|
|
99
|
-
|
|
114
|
+
headerDict[line.left(":").trim()] = line.right(":").trim();
|
|
100
115
|
}
|
|
101
116
|
}
|
|
102
|
-
return
|
|
117
|
+
return headerDict;
|
|
103
118
|
}
|
|
104
119
|
} else {
|
|
105
120
|
return headers;
|
|
106
121
|
}
|
|
107
122
|
}
|
|
108
|
-
return {}
|
|
123
|
+
return {};
|
|
109
124
|
}
|
|
110
125
|
|
|
111
|
-
|
|
126
|
+
/**
|
|
127
|
+
* 获取默认选项
|
|
128
|
+
* @returns {Object} 默认选项对象
|
|
129
|
+
*/
|
|
130
|
+
Http.prototype.options = function () {
|
|
112
131
|
return {
|
|
113
132
|
href: "",
|
|
114
133
|
method: "GET",
|
|
115
134
|
body: {},
|
|
116
135
|
proxy: "http://127.0.0.1:10809",
|
|
117
|
-
headers: Object.assign({}, this.
|
|
136
|
+
headers: Object.assign({}, this._headers)
|
|
118
137
|
}
|
|
119
138
|
}
|
|
120
139
|
|
|
121
|
-
|
|
122
|
-
|
|
140
|
+
/**
|
|
141
|
+
* 生成HTTP选项
|
|
142
|
+
* @param {Object} options - 选项参数
|
|
143
|
+
* @returns {Object} HTTP选项对象
|
|
144
|
+
*/
|
|
145
|
+
Http.prototype.httpOptions = function (options) {
|
|
146
|
+
var cfg = this._config;
|
|
123
147
|
var {
|
|
124
148
|
href,
|
|
125
149
|
body,
|
|
@@ -128,96 +152,104 @@ Http.prototype.http_options = function(options) {
|
|
|
128
152
|
cookie,
|
|
129
153
|
type
|
|
130
154
|
} = options;
|
|
131
|
-
var
|
|
155
|
+
var urlObj = Url.parse(href, true);
|
|
132
156
|
if (cookie) {
|
|
133
|
-
this.
|
|
157
|
+
this.setCookie(cookie);
|
|
134
158
|
}
|
|
135
|
-
var
|
|
159
|
+
var protocol = href.left(":/") || 'http';
|
|
136
160
|
return Object.assign({}, {
|
|
137
|
-
|
|
161
|
+
hostProtocol: protocol,
|
|
138
162
|
body,
|
|
139
163
|
type,
|
|
140
164
|
method: method || "GET",
|
|
141
|
-
headers: headers ? Object.assign({}, this.
|
|
142
|
-
agent:
|
|
143
|
-
cookie: this.
|
|
144
|
-
},
|
|
165
|
+
headers: headers ? Object.assign({}, this._headers, headers) : Object.assign({}, this._headers),
|
|
166
|
+
agent: cfg.proxy ? new HttpProxyAgent(cfg.proxy) : null,
|
|
167
|
+
cookie: this.getCookie()
|
|
168
|
+
}, urlObj);
|
|
145
169
|
}
|
|
146
170
|
|
|
147
|
-
|
|
171
|
+
/**
|
|
172
|
+
* 处理POST选项
|
|
173
|
+
* @param {Object} op - 选项对象
|
|
174
|
+
*/
|
|
175
|
+
Http.prototype.postOption = function (op) {
|
|
148
176
|
var param = op.body;
|
|
149
|
-
var
|
|
150
|
-
var
|
|
151
|
-
var
|
|
152
|
-
if (
|
|
153
|
-
if (!
|
|
154
|
-
|
|
155
|
-
} else if (
|
|
156
|
-
|
|
157
|
-
} else if (
|
|
158
|
-
|
|
159
|
-
} else if (
|
|
160
|
-
|
|
177
|
+
var bodyStr = "";
|
|
178
|
+
var paramType = typeof (param);
|
|
179
|
+
var contentType = op.type;
|
|
180
|
+
if (paramType === "object") {
|
|
181
|
+
if (!contentType) {
|
|
182
|
+
contentType = 'application/json; charset=UTF-8';
|
|
183
|
+
} else if (contentType.indexOf('json') !== -1) {
|
|
184
|
+
contentType = "application/json; charset=UTF-8";
|
|
185
|
+
} else if (contentType.indexOf('html') !== -1) {
|
|
186
|
+
contentType = "text/html; charset=UTF-8";
|
|
187
|
+
} else if (contentType.indexOf('form') !== -1) {
|
|
188
|
+
contentType = "application/x-www-form-urlencoded";
|
|
161
189
|
} else {
|
|
162
|
-
|
|
190
|
+
contentType = "text/plain; charset=UTF-8";
|
|
163
191
|
}
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
} else if (
|
|
167
|
-
|
|
192
|
+
if (contentType.indexOf('/json') !== -1) {
|
|
193
|
+
bodyStr = JSON.stringify(param);
|
|
194
|
+
} else if (contentType.indexOf('form') !== -1) {
|
|
195
|
+
bodyStr = $.toUrl(param);
|
|
168
196
|
} else {
|
|
169
|
-
|
|
197
|
+
bodyStr = $.toXml(param);
|
|
170
198
|
}
|
|
171
199
|
} else {
|
|
172
|
-
|
|
173
|
-
if (!
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
} else if (
|
|
177
|
-
|
|
178
|
-
} else if (
|
|
179
|
-
|
|
200
|
+
bodyStr = param.trim();
|
|
201
|
+
if (!contentType) {
|
|
202
|
+
if (bodyStr.startsWith('{') && bodyStr.endsWith('}')) {
|
|
203
|
+
contentType = "application/json; charset=UTF-8";
|
|
204
|
+
} else if (bodyStr.startsWith('[') && bodyStr.endsWith(']')) {
|
|
205
|
+
contentType = "application/json; charset=UTF-8";
|
|
206
|
+
} else if (bodyStr.indexOf("<html>") !== -1) {
|
|
207
|
+
contentType = "text/html";
|
|
180
208
|
} else {
|
|
181
|
-
|
|
209
|
+
contentType = "text/plain; charset=UTF-8";
|
|
182
210
|
}
|
|
183
211
|
}
|
|
184
212
|
}
|
|
185
|
-
op.body =
|
|
213
|
+
op.body = bodyStr;
|
|
186
214
|
if (!op.headers['Content-Type']) {
|
|
187
|
-
op.headers['Content-Type'] =
|
|
215
|
+
op.headers['Content-Type'] = contentType;
|
|
188
216
|
}
|
|
189
|
-
op.headers['Content-Length'] = Buffer.byteLength(
|
|
217
|
+
op.headers['Content-Length'] = Buffer.byteLength(bodyStr);
|
|
190
218
|
}
|
|
191
219
|
|
|
192
220
|
/**
|
|
193
|
-
*
|
|
194
|
-
* @param {Object} options
|
|
195
|
-
* @
|
|
221
|
+
* HTTP请求
|
|
222
|
+
* @param {Object} options - 配置选项
|
|
223
|
+
* @returns {Promise<Object>} 执行结果
|
|
196
224
|
*/
|
|
197
|
-
Http.prototype.req = async function(options) {
|
|
198
|
-
var op = this.
|
|
225
|
+
Http.prototype.req = async function (options) {
|
|
226
|
+
var op = this.httpOptions(options);
|
|
199
227
|
if (op.method.toLocaleUpperCase() === "POST") {
|
|
200
|
-
this.
|
|
228
|
+
this.postOption(op);
|
|
201
229
|
}
|
|
202
230
|
var res = await this.request(op);
|
|
203
231
|
if (res.status.code == 302 || res.status.code == 307) {
|
|
204
|
-
var
|
|
205
|
-
var
|
|
206
|
-
if (
|
|
207
|
-
var len =
|
|
232
|
+
var redirectUrl = res.headers.location;
|
|
233
|
+
var cookieList = res.headers['set-cookie'];
|
|
234
|
+
if (cookieList) {
|
|
235
|
+
var len = cookieList.length;
|
|
208
236
|
for (var i = 0; i < len; i++) {
|
|
209
|
-
this.
|
|
237
|
+
this.setCookie(cookieList[i]);
|
|
210
238
|
}
|
|
211
239
|
}
|
|
212
|
-
options.href =
|
|
213
|
-
op = this.
|
|
240
|
+
options.href = redirectUrl;
|
|
241
|
+
op = this.httpOptions(options);
|
|
214
242
|
op.referer = options.href;
|
|
215
243
|
res = await this.request(op);
|
|
216
244
|
}
|
|
217
245
|
return res;
|
|
218
246
|
}
|
|
219
247
|
|
|
220
|
-
|
|
248
|
+
/**
|
|
249
|
+
* 解压数据
|
|
250
|
+
* @param {string} encoding - 编码方式
|
|
251
|
+
*/
|
|
252
|
+
Http.prototype.undata = function (encoding) {
|
|
221
253
|
var bufarr = [];
|
|
222
254
|
var errored = false;
|
|
223
255
|
var zpipe;
|
|
@@ -226,23 +258,23 @@ Http.prototype.undata = function(encoding) {
|
|
|
226
258
|
} else {
|
|
227
259
|
zpipe = zlib.createInflate();
|
|
228
260
|
}
|
|
229
|
-
zpipe.on('data', function(d) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
.on('end', function() {
|
|
261
|
+
zpipe.on('data', function (d) {
|
|
262
|
+
bufarr.push(d);
|
|
263
|
+
})
|
|
264
|
+
.on('end', function () {
|
|
233
265
|
if (errored) return;
|
|
234
266
|
errored = true;
|
|
235
267
|
cb(null, response, enc ? Buffer.concat(bufarr).toString(
|
|
236
268
|
enc) : Buffer.concat(bufarr));
|
|
237
269
|
})
|
|
238
|
-
.on('error', function(err) {
|
|
270
|
+
.on('error', function (err) {
|
|
239
271
|
if (errored) return;
|
|
240
272
|
errored = true;
|
|
241
273
|
cb(err, response, null);
|
|
242
274
|
});
|
|
243
275
|
response.pipe(zpipe);
|
|
244
276
|
response
|
|
245
|
-
.on('error', function(err) {
|
|
277
|
+
.on('error', function (err) {
|
|
246
278
|
if (errored) return;
|
|
247
279
|
errored = true;
|
|
248
280
|
cb(err, response, null);
|
|
@@ -250,15 +282,16 @@ Http.prototype.undata = function(encoding) {
|
|
|
250
282
|
}
|
|
251
283
|
|
|
252
284
|
/**
|
|
253
|
-
*
|
|
254
|
-
* @param {Object} options
|
|
285
|
+
* 发送请求
|
|
286
|
+
* @param {Object} options - 配置选项
|
|
287
|
+
* @returns {Promise<Object>} 请求结果
|
|
255
288
|
*/
|
|
256
|
-
Http.prototype.request = function(options) {
|
|
257
|
-
var
|
|
258
|
-
var
|
|
259
|
-
return new Promise(function(resolve, reject) {
|
|
289
|
+
Http.prototype.request = function (options) {
|
|
290
|
+
var protocol = options.hostProtocol === "https" ? https : http;
|
|
291
|
+
var self = this;
|
|
292
|
+
return new Promise(function (resolve, reject) {
|
|
260
293
|
var body = '';
|
|
261
|
-
var req =
|
|
294
|
+
var req = protocol.request(options, (res) => {
|
|
262
295
|
var {
|
|
263
296
|
headers
|
|
264
297
|
} = res;
|
|
@@ -267,9 +300,9 @@ Http.prototype.request = function(options) {
|
|
|
267
300
|
var chunks = [];
|
|
268
301
|
var encoding = headers['content-encoding'];
|
|
269
302
|
|
|
270
|
-
res.on('data', function(
|
|
271
|
-
chunks.push(
|
|
272
|
-
}).on('end', function(
|
|
303
|
+
res.on('data', function (chunk) {
|
|
304
|
+
chunks.push(chunk);
|
|
305
|
+
}).on('end', function () {
|
|
273
306
|
var buffer = Buffer.concat(chunks);
|
|
274
307
|
switch (encoding) {
|
|
275
308
|
case 'br':
|
|
@@ -282,20 +315,20 @@ Http.prototype.request = function(options) {
|
|
|
282
315
|
body = buffer.toString();
|
|
283
316
|
break;
|
|
284
317
|
}
|
|
285
|
-
var
|
|
286
|
-
if (
|
|
318
|
+
var charsetMatch = body.match(/charset=gb2312"/i);
|
|
319
|
+
if (charsetMatch) {
|
|
287
320
|
body = iconv.decode(buffer, 'gb2312');
|
|
288
321
|
} else {
|
|
289
|
-
|
|
290
|
-
if (
|
|
322
|
+
charsetMatch = body.match(/charset=GBK"/i);
|
|
323
|
+
if (charsetMatch) {
|
|
291
324
|
body = iconv.decode(buffer, 'GBK');
|
|
292
325
|
}
|
|
293
326
|
}
|
|
294
|
-
var
|
|
295
|
-
if (
|
|
296
|
-
var len =
|
|
327
|
+
var cookieList = headers['set-cookie'];
|
|
328
|
+
if (cookieList) {
|
|
329
|
+
var len = cookieList.length;
|
|
297
330
|
for (var i = 0; i < len; i++) {
|
|
298
|
-
|
|
331
|
+
self.setCookie(cookieList[i]);
|
|
299
332
|
}
|
|
300
333
|
}
|
|
301
334
|
resolve({
|
|
@@ -317,8 +350,8 @@ Http.prototype.request = function(options) {
|
|
|
317
350
|
}
|
|
318
351
|
});
|
|
319
352
|
|
|
320
|
-
req.on("error", function(err) {
|
|
321
|
-
|
|
353
|
+
req.on("error", function (err) {
|
|
354
|
+
$.log.debug("错误", err);
|
|
322
355
|
resolve({
|
|
323
356
|
status: 0,
|
|
324
357
|
message: err.message
|
|
@@ -333,13 +366,13 @@ Http.prototype.request = function(options) {
|
|
|
333
366
|
}
|
|
334
367
|
|
|
335
368
|
/**
|
|
336
|
-
*
|
|
337
|
-
* @param {
|
|
338
|
-
* @param {Object} headers 请求头
|
|
339
|
-
* @param {
|
|
340
|
-
* @
|
|
369
|
+
* GET请求
|
|
370
|
+
* @param {string} url - 请求地址
|
|
371
|
+
* @param {Object} headers - 请求头
|
|
372
|
+
* @param {string} cookie - Cookie字符串
|
|
373
|
+
* @returns {Promise<Object>} 响应结果
|
|
341
374
|
*/
|
|
342
|
-
Http.prototype.get = async function(url, headers, cookie) {
|
|
375
|
+
Http.prototype.get = async function (url, headers, cookie) {
|
|
343
376
|
return await this.req({
|
|
344
377
|
method: "GET",
|
|
345
378
|
href: url,
|
|
@@ -348,17 +381,16 @@ Http.prototype.get = async function(url, headers, cookie) {
|
|
|
348
381
|
});
|
|
349
382
|
};
|
|
350
383
|
|
|
351
|
-
|
|
352
384
|
/**
|
|
353
|
-
*
|
|
354
|
-
* @param {
|
|
355
|
-
* @param {Object} body
|
|
356
|
-
* @param {Object} headers 请求头
|
|
357
|
-
* @param {
|
|
358
|
-
* @param {
|
|
359
|
-
* @
|
|
385
|
+
* POST请求
|
|
386
|
+
* @param {string} url - 请求地址
|
|
387
|
+
* @param {Object} body - 提交参数
|
|
388
|
+
* @param {Object} headers - 请求头
|
|
389
|
+
* @param {string} type - 传参方式 json, form, text
|
|
390
|
+
* @param {string} cookie - Cookie字符串
|
|
391
|
+
* @returns {Promise<Object>} 响应结果
|
|
360
392
|
*/
|
|
361
|
-
Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
393
|
+
Http.prototype.post = async function (url, body, headers, type, cookie) {
|
|
362
394
|
return await this.req({
|
|
363
395
|
method: "POST",
|
|
364
396
|
href: url,
|
|
@@ -370,60 +402,61 @@ Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
|
370
402
|
};
|
|
371
403
|
|
|
372
404
|
/**
|
|
373
|
-
*
|
|
374
|
-
* @param {
|
|
375
|
-
* @param {
|
|
376
|
-
* @param {
|
|
377
|
-
* @param {Object} headers 协议头
|
|
378
|
-
* @param {
|
|
379
|
-
* @
|
|
405
|
+
* 下载文件
|
|
406
|
+
* @param {string} url - 下载地址
|
|
407
|
+
* @param {string} filename - 文件名
|
|
408
|
+
* @param {string} auto - 自动类型
|
|
409
|
+
* @param {Object} headers - 协议头
|
|
410
|
+
* @param {string} cookie - Cookie字符串
|
|
411
|
+
* @returns {Promise<string>} 成功返回保存路径,失败返回null
|
|
380
412
|
*/
|
|
381
|
-
Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
413
|
+
Http.prototype.download = async function (url, filename, auto, headers, cookie) {
|
|
382
414
|
var res = await this.get(url, headers, cookie);
|
|
383
|
-
var
|
|
415
|
+
var filePath = null;
|
|
384
416
|
if (res.body) {
|
|
385
417
|
if (auto) {
|
|
386
|
-
var
|
|
387
|
-
var
|
|
388
|
-
if (
|
|
389
|
-
var
|
|
390
|
-
if (
|
|
391
|
-
|
|
418
|
+
var parts = url.split('/');
|
|
419
|
+
var fullName = parts[parts.length - 1];
|
|
420
|
+
if (fullName.indexOf('.') !== -1) {
|
|
421
|
+
var ext = fullName.right('.');
|
|
422
|
+
if (ext) {
|
|
423
|
+
filePath = filename + "." + ext;
|
|
392
424
|
}
|
|
393
425
|
} else {
|
|
394
|
-
var
|
|
395
|
-
var
|
|
396
|
-
.replace("audio/",
|
|
397
|
-
|
|
398
|
-
file = filename + "." + type;
|
|
426
|
+
var contentType = res.headers["content-type"];
|
|
427
|
+
var ext = contentType.replace("image/", "").replace("application/x-", "").replace("text/", "")
|
|
428
|
+
.replace("audio/", "").replace("jpeg", "jpg");
|
|
429
|
+
filePath = filename + "." + ext;
|
|
399
430
|
}
|
|
400
431
|
} else {
|
|
401
|
-
|
|
432
|
+
filePath = filename;
|
|
402
433
|
}
|
|
403
|
-
if (
|
|
404
|
-
var
|
|
405
|
-
if (
|
|
406
|
-
|
|
434
|
+
if (filePath) {
|
|
435
|
+
var bytesWritten = await fs.writeFileSync(filePath, res.buffer, "binary");
|
|
436
|
+
if (bytesWritten == 0) {
|
|
437
|
+
filePath = null;
|
|
407
438
|
}
|
|
408
439
|
} else {
|
|
409
|
-
|
|
440
|
+
$.log.debug('保存文件名(filename)不能为!');
|
|
410
441
|
}
|
|
411
442
|
}
|
|
412
|
-
return
|
|
443
|
+
return filePath;
|
|
413
444
|
};
|
|
414
445
|
|
|
415
446
|
/**
|
|
416
447
|
* 清除缓存
|
|
417
448
|
*/
|
|
418
|
-
Http.prototype.clear = function() {
|
|
419
|
-
this.
|
|
449
|
+
Http.prototype.clear = function () {
|
|
450
|
+
this._cookies = {};
|
|
420
451
|
};
|
|
421
452
|
|
|
422
|
-
if (global.$) {
|
|
453
|
+
if (global.$ && !$.Http) {
|
|
423
454
|
$.Http = Http;
|
|
424
455
|
}
|
|
425
456
|
|
|
426
457
|
/**
|
|
427
|
-
*
|
|
458
|
+
* 导出构造函数
|
|
428
459
|
*/
|
|
429
|
-
module.exports =
|
|
460
|
+
module.exports = {
|
|
461
|
+
Http
|
|
462
|
+
};
|