feat: Support the Forwarded header.

This commit is contained in:
Ruben Verborgh
2020-12-01 21:24:43 +01:00
committed by Joachim Van Herwegen
parent 3362eee2c2
commit ecfe3cfc46
7 changed files with 162 additions and 53 deletions

View File

@@ -378,3 +378,37 @@ export const addHeader = (response: HttpResponse, name: string, value: string |
}
response.setHeader(name, allValues.length === 1 ? allValues[0] : allValues);
};
/**
* The Forwarded header from RFC7239
*/
export interface Forwarded {
/** The user-agent facing interface of the proxy */
by?: string;
/** The node making the request to the proxy */
for?: string;
/** The host request header field as received by the proxy */
host?: string;
/** The protocol used to make the request */
proto?: string;
}
/**
* Parses a Forwarded header value.
*
* @param value - The Forwarded header value.
*
* @returns The parsed Forwarded header.
*/
export const parseForwarded = (value = ''): Forwarded => {
const forwarded: Record<string, string> = {};
if (value) {
for (const pair of value.replace(/\s*,.*$/u, '').split(';')) {
const components = /^(by|for|host|proto)=(.+)$/u.exec(pair);
if (components) {
forwarded[components[1]] = components[2];
}
}
}
return forwarded;
};