Comparing version 1.3.3 to 2.0.0
@@ -12,7 +12,7 @@ 'use strict'; | ||
//上传文件最大数据量,JS限制最大字符串不能超过 1073741824 | ||
this.max_upload_size = 1073000000; | ||
//上传文件最大数据量 | ||
this.max_upload_size = 2000000000; | ||
//单个文件最大上传大小 | ||
this.max_file_size = 220000000; | ||
this.max_file_size = 1000000000; | ||
@@ -61,3 +61,3 @@ this.mime_map = { | ||
bodymaker.prototype.makeUploadData = function (r) { | ||
bodymaker.prototype.makeUploadData = async function (r) { | ||
var bdy = this.boundary(); | ||
@@ -74,10 +74,13 @@ | ||
var bodyfi = {}; | ||
var header_data = ''; | ||
var payload = ''; | ||
var body_data = Buffer.from(formData).toString('binary'); | ||
var content_length = Buffer.byteLength(formData); | ||
var end_data = `\r\n--${bdy}--\r\n`; | ||
content_length += Buffer.byteLength(end_data); | ||
if (r.files && typeof r.files === 'object') { | ||
let t = ''; | ||
let tmp = ''; | ||
for (var k in r.files) { | ||
@@ -89,2 +92,3 @@ if (typeof r.files[k] === 'string') { | ||
} | ||
let fst = null; | ||
for (let i=0; i<t.length; i++) { | ||
@@ -94,17 +98,55 @@ header_data = `Content-Disposition: form-data; name=${'"'}${k}${'"'}; filename=${'"'}${t[i]}${'"'}\r\nContent-Type: ${this.mimeType(t[i])}`; | ||
payload = `\r\n--${bdy}\r\n${header_data}\r\n\r\n`; | ||
tmp = fs.readFileSync(t[i], {encoding:'binary'}); | ||
content_length += Buffer.byteLength(payload) + tmp.length; | ||
body_data += Buffer.from(payload).toString('binary') + tmp; | ||
tmp = ''; | ||
content_length += Buffer.byteLength(payload); | ||
try { | ||
fst = fs.statSync(t[i]); | ||
content_length += fst.size; | ||
} catch (err) { | ||
console.log(err); | ||
continue ; | ||
} | ||
bodyfi[ t[i] ] = { | ||
payload : payload, | ||
length: fst.size | ||
}; | ||
} | ||
} | ||
} | ||
var end_data = `\r\n--${bdy}--\r\n`; | ||
content_length += Buffer.byteLength(end_data); | ||
body_data += Buffer.from(end_data).toString('binary'); | ||
var seek = 0; | ||
var bodyData = Buffer.alloc(content_length); | ||
seek = Buffer.from(formData).copy(bodyData); | ||
let fd = -1; | ||
for(let f in bodyfi) { | ||
seek += Buffer.from(bodyfi[f].payload).copy(bodyData, seek); | ||
try { | ||
fd = fs.openSync(f); | ||
await new Promise((rv, rj) => { | ||
fs.read(fd, bodyData, seek, bodyfi[f].length, 0, (err, bytesRead, buffer) => { | ||
if (err) { | ||
rj(err); | ||
} else { | ||
seek += bytesRead; | ||
rv(bytesRead); | ||
} | ||
}); | ||
}); | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
if (fd > 0) { | ||
fs.close(fd, err => {}); | ||
} | ||
} | ||
} | ||
Buffer.from(end_data).copy(bodyData, seek); | ||
return { | ||
'content-type' : `multipart/form-data; boundary=${bdy}`, | ||
'body' : body_data, | ||
'body' : bodyData, | ||
'content-length' : content_length | ||
@@ -111,0 +153,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
const hcli = (require('../httpcli.js')()); | ||
const hcli = require('../httpcli.js'); | ||
for(let i=0; i<600; i++) { | ||
for(let i=0; i<2000; i++) { | ||
hcli.get('http://localhost:2019/') | ||
@@ -14,3 +14,3 @@ .then(data => { | ||
hcli.post('http://localhost:2019/p', { | ||
hcli.post('https://localhost:2019/p', { | ||
body : {user : 'brave'} | ||
@@ -17,0 +17,0 @@ }) |
/** | ||
* gohttp 1.3.3 | ||
* gohttp 2.0.0 | ||
* Copyright (c) [2019.08] BraveWang | ||
@@ -27,10 +27,21 @@ * This software is licensed under the MPL-2.0. | ||
ignoretls: true, | ||
//不验证证书,针对HTTPS | ||
ignoreTLSAuth : true, | ||
//ignoreTLSAuth : true, | ||
set ignoreTLSAuth (b) { | ||
if (b) { | ||
this.config.ignoretls = true; | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | ||
} else { | ||
this.config.ignoretls = false; | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "1"; | ||
} | ||
} | ||
}; | ||
//针对HTTPS协议,不验证证书 | ||
if (this.config.ignoreTLSAuth) { | ||
/* if (this.config.ignoreTLSAuth) { | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | ||
} | ||
} */ | ||
@@ -55,3 +66,3 @@ this.bodymaker = new bodymaker(options); | ||
} | ||
if (u.protocol === 'https:' && this.config.ignoreTLSAuth) { | ||
if (u.protocol === 'https:' && this.config.ignoretls) { | ||
urlobj.requestCert = false; | ||
@@ -78,3 +89,3 @@ urlobj.rejectUnauthorized = false; | ||
gohttp.prototype.request = function (url, options = {}) { | ||
gohttp.prototype.request = async function (url, options = {}) { | ||
var opts = this.parseUrl(url); | ||
@@ -146,19 +157,19 @@ if (typeof options !== 'object') { options = {}; } | ||
postState.isPost = true; | ||
switch (opts.headers['content-type']) { | ||
case 'application/x-www-form-urlencoded': | ||
postData.body = qs.stringify(opts.body); break; | ||
postData.body = Buffer.from(qs.stringify(opts.body)); break; | ||
case 'multipart/form-data': | ||
postState.isUpload = true; | ||
postData = this.bodymaker.makeUploadData(opts.body); | ||
postData = await this.bodymaker.makeUploadData(opts.body); | ||
opts.headers['content-type'] = postData['content-type']; | ||
break; | ||
default: | ||
postData.body = JSON.stringify(opts.body); | ||
postData.body = Buffer.from(JSON.stringify(opts.body)); | ||
} | ||
} | ||
if (postState.isPost && !postState.isUpload) { | ||
postData['content-type'] = opts.headers['content-type']; | ||
postData['content-length'] = Buffer.byteLength(postData.body); | ||
postData['content-length'] = postData.body.length; | ||
} | ||
@@ -173,2 +184,3 @@ | ||
} | ||
return this._coreRequest(opts, postData, postState, writeStream); | ||
@@ -219,3 +231,4 @@ }; | ||
if (postState.isPost) { | ||
r.write(postData.body, postState.isUpload ? 'binary' : 'utf8'); | ||
//r.write(postData.body, postState.isUpload ? 'binary' : 'utf8'); | ||
r.write(postData.body); | ||
} | ||
@@ -419,3 +432,2 @@ r.end(); | ||
module.exports = gohttp; | ||
module.exports = new gohttp(); |
{ | ||
"name": "gohttp", | ||
"version": "1.3.3", | ||
"version": "2.0.0", | ||
"description": "http client for HTTP/1.1", | ||
@@ -5,0 +5,0 @@ "main": "httpcli.js", |
52289
8
555
5