-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
140 lines (128 loc) · 3.27 KB
/
handlers.go
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
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
// SPDX-License-Identifier: Apache-2.0
package consent
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
// ServeHTTP handles a HTTP request
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/healthz":
w.Write([]byte("OK"))
case "/client.js":
h.handleClientScript(w, r)
case "/proxy":
h.handleProxyHost(w, r)
case "/consent":
h.handleConsentRequest(w, r)
default:
http.NotFound(w, r)
}
}
func (h *handler) handleProxyHost(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
if err := h.tpl.Execute(w, h.templateData); err != nil {
http.Error(
w,
fmt.Sprintf("error rendering template: %s", err.Error()),
http.StatusInternalServerError,
)
return
}
}
func (h *handler) handleClientScript(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/javascript")
w.Write(h.clientScript)
}
type payload struct {
Decisions map[scope]interface{} `json:"decisions"`
}
func (h *handler) handleConsentRequest(w http.ResponseWriter, r *http.Request) {
d := decisions{}
referrerURL, err := url.Parse(r.Referer())
if err != nil {
http.Error(
w,
fmt.Sprintf("error parsing Referer header: %s", err.Error()),
http.StatusBadRequest,
)
return
}
referrer := domain(referrerURL.Host)
if c, _ := r.Cookie(h.cookieName); c != nil {
raw, err := url.QueryUnescape(c.Value)
if err != nil {
http.Error(
w,
fmt.Sprintf("error unescaping cookie value: %s", err.Error()),
http.StatusBadRequest,
)
return
}
decisionsFromCookie, err := parseDecisions(raw)
if err != nil {
http.Error(
w,
fmt.Sprintf("error parsing unescaped cookie value: %s", err.Error()),
http.StatusBadRequest,
)
return
}
d.update(decisionsFromCookie)
}
switch r.Method {
case http.MethodGet:
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(payload{Decisions: d[referrer]}); err != nil {
http.Error(
w,
fmt.Sprintf("error encoding response payload: %s", err.Error()),
http.StatusInternalServerError,
)
}
case http.MethodPost:
body := payload{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(
w,
fmt.Sprintf("error decoding body: %s", err.Error()),
http.StatusBadRequest,
)
return
}
d.update(&decisions{referrer: body.Decisions})
encodedBody, err := d.encode()
if err != nil {
http.Error(
w,
fmt.Sprintf("error encoding decisions as cookie: %s", err.Error()),
http.StatusInternalServerError,
)
return
}
http.SetCookie(
w,
h.makeCookie(h.cookieName, url.QueryEscape(encodedBody), time.Now().Add(h.cookieTTL)),
)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&payload{Decisions: d[referrer]}); err != nil {
http.Error(
w,
fmt.Sprintf("error encoding response payload: %s", err.Error()),
http.StatusInternalServerError,
)
}
case http.MethodDelete:
http.SetCookie(
w,
h.makeCookie(h.cookieName, "", time.Now().Add(-time.Hour)),
)
w.WriteHeader(http.StatusNoContent)
default:
http.Error(w, fmt.Sprintf("Method %s not allowed", r.Method), http.StatusMethodNotAllowed)
}
}