-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
204 lines (171 loc) · 5.38 KB
/
index.js
File metadata and controls
204 lines (171 loc) · 5.38 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict'
const pump = require('pump')
const lru = require('tiny-lru')
const Stream = require('stream')
const buildRequest = require('./lib/request')
const {
filterPseudoHeaders,
copyHeaders,
stripHttp1ConnectionHeaders,
filterHeaders,
buildURL,
populateHeaders
} = require('./lib/utils')
function fastProxy (opts = {}) {
const { request, close } = buildRequest({
...opts
})
const cache = getCacheStorage(opts.cacheURLs)
const base = opts.base
return {
close,
proxy (req, res, source, opts) {
opts = opts || {}
const reqOpts = opts.request || {}
const onResponse = opts.onResponse
const onClientConnectionTerminated = opts.onClientConnectionTerminated || onClientConnectionTerminatedNoOp
const rewriteHeaders = opts.rewriteHeaders || rewriteHeadersNoOp
const rewriteRequestHeaders = opts.rewriteRequestHeaders || rewriteRequestHeadersNoOp
let url
try {
url = getReqUrl(source || req.url, cache, base, opts)
} catch (err) {
res.statusCode = 400
res.end('Bad Request')
return
}
const sourceHttp2 = req.httpVersionMajor === 2
let headers = { ...sourceHttp2 ? filterPseudoHeaders(req.headers) : req.headers }
if (!headers.host) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
// @TODO: Should add performance-aware host header value validation(regex-based) as a further step?
res.statusCode = 400
res.end()
return
}
headers['x-forwarded-host'] = headers.host
headers.host = url.hostname
if (url.port) {
headers.host += `:${url.port}`
}
const qs = getQueryString(url.search, req.url, opts)
let body = null
// according to https://tools.ietf.org/html/rfc2616#section-4.3
// proxy should ignore message body when it's a GET or HEAD request
// when proxy this request, we should reset the content-length to make it a valid http request
if (req.method === 'GET' || req.method === 'HEAD') {
if (headers['content-length']) {
headers = filterHeaders(headers, 'content-length')
}
} else {
if (req.body) {
if (req.body instanceof Stream) {
body = req.body
} else if (typeof req.body === 'string') {
body = req.body
populateHeaders(headers, body, 'text/plain')
} else if (headers['content-type'] === 'application/x-www-form-urlencoded') {
body = new URLSearchParams(req.body).toString()
populateHeaders(headers, body, 'application/x-www-form-urlencoded')
} else {
body = JSON.stringify(req.body)
populateHeaders(headers, body, 'application/json')
}
} else {
body = req
}
}
const reqParams = {
method: req.method,
url,
qs,
headers: rewriteRequestHeaders(req, headers),
body,
request: reqOpts
}
request(reqParams, (err, response) => {
if (!res.socket || res.socket.destroyed || res.writableEnded) {
return onClientConnectionTerminated(res, err, response)
}
if (err) {
if (err.code === 'ECONNREFUSED') {
res.statusCode = 503
res.end('Service Unavailable')
} else if (
err.code === 'ECONNRESET' ||
err.code === 'UND_ERR_HEADERS_TIMEOUT' ||
err.code === 'UND_ERR_BODY_TIMEOUT') {
res.statusCode = 504
res.end('Gateway Timeout')
} else {
res.statusCode = 500
res.end('Bad Gateway')
}
return
}
// destructing response from remote
const { headers, statusCode, stream } = response
// Strip hop-by-hop headers for all responses (HTTP/1.1 and HTTP/2).
// Per RFC 7230 §6.1, hop-by-hop headers MUST NOT be forwarded by proxies.
const safeHeaders = stripHttp1ConnectionHeaders(headers)
if (sourceHttp2) {
copyHeaders(rewriteHeaders(safeHeaders), res)
} else {
copyHeaders(rewriteHeaders(safeHeaders), res)
}
// set origin response code
res.statusCode = statusCode
if (onResponse) {
onResponse(req, res, stream)
} else {
pump(stream, res)
}
})
}
}
}
function getQueryString (search, reqUrl, opts) {
if (opts.queryString) {
return '?' + new URLSearchParams(opts.queryString).toString()
}
if (search.length > 0) {
return search
}
const queryIndex = reqUrl.indexOf('?')
if (queryIndex > 0) {
return reqUrl.slice(queryIndex)
}
return ''
}
function rewriteHeadersNoOp (headers) {
return headers
}
function onClientConnectionTerminatedNoOp (_err, response) {
}
function rewriteRequestHeadersNoOp (req, headers) {
return headers
}
function getCacheStorage (size) {
if (size === 0) {
return null
}
return lru(size || 100)
}
function getReqUrl (source, cache, base, opts) {
const reqBase = opts.base || base
let url
if (cache) {
const cacheKey = reqBase + source
url = cache.get(cacheKey)
if (!url) {
url = buildURL(source, reqBase)
cache.set(cacheKey, url)
}
} else {
url = buildURL(source, reqBase)
}
return url
}
module.exports = fastProxy
module.exports.default = fastProxy
module.exports.fastProxy = fastProxy