@cloud-cli/px 3.3.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/proxy.js +9 -7
- package/package.json +1 -1
package/dist/proxy.js
CHANGED
|
@@ -105,14 +105,15 @@ export class ProxyServer {
|
|
|
105
105
|
const url = new URL(req.url, target);
|
|
106
106
|
const isCorsPreflight = req.method === 'OPTIONS' && proxyEntry.cors;
|
|
107
107
|
if (isCorsPreflight) {
|
|
108
|
-
|
|
109
|
-
res.writeHead(204, '
|
|
108
|
+
setCorsHeaders(req, res);
|
|
109
|
+
res.writeHead(204, { 'Content-Length': '0' });
|
|
110
110
|
res.end();
|
|
111
111
|
return;
|
|
112
112
|
}
|
|
113
113
|
const proxyRequest = (url.protocol === 'https:' ? httpsRequest : httpRequest)(url, { method: req.method });
|
|
114
114
|
setHeaders(req, proxyRequest);
|
|
115
115
|
proxyRequest.setHeader('host', getHost(target));
|
|
116
|
+
proxyRequest.setHeader('x-forwarded-for', req.headers.host);
|
|
116
117
|
req.on('data', (chunk) => proxyRequest.write(chunk));
|
|
117
118
|
req.on('end', () => proxyRequest.end());
|
|
118
119
|
proxyRequest.on('error', (error) => handleError(error, res));
|
|
@@ -120,7 +121,7 @@ export class ProxyServer {
|
|
|
120
121
|
setHeaders(proxyRes, res);
|
|
121
122
|
const isCorsSimple = req.method !== 'OPTIONS' && proxyEntry.cors && req.headers.origin;
|
|
122
123
|
if (isCorsSimple) {
|
|
123
|
-
|
|
124
|
+
setCorsHeaders(req, res);
|
|
124
125
|
}
|
|
125
126
|
res.writeHead(proxyRes.statusCode, proxyRes.statusMessage);
|
|
126
127
|
proxyRes.on('data', (chunk) => res.write(chunk));
|
|
@@ -179,10 +180,11 @@ function getHost(string) {
|
|
|
179
180
|
const url = new URL(string);
|
|
180
181
|
return url.hostname + (url.port ? ':' + url.port : '');
|
|
181
182
|
}
|
|
182
|
-
function
|
|
183
|
-
const corsOrigin = new URL(req.headers.origin);
|
|
184
|
-
res.setHeader('
|
|
183
|
+
function setCorsHeaders(req, res) {
|
|
184
|
+
const corsOrigin = new URL(req.headers.origin).origin;
|
|
185
|
+
res.setHeader('Vary', 'Origin');
|
|
186
|
+
res.setHeader('Access-Control-Allow-Origin', corsOrigin);
|
|
185
187
|
res.setHeader('Access-Control-Allow-Headers', '*');
|
|
186
|
-
res.setHeader('Access-Control-Allow-Methods', '
|
|
188
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
|
|
187
189
|
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
|
188
190
|
}
|