-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpsubr.js
159 lines (139 loc) · 4.26 KB
/
httpsubr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// HTTP関連のサブルーチン
var http = require('http'),
iconv = require('iconv'),
url = require('url');
// Bufferを連結する
function concatBuffer(src1 /* , src2, ... */) {
var i, buf, start;
var len = 0;
for (i = 0; i < arguments.length; ++i) {
len += arguments[i].length;
}
buf = new Buffer(len);
start = 0;
for (i = 0; i < arguments.length; ++i) {
var chunk = arguments[i];
chunk.copy(buf, start, 0);
start += chunk.length;
}
return buf;
}
// HTTPレスポンスとBufferからエンコーディングを検出し
// レスポンスボディを文字列で返す
exports.convertCharset = function(response, buf) {
var charset = null;
var content_type = response.headers['content-type'];
if (content_type) {
re = content_type.match(/\bcharset=([\w\-]+)\b/i);
if (re) {
charset = re[1];
}
}
if (!charset) {
var bin = buf.toString('binary');
re = bin.match(/<meta\b[^>]*charset=([\w\-]+)/i);
if (re) {
charset = re[1];
} else {
charset = 'utf-8';
}
}
switch (charset) {
case 'ascii':
case 'utf-8':
return buf.toString(charset);
default:
var ic = new (iconv.Iconv)(charset, 'utf-8');
var buf2 = ic.convert(buf);
return buf2.toString('utf8');
}
};
// 文字列ではなくBufferを返す版の
// requestパッケージ (https://github.com/mikeal/node-utils) のrequest関数
// とほぼ同等な関数
// request関数にある一部機能は実装していない
exports.httpRequest = function(options, callback) {
options = options || {};
if (typeof(options.uri) == 'string') {
options.uri = url.parse(options.uri);
}
options.method = options.method || 'GET';
options.headers = options.headers || {};
options._nRedirect = options._nRedirect || 0;
if (typeof(options.maxRedirects) == 'undefined') {
options.maxRedirects = 10;
}
if (!options.headers.host) {
options.headers.host = options.uri.hostname;
if (options.uri.port) {
options.headers.host += ':' + options.uri.port;
}
}
var port = 80;
var https = false;
if (options.uri.protocol == 'https:') {
port = 443;
https = true;
}
if (options.uri.port) {
port = port;
}
var path = (options.uri.pathname ? options.uri.pathname : '/');
if (options.uri.search) {
path += options.uri.search;
}
if (options.uri.hash) {
path += options.uri.hash;
}
var client = http.createClient(port, options.uri.hostname, https);
client.addListener('error', function(err) {
if (callback) {
callback(err);
} else {
throw err;
}
});
var request = client.request(options.method, path, options.headers);
request.addListener('response', function(response) {
if (response.headers.location) {
if (options._nRedirect++ >= options.maxRedirect) {
client.emit('error', new Error('Too many redirects'));
}
var loc = response.headers.location;
if (!loc.match(/^https?:/i)) {
loc = url.resolve(options.uri.href, response.headers.location);
}
options.uri = loc;
exports.httpRequest(options, callback);
} else {
var chunks = [];
response.on('data', function(chunk) {
chunks.push(chunk);
})
.on('end', function() {
if (callback) {
var buf = concatBuffer.apply({}, chunks);
delete(chunks);
callback(null, response, buf);
}
});
}
});
if (options.requestBody) {
if (typeof(options.requestBody) == 'string') {
request.write(options.requestBody);
request.end();
} else {
sys.pump(options.requestBody, request);
}
} else {
request.end();
}
}
exports.get = exports.httpRequest;
exports.post = function(options, callback) {
if (!options.requestBody) {
options.requestBody = '';
}
exports.request(options, callback);
}