/
main.go
79 lines (70 loc) · 2.79 KB
/
main.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
// Copyright © 2013 Hraban Luyat <hraban@0brg.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Utility wrappers for HTTP handlers.
package godspeed
import (
"io"
"net/http"
)
type bodyWrapperFactory func(http.ResponseWriter) io.Writer
type bodyWrapper struct {
respw http.ResponseWriter
w io.Writer
// Called after the wrapper handler has done its Head business
posthandler bodyWrapperFactory
}
func (w *bodyWrapper) Header() http.Header {
return w.respw.Header()
}
func (w *bodyWrapper) Write(data []byte) (int, error) {
if w.w == nil {
w.w = w.posthandler(w.respw)
}
return w.w.Write(data)
}
func (w *bodyWrapper) WriteHeader(s int) {
w.respw.WriteHeader(s)
}
func (w *bodyWrapper) Close() error {
var err error
if c, ok := w.w.(io.Closer); ok {
err = c.Close()
}
return err
}
// Wrap response body in a different writer. This writer is generated by a
// factory function once all headers have been written by the handler operating
// on this ResponseWriter (i.e.: just before the first call to Write()). This
// allows wrappers to determine action based on response headers generated by
// whatever handler is operating. E.g.: the compression wrapper wants to know
// the content-type header before it decides whether to wrap the body in a
// compression writer at all.
//
// At the same time this allows the generators to change the headers after the
// real handler. E.g.: the mime-type handler that guesses a content-type based
// on the request path, but only if no custom content-type header has been set
// by the application. These wrapper generators just return the original
// response writer as the "new" body writer.
func wrapBody(respw http.ResponseWriter, writerGen bodyWrapperFactory) *bodyWrapper {
return &bodyWrapper{
respw: respw,
posthandler: writerGen,
}
}