UNPKG

1.01 kBJavaScriptView Raw
1/* @flow */
2'use strict'
3
4/* ::
5import type {
6 BmRequest,
7 Headers,
8 MapObject,
9 Protocol
10} from '../types'
11*/
12
13function keysToLowerCase (
14 object /* : MapObject */
15) /* : MapObject */ {
16 return Object.keys(object).reduce((result, key) => {
17 result[key.toLowerCase()] = object[key]
18 return result
19 }, {})
20}
21
22function normaliseMethod (
23 method /* : string */
24) /* : string */ {
25 return method.toLowerCase()
26}
27
28/**
29https://www.w3.org/TR/url-1/#dom-urlutils-protocol
30protocol ends with ':', same as in Node.js 'url' module
31https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
32*/
33function protocolFromHeaders (
34 headers /* : Headers */
35) /* : Protocol */ {
36 if (headers['x-forwarded-proto'] === 'https') {
37 return `https:`
38 }
39 if (headers.forwarded && ~headers.forwarded.indexOf('proto=https')) {
40 return `https:`
41 }
42 if (headers['front-end-https'] === 'on') {
43 return `https:`
44 }
45 return 'http:'
46}
47
48module.exports = {
49 keysToLowerCase,
50 normaliseMethod,
51 protocolFromHeaders
52}