forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
195 lines (175 loc) · 6.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package assets
import (
"bytes"
"compress/gzip"
"encoding/hex"
"fmt"
"html/template"
"io"
"net/http"
"path"
"regexp"
"sort"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
var varyHeaderRegexp = regexp.MustCompile("\\s*,\\s*")
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
sniffDone bool
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
if !w.sniffDone {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(b))
}
w.sniffDone = true
}
return w.Writer.Write(b)
}
// GzipHandler wraps a http.Handler to support transparent gzip encoding.
func GzipHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Vary", "Accept-Encoding")
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
h.ServeHTTP(w, r)
return
}
// Normalize the Accept-Encoding header for improved caching
r.Header.Set("Accept-Encoding", "gzip")
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
h.ServeHTTP(&gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
})
}
func generateEtag(r *http.Request, version string, varyHeaders []string) string {
varyHeaderValues := ""
for _, varyHeader := range varyHeaders {
varyHeaderValues += r.Header.Get(varyHeader)
}
return fmt.Sprintf("W/\"%s_%s\"", version, hex.EncodeToString([]byte(varyHeaderValues)))
}
func CacheControlHandler(version string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vary := w.Header().Get("Vary")
varyHeaders := []string{}
if vary != "" {
varyHeaders = varyHeaderRegexp.Split(vary, -1)
}
etag := generateEtag(r, version, varyHeaders)
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Add("ETag", etag)
h.ServeHTTP(w, r)
})
}
type LongestToShortest []string
func (s LongestToShortest) Len() int {
return len(s)
}
func (s LongestToShortest) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s LongestToShortest) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}
// HTML5ModeHandler will serve any static assets we know about, all other paths
// are assumed to be HTML5 paths for the console application and index.html will
// be served.
// contextRoot must contain leading and trailing slashes, e.g. /console/
//
// subcontextMap is a map of keys (subcontexts, no leading or trailing slashes) to the asset path (no
// leading slash) to serve for that subcontext if a resource that does not exist is requested
func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h http.Handler, getAsset AssetFunc) (http.Handler, error) {
subcontextData := map[string][]byte{}
subcontexts := []string{}
for subcontext, index := range subcontextMap {
b, err := getAsset(index)
if err != nil {
return nil, err
}
base := path.Join(contextRoot, subcontext)
// Make sure the base always ends in a trailing slash but don't end up with a double trailing slash
if !strings.HasSuffix(base, "/") {
base += "/"
}
b = bytes.Replace(b, []byte(`<base href="/">`), []byte(fmt.Sprintf(`<base href="%s">`, base)), 1)
subcontextData[subcontext] = b
subcontexts = append(subcontexts, subcontext)
}
// Sort by length, longest first
sort.Sort(LongestToShortest(subcontexts))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
urlPath := strings.TrimPrefix(r.URL.Path, "/")
if _, err := getAsset(urlPath); err != nil {
// find the index we want to serve instead
for _, subcontext := range subcontexts {
prefix := subcontext
if subcontext != "" {
prefix += "/"
}
if urlPath == subcontext || strings.HasPrefix(urlPath, prefix) {
w.Write(subcontextData[subcontext])
return
}
}
}
h.ServeHTTP(w, r)
}), nil
}
var configTemplate = template.Must(template.New("webConsoleConfig").Parse(`
window.OPENSHIFT_CONFIG = {
api: {
openshift: {
hostPort: "{{ .MasterAddr | js}}",
prefix: "{{ .MasterPrefix | js}}"
},
k8s: {
hostPort: "{{ .KubernetesAddr | js}}",
prefix: "{{ .KubernetesPrefix | js}}"
}
},
auth: {
oauth_authorize_uri: "{{ .OAuthAuthorizeURI | js}}",
oauth_redirect_base: "{{ .OAuthRedirectBase | js}}",
oauth_client_id: "{{ .OAuthClientID | js}}",
logout_uri: "{{ .LogoutURI | js}}",
}
};
`))
type WebConsoleConfig struct {
// MasterAddr is the host:port the UI should call the master API on. Scheme is derived from the scheme the UI is served on, so they must be the same.
MasterAddr string
// MasterPrefix is the OpenShift API context root
MasterPrefix string
// KubernetesAddr is the host:port the UI should call the kubernetes API on. Scheme is derived from the scheme the UI is served on, so they must be the same.
// TODO this is probably unneeded since everything goes through the openshift master's proxy
KubernetesAddr string
// KubernetesPrefix is the Kubernetes API context root
KubernetesPrefix string
// OAuthAuthorizeURI is the OAuth2 endpoint to use to request an API token. It must support request_type=token.
OAuthAuthorizeURI string
// OAuthRedirectBase is the base URI of the web console. It must be a valid redirect_uri for the OAuthClientID
OAuthRedirectBase string
// OAuthClientID is the OAuth2 client_id to use to request an API token. It must be authorized to redirect to the web console URL.
OAuthClientID string
// LogoutURI is an optional (absolute) URI to redirect to after completing a logout. If not specified, the built-in logout page is shown.
LogoutURI string
}
func GeneratedConfigHandler(config WebConsoleConfig, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.TrimPrefix(r.URL.Path, "/") == "config.js" {
w.Header().Add("Cache-Control", "no-cache, no-store")
w.Header().Add("Content-Type", "application/json")
if err := configTemplate.Execute(w, config); err != nil {
util.HandleError(fmt.Errorf("unable to render config template: %v", err))
}
return
}
h.ServeHTTP(w, r)
})
}