Skip to content

Commit 8cb4206

Browse files
fix: auto-unescape query parameters on ALB (#219, #241) (#393)
1 parent 5cd87c8 commit 8cb4206

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/event-sources/aws/alb.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1+
const url = require('url')
12
const { getRequestValuesFromEvent, getMultiValueHeaders } = require('../utils')
23

3-
const getRequestValuesFromAlbEvent = ({ event }) => getRequestValuesFromEvent({ event })
4+
function getPathWithQueryStringUseUnescapeParams ({
5+
event,
6+
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
7+
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path,
8+
// NOTE: Strip base path for custom domains
9+
stripBasePath = '',
10+
replaceRegex = new RegExp(`^${stripBasePath}`)
11+
}) {
12+
const query = {}
13+
// decode everything back into utf-8 text.
14+
if (event.multiValueQueryStringParameters) {
15+
for (const key in event.multiValueQueryStringParameters) {
16+
const formattedKey = decodeURIComponent(key)
17+
query[formattedKey] = event.multiValueQueryStringParameters[key].map(value => decodeURIComponent(value))
18+
}
19+
} else {
20+
for (const key in event.queryStringParameters) {
21+
const formattedKey = decodeURIComponent(key)
22+
query[formattedKey] = decodeURIComponent(event.queryStringParameters[key])
23+
}
24+
}
25+
26+
return url.format({
27+
pathname: path.replace(replaceRegex, ''),
28+
query
29+
})
30+
}
31+
32+
const getRequestValuesFromAlbEvent = ({ event }) => {
33+
const values = getRequestValuesFromEvent({
34+
event,
35+
path: getPathWithQueryStringUseUnescapeParams({ event })
36+
})
37+
return values
38+
}
39+
440
const getResponseToAlb = ({
541
statusCode,
642
body,

0 commit comments

Comments
 (0)