-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorujo.go
96 lines (81 loc) · 2.07 KB
/
orujo.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
// Copyright 2014 The orujo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package orujo
import "net/http"
type pipe struct {
handlers []pipeHandler
}
// NewPipe returns a new HTTP pipe.
func NewPipe(handlers ...http.Handler) http.Handler {
p := pipe{}
p.handlers = []pipeHandler{}
for _, h := range handlers {
var ph pipeHandler
ph, isPipeHandler := h.(pipeHandler)
if !isPipeHandler {
ph = pipeHandler{handler: h, mandatory: false}
}
p.handlers = append(p.handlers, ph)
}
return p
}
func (p pipe) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := newPipeContext()
for _, ph := range p.handlers {
if ctx.quit && !ph.mandatory {
continue
}
pw := newPipeWriter(w, ctx)
ph.handler.ServeHTTP(pw, r)
}
}
type pipeHandler struct {
handler http.Handler
mandatory bool
}
func (h pipeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handler.ServeHTTP(w, r)
}
type pipeContext struct {
errors []error
quit bool
}
func newPipeContext() *pipeContext {
ctx := &pipeContext{}
ctx.quit = false
ctx.errors = []error{}
return ctx
}
type pipeWriter struct {
http.ResponseWriter
ctx *pipeContext
}
func newPipeWriter(w http.ResponseWriter, ctx *pipeContext) pipeWriter {
return pipeWriter{ResponseWriter: w, ctx: ctx}
}
func (pw pipeWriter) WriteHeader(code int) {
pw.ctx.quit = true
pw.ResponseWriter.WriteHeader(code)
}
// M is a helper to set a handler as "mandatory".
func M(h http.Handler) http.Handler {
return pipeHandler{handler: h, mandatory: true}
}
// RegisterError can be used by Handlers to register errors.
func RegisterError(w http.ResponseWriter, err error) {
pw, isPipeWriter := w.(pipeWriter)
if !isPipeWriter || err == nil {
return
}
pw.ctx.errors = append(pw.ctx.errors, err)
}
// Errors is used to retrieve the errors registered via RegisterError()
// during the execution of the handlers pipe.
func Errors(w http.ResponseWriter) []error {
pw, isPipeWriter := w.(pipeWriter)
if isPipeWriter {
return pw.ctx.errors
}
return nil
}