mm_https 1.4.7 → 1.4.9
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/index.js +11 -12
- package/package.json +1 -1
- package/test.js +23 -23
package/index.js
CHANGED
|
@@ -15,9 +15,6 @@ const iconv = require('iconv-lite');
|
|
|
15
15
|
const {
|
|
16
16
|
hostname
|
|
17
17
|
} = require('os');
|
|
18
|
-
const {
|
|
19
|
-
header
|
|
20
|
-
} = require('request/lib/hawk');
|
|
21
18
|
const {
|
|
22
19
|
HttpsProxyAgent
|
|
23
20
|
} = require('https-proxy-agent');
|
|
@@ -71,8 +68,7 @@ Http.prototype.set_cookie = function(cookieStr) {
|
|
|
71
68
|
Http.prototype.get_cookie = function() {
|
|
72
69
|
var dist = this.cookies;
|
|
73
70
|
var str = "";
|
|
74
|
-
for(var k in dist)
|
|
75
|
-
{
|
|
71
|
+
for (var k in dist) {
|
|
76
72
|
str += "; " + k + "=" + dist[k];
|
|
77
73
|
}
|
|
78
74
|
return str;
|
|
@@ -95,7 +91,8 @@ Http.prototype.http_options = function(options) {
|
|
|
95
91
|
body,
|
|
96
92
|
headers,
|
|
97
93
|
method,
|
|
98
|
-
cookie
|
|
94
|
+
cookie,
|
|
95
|
+
type
|
|
99
96
|
} = options;
|
|
100
97
|
var protocol = url.left(":/") || 'http';
|
|
101
98
|
// 移除协议头 取端口号
|
|
@@ -107,7 +104,7 @@ Http.prototype.http_options = function(options) {
|
|
|
107
104
|
// 移除主机地址,取路由路径
|
|
108
105
|
u = u.replace(hostname, "");
|
|
109
106
|
var path = u;
|
|
110
|
-
if(cookie){
|
|
107
|
+
if (cookie) {
|
|
111
108
|
this.set_cookie(cookie);
|
|
112
109
|
}
|
|
113
110
|
return {
|
|
@@ -115,6 +112,7 @@ Http.prototype.http_options = function(options) {
|
|
|
115
112
|
protocol,
|
|
116
113
|
hostname,
|
|
117
114
|
body,
|
|
115
|
+
type,
|
|
118
116
|
port: Number(port),
|
|
119
117
|
path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
120
118
|
method: method || "GET",
|
|
@@ -208,7 +206,9 @@ Http.prototype.request = function(options) {
|
|
|
208
206
|
var body = '';
|
|
209
207
|
var req = hp.request(options, (res) => {
|
|
210
208
|
if (res.statusCode == 200) {
|
|
211
|
-
var {
|
|
209
|
+
var {
|
|
210
|
+
headers
|
|
211
|
+
} = res;
|
|
212
212
|
var buffer = '';
|
|
213
213
|
var encoding = headers['content-encoding'];
|
|
214
214
|
if (encoding) {
|
|
@@ -243,7 +243,6 @@ Http.prototype.request = function(options) {
|
|
|
243
243
|
resolve({
|
|
244
244
|
status: 200,
|
|
245
245
|
headers,
|
|
246
|
-
binary: buffer,
|
|
247
246
|
body: body
|
|
248
247
|
});
|
|
249
248
|
});
|
|
@@ -303,6 +302,7 @@ Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
|
303
302
|
method: "POST",
|
|
304
303
|
url,
|
|
305
304
|
body,
|
|
305
|
+
type,
|
|
306
306
|
headers,
|
|
307
307
|
cookie
|
|
308
308
|
});
|
|
@@ -318,10 +318,9 @@ Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
|
318
318
|
* @return {String} 成功返回保存路径,失败返回null
|
|
319
319
|
*/
|
|
320
320
|
Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
321
|
-
this.encoding = "binary";
|
|
322
321
|
var res = await this.get(url, headers, cookie);
|
|
323
322
|
var file = null;
|
|
324
|
-
if (res.
|
|
323
|
+
if (res.body) {
|
|
325
324
|
if (auto) {
|
|
326
325
|
var arr = url.split('/');
|
|
327
326
|
var fullname = arr[arr.length - 1];
|
|
@@ -341,7 +340,7 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
|
341
340
|
file = filename;
|
|
342
341
|
}
|
|
343
342
|
if (file) {
|
|
344
|
-
var length = await fs.writeFileSync(file, res.
|
|
343
|
+
var length = await fs.writeFileSync(file, res.body, "binary");
|
|
345
344
|
if (length == 0) {
|
|
346
345
|
file = null;
|
|
347
346
|
}
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -75,35 +75,35 @@ var fs = require("fs");
|
|
|
75
75
|
// }
|
|
76
76
|
|
|
77
77
|
// 测试
|
|
78
|
-
async function test() {
|
|
79
|
-
var hp = new Http();
|
|
80
|
-
// var res = await hp.get("http://www.baidu.com");
|
|
81
|
-
// console.log('百度:' + JSON.stringify(res));
|
|
82
|
-
res = await hp.post("http://127.0.0.1:8000/login", {
|
|
83
|
-
'phone': "15817188815",
|
|
84
|
-
'password': "asd123"
|
|
85
|
-
}, null, 'text');
|
|
86
|
-
console.log('登录:' + JSON.stringify(res));
|
|
87
|
-
var token = hp.cookies['x-auth-token'];
|
|
88
|
-
console.log('访问牌:' + token);
|
|
89
|
-
res = await hp.get("http://127.0.0.1:8000/paper/id?id=1", {
|
|
90
|
-
"x-auth-token": token || ""
|
|
91
|
-
});
|
|
92
|
-
console.log('结果:' + JSON.stringify(res));
|
|
93
|
-
console.log('结果:', hp.cookies);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// async function test () {
|
|
78
|
+
// async function test() {
|
|
97
79
|
// var hp = new Http();
|
|
98
|
-
// var res = await hp.
|
|
99
|
-
// console.log(res
|
|
80
|
+
// // var res = await hp.get("http://www.baidu.com");
|
|
81
|
+
// // console.log('百度:' + JSON.stringify(res));
|
|
82
|
+
// res = await hp.post("http://127.0.0.1:8000/login", {
|
|
83
|
+
// 'phone': "15817188815",
|
|
84
|
+
// 'password': "asd123"
|
|
85
|
+
// }, null, 'json');
|
|
86
|
+
// console.log('登录:' + JSON.stringify(res));
|
|
87
|
+
// var token = hp.cookies['x-auth-token'];
|
|
88
|
+
// console.log('访问牌:' + token);
|
|
89
|
+
// // res = await hp.get("http://127.0.0.1:8000/paper/id?id=1", {
|
|
90
|
+
// // "x-auth-token": token || ""
|
|
91
|
+
// // });
|
|
92
|
+
// // console.log('结果:' + JSON.stringify(res));
|
|
93
|
+
// // console.log('结果:', hp.cookies);
|
|
100
94
|
// }
|
|
101
95
|
|
|
96
|
+
async function test () {
|
|
97
|
+
var hp = new Http();
|
|
98
|
+
var res = await hp.get('http://www.hao123.com');
|
|
99
|
+
console.log(res.body);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
102
|
// async function test() {
|
|
103
103
|
// var hp = new Http();
|
|
104
|
-
// hp.encoding = "binary";
|
|
105
104
|
// var res = await hp.get('http://localhost:8080/dev/img/logo_gray.png');
|
|
106
|
-
//
|
|
105
|
+
// console.log(res);
|
|
106
|
+
// fs.writeFile("./logonew.png", res.body, "binary", function(err) {
|
|
107
107
|
// if (err) {
|
|
108
108
|
// console.log("下载失败");
|
|
109
109
|
// }
|