forked from ungerik/go-start
/
response.go
283 lines (238 loc) · 7.78 KB
/
response.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
package view
import (
"bytes"
"container/heap"
"fmt"
"hash/crc32"
"net/http"
"github.com/ungerik/go-start/utils"
"github.com/ungerik/web.go"
)
func newResponse(webContext *web.Context) *Response {
response := &Response{
webContext: webContext,
}
response.PushBody()
return response
}
type responseBody struct {
buffer *bytes.Buffer
xml *utils.XMLWriter
}
type Response struct {
webContext *web.Context
Session *Session
bodyStack []responseBody
// XML allowes the Response to be used as utils.XMLWriter
XML *utils.XMLWriter
dynamicStyle dependencyHeap
dynamicHeadScripts dependencyHeap
dynamicScripts dependencyHeap
}
// PushBody pushes the buffer of the response body on a stack
// and sets a new empty buffer.
// This can be used to render intermediate text results.
// Note: Only the response body is pushed, all other state changes
// like setting headers will affect the final response.
func (self *Response) PushBody() {
var b responseBody
b.buffer = new(bytes.Buffer)
b.xml = utils.NewXMLWriter(b.buffer)
self.bodyStack = append(self.bodyStack, b)
self.XML = b.xml
}
// PopBody pops the buffer of the response body from the stack
// and returns its content.
func (self *Response) PopBody() (bufferData []byte) {
last := len(self.bodyStack) - 1
bufferData = self.bodyStack[last].buffer.Bytes()
self.bodyStack = self.bodyStack[0:last]
self.XML = self.bodyStack[last-1].xml
return bufferData
}
// PopBodyString pops the buffer of the response body from the stack
// and returns its content as string.
func (self *Response) PopBodyString() (bufferData string) {
return string(self.PopBody())
}
func (self *Response) Write(p []byte) (n int, err error) {
return self.XML.Write(p)
}
func (self *Response) WriteByte(c byte) error {
_, err := self.XML.Write([]byte{c})
return err
}
func (self *Response) WriteString(s string) (n int, err error) {
return self.XML.Write([]byte(s))
}
func (self *Response) Printf(format string, args ...interface{}) (n int, err error) {
return fmt.Fprintf(self.XML, format, args...)
}
func (self *Response) String() string {
return self.bodyStack[len(self.bodyStack)-1].buffer.String()
}
func (self *Response) Bytes() []byte {
return self.bodyStack[len(self.bodyStack)-1].buffer.Bytes()
}
func (self *Response) SetSecureCookie(name string, val string, age int64, path string) {
self.webContext.SetSecureCookie(name, val, age, path)
}
func (self *Response) Abort(status int, body string) {
self.webContext.Abort(status, body)
}
func (self *Response) RedirectPermanently301(url string) {
self.webContext.Redirect(301, url)
}
func (self *Response) RedirectTemporary302(url string) {
self.webContext.Redirect(302, url)
}
func (self *Response) NotModified304() {
self.webContext.NotModified()
}
func (self *Response) Forbidden403(message string) {
self.Abort(403, message)
}
func (self *Response) NotFound404(message string) {
self.Abort(404, message)
}
func (self *Response) AuthorizationRequired401() {
self.Abort(401, "Authorization Required")
}
func (self *Response) Header() http.Header {
return self.webContext.Header()
}
func (self *Response) ContentTypeByExt(ext string) {
self.webContext.ContentType(ext)
}
// ContentDispositionAttachment makes the webbrowser open a
// "Save As.." dialog for the response.
func (self *Response) ContentDispositionAttachment(filename string) {
self.Header().Add("Content-Type", "application/x-unknown")
self.Header().Add("Content-Disposition", "attachment;filename="+filename)
}
// RequireStyle adds dynamic CSS content to the page.
// Multiple dynamic entries will be sorted by priority.
// Dynamic CSS will be inserted after the regular CSS of the page.
//
// Use this feature to dynamically add CSS to the page if the
// HTML content requires it.
func (self *Response) RequireStyle(css string, priority int) {
if self.dynamicStyle == nil {
self.dynamicStyle = newDependencyHeap()
}
self.dynamicStyle.AddIfNew("<style>"+css+"</style>", priority)
}
// RequireStyleURL adds a dynamic CSS link to the page.
// Multiple dynamic entries will be sorted by priority.
// Dynamic CSS will be inserted after the regular CSS of the page.
//
// Use this feature to dynamically add CSS to the page if the
// HTML content requires it.
func (self *Response) RequireStyleURL(url string, priority int) {
if self.dynamicStyle == nil {
self.dynamicStyle = newDependencyHeap()
}
self.dynamicStyle.AddIfNew("<link rel='stylesheet' href='"+url+"'>", priority)
}
// RequireHeadScript adds dynamic JavaScript to the page.
// Multiple dynamic entries will be sorted by priority.
// The dynamic JavaScript will be inserted after the regular
// head-scripts of the page.
//
// Use this feature to dynamically add JavaScript to
// the page if the HTML content requires it.
func (self *Response) RequireHeadScript(script string, priority int) {
if self.dynamicHeadScripts == nil {
self.dynamicHeadScripts = newDependencyHeap()
}
self.dynamicHeadScripts.AddIfNew("<script>"+script+"</script>", priority)
}
// RequireHeadScriptURL adds a dynamic JavaScript link to the page.
// Multiple dynamic entries will be sorted by priority.
// The dynamic JavaScript will be inserted after the regular
// head-scripts of the page.
//
// Use this feature to dynamically add JavaScript to
// the page if the HTML content requires it.
func (self *Response) RequireHeadScriptURL(url string, priority int) {
if self.dynamicHeadScripts == nil {
self.dynamicHeadScripts = newDependencyHeap()
}
self.dynamicHeadScripts.AddIfNew("<script src='"+url+"'></script>", priority)
}
// RequireScript adds dynamic JavaScript to the page.
// Multiple dynamic entries will be sorted by priority.
// The dynamic JavaScript will be inserted after the regular
// scripts near the end of the page.
//
// Use this feature to dynamically add JavaScript to
// the page if the HTML content requires it.
func (self *Response) RequireScript(script string, priority int) {
if self.dynamicScripts == nil {
self.dynamicScripts = newDependencyHeap()
}
self.dynamicScripts.AddIfNew("<script>"+script+"</script>", priority)
}
// RequireScriptURL adds a dynamic JavaScript link to the page.
// Multiple dynamic entries will be sorted by priority.
// The dynamic JavaScript will be inserted after the regular
// scripts near the end of the page.
//
// Use this feature to dynamically add JavaScript to
// the page if the HTML content requires it.
func (self *Response) RequireScriptURL(url string, priority int) {
if self.dynamicScripts == nil {
self.dynamicScripts = newDependencyHeap()
}
self.dynamicScripts.AddIfNew("<script src='"+url+"'></script>", priority)
}
///////////////////////////////////////////////////////////////////////////////
// dependencyHeap
func newDependencyHeap() dependencyHeap {
dh := make(dependencyHeap, 0, 1)
heap.Init(&dh)
return dh
}
type dependencyHeapItem struct {
text string
hash uint32
priority int
}
type dependencyHeap []dependencyHeapItem
func (self *dependencyHeap) Len() int {
return len(*self)
}
func (self *dependencyHeap) Less(i, j int) bool {
return (*self)[i].priority < (*self)[j].priority
}
func (self *dependencyHeap) Swap(i, j int) {
(*self)[i], (*self)[j] = (*self)[j], (*self)[i]
}
func (self *dependencyHeap) Push(item interface{}) {
*self = append(*self, item.(dependencyHeapItem))
}
func (self *dependencyHeap) Pop() interface{} {
end := len(*self) - 1
item := (*self)[end]
*self = (*self)[:end]
return item
}
func (self *dependencyHeap) AddIfNew(text string, priority int) {
hash := crc32.ChecksumIEEE([]byte(text))
for i := range *self {
if (*self)[i].hash == hash {
return // text is not new
}
}
heap.Push(self, dependencyHeapItem{text, hash, priority})
}
func (self *dependencyHeap) String() string {
if self == nil {
return ""
}
var buf bytes.Buffer
for i := range *self {
buf.WriteString((*self)[i].text)
}
return buf.String()
}