UNPKG

885 BJavaScriptView Raw
1"use strict";
2
3module.exports = {
4 getHeaders,
5 normalizeHeaders,
6 getLocationHost,
7};
8
9function getHeaders(request) {
10 if (!request || !request.header) return {};
11
12 const headers = normalizeHeaders(request.header);
13 if (headers.cookie) headers.cookie = cleanCookie(headers.cookie);
14 return headers;
15}
16
17function normalizeHeaders(headers) {
18 const result = {};
19 if (!headers) return result;
20 for (const key in headers) {
21 result[key.toLowerCase()] = headers[key];
22 }
23 return result;
24}
25
26function cleanCookie(cookie) {
27 if (!cookie) return "";
28 if (!/;$/.test(cookie)) return `${cookie};`;
29 return cookie;
30}
31
32function getLocationHost(headers) {
33 let host;
34 if (!headers) return host;
35
36 for (const key in headers) {
37 if (key.toLowerCase() === "host") host = headers[key];
38 if (key.toLowerCase() === "x-forwarded-host") return headers[key];
39 }
40
41 return host;
42}