1 |
|
2 |
|
3 |
|
4 |
|
5 | var http = require('http');
|
6 | var util = require('utils-extend');
|
7 | var url = require('url');
|
8 | var path = require('path');
|
9 | var querystring = require('querystring');
|
10 | var file = require('file-system');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function request(options, callback) {
|
22 | var opts = {
|
23 | headers: {
|
24 | 'Content-Type': 'application/json'
|
25 | },
|
26 | method: 'GET',
|
27 | encoding: 'utf8',
|
28 |
|
29 | isBuffer: false,
|
30 | json: false
|
31 | };
|
32 |
|
33 | if (util.isString(options)) {
|
34 | opts.url = options;
|
35 | } else {
|
36 | util.extend(opts, options);
|
37 | }
|
38 |
|
39 |
|
40 | if (opts.data) {
|
41 | if (opts.method === 'GET') {
|
42 | opts.url += '?' + querystring.stringify(opts.data);
|
43 | } else {
|
44 | opts.data = JSON.stringify(opts.data);
|
45 | opts.headers['Content-Length'] = opts.data.length;
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 | util.extend(opts, util.pick(url.parse(opts.url), 'hostname', 'port', 'path', 'auth'));
|
51 | delete opts.url;
|
52 |
|
53 | var req = http.request(opts, function(res) {
|
54 | var body = [];
|
55 | var size = 0;
|
56 |
|
57 | res.on('data', function(chunk) {
|
58 | body.push(chunk);
|
59 | size += chunk.length;
|
60 | });
|
61 |
|
62 | res.on('end', function() {
|
63 | var result = '';
|
64 |
|
65 |
|
66 | if (opts.isBuffer) {
|
67 | result = Buffer.concat(body, size);
|
68 | } else {
|
69 | body.forEach(function(item) {
|
70 | result += item.toString(opts.encoding);
|
71 | });
|
72 |
|
73 | if (opts.json) {
|
74 | result = JSON.parse(result);
|
75 | }
|
76 | }
|
77 |
|
78 | callback(null, res, result);
|
79 | });
|
80 | });
|
81 |
|
82 | req.on('error', callback);
|
83 |
|
84 | if (opts.method !== 'GET' && opts.data) {
|
85 | req.write(opts.data);
|
86 | }
|
87 |
|
88 | req.end();
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | request.post = function(options, callback) {
|
98 | if (util.isString(options)) {
|
99 | options = {
|
100 | url: options
|
101 | };
|
102 | }
|
103 |
|
104 | options.method = 'POST';
|
105 | request(options, callback);
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | request.download = function(options, callback) {
|
121 | var opts = util.extend({
|
122 | rootPath: '',
|
123 | ignore: false
|
124 | }, options);
|
125 |
|
126 | request({
|
127 | url: opts.url,
|
128 | isBuffer: true
|
129 | }, function(err, res, body) {
|
130 | if (err) return callback(err);
|
131 | if (res.statusCode !== 200) return callback(err, res, body);
|
132 | var destPath;
|
133 | var pathname = url.parse(options.url).pathname.replace(/^\//, '');
|
134 |
|
135 | if (opts.destPath) {
|
136 | if (util.isFunction(opts.destPath)) {
|
137 | destPath = opts.destPath(path.basename(pathname));
|
138 | } else {
|
139 | destPath = opts.destPath;
|
140 | }
|
141 | } else {
|
142 | destPath = path.join(
|
143 | options.rootPath,
|
144 | pathname
|
145 | );
|
146 | }
|
147 |
|
148 | if (opts.ignore) {
|
149 | destPath = destPath.toLowerCase();
|
150 | }
|
151 |
|
152 | file.writeFile(destPath, body, function(err) {
|
153 | if (err) return callback(err);
|
154 |
|
155 | callback(null, res, body, destPath);
|
156 | });
|
157 | });
|
158 | };
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | request.base64 = function(url, callback) {
|
172 | request({
|
173 | url: url,
|
174 | isBuffer: true
|
175 | }, function (err, res, body) {
|
176 | if (err) return callback(err);
|
177 |
|
178 | var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64');
|
179 |
|
180 | callback(err, res, data);
|
181 | });
|
182 | };
|
183 |
|
184 | module.exports = request; |
\ | No newline at end of file |