All files / lib/internal/http httpHeaders.ts

81.25% Statements 13/16
0% Branches 0/4
80% Functions 4/5
81.25% Lines 13/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x   1x                                                                                                                             1x                                           19x                         1x       257x     1x       36x                   1x 36x 36x     36x 36x    
import { SideEffect2, pipe } from "@reactive-js/core/lib/functions";
import { Option } from "@reactive-js/core/lib/option";
import { map } from "@reactive-js/core/lib/readonlyArray";
import { HttpHeaders } from "./interfaces";
 
export const enum HttpStandardHeader {
  Accept = "Accept",
  AcceptCharset = "Accept-Charset",
  AcceptEncoding = "Accept-Encoding",
  AcceptLanguage = "Accept-Language",
  AcceptRange = "Accept-Ranges",
  Age = "Age",
  Allow = "Allow",
  Authorization = "Authorization",
  CacheControl = "Cache-Control",
  Connection = "Connection",
  ContentEncoding = "Content-Encoding",
  ContentLanguage = "Content-Language",
  ContentLength = "Content-Length",
  ContentLocation = "Content-Location",
  ContentMD5 = "Content-MD5",
  ContentRange = "Content-Range",
  ContentType = "Content-Type",
  Cookie = "Cookie",
  Date = "Date",
  ETag = "ETag",
  Expect = "Expect",
  Expires = "Expires",
  From = "From",
  Host = "Host",
  IfMatch = "If-Match",
  IfModifiedSince = "If-Modified-Since",
  IfNoneMatch = "If-None-Match",
  IfRange = "If-Range",
  IfUnmodifiedSince = "If-Unmodified-Since",
  LastModified = "Last-Modified",
  Location = "Location",
  MaxForwards = "Max-Forwards",
  Pragma = "Pragma",
  ProxyAuthenticate = "Proxy-Authenticate",
  ProxyAuthorization = "Proxy-Authorization",
  Range = "Range",
  Referer = "Referer",
  RetryAfter = "Retry-After",
  Server = "Server",
  SetCookie = "Set-Cookie",
  TE = "TE",
  railer = "Trailer",
  TransferEncoding = "Transfer-Encoding",
  Upgrade = "Upgrade",
  UserAgent = "User-Agent",
  Vary = "Vary",
  Via = "Via",
  Warning = "Warning",
  WWWAuthenticate = "WWW-Authenticate",
}
 
export const enum HttpExtensionHeader {
  XForwardedProto = "X-Forwarded-Proto",
  XForwardedHost = "X-Forwarded-Host",
  XHttpMethod = "X-HTTP-Method",
  XHttpMethodOverride = "X-HTTP-Method-Override",
  XMethodOverride = "X-Method-Override",
}
 
const bannedHeaders = pipe(
  [
    HttpStandardHeader.Accept,
    HttpStandardHeader.AcceptCharset,
    HttpStandardHeader.AcceptEncoding,
    HttpStandardHeader.AcceptLanguage,
    HttpStandardHeader.CacheControl,
    HttpStandardHeader.ContentEncoding,
    HttpStandardHeader.ContentLength,
    HttpStandardHeader.ContentType,
    HttpStandardHeader.ETag,
    HttpStandardHeader.Expect,
    HttpStandardHeader.Expires,
    HttpStandardHeader.IfMatch,
    HttpStandardHeader.IfNoneMatch,
    HttpStandardHeader.IfModifiedSince,
    HttpStandardHeader.IfUnmodifiedSince,
    HttpStandardHeader.IfRange,
    HttpStandardHeader.LastModified,
    HttpStandardHeader.TransferEncoding,
    HttpStandardHeader.Vary,
  ],
  map(s => s.toLowerCase()),
);
 
export function getHeaderValue(
  headers: HttpHeaders,
  key: HttpStandardHeader,
): Option<string>;
 
export function getHeaderValue(
  headers: HttpHeaders,
  key: HttpExtensionHeader,
): Option<string>;
 
export function getHeaderValue(
  headers: HttpHeaders,
  key: string,
): Option<string> {
  return headers[key.toLowerCase()];
}
 
export const writeHttpHeaders = (
  headers: HttpHeaders,
  writeHeader: SideEffect2<string, string>,
) => {
  for (const header in headers) {
    if (
      headers.hasOwnProperty(header) &&
      !bannedHeaders.includes(header.toLowerCase())
    ) {
      writeHeader(header, headers[header]);
    }
  }
};
 
export const filterHeaders = (headers: HttpHeaders): HttpHeaders => {
  const result: { [key: string]: string } = {};
  const writeHeader = (k: string, v: string) => {
    result[k] = v;
  };
  writeHttpHeaders(headers, writeHeader);
  return result;
};