mm_https 1.5.5 → 1.5.7

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.
Files changed (3) hide show
  1. package/index.js +57 -24
  2. package/package.json +3 -3
  3. package/test.js +22 -16
package/index.js CHANGED
@@ -23,24 +23,7 @@ const {
23
23
  class Http {
24
24
  constructor(config) {
25
25
  this.config = Object.assign({
26
- headers: {
27
- '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',
28
- // 'accept-encoding': 'gzip, deflate, br',
29
- 'Accept-Encoding': 'gzip, deflate',
30
- 'Accept-Language': 'zh-CN,zh;q=0.9',
31
- 'Cache-Control': 'no-cache',
32
- 'Connection': 'keep-alive',
33
- 'Pragma': 'no-cache',
34
- // 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
35
- // 'sec-ch-ua-mobile': '?0',
36
- // 'sec-ch-ua-platform': '"Windows"',
37
- // 'sec-fetch-dest': 'document',
38
- // 'sec-fetch-mode': 'navigate',
39
- // 'sec-fetch-site': 'none',
40
- // 'sec-fetch-user': '?1',
41
- // 'Upgrade-Insecure-Requests': '1',
42
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
43
- },
26
+ headers: "",
44
27
  rejectUnauthorized: false,
45
28
  cookie: "",
46
29
  proxy: null
@@ -48,10 +31,29 @@ class Http {
48
31
  }, config);
49
32
 
50
33
  /**
51
- * cookie缓存
34
+ * cookies 缓存
52
35
  */
53
- this.cookies = {
36
+ this.cookies = {};
54
37
 
38
+ /**
39
+ * headers 协议头
40
+ */
41
+ this.headers = {
42
+ '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
+ 'Accept-Encoding': 'gzip, deflate',
44
+ 'Accept-Language': 'zh-CN,zh;q=0.9',
45
+ 'Cache-Control': 'no-cache',
46
+ 'Connection': 'keep-alive',
47
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
48
+ };
49
+
50
+ if (this.config.cookie) {
51
+ this.set_cookie(this.config.cookie);
52
+ }
53
+
54
+ if (this.config.headers) {
55
+ var headers = this.change_headers(config.headers);
56
+ Object.assign(this.headers, headers);
55
57
  }
56
58
  }
57
59
  }
@@ -79,13 +81,40 @@ Http.prototype.get_cookie = function() {
79
81
  return cookie
80
82
  }
81
83
 
84
+ /**
85
+ * 转换协议头
86
+ * @param {Object|String} headers 协议头
87
+ */
88
+ Http.prototype.change_headers = function(headers) {
89
+ if (headers) {
90
+ if (typeof(headers) == "string") {
91
+ if (headers.indexOf("{") === 0) {
92
+ return headers.toJson();
93
+ } else {
94
+ var dict = {};
95
+ var lines = headers.split('\n');
96
+ for (var i = 0; i < lines.length; i++) {
97
+ var line = lines[i].trim();
98
+ if (line) {
99
+ dict[line.left(":").trim()] = line.right(":").trim();
100
+ }
101
+ }
102
+ return dict;
103
+ }
104
+ } else {
105
+ return headers;
106
+ }
107
+ }
108
+ return {}
109
+ }
110
+
82
111
  Http.prototype.options = function(options) {
83
112
  return {
84
113
  href: "",
85
114
  method: "GET",
86
115
  body: {},
87
116
  proxy: "http://127.0.0.1:10809",
88
- headers: Object.assign({}, this.config.headers)
117
+ headers: Object.assign({}, this.headers)
89
118
  }
90
119
  }
91
120
 
@@ -109,7 +138,7 @@ Http.prototype.http_options = function(options) {
109
138
  body,
110
139
  type,
111
140
  method: method || "GET",
112
- headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
141
+ headers: headers ? Object.assign({}, this.headers, headers) : Object.assign({}, this.headers),
113
142
  agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
114
143
  cookie: this.get_cookie()
115
144
  }, u);
@@ -272,9 +301,11 @@ Http.prototype.request = function(options) {
272
301
  var {
273
302
  headers
274
303
  } = res;
304
+
275
305
  if (res.statusCode == 200) {
276
306
  var chunks = [];
277
307
  var encoding = headers['content-encoding'];
308
+
278
309
  res.on('data', function(d) {
279
310
  chunks.push(d);
280
311
  }).on('end', function(de) {
@@ -283,7 +314,8 @@ Http.prototype.request = function(options) {
283
314
  case 'br':
284
315
  case 'gzip':
285
316
  case 'deflate':
286
- body = unzipSync(buffer).toString();
317
+ buffer = unzipSync(buffer);
318
+ body = buffer.toString();
287
319
  break;
288
320
  default:
289
321
  body = buffer.toString();
@@ -320,6 +352,7 @@ Http.prototype.request = function(options) {
320
352
  });
321
353
 
322
354
  req.on("error", function(err) {
355
+ console.log("错误", err);
323
356
  resolve({
324
357
  status: 0,
325
358
  message: err.message
@@ -427,4 +460,4 @@ if (global.$) {
427
460
  /**
428
461
  * @description 导入构造函数
429
462
  */
430
- module.exports = Http;
463
+ module.exports = Http;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_https",
3
- "version": "1.5.5",
3
+ "version": "1.5.7",
4
4
  "description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "homepage": "https://github.com/qiuwenwu/mm_https#readme",
33
33
  "dependencies": {
34
34
  "https-proxy-agent": "^5.0.1",
35
- "iconv-lite": "^0.5.0",
36
- "mm_expand": "^1.4.7"
35
+ "iconv-lite": "^0.5.2",
36
+ "mm_expand": "^1.5.6"
37
37
  }
38
38
  }
package/test.js CHANGED
@@ -2,11 +2,11 @@
2
2
  var Http = require('./index.js');
3
3
  var fs = require("fs");
4
4
 
5
- async function test(){
6
- var http = new Http();
7
- var res = await http.get("https://www.baidu.com/");
8
- console.log("html", res);
9
- }
5
+ // async function test(){
6
+ // var http = new Http();
7
+ // var res = await http.get("https://www.baidu.com/");
8
+ // console.log("html", res);
9
+ // }
10
10
 
11
11
  // async function test3() {
12
12
  // // 将「微信对话开放平台」得到的信息填入
@@ -140,16 +140,22 @@ async function test(){
140
140
  // test();
141
141
 
142
142
 
143
- // async function download(){
144
- // var hp = new Http();
145
- // // var url = "http://www.haoduoma.com/d/file/p/2021-06-18/76df67e451ca9dec9991588ad7fda6e6.jpg";
146
- // var url = "https://www.copipi.com/p/224/224/becce1fc21d723614b598f5377d5d676.jpg";
147
- // "./二维码/中文".addDir();
148
- // var filename = "./二维码/你好.jpg".fullname();
149
- // var file = await hp.download(url, filename);
150
- // console.log(file);
151
- // }
152
- // download();
143
+ async function download(){
144
+ var hp = new Http({
145
+ headers: `
146
+ Referer: https://www.aieliantong.com/admin/index
147
+ X-Auth-Token: xxx
148
+ `,
149
+ cookie: "Hm_lvt_c73f94a74e41202e67d251e6e3f591cb=1697871513; Hm_lpvt_c73f94a74e41202e67d251e6e3f591cb=1698886851; mm:uuid=3QSuThREzVFdRW6/YrT0o0WpriBhXhMdnLmhtJOXwBg="
150
+ });
151
+ console.log(hp.headers, hp.cookies);
152
+ var url = "https://www.copipi.com/p/224/224/becce1fc21d723614b598f5377d5d676.jpg";
153
+ "./二维码/中文".addDir();
154
+ var filename = "./二维码/qrcode.jpg".fullname();
155
+ var file = await hp.download(url, filename);
156
+ console.log(file);
157
+ }
158
+ download();
153
159
 
154
160
  // async function test() {
155
161
  // var http = new Http();
@@ -205,4 +211,4 @@ async function test(){
205
211
  // // console.log("结果", res.body);
206
212
  // }
207
213
 
208
- test();
214
+ // test();