-
Notifications
You must be signed in to change notification settings - Fork 22
/
handler.go
82 lines (68 loc) · 1.92 KB
/
handler.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package listenerutil
import (
"net/http"
)
type uiRequestFunc func(*http.Request) bool
func (w *ResponseWriter) WriteHeader(statusCode int) {
w.headerWritten = true
w.setCustomResponseHeaders(statusCode)
w.wrapped.WriteHeader(statusCode)
}
func (w *ResponseWriter) Header() http.Header {
return w.wrapped.Header()
}
func (w *ResponseWriter) Write(data []byte) (int, error) {
// The default behavior of http.ResponseWriter.Write is such that if WriteHeader has not
// yet been called, it calls it with the below line. We will copy that logic so that our
// WriteHeader function is called rather than http.ResponseWriter.WriteHeader
if !w.headerWritten {
w.WriteHeader(http.StatusOK)
}
return w.wrapped.Write(data)
}
// Provide Unwrap for users of http.ResponseController
func (w *ResponseWriter) Unwrap() http.ResponseWriter {
return w.wrapped
}
// Implement http.Pusher if available.
func (w *ResponseWriter) Push(target string, opts *http.PushOptions) error {
p, ok := w.wrapped.(http.Pusher)
if ok {
return p.Push(target, opts)
}
return http.ErrNotSupported
}
func (w *ResponseWriter) setCustomResponseHeaders(statusCode int) {
sch := w.headers
if sch == nil {
return
}
// Check the validity of the status code
if statusCode >= 600 || statusCode < 100 {
return
}
// Setter function to set headers
setter := func(headerMap map[string][]string) {
for header, values := range headerMap {
w.Header().Del(header)
for _, value := range values {
w.Header().Add(header, value)
}
}
}
// Setting the default headers first
if val, ok := sch[0]; ok {
setter(val)
}
// Then setting the generic hundred-level headers
// Note: integer division always rounds down, so 499/100 = 4
if val, ok := sch[statusCode/100]; ok {
setter(val)
}
// Finally setting the status-specific headers
if val, ok := sch[statusCode]; ok {
setter(val)
}
}