1 | | /** |
2 | | * @fileOverview http请求的工具操作集,包含请求超时时间设置 |
3 | | * @author waterchestnut |
4 | | * @module tool/httpHelper |
5 | | */ |
6 | | |
7 | 1 | var http = require('http'); |
8 | 1 | var https = require('https'); |
9 | 1 | var qs = require('querystring'); |
10 | 1 | var iconv = require('iconv-lite'); |
11 | 1 | var BufferHelper = require('bufferhelper'); |
12 | | |
13 | | /** |
14 | | * @exports tool/httpHelper |
15 | | */ |
16 | 1 | var 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) { |
33 | 5 | var httpLib = http; |
34 | 5 | if (options.protocol && options.protocol === 'https:') { |
35 | 0 | httpLib = https; |
36 | | } |
37 | 5 | var content = {}; |
38 | 5 | if (options.json) { |
39 | 1 | content = JSON.stringify(data); |
40 | | } else { |
41 | 4 | content = (options.encode && options.encode.toLocaleLowerCase() == 'gbk') ? qs.stringify(data, null, null, {encodeURIComponent: escape}) : qs.stringify(data); |
42 | | } |
43 | 5 | if (options.method.toLowerCase() === 'post') { |
44 | 2 | options.headers = options.headers || {}; |
45 | 2 | options.headers['Content-Type'] = options.json ? 'application/json' : 'application/x-www-form-urlencoded'; |
46 | 2 | options.headers['Content-Length'] = Buffer.byteLength(content); |
47 | | } |
48 | | /** 为true时直接返回数据流 */ |
49 | 5 | options.buffer = options.buffer || false; |
50 | | |
51 | 5 | var req = httpLib.request(options, function (res) { |
52 | 4 | var bufferHelper = new BufferHelper(); |
53 | 4 | res.on('data', function (chunk) { |
54 | 5 | bufferHelper.concat(chunk); |
55 | | }); |
56 | 4 | res.on('end', function () { |
57 | 4 | var _data; |
58 | 4 | if (options.buffer) { |
59 | 1 | _data = bufferHelper.toBuffer(); |
60 | | } |
61 | | else { |
62 | 3 | if (typeof encoding != 'undefined' && encoding !== null) { |
63 | 3 | _data = iconv.decode(bufferHelper.toBuffer(), encoding); |
64 | | } else { |
65 | 0 | _data = iconv.decode(bufferHelper.toBuffer(), 'utf-8'); |
66 | | } |
67 | | } |
68 | 4 | callback(null, _data, res, req); |
69 | | }); |
70 | | }); |
71 | | |
72 | 5 | req.on('error', function (err) { |
73 | 0 | callback(err); |
74 | | }); |
75 | | |
76 | 5 | req.write(content); |
77 | | |
78 | 5 | if (timeout && timeout > 0) { |
79 | 5 | req.setTimeout(timeout, function () { |
80 | 1 | callback(new Error('request timeout'), ''); |
81 | | }); |
82 | | } |
83 | | |
84 | 5 | 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) { |
96 | 2 | var options = require('url').parse(url); |
97 | 2 | options.method = 'GET'; |
98 | 2 | if (header) { |
99 | 1 | options.headers = header; |
100 | | } |
101 | | |
102 | 2 | 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) { |
117 | 2 | var options = require('url').parse(url); |
118 | 2 | options.method = 'POST'; |
119 | 2 | if (header) { |
120 | 1 | options.headers = header; |
121 | | } |
122 | 2 | if (reqEncoding) { |
123 | 1 | options.encode = reqEncoding; |
124 | | } |
125 | 2 | if (json) { |
126 | 1 | options.json = json; |
127 | | } |
128 | 2 | 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 | | |
140 | 1 | module.exports = httpHelper; |