Skip to content

Commit

Permalink
fix: auto-unescape query parameters on ALB (#219, #241) (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericgermain authored Jun 9, 2021
1 parent 5cd87c8 commit 8cb4206
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/event-sources/aws/alb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
const url = require('url')
const { getRequestValuesFromEvent, getMultiValueHeaders } = require('../utils')

const getRequestValuesFromAlbEvent = ({ event }) => getRequestValuesFromEvent({ event })
function getPathWithQueryStringUseUnescapeParams ({
event,
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path,
// NOTE: Strip base path for custom domains
stripBasePath = '',
replaceRegex = new RegExp(`^${stripBasePath}`)
}) {
const query = {}
// decode everything back into utf-8 text.
if (event.multiValueQueryStringParameters) {
for (const key in event.multiValueQueryStringParameters) {
const formattedKey = decodeURIComponent(key)
query[formattedKey] = event.multiValueQueryStringParameters[key].map(value => decodeURIComponent(value))
}
} else {
for (const key in event.queryStringParameters) {
const formattedKey = decodeURIComponent(key)
query[formattedKey] = decodeURIComponent(event.queryStringParameters[key])
}
}

return url.format({
pathname: path.replace(replaceRegex, ''),
query
})
}

const getRequestValuesFromAlbEvent = ({ event }) => {
const values = getRequestValuesFromEvent({
event,
path: getPathWithQueryStringUseUnescapeParams({ event })
})
return values
}

const getResponseToAlb = ({
statusCode,
body,
Expand Down

0 comments on commit 8cb4206

Please sign in to comment.