This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderers.go
167 lines (119 loc) · 4.5 KB
/
renderers.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
package detour
import (
"encoding/json"
"encoding/xml"
"io"
"net/http"
)
/* ------------------------------------------------------------------------- */
type CompoundRenderer []Renderer
func (this CompoundRenderer) Render(response http.ResponseWriter, request *http.Request) {
for _, renderer := range this {
renderer.Render(response, request)
}
}
/* ------------------------------------------------------------------------- */
func IfElseRenderer(condition bool, True, False Renderer) Renderer {
if condition {
return True
} else {
return False
}
}
/* ------------------------------------------------------------------------- */
func IfRenderer(condition bool, renderer Renderer) Renderer {
return IfElseRenderer(condition, renderer, NopRenderer{})
}
/* ------------------------------------------------------------------------- */
type NopRenderer struct{}
func (this NopRenderer) Render(http.ResponseWriter, *http.Request) {}
/* ------------------------------------------------------------------------- */
type StatusCodeRenderer int
func (this StatusCodeRenderer) Render(response http.ResponseWriter, _ *http.Request) {
response.WriteHeader(int(this))
}
/* ------------------------------------------------------------------------- */
type HeadersRenderer http.Header
func (this HeadersRenderer) Render(response http.ResponseWriter, _ *http.Request) {
copyHeaders(http.Header(this), response.Header())
}
/* ------------------------------------------------------------------------- */
type SetHeaderPairsRenderer []string
func (this SetHeaderPairsRenderer) Render(response http.ResponseWriter, _ *http.Request) {
if len(this)%2 != 0 {
panic("odd length")
}
header := response.Header()
for x := 0; x < len(this); x += 2 {
header.Set(this[x], this[x+1])
}
}
/* ------------------------------------------------------------------------- */
type AddHeaderPairsRenderer []string
func (this AddHeaderPairsRenderer) Render(response http.ResponseWriter, _ *http.Request) {
if len(this)%2 != 0 {
panic("odd length")
}
header := response.Header()
for x := 0; x < len(this); x += 2 {
header.Add(this[x], this[x+1])
}
}
/* ------------------------------------------------------------------------- */
type CookieRenderer http.Cookie
func (this CookieRenderer) Render(response http.ResponseWriter, _ *http.Request) {
cookie := http.Cookie(this)
http.SetCookie(response, &cookie)
}
/* ------------------------------------------------------------------------- */
type RedirectRenderer string
func (this RedirectRenderer) Render(response http.ResponseWriter, request *http.Request) {
http.Redirect(response, request, string(this), response.(*responseBuffer).StatusCode())
}
/* ------------------------------------------------------------------------- */
type BytesBodyRenderer []byte
func (this BytesBodyRenderer) Render(response http.ResponseWriter, _ *http.Request) {
_, _ = response.Write(this)
}
/* ------------------------------------------------------------------------- */
type StringBodyRenderer string
func (this StringBodyRenderer) Render(response http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(response, string(this))
}
/* ------------------------------------------------------------------------- */
type ReaderBodyRenderer struct{ io.Reader }
func (this ReaderBodyRenderer) Render(response http.ResponseWriter, _ *http.Request) {
_, _ = io.Copy(response, this.Reader)
}
/* ------------------------------------------------------------------------- */
type XMLBodyRenderer struct{ Content interface{} }
func (this XMLBodyRenderer) Render(response http.ResponseWriter, _ *http.Request) {
_ = xml.NewEncoder(response).Encode(this.Content)
}
/* ------------------------------------------------------------------------- */
type JSONBodyRenderer struct {
Content interface{}
Indent string
JSONp bool
}
func (this JSONBodyRenderer) Render(response http.ResponseWriter, request *http.Request) {
callback := request.URL.Query().Get("callback")
canRenderJSONp := this.JSONp && callback != ""
if canRenderJSONp {
_, _ = io.WriteString(response, callback)
_, _ = io.WriteString(response, "(")
}
_, _ = response.Write(this.renderContent())
if canRenderJSONp {
_, _ = io.WriteString(response, ")")
}
}
func (this JSONBodyRenderer) renderContent() (content []byte) {
if this.Indent != "" {
content, _ = json.MarshalIndent(this.Content, "", this.Indent)
} else {
content, _ = json.Marshal(this.Content)
}
return content
}
/* ------------------------------------------------------------------------- */