forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
386 lines (336 loc) Β· 11.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package goyave
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
htmltemplate "html/template"
"io"
"net"
"net/http"
"os"
"runtime/debug"
"strconv"
"text/template"
"gorm.io/gorm"
"goyave.dev/goyave/v3/config"
"goyave.dev/goyave/v3/helper/filesystem"
)
var (
// ErrNotHijackable returned by response.Hijack() if the underlying
// http.ResponseWriter doesn't implement http.Hijacker. This can
// happen with HTTP/2 connections.
ErrNotHijackable = errors.New("Underlying http.ResponseWriter doesn't implement http.Hijacker")
)
// PreWriter is a writter that needs to alter the response headers or status
// before they are written.
// If implemented, PreWrite will be called right before the Write operation.
type PreWriter interface {
PreWrite(b []byte)
}
// Response represents a controller response.
type Response struct {
writer io.Writer
responseWriter http.ResponseWriter
err interface{}
httpRequest *http.Request
stacktrace string
status int
// Used to check if controller didn't write anything so
// core can write default 204 No Content.
// See RFC 7231, 6.3.5
empty bool
wroteHeader bool
hijacked bool
}
// newResponse create a new Response using the given http.ResponseWriter and raw request.
func newResponse(writer http.ResponseWriter, rawRequest *http.Request) *Response {
return &Response{
responseWriter: writer,
writer: writer,
httpRequest: rawRequest,
empty: true,
status: 0,
wroteHeader: false,
err: nil,
}
}
// --------------------------------------
// PreWriter implementation
// PreWrite writes the response header after calling PreWrite on the
// child writer if it implements PreWriter.
func (r *Response) PreWrite(b []byte) {
r.empty = false
if pr, ok := r.writer.(PreWriter); ok {
pr.PreWrite(b)
}
if !r.wroteHeader {
if r.status == 0 {
r.status = http.StatusOK
}
r.WriteHeader(r.status)
}
}
// --------------------------------------
// http.ResponseWriter implementation
// Write writes the data as a response.
// See http.ResponseWriter.Write
func (r *Response) Write(data []byte) (int, error) {
r.PreWrite(data)
return r.writer.Write(data)
}
// WriteHeader sends an HTTP response header with the provided
// status code.
// Prefer using "Status()" method instead.
// Calling this method a second time will have no effect.
func (r *Response) WriteHeader(status int) {
if !r.wroteHeader {
r.status = status
r.wroteHeader = true
r.responseWriter.WriteHeader(status)
}
}
// Header returns the header map that will be sent.
func (r *Response) Header() http.Header {
return r.responseWriter.Header()
}
// --------------------------------------
// http.Hijacker implementation
// Hijack implements the Hijacker.Hijack method.
// For more details, check http.Hijacker.
//
// Returns ErrNotHijackable if the underlying http.ResponseWriter doesn't
// implement http.Hijacker. This can happen with HTTP/2 connections.
//
// Middleware executed after controller handlers, as well as status handlers,
// keep working as usual after a connection has been hijacked.
// Callers should properly set the response status to ensure middleware and
// status handler execute correctly. Usually, callers of the Hijack method
// set the HTTP status to http.StatusSwitchingProtocols.
// If no status is set, the regular behavior will be kept and `204 No Content`
// will be set as the response status.
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := r.responseWriter.(http.Hijacker)
if !ok {
return nil, nil, ErrNotHijackable
}
c, b, e := hijacker.Hijack()
if e == nil {
r.hijacked = true
}
return c, b, e
}
// Hijacked returns true if the underlying connection has been successfully hijacked
// via the Hijack method.
func (r *Response) Hijacked() bool {
return r.hijacked
}
// --------------------------------------
// Chained writers
// Writer return the current writer used to write the response.
// Note that the returned writer is not necessarily a http.ResponseWriter, as
// it can be replaced using SetWriter.
func (r *Response) Writer() io.Writer {
return r.writer
}
// SetWriter set the writer used to write the response.
// This can be used to chain writers, for example to enable
// gzip compression, or for logging.
//
// The original http.ResponseWriter is always kept.
func (r *Response) SetWriter(writer io.Writer) {
r.writer = writer
}
func (r *Response) close() error {
if wr, ok := r.writer.(io.Closer); ok {
return wr.Close()
}
return nil
}
// --------------------------------------
// Accessors
// GetStatus return the response code for this request or 0 if not yet set.
func (r *Response) GetStatus() int {
return r.status
}
// GetError return the value which caused a panic in the request's handling, or nil.
func (r *Response) GetError() interface{} {
return r.err
}
// GetStacktrace return the stacktrace of when the error occurred, or an empty string.
// The stacktrace is captured by the recovery middleware.
func (r *Response) GetStacktrace() string {
return r.stacktrace
}
// IsEmpty return true if nothing has been written to the response body yet.
func (r *Response) IsEmpty() bool {
return r.empty
}
// IsHeaderWritten return true if the response header has been written.
// Once the response header is written, you cannot change the response status
// and headers anymore.
func (r *Response) IsHeaderWritten() bool {
return r.wroteHeader
}
// --------------------------------------
// Write methods
// Status set the response status code.
// Calling this method a second time will have no effect.
func (r *Response) Status(status int) {
if r.status == 0 {
r.status = status
}
}
// JSON write json data as a response.
// Also sets the "Content-Type" header automatically.
func (r *Response) JSON(responseCode int, data interface{}) error {
r.responseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
r.status = responseCode
return json.NewEncoder(r).Encode(data)
}
// String write a string as a response
func (r *Response) String(responseCode int, message string) error {
r.status = responseCode
_, err := r.Write([]byte(message))
return err
}
func (r *Response) writeFile(file string, disposition string) (int64, error) {
if !filesystem.FileExists(file) {
r.Status(http.StatusNotFound)
return 0, &os.PathError{Op: "open", Path: file, Err: fmt.Errorf("no such file or directory")}
}
r.empty = false
r.status = http.StatusOK
mime, size := filesystem.GetMIMEType(file)
header := r.responseWriter.Header()
header.Set("Content-Disposition", disposition)
if header.Get("Content-Type") == "" {
header.Set("Content-Type", mime)
}
header.Set("Content-Length", strconv.FormatInt(size, 10))
f, _ := os.Open(file)
// No need to check for errors, filesystem.FileExists(file) and
// filesystem.GetMIMEType(file) already handled that.
defer f.Close()
return io.Copy(r, f)
}
// File write a file as an inline element.
// Automatically detects the file MIME type and sets the "Content-Type" header accordingly.
// If the file doesn't exist, respond with status 404 Not Found.
// The given path can be relative or absolute.
//
// If you want the file to be sent as a download ("Content-Disposition: attachment"), use the "Download" function instead.
func (r *Response) File(file string) error {
_, err := r.writeFile(file, "inline")
return err
}
// Download write a file as an attachment element.
// Automatically detects the file MIME type and sets the "Content-Type" header accordingly.
// If the file doesn't exist, respond with status 404 Not Found.
// The given path can be relative or absolute.
//
// The "fileName" parameter defines the name the client will see. In other words, it sets the header "Content-Disposition" to
// "attachment; filename="${fileName}""
//
// If you want the file to be sent as an inline element ("Content-Disposition: inline"), use the "File" function instead.
func (r *Response) Download(file string, fileName string) error {
_, err := r.writeFile(file, fmt.Sprintf("attachment; filename=\"%s\"", fileName))
return err
}
// Error print the error in the console and return it with an error code 500.
// If debugging is enabled in the config, the error is also written in the response
// and the stacktrace is printed in the console.
// If debugging is not enabled, only the status code is set, which means you can still
// write to the response, or use your error status handler.
func (r *Response) Error(err interface{}) error {
ErrLogger.Println(err)
return r.error(err)
}
func (r *Response) error(err interface{}) error {
r.err = err
if config.GetBool("app.debug") {
stacktrace := r.stacktrace
if stacktrace == "" {
stacktrace = string(debug.Stack())
}
ErrLogger.Print(stacktrace)
if !r.Hijacked() {
var message interface{}
if e, ok := err.(error); ok {
message = e.Error()
} else {
message = err
}
return r.JSON(http.StatusInternalServerError, map[string]interface{}{"error": message})
}
}
// Don't set r.empty to false to let error status handler process the error
r.Status(http.StatusInternalServerError)
return nil
}
// Cookie add a Set-Cookie header to the response.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (r *Response) Cookie(cookie *http.Cookie) {
http.SetCookie(r.responseWriter, cookie)
}
// Redirect send a permanent redirect response
func (r *Response) Redirect(url string) {
http.Redirect(r, r.httpRequest, url, http.StatusPermanentRedirect)
}
// TemporaryRedirect send a temporary redirect response
func (r *Response) TemporaryRedirect(url string) {
http.Redirect(r, r.httpRequest, url, http.StatusTemporaryRedirect)
}
// Render a text template with the given data.
// The template path is relative to the "resources/template" directory.
func (r *Response) Render(responseCode int, templatePath string, data interface{}) error {
tmplt, err := template.ParseFiles(r.getTemplateDirectory() + templatePath)
if err != nil {
return err
}
var b bytes.Buffer
if err := tmplt.Execute(&b, data); err != nil {
return err
}
return r.String(responseCode, b.String())
}
// RenderHTML an HTML template with the given data.
// The template path is relative to the "resources/template" directory.
func (r *Response) RenderHTML(responseCode int, templatePath string, data interface{}) error {
tmplt, err := htmltemplate.ParseFiles(r.getTemplateDirectory() + templatePath)
if err != nil {
return err
}
var b bytes.Buffer
if err := tmplt.Execute(&b, data); err != nil {
return err
}
return r.String(responseCode, b.String())
}
func (r *Response) getTemplateDirectory() string {
sep := string(os.PathSeparator)
workingDir, err := os.Getwd()
if err != nil {
panic(err)
}
return workingDir + sep + "resources" + sep + "template" + sep
}
// HandleDatabaseError takes a database query result and checks if any error has occurred.
//
// Automatically writes HTTP status code 404 Not Found if the error is a "Not found" error.
// Calls "Response.Error()" if there is another type of error.
//
// Returns true if there is no error.
func (r *Response) HandleDatabaseError(db *gorm.DB) bool {
if db.Error != nil {
if errors.Is(db.Error, gorm.ErrRecordNotFound) {
r.Status(http.StatusNotFound)
} else {
r.Error(db.Error)
}
return false
}
return true
}