-
Notifications
You must be signed in to change notification settings - Fork 13
/
handlers.go
175 lines (144 loc) · 3.62 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
package main
import (
"io"
"net/http"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
)
func NewSSLRedirectHandler(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
dest := "https://" + r.Host + r.URL.Path
if r.URL.RawQuery != "" {
dest += "?" + r.URL.RawQuery
}
http.Redirect(w, r, dest, http.StatusTemporaryRedirect)
return
}
next.ServeHTTP(w, r)
}
}
type HostDispatchingHandler struct {
hosts map[string]http.Handler
}
func NewHostDispatchingHandler() *HostDispatchingHandler {
return &HostDispatchingHandler{
hosts: make(map[string]http.Handler),
}
}
func (h *HostDispatchingHandler) HandleHost(host string, handler http.Handler) {
h.hosts[host] = handler
}
func (h *HostDispatchingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler, ok := h.hosts[getHost(r)]
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
handler.ServeHTTP(w, r)
}
func NewBasicAuthHandler(users []User, next http.Handler) http.HandlerFunc {
m := make(map[string]string)
for _, u := range users {
m[u.Name] = u.Password
}
return func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok {
challenge(w, r)
return
}
p, ok := m[username]
if !ok {
challenge(w, r)
return
}
if password != p {
challenge(w, r)
return
}
next.ServeHTTP(w, r)
}
}
func NewWebsiteHandler(next http.Handler, cfg *s3.GetBucketWebsiteOutput) http.HandlerFunc {
suffix := cfg.IndexDocument.Suffix
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if path == "" || path[len(path)-1] == '/' {
r.URL.Path += *suffix
}
next.ServeHTTP(w, r)
}
}
func NewProxyHandler(proxy S3Proxy, prefix string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if prefix != "" {
path = "/" + prefix + path
}
obj, err := proxy.Get(path)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case s3.ErrCodeNoSuchBucket, s3.ErrCodeNoSuchKey:
http.Error(w, err.Error(), http.StatusNotFound)
default:
http.Error(w, err.Error(), http.StatusUnauthorized)
}
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
setHeader(w, "Cache-Control", s2s(obj.CacheControl))
setHeader(w, "Content-Disposition", s2s(obj.ContentDisposition))
setHeader(w, "Content-Encoding", s2s(obj.ContentEncoding))
setHeader(w, "Content-Language", s2s(obj.ContentLanguage))
setHeader(w, "Content-Length", i2s(obj.ContentLength))
setHeader(w, "Content-Range", s2s(obj.ContentRange))
setHeader(w, "Content-Type", s2s(obj.ContentType))
setHeader(w, "ETag", s2s(obj.ETag))
setHeader(w, "Expires", s2s(obj.Expires))
setHeader(w, "Last-Modified", t2s(obj.LastModified))
io.Copy(w, obj.Body)
}
}
func challenge(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="`+getHost(r)+`"`)
http.Error(w, "", http.StatusUnauthorized)
}
func getHost(r *http.Request) string {
host := r.Header.Get("Host")
if host == "" {
host = r.Host
}
return host
}
func s2s(s *string) string {
if s != nil {
return *s
} else {
return ""
}
}
func i2s(i *int64) string {
if i != nil {
return strconv.FormatInt(*i, 10)
} else {
return ""
}
}
func t2s(t *time.Time) string {
if t != nil {
return t.UTC().Format(http.TimeFormat)
} else {
return ""
}
}
func setHeader(w http.ResponseWriter, key, value string) {
if value != "" {
w.Header().Add(key, value)
}
}