-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
300 lines (273 loc) · 8.54 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package httputil
import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/shurcooL/httperror"
"github.com/shurcooL/users"
"golang.org/x/net/http/httpguts"
)
// ErrorHandler factors error handling out of the HTTP handler.
// If users is nil, it treats all requests as made by an unauthenticated user.
func ErrorHandler(
users interface {
// GetAuthenticated fetches the currently authenticated user,
// or User{UserSpec: UserSpec{ID: 0}} if there is no authenticated user.
GetAuthenticated(context.Context) (users.User, error)
},
handler func(w http.ResponseWriter, req *http.Request) error,
) http.Handler {
if users == nil {
users = noUsers{}
}
return &errorHandler{handler: handler, users: users}
}
type errorHandler struct {
handler func(w http.ResponseWriter, req *http.Request) error
users interface {
GetAuthenticated(context.Context) (users.User, error)
}
}
func (h *errorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
rw := &headerResponseWriter{ResponseWriter: w, Flusher: w.(http.Flusher)}
err := h.handler(rw, req)
handleError(w, req, err, h.users, rw.WroteHeader)
}
// ErrorHandleMaybe factors error handling out of the HTTP maybe handler.
// If users is nil, it treats all requests as made by an unauthenticated user.
func ErrorHandleMaybe(
w http.ResponseWriter, req *http.Request,
users interface {
// GetAuthenticated fetches the currently authenticated user,
// or User{UserSpec: UserSpec{ID: 0}} if there is no authenticated user.
GetAuthenticated(context.Context) (users.User, error)
},
// maybeHandler serves an HTTP request, if it matches.
// It returns httperror.NotHandle if the HTTP request was explicitly not handled.
maybeHandler func(w http.ResponseWriter, req *http.Request) error,
) (ok bool) {
if users == nil {
users = noUsers{}
}
rw := &headerResponseWriter{ResponseWriter: w, Flusher: w.(http.Flusher)}
err := maybeHandler(rw, req)
if err == httperror.NotHandle {
if rw.WroteHeader {
panic(fmt.Errorf("internal error: maybe handler wrote HTTP header and then returned httperror.NotHandle"))
}
// The request was explicitly not handled by the maybe handler.
// Do nothing, return ok==false.
return false
}
// The request was handled by the maybe handler.
// Handle error and return ok==true.
handleError(w, req, err, users, rw.WroteHeader)
return true
}
// handleError handles error err, which may be nil.
func handleError(
w http.ResponseWriter, req *http.Request,
err error,
users interface {
// GetAuthenticated fetches the currently authenticated user,
// or User{UserSpec: UserSpec{ID: 0}} if there is no authenticated user.
GetAuthenticated(context.Context) (users.User, error)
},
wroteHeader bool,
) {
if err == nil {
// Do nothing.
return
}
if err != nil && wroteHeader {
// The header has already been written, so it's too late to send
// a different status code. Just log the error and move on.
log.Println(err)
return
}
if err, ok := httperror.IsMethod(err); ok {
httperror.HandleMethod(w, err)
return
}
if err, ok := httperror.IsRedirect(err); ok {
http.Redirect(w, req, err.URL, http.StatusSeeOther)
return
}
if err, ok := httperror.IsBadRequest(err); ok {
httperror.HandleBadRequest(w, err)
return
}
if err, ok := httperror.IsHTTP(err); ok {
code := err.Code
error := fmt.Sprintf("%d %s", code, http.StatusText(code))
if user, e := users.GetAuthenticated(req.Context()); e == nil && user.SiteAdmin {
error += "\n\n" + err.Error()
}
http.Error(w, error, code)
return
}
if err, ok := httperror.IsJSONResponse(err); ok {
w.Header().Set("Content-Type", "application/json")
jw := json.NewEncoder(w)
jw.SetIndent("", "\t")
jw.SetEscapeHTML(false)
err := jw.Encode(err.V)
if err != nil {
log.Println("error encoding JSONResponse:", err)
}
return
}
if os.IsNotExist(err) {
log.Println(err)
error := "404 Not Found"
if user, e := users.GetAuthenticated(req.Context()); e == nil && user.SiteAdmin {
error += "\n\n" + err.Error()
}
http.Error(w, error, http.StatusNotFound)
return
}
if os.IsPermission(err) {
// TODO: Factor in a GetAuthenticatedSpec.ID == 0 check out here. (But this shouldn't apply for APIs.)
log.Println(err)
error := "403 Forbidden"
if user, e := users.GetAuthenticated(req.Context()); e == nil && user.SiteAdmin {
error += "\n\n" + err.Error()
}
http.Error(w, error, http.StatusForbidden)
return
}
log.Println(err)
error := "500 Internal Server Error"
if user, e := users.GetAuthenticated(req.Context()); e == nil && user.SiteAdmin {
error += "\n\n" + err.Error()
}
http.Error(w, error, http.StatusInternalServerError)
}
// headerResponseWriter wraps a real http.ResponseWriter and captures
// whether or not the header has been written.
type headerResponseWriter struct {
http.ResponseWriter
http.Flusher
WroteHeader bool // Write or WriteHeader was called.
}
func (rw *headerResponseWriter) Write(p []byte) (n int, err error) {
rw.WroteHeader = true
return rw.ResponseWriter.Write(p)
}
func (rw *headerResponseWriter) WriteHeader(code int) {
rw.WroteHeader = true
rw.ResponseWriter.WriteHeader(code)
}
// GzipHandler applies gzip compression on top of handler, unless handler
// has already handled it (i.e., the "Content-Encoding" header is set).
func GzipHandler(handler http.Handler) http.Handler {
return gzipHandler{handler}
}
type gzipHandler struct {
handler http.Handler
}
func (h gzipHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// If request doesn't accept gzip encoding, serve without compression.
if !httpguts.HeaderValuesContainsToken(req.Header["Accept-Encoding"], "gzip") {
h.handler.ServeHTTP(w, req)
return
}
// Otherwise, use gzipResponseWriter to start gzip compression when WriteHeader
// is called, but only if the handler didn't already take care of it.
rw := &gzipResponseWriter{ResponseWriter: w, Flusher: w.(http.Flusher)}
defer rw.Close()
h.handler.ServeHTTP(rw, req)
}
// gzipResponseWriter starts gzip compression when WriteHeader is called, unless compression
// has already been applied by that time (i.e., the "Content-Encoding" header is set).
// Close must be called when done with gzipResponseWriter.
type gzipResponseWriter struct {
http.ResponseWriter
http.Flusher
// These fields are set by setWriterAndCloser
// during first call to Write or WriteHeader.
w io.Writer // When set, must be non-nil.
c flushCloser // May be nil.
}
type flushCloser interface {
Flush() error
io.Closer
}
func (rw *gzipResponseWriter) WriteHeader(code int) {
if rw.w != nil {
panic(fmt.Errorf("internal error: gzipResponseWriter: WriteHeader called twice or after Write"))
}
rw.setWriterAndCloser(code)
rw.ResponseWriter.WriteHeader(code)
}
func (rw *gzipResponseWriter) Write(p []byte) (n int, err error) {
if rw.w == nil {
rw.setWriterAndCloser(http.StatusOK)
}
return rw.w.Write(p)
}
func (rw *gzipResponseWriter) setWriterAndCloser(status int) {
if _, ok := rw.Header()["Content-Encoding"]; ok {
// Compression already handled by the handler.
rw.w = rw.ResponseWriter
return
}
if !bodyAllowedForStatus(status) {
// Body not allowed, don't use gzip.
rw.w = rw.ResponseWriter
return
}
// Update headers, start using a gzip writer.
rw.Header().Set("Content-Encoding", "gzip")
rw.Header().Del("Content-Length")
gw := gzip.NewWriter(rw.ResponseWriter)
rw.w = gw
rw.c = gw
}
func (rw *gzipResponseWriter) Flush() {
if rw.c != nil {
err := rw.c.Flush()
if err != nil {
log.Printf("gzipResponseWriter.Flush: error flushing *gzip.Writer: %v", err)
}
}
rw.Flusher.Flush()
}
func (rw *gzipResponseWriter) Close() {
if rw.c == nil {
return
}
err := rw.c.Close()
if err != nil {
log.Printf("gzipResponseWriter.Close: error closing *gzip.Writer: %v", err)
}
}
// bodyAllowedForStatus reports whether a given response status code
// permits a body. See RFC 7230, section 3.3.
func bodyAllowedForStatus(status int) bool {
switch {
case status >= 100 && status < 200:
return false
case status == http.StatusNoContent:
return false
case status == http.StatusNotModified:
return false
default:
return true
}
}
// noUsers implements a subset of the users.Service interface
// relevant to fetching the currently authenticated user.
//
// It does not perform authentication, instead opting to
// always report that there is an unauthenticated user.
type noUsers struct{}
// GetAuthenticated always reports that there is an unauthenticated user.
func (noUsers) GetAuthenticated(context.Context) (users.User, error) {
return users.User{UserSpec: users.UserSpec{ID: 0, Domain: ""}}, nil
}