Skip to content

Commit fff8d75

Browse files
ittusdaniel-cottone
authored andcommitted
Support another HTTP Proxy methods (dherault#299)
* Support another HTTP Proxy methods * Support greedy path variable {proxy+} * Update package.json * Add log when using HTTP Proxy
1 parent 0f6389b commit fff8d75

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Shine Li (https://github.com/shineli)",
8787
"Stefan Siegl (https://github.com/stesie)",
8888
"Stewart Gleadow (https://github.com/sgleadow)",
89+
"Thang Minh Vu (https://github.com/ittus)",
8990
"Tuan Minh Huynh (https://github.com/tuanmh)",
9091
"Utku Turunc (https://github.com/utkuturunc)",
9192
"Vasiliy Solovey (https://github.com/miltador)",

src/index.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class Offline {
229229
skipCacheInvalidation: false,
230230
noAuth: false,
231231
corsAllowOrigin: '*',
232-
corsAllowHeaders: 'accept,content-type,x-api-key',
232+
corsAllowHeaders: 'accept,content-type,x-api-key,authorization',
233233
corsAllowCredentials: true,
234234
apiKey: crypto.createHash('md5').digest('hex'),
235235
useSeparateProcesses: false,
@@ -952,35 +952,42 @@ class Offline {
952952
if (!isProxy) {
953953
return this.serverlessLog(`WARNING: Only HTTP_PROXY is supported. Path '${pathResource}' is ignored.`);
954954
}
955-
if (`${method}`.toUpperCase() !== 'GET') {
956-
return this.serverlessLog(`WARNING: ${method} proxy is not supported. Path '${pathResource}' is ignored.`);
957-
}
958955
if (!path) {
959956
return this.serverlessLog(`WARNING: Could not resolve path for '${methodId}'.`);
960957
}
961958

959+
let fullPath = this.options.prefix + (pathResource.startsWith('/') ? pathResource.slice(1) : pathResource);
960+
if (fullPath !== '/' && fullPath.endsWith('/')) fullPath = fullPath.slice(0, -1);
961+
fullPath = fullPath.replace(/\+}/g, '*}');
962+
962963
const proxyUriOverwrite = resourceRoutesOptions[methodId] || {};
963964
const proxyUriInUse = proxyUriOverwrite.Uri || proxyUri;
964965

965966
if (!proxyUriInUse) {
966967
return this.serverlessLog(`WARNING: Could not load Proxy Uri for '${methodId}'`);
967968
}
969+
const routeMethod = method === 'ANY' ? '*' : method;
970+
const routeConfig = {
971+
cors: this.options.corsConfig
972+
}
973+
if (routeMethod !== 'HEAD' && routeMethod !== 'GET') {
974+
routeConfig.payload = { parse: false };
975+
}
968976

969-
this.serverlessLog(`${method} ${pathResource} -> ${proxyUriInUse}`);
970-
977+
this.serverlessLog(`${method} ${fullPath} -> ${proxyUriInUse}`);
971978
this.server.route({
972-
method,
973-
path,
974-
config: { cors: this.options.corsConfig },
979+
method: routeMethod,
980+
path: fullPath,
981+
config: routeConfig,
975982
handler: (request, reply) => {
976983
const params = request.params;
977984
let resultUri = proxyUriInUse;
978985

979986
Object.keys(params).forEach(key => {
980987
resultUri = resultUri.replace(`{${key}}`, params[key]);
981988
});
982-
983-
reply.proxy({ uri: resultUri });
989+
this.serverlessLog(`PROXY ${request.method} ${request.url.path} -> ${resultUri}`);
990+
reply.proxy({ uri: resultUri, passThrough: true });
984991
},
985992
});
986993
});

0 commit comments

Comments
 (0)