about summary refs log tree commit diff stats
path: root/src/types/referer.nim
blob: 77cb4e4ddb2d4883700a422bd3c54e2546ed3dfa (plain) (blame)
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
import url
import options

type ReferrerPolicy* = enum
  STRICT_ORIGIN_WHEN_CROSS_ORIGIN
  NO_REFERRER
  NO_REFERRER_WHEN_DOWNGRADE
  STRICT_ORIGIN
  ORIGIN
  SAME_ORIGIN
  ORIGIN_WHEN_CROSS_ORIGIN
  UNSAFE_URL

const DefaultPolicy* = STRICT_ORIGIN_WHEN_CROSS_ORIGIN

proc getReferrerPolicy*(s: string): Option[ReferrerPolicy] =
  case s
  of "no-referrer":
    return some(NO_REFERRER)
  of "no-referrer-when-downgrade":
    return some(NO_REFERRER_WHEN_DOWNGRADE)
  of "origin":
    return some(ORIGIN)
  of "origin-when-cross-origin":
    return some(ORIGIN_WHEN_CROSS_ORIGIN)
  of "same-origin":
    return some(SAME_ORIGIN)
  of "strict-origin":
    return some(STRICT_ORIGIN)
  of "strict-origin-when-cross-origin":
    return some(STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
  of "unsafe-url":
    return some(UNSAFE_URL)

proc getReferer*(prev, target: URL, policy: ReferrerPolicy): string =
  let origin = prev.origin0
  if origin.isNone:
    return ""
  if prev.scheme != "http" and prev.scheme != "https":
    return ""
  if target.scheme != "http" and target.scheme != "https":
    return ""
  case policy
  of NO_REFERRER:
    return ""
  of NO_REFERRER_WHEN_DOWNGRADE:
    if prev.scheme == "https" and target.scheme == "http":
      return ""
    return $origin & prev.pathname & prev.search
  of SAME_ORIGIN:
    if origin == target.origin0:
      return $origin
    return ""
  of ORIGIN:
    return $origin
  of STRICT_ORIGIN:
    if prev.scheme == "https" and target.scheme == "http":
      return ""
    return $origin
  of ORIGIN_WHEN_CROSS_ORIGIN:
    if origin != target.origin0:
      return $origin
    return $origin & prev.pathname & prev.search
  of STRICT_ORIGIN_WHEN_CROSS_ORIGIN:
    if prev.scheme == "https" and target.scheme == "http":
      return $origin
    if origin != target.origin0:
      return $origin
    return $origin & prev.pathname & prev.search
  of UNSAFE_URL:
    return $origin & prev.pathname & prev.search