-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriters.go
214 lines (180 loc) · 6.17 KB
/
writers.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
package shuttle
import (
"io"
"net/http"
"strings"
)
type defaultWriter struct {
serializers map[string]Serializer
defaultSerializer Serializer
monitor Monitor
bodyBuffer []byte
contentTypeBuffer []string
contentDispositionBuffer []string
serializeBuffer *SerializeResult
}
func newWriter(serializerFactories map[string]func() Serializer, monitor Monitor) Writer {
serializers := make(map[string]Serializer, len(serializerFactories))
for acceptType, callback := range serializerFactories {
serializers[acceptType] = callback()
}
return &defaultWriter{
serializers: serializers,
defaultSerializer: serializers[emptyContentType],
monitor: monitor,
bodyBuffer: make([]byte, 1024*4),
contentTypeBuffer: make([]string, 1),
contentDispositionBuffer: make([]string, 1),
serializeBuffer: &SerializeResult{},
}
}
func (this *defaultWriter) Write(response http.ResponseWriter, request *http.Request, result any) {
if result == nil {
response.WriteHeader(http.StatusNoContent)
} else if handler, ok := result.(http.Handler); ok {
handler.ServeHTTP(response, request)
} else {
this.write(response, request, result)
}
}
func (this *defaultWriter) write(response http.ResponseWriter, request *http.Request, result any) {
switch typed := result.(type) {
case *TextResult:
this.responseStatus(this.writeTextResult(response, typed))
case TextResult:
this.responseStatus(this.writeTextResult(response, &typed))
case *BinaryResult:
this.responseStatus(this.writeBinaryResult(response, typed))
case BinaryResult:
this.responseStatus(this.writeBinaryResult(response, &typed))
case *StreamResult:
this.responseStatus(this.writeStreamResult(response, typed))
case StreamResult:
this.responseStatus(this.writeStreamResult(response, &typed))
case *SerializeResult:
this.responseStatus(this.writeSerializeResult(response, request, typed))
case SerializeResult:
this.responseStatus(this.writeSerializeResult(response, request, &typed))
case string:
this.responseStatus(this.writeStringResult(response, typed))
case []byte:
this.responseStatus(this.writeByteResult(response, typed))
case bool:
this.responseStatus(this.writeBoolResult(response, typed))
default:
this.serializeBuffer.Content = result
this.responseStatus(this.writeSerializeResult(response, request, this.serializeBuffer))
}
}
func (this *defaultWriter) writeTextResult(response http.ResponseWriter, typed *TextResult) (err error) {
this.monitor.TextResult()
hasContent := len(typed.Content) > 0
headers := response.Header()
for key, values := range typed.Headers {
headers[key] = values
}
this.writeHeader(response, typed.StatusCode, typed.ContentType, "", hasContent)
if hasContent {
_, err = io.WriteString(response, typed.Content)
}
return err
}
func (this *defaultWriter) writeBinaryResult(response http.ResponseWriter, typed *BinaryResult) (err error) {
this.monitor.BinaryResult()
hasContent := len(typed.Content) > 0
headers := response.Header()
for key, values := range typed.Headers {
headers[key] = values
}
this.writeHeader(response, typed.StatusCode, typed.ContentType, typed.ContentDisposition, hasContent)
if hasContent {
_, err = response.Write(typed.Content)
}
return err
}
func (this *defaultWriter) writeStreamResult(response http.ResponseWriter, typed *StreamResult) (err error) {
this.monitor.StreamResult()
hasContent := typed.Content != nil
headers := response.Header()
for key, values := range typed.Headers {
headers[key] = values
}
this.writeHeader(response, typed.StatusCode, typed.ContentType, typed.ContentDisposition, hasContent)
if hasContent {
_, err = io.CopyBuffer(response, typed.Content, this.bodyBuffer)
}
return err
}
func (this *defaultWriter) writeSerializeResult(response http.ResponseWriter, request *http.Request, typed *SerializeResult) error {
this.monitor.SerializeResult()
hasContent := typed.Content != nil
serializer := this.loadSerializer(request.Header[headerAccept])
contentType := typed.ContentType
if len(contentType) == 0 {
contentType = serializer.ContentType()
}
headers := response.Header()
for key, values := range typed.Headers {
headers[key] = values
}
this.writeHeader(response, typed.StatusCode, contentType, "", hasContent)
if hasContent {
if strings.Contains(request.Header.Get("Accept"), "/xml") {
this.write(response, request, xmlPrefix)
}
return serializer.Serialize(response, typed.Content)
}
return nil
}
func (this *defaultWriter) loadSerializer(acceptTypes []string) Serializer {
for _, acceptType := range acceptTypes {
if serializer, contains := this.serializers[normalizeMediaType(acceptType)]; contains {
return serializer
}
}
return this.defaultSerializer
}
func (this *defaultWriter) writeStringResult(response http.ResponseWriter, typed string) (err error) {
this.monitor.NativeResult()
if len(typed) > 0 {
_, err = io.WriteString(response, typed)
}
return err
}
func (this *defaultWriter) writeByteResult(response http.ResponseWriter, typed []byte) (err error) {
this.monitor.NativeResult()
if len(typed) > 0 {
_, err = response.Write(typed)
}
return err
}
func (this *defaultWriter) writeBoolResult(response http.ResponseWriter, typed bool) (err error) {
this.monitor.NativeResult()
if typed {
_, err = io.WriteString(response, "true")
} else {
_, err = io.WriteString(response, "false")
}
return err
}
func (this *defaultWriter) writeHeader(response http.ResponseWriter, statusCode int, contentType, contentDisposition string, hasContent bool) {
if hasContent && len(contentType) > 0 {
this.contentTypeBuffer[0] = contentType
response.Header()[headerContentType] = this.contentTypeBuffer
}
if hasContent && len(contentDisposition) > 0 {
this.contentDispositionBuffer[0] = contentDisposition
response.Header()[headerContentDisposition] = this.contentDispositionBuffer
}
if statusCode > 0 {
this.monitor.ResponseStatus(statusCode)
response.WriteHeader(statusCode)
} else {
this.monitor.ResponseStatus(http.StatusOK)
}
}
func (this *defaultWriter) responseStatus(err error) {
if err != nil {
this.monitor.ResponseFailed(err)
}
}