Coverage

94%
52
49
3

tool/httpHelper.js

94%
52
49
3
LineHitsSource
1/**
2 * @fileOverview http请求的工具操作集,包含请求超时时间设置
3 * @author waterchestnut
4 * @module tool/httpHelper
5 */
6
71var http = require('http');
81var https = require('https');
91var qs = require('querystring');
101var iconv = require('iconv-lite');
111var BufferHelper = require('bufferhelper');
12
13/**
14 * @exports tool/httpHelper
15 */
161var httpHelper = {
17
18 /**
19 * @description 发起远程请求的基础方法
20 * @param {Object} options 请求选项
21 * @param {String} [options.protocol='http'] 请求协议
22 * @param {String} [options.method='get'] 请求方法,get、post...
23 * @param {Object=} options.headers 请求头
24 * @param {String=} options.encode 请求数据的编码格式,如果是gbk,使用escape编码
25 * @param {Boolean=} [options.json=false] 发送的是否json数据
26 * @param {Boolean=} [options.buffer=false] 是否直接返回二进制数据
27 * @param {Number=} timeout 超时时间,单位为毫秒
28 * @param {Object=} data 请求发送的数据对象
29 * @param {RequestCallback} callback 处理请求响应的回调方法,查看 {@link RequestCallback}
30 * @param {String} [encoding='utf-8'] 编码格式
31 */
32 request: function (options, timeout, data, callback, encoding) {
335 var httpLib = http;
345 if (options.protocol && options.protocol === 'https:') {
350 httpLib = https;
36 }
375 var content = {};
385 if (options.json) {
391 content = JSON.stringify(data);
40 } else {
414 content = (options.encode && options.encode.toLocaleLowerCase() == 'gbk') ? qs.stringify(data, null, null, {encodeURIComponent: escape}) : qs.stringify(data);
42 }
435 if (options.method.toLowerCase() === 'post') {
442 options.headers = options.headers || {};
452 options.headers['Content-Type'] = options.json ? 'application/json' : 'application/x-www-form-urlencoded';
462 options.headers['Content-Length'] = Buffer.byteLength(content);
47 }
48 /** 为true时直接返回数据流 */
495 options.buffer = options.buffer || false;
50
515 var req = httpLib.request(options, function (res) {
524 var bufferHelper = new BufferHelper();
534 res.on('data', function (chunk) {
545 bufferHelper.concat(chunk);
55 });
564 res.on('end', function () {
574 var _data;
584 if (options.buffer) {
591 _data = bufferHelper.toBuffer();
60 }
61 else {
623 if (typeof encoding != 'undefined' && encoding !== null) {
633 _data = iconv.decode(bufferHelper.toBuffer(), encoding);
64 } else {
650 _data = iconv.decode(bufferHelper.toBuffer(), 'utf-8');
66 }
67 }
684 callback(null, _data, res, req);
69 });
70 });
71
725 req.on('error', function (err) {
730 callback(err);
74 });
75
765 req.write(content);
77
785 if (timeout && timeout > 0) {
795 req.setTimeout(timeout, function () {
801 callback(new Error('request timeout'), '');
81 });
82 }
83
845 req.end();
85 },
86
87 /**
88 * @description 以GET的方式发起远程请求
89 * @param {String} url 请求地址
90 * @param {Number=} timeout 超时时间,单位为毫秒
91 * @param {RequestCallback} callback 处理请求响应的回调方法,查看 {@link RequestCallback}
92 * @param {String} [encoding='utf-8'] 编码格式
93 * @param {Object=} header 请求头对象
94 */
95 get: function (url, timeout, callback, encoding, header) {
962 var options = require('url').parse(url);
972 options.method = 'GET';
982 if (header) {
991 options.headers = header;
100 }
101
1022 this.request(options, timeout, {}, callback, encoding);
103 },
104
105 /**
106 * @description 以POST的方式发起远程请求
107 * @param {String} url 请求地址
108 * @param {Number=} timeout 超时时间,单位为毫秒
109 * @param {Object=} data 请求发送的数据对象
110 * @param {RequestCallback} callback 处理请求响应的回调方法,查看 {@link RequestCallback}
111 * @param {String} [encoding='utf-8'] 编码格式
112 * @param {Object=} header 请求头对象
113 * @param {String=} reqEncoding 请求数据的编码格式,如果是gbk,使用escape编码
114 * @param {Boolean=} [json=false] 发送的是否json数据
115 */
116 post: function (url, timeout, data, callback, encoding, header, reqEncoding, json) {
1172 var options = require('url').parse(url);
1182 options.method = 'POST';
1192 if (header) {
1201 options.headers = header;
121 }
1222 if (reqEncoding) {
1231 options.encode = reqEncoding;
124 }
1252 if (json) {
1261 options.json = json;
127 }
1282 this.request(options, timeout, data, callback, encoding);
129 }
130};
131
132/**
133 * @description 处理请求响应的回调方法
134 * @callback RequestCallback
135 * @param {Object} err 请求或响应的错误对象
136 * @param {string} data 响应的数据
137 * @param {Object} res 响应流对象
138 */
139
1401module.exports = httpHelper;