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
|
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package dash
import (
"bytes"
"net/http"
"github.com/google/syzkaller/pkg/html"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
)
// This file contains common middleware for UI handlers (auth, html templates, etc).
type contextHandler func(c context.Context, w http.ResponseWriter, r *http.Request) error
func handlerWrapper(fn contextHandler) http.Handler {
return handleContext(handleAuth(fn))
}
func handleContext(fn contextHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if err := fn(c, w, r); err != nil {
data := &struct {
Header *uiHeader
Error string
}{
Header: commonHeader(c, r),
Error: err.Error(),
}
if err == ErrAccess {
w.WriteHeader(http.StatusForbidden)
err1 := templates.ExecuteTemplate(w, "forbidden.html", data)
if err1 != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
if _, dontlog := err.(ErrDontLog); !dontlog {
log.Errorf(c, "%v", err)
}
w.WriteHeader(http.StatusInternalServerError)
if err1 := templates.ExecuteTemplate(w, "error.html", data); err1 != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
})
}
type ErrDontLog error
func handleAuth(fn contextHandler) contextHandler {
return func(c context.Context, w http.ResponseWriter, r *http.Request) error {
if err := checkAccessLevel(c, r, config.AccessLevel); err != nil {
return err
}
return fn(c, w, r)
}
}
func serveTemplate(w http.ResponseWriter, name string, data interface{}) error {
buf := new(bytes.Buffer)
if err := templates.ExecuteTemplate(buf, name, data); err != nil {
return err
}
w.Write(buf.Bytes())
return nil
}
type uiHeader struct {
LoginLink string
AnalyticsTrackingID string
}
func commonHeader(c context.Context, r *http.Request) *uiHeader {
h := &uiHeader{
AnalyticsTrackingID: config.AnalyticsTrackingID,
}
if user.Current(c) == nil {
h.LoginLink, _ = user.LoginURL(c, r.URL.String())
}
return h
}
var templates = html.CreateGlob("*.html")
|