-
Notifications
You must be signed in to change notification settings - Fork 5
/
response.go
745 lines (681 loc) · 19.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package there
// This file contains all responses there provides by default.
import (
"bytes"
"compress/gzip"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/gebes/there/v2/header"
"github.com/gebes/there/v2/status"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
// Response is the base for every return you can make in an Endpoint.
// Necessary to render the Response by calling Execute and for the Headers Builder.
type Response http.Handler
// ResponseFunc is the type for a http.Handler
type ResponseFunc func(http.ResponseWriter, *http.Request)
// ServeHTTP calls f(w, r).
func (f ResponseFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f(w, r)
}
// Bytes writes the data parameter with the given status code
// to the http.ResponseWriter
//
// The Content-Type header is set to nothing at all.
//
// func ExampleStringGet(request there.Request) there.Response {
// return there.String(status.OK, "Hello there")
// }
//
// When this handler gets called, the final rendered result will be
//
// Hello there
func Bytes(code int, data []byte) Response {
return &bytesResponse{code: code, data: data}
}
type bytesResponse struct {
code int
data []byte
}
func (j bytesResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
_, err := rw.Write(j.data)
if err != nil {
log.Printf("bytesResponse: ServeHttp write failed: %v", err)
}
}
// Status sets the given status code to the http.ResponseWriter.
//
// No Content-Type header is set at all.
//
// func ExampleStatusGet(request there.Request) there.Response {
// return there.Status(status.OK)
// }
//
// When this handler gets called, the final rendered result will be
// an empty body with the status 200
func Status(code int) Response {
return &statusResponse{code: code}
}
type statusResponse struct {
code int
}
func (j statusResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(j.code)
}
// Headers wraps around your current Response and sets all the headers
// parsed in the headers parameter provided they are not set. If a header
// was already set by a previous Response, then it will be skipped.
//
// func Cors(configuration CorsConfiguration) there.Middleware {
// return func(request there.Request, next there.Response) there.Response {
// headers := map[string]string{
// header.ResponseAccessControlAllowOrigin: configuration.AccessControlAllowOrigin,
// header.ResponseAccessControlAllowMethods: configuration.AccessControlAllowMethods,
// header.ResponseAccessControlAllowHeaders: configuration.AccessControlAllowHeaders,
// }
// if request.Method == there.MethodOptions {
// return there.Headers(headers, there.Status(status.OK))
// }
// return there.Headers(headers, next)
// }
// }
//
// When this middleware gets called, all the Cors Headers will be set.
func Headers(headers map[string]string, response Response) Response {
return &headerResponse{headers: headers, response: response}
}
type headerResponse struct {
headers map[string]string
response Response
}
func (h headerResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
for key, value := range h.headers {
// Only set the header, if it wasn't set yet
if rw.Header().Get(key) == "" {
rw.Header().Set(key, value)
}
}
if h.response != nil {
h.response.ServeHTTP(rw, r)
}
}
// Gzip wraps around your current Response and compresses all the data
// written to it, if the client has specified 'gzip' in the Accept-Encoding
// header.
func Gzip(response Response) Response {
r := &gzipMiddleware{response}
return r
}
type gzipMiddleware struct {
response Response
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (j gzipMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get(header.RequestAcceptEncoding), "gzip") {
j.response.ServeHTTP(rw, r)
return
}
gz := gzip.NewWriter(rw)
defer gz.Close()
rw.Header().Set(header.ContentEncoding, "gzip")
var responseWriter http.ResponseWriter = gzipResponseWriter{Writer: gz, ResponseWriter: rw}
j.response.ServeHTTP(responseWriter, r)
}
// String writes the data parameter with the given status code
// to the http.ResponseWriter
//
// The Content-Type header is set accordingly to text/plain
//
// func ExampleStringGet(request there.Request) there.Response {
// return there.String(status.OK, "Hello there")
// }
//
// When this handler gets called, the final rendered result will be
//
// Hello there
func String(code int, data string) Response {
return stringResponse{code: code, data: []byte(data)}
}
type stringResponse struct {
code int
data []byte
}
func (s stringResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(s.code)
if rw.Header().Get(header.ContentType) == "" {
rw.Header().Set(header.ContentType, ContentTypeTextPlain)
}
_, err := rw.Write(s.data)
if err != nil {
log.Printf("stringResponse: ServeHttp write failed: %v", err)
}
}
// Error builds a json response using the err parameter and writes
// the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/json
//
// func ExampleErrorGet(request there.Request) there.Response {
// if 1 != 2 {
// return there.Error(status.InternalServerError, errors.New("something went wrong"))
// }
// return there.Status(status.OK)
// }
//
// When this handler gets called, the final rendered result will be
//
// {"error":"something went wrong"}
//
// For optimal performance the use of json.Marshal is avoided and the response
// body is built directly. With this way, there is no error that could occur.
func Error(code int, err error) Response {
e := err.Error()
var b bytes.Buffer
b.Grow(len(e) + errorJsonLength)
b.Write(errorJsonOpen)
for i := range e {
switch e[i] {
case '"':
b.WriteString("\\\"")
case '\'':
b.WriteString("\\'")
case '\v':
b.WriteString("\\v")
case '\f':
b.WriteString("\\f")
case '\r':
b.WriteString("\\r")
case '\n':
b.WriteString("\\n")
case '\t':
b.WriteString("\\t")
case '\b':
b.WriteString("\\b")
case '\a':
b.WriteString("\\a")
default:
b.WriteByte(e[i])
}
}
b.Write(errorJsonClose)
return &jsonResponse{code: code, data: b.Bytes()}
}
var (
errorJsonOpen = []byte("{\"error\":\"")
errorJsonClose = []byte("\"}")
)
const errorJsonLength = 14 // the total length of errorJsonOpen + errorJsonClose
// Html takes a status code, the path to the html file and a map for the template parsing
func Html(code int, file string, template any) Response {
content, err := parseTemplate(file, template)
if err != nil {
return Error(status.InternalServerError, fmt.Errorf("html: parseTemplate: %v", err))
}
return htmlResponse{code: code, data: []byte(*content)}
}
type htmlResponse struct {
code int
data []byte
}
func (h htmlResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(h.code)
rw.Header().Set(header.ContentType, ContentTypeTextHtml)
_, err := rw.Write(h.data)
if err != nil {
log.Printf("htmlResponse: ServeHttp write failed: %v", err)
}
}
func parseTemplate(templateFileName string, data any) (*string, error) {
t, err := template.ParseFiles(templateFileName)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
return nil, err
}
body := buf.String()
return &body, nil
}
// Json marshalls the given data parameter with the json.Marshal function
// and writes the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/json
//
// func ExampleGet(request there.Request) there.Response {
// user := map[string]string{
// "firstname": "John",
// "surname": "Smith",
// }
// return there.Json(status.OK, user)
// }
//
// When this handler gets called, the final rendered result will be
//
// {"firstname":"John","surname":"Smith"}
//
// If the json.Marshal fails with an error, then an Error with StatusInternalServerError will be returned, with the error format "json: json.Marshal: %v"
func Json(code int, data any) Response {
jsonData, err := json.Marshal(data)
if err != nil {
return Error(status.InternalServerError, fmt.Errorf("json: json.Marshal: %v", err))
}
return jsonResponse{code: code, data: jsonData}
}
// JsonError marshalls the given data parameter with the json.Marshal function
// and writes the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/json
//
// func ExampleJsonErrorGet(request there.Request) there.Response{
// user := map[string]string{
// "firstname": "John",
// "surname": "Smith",
// }
// resp, err := there.JsonError(status.OK, user)
// if err != nil {
// return there.Error(status.InternalServerError, fmt.Errorf("something went wrong: %v", err))
// }
// return resp
// }
//
// When this handler gets called, the final rendered result will be
//
// {"firstname":"John","surname":"Smith"}
//
// If the json.Marshal fails with an error, then a nil response with a non-nil error will be returned to handle.
func JsonError(code int, data any) (Response, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
return jsonResponse{code: code, data: jsonData}, nil
}
type jsonResponse struct {
code int
data []byte
}
func (j jsonResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(j.code)
if rw.Header().Get(header.ContentType) == "" {
rw.Header().Set(header.ContentType, ContentTypeApplicationJson)
}
_, err := rw.Write(j.data)
if err != nil {
log.Printf("jsonResponse: ServeHttp write failed: %v", err)
}
}
// Message builds a json response using the message parameter and writes
// the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/json
//
// func ExampleMessageGet(request there.Request) there.Response {
// return there.Message(status.OK, "Hello there")
// }
//
// When this handler gets called, the final rendered result will be
//
// {"message":"Hello there"}
//
// For optimal performance the use of json.Marshal is avoided and the response
// body is built directly. With this way, there is no error that could occur.
func Message(code int, message string) Response {
var b strings.Builder
b.Grow(len(message))
for i := range message {
switch message[i] {
case '"':
b.WriteString("\\\"")
case '\'':
b.WriteString("\\'")
case '\v':
b.WriteString("\\v")
case '\f':
b.WriteString("\\f")
case '\r':
b.WriteString("\\r")
case '\n':
b.WriteString("\\n")
case '\t':
b.WriteString("\\t")
case '\b':
b.WriteString("\\b")
case '\a':
b.WriteString("\\a")
default:
b.WriteByte(message[i])
}
}
const (
jsonOpen = "{\"message\":\""
jsonClose = "\"}"
)
return jsonResponse{code: code, data: []byte(jsonOpen + b.String() + jsonClose)}
}
// Redirect redirects to the specific URL
func Redirect(code int, url string) Response {
return &redirectResponse{code: code, url: url}
}
type redirectResponse struct {
code int
url string
}
func (j redirectResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
http.Redirect(rw, r, j.url, j.code)
}
// Xml marshalls the given data parameter with the xml.Marshal function and
// writes the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/xml
//
// type User struct {
// Firstname string `xml:"firstname"`
// Surname string `xml:"surname"`
// }
//
// func ExampleXmlGet(request there.Request) there.Response{
// user := User{"John", "Smith"}
// return there.Xml(status.OK, user)
// }
//
// When this handler gets called, the final rendered result will be
//
// <User><firstname>John</firstname><surname>Smith</surname></User>
//
// If the xml.Marshal fails with an error, then an Error with StatusInternalServerError will be returned, with the error format "xml: xml.Marshal: %v"
func Xml(code int, data any) Response {
xmlData, err := xml.Marshal(data)
if err != nil {
return Error(status.InternalServerError, fmt.Errorf("xml: xml.Marshal: %v", err))
}
return xmlResponse{code: code, data: xmlData}
}
// XmlError marshalls the given data parameter with the xml.Marshal function and
// writes the result with the given status code to the http.ResponseWriter
//
// The Content-Type header is set accordingly to application/xml
//
// type User struct {
// Firstname string `xml:"firstname"`
// Surname string `xml:"surname"`
// }
//
// func ExampleXmlErrorGet(request there.Request) there.Response {
// user := User{"John", "Smith"}
// resp, err := there.XmlError(status.OK, user)
// if err != nil {
// return there.Error(status.InternalServerError, fmt.Errorf("something went wrong: %v", err))
// }
// return resp
// }
//
// When this handler gets called, the final rendered result will be
//
// <User><firstname>John</firstname><surname>Smith</surname></User>
//
// If the xml.Marshal fails with an error, then a nil response with a non-nil error will be returned to handle.
func XmlError(code int, data any) (Response, error) {
xmlData, err := xml.Marshal(data)
if err != nil {
return nil, err
}
return xmlResponse{code: code, data: xmlData}, nil
}
type xmlResponse struct {
code int
data []byte
}
func (x xmlResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if rw.Header().Get(header.ContentType) == "" {
rw.Header().Set(header.ContentType, ContentTypeApplicationXml)
}
rw.WriteHeader(x.code)
_, err := rw.Write(x.data)
if err != nil {
log.Printf("xmlResponse: ServeHttp write failed: %v", err)
}
}
var AutoHandlers = map[string]func(code int, data any) Response{
"fallback": Json,
ContentTypeApplicationJson: Json,
ContentTypeApplicationXml: Xml,
}
func Auto(code int, data any) Response {
return autoResponse{code, data}
}
type autoResponse struct {
code int
data any
}
func (a autoResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
contentTypes := make([]string, 0, len(AutoHandlers))
for s := range AutoHandlers {
contentTypes = append(contentTypes, s)
}
contentType := NegotiateContentType(r.Header[header.RequestAccept], contentTypes, "fallback")
handler, ok := AutoHandlers[contentType]
if !ok {
Error(status.BadRequest, errors.New("no suitable content-type provided")).ServeHTTP(rw, r)
} else {
handler(a.code, a.data).ServeHTTP(rw, r)
}
}
// NegotiateContentType returns the best offered content type for the request's
// Accept header. If two offers match with equal weight, then the more specific
// offer is preferred. For example, text/* trumps */*. If two offers match
// with equal weight and specificity, then the offer earlier in the list is
// preferred. If no offers match, then defaultOffer is returned.
func NegotiateContentType(headerValue, offers []string, defaultOffer string) string {
bestOffer := defaultOffer
bestQ := -1.0
bestWild := 3
specs := ParseAccept(headerValue)
for _, offer := range offers {
for _, spec := range specs {
switch {
case spec.Q == 0.0:
// ignore
case spec.Q < bestQ:
// better match found
case spec.Value == "*/*":
if spec.Q > bestQ || bestWild > 2 {
bestQ = spec.Q
bestWild = 2
bestOffer = offer
}
case strings.HasSuffix(spec.Value, "/*"):
if strings.HasPrefix(offer, spec.Value[:len(spec.Value)-1]) &&
(spec.Q > bestQ || bestWild > 1) {
bestQ = spec.Q
bestWild = 1
bestOffer = offer
}
default:
if spec.Value == offer &&
(spec.Q > bestQ || bestWild > 0) {
bestQ = spec.Q
bestWild = 0
bestOffer = offer
}
}
}
}
return bestOffer
}
// AcceptSpec describes an Accept* header.
type AcceptSpec struct {
Value string
Q float64
}
// ParseAccept parses Accept* headers.
func ParseAccept(header []string) (specs []AcceptSpec) {
loop:
for _, s := range header {
for {
var spec AcceptSpec
spec.Value, s = expectTokenSlash(s)
if spec.Value == "" {
continue loop
}
spec.Q = 1.0
s = skipSpace(s)
if strings.HasPrefix(s, ";") {
s = skipSpace(s[1:])
if !strings.HasPrefix(s, "q=") {
continue loop
}
spec.Q, s = expectQuality(s[2:])
if spec.Q < 0.0 {
continue loop
}
}
specs = append(specs, spec)
s = skipSpace(s)
if !strings.HasPrefix(s, ",") {
continue loop
}
s = skipSpace(s[1:])
}
}
return
}
func skipSpace(s string) (rest string) {
i := 0
for ; i < len(s); i++ {
if octetTypes[s[i]]&isSpace == 0 {
break
}
}
return s[i:]
}
func expectTokenSlash(s string) (token, rest string) {
i := 0
for ; i < len(s); i++ {
b := s[i]
if (octetTypes[b]&isToken == 0) && b != '/' {
break
}
}
return s[:i], s[i:]
}
func expectQuality(s string) (quality float64, rest string) {
switch {
case s == "":
return -1, ""
case s[0] == '0':
quality = 0
case s[0] == '1':
quality = 1
default:
return -1, ""
}
s = s[1:]
if !strings.HasPrefix(s, ".") {
return quality, s
}
s = s[1:]
i := 0
n := 0
d := 1
for ; i < len(s); i++ {
b := s[i]
if b < '0' || b > '9' {
break
}
n = n*10 + int(b) - '0'
d *= 10
}
return quality + float64(n)/float64(d), s[i:]
}
// Octet types from RFC 2616.
var octetTypes [256]octetType
type octetType byte
const (
isToken octetType = 1 << iota
isSpace
)
func init() {
// OCTET = <any 8-bit sequence of data>
// CHAR = <any US-ASCII character (octets 0 - 127)>
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
// CR = <US-ASCII CR, carriage return (13)>
// LF = <US-ASCII LF, linefeed (10)>
// SP = <US-ASCII SP, space (32)>
// HT = <US-ASCII HT, horizontal-tab (9)>
// <"> = <US-ASCII double-quote mark (34)>
// CRLF = CR LF
// LWS = [CRLF] 1*( SP | HT )
// TEXT = <any OCTET except CTLs, but including LWS>
// separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <">
// | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
// token = 1*<any CHAR except CTLs or separators>
// qdtext = <any TEXT except <">>
for c := 0; c < 256; c++ {
var t octetType
isCtl := c <= 31 || c == 127
isChar := 0 <= c && c <= 127
isSeparator := strings.ContainsRune(" \t\"(),/:;<=>?@[]\\{}", rune(c))
if strings.ContainsRune(" \t\r\n", rune(c)) {
t |= isSpace
}
if isChar && !isCtl && !isSeparator {
t |= isToken
}
octetTypes[c] = t
}
}
// File returns the contents of the file provided by the path. The content type gets automatically guessed by the file extension.
// If the extension is unknown, then the fallback ContentType is ContentTypeTextPlain. Additionally, a custom ContentType can be set,
// by providing a second argument.
func File(path string, contentType ...string) Response {
data, err := os.ReadFile(path)
if err != nil {
return Error(status.NotFound, err)
}
var header string
if len(contentType) >= 1 {
header = contentType[0]
} else {
extension := filepath.Ext(path)
if extension != "" {
extension = extension[1:]
}
header = ContentType(extension)
if header == "" {
header = ContentTypeTextPlain
}
}
return fileResponse{
code: status.OK,
header: header,
data: data,
}
}
type fileResponse struct {
code int
header string
data []byte
}
func (f fileResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set(header.ContentType, f.header)
rw.WriteHeader(f.code)
_, err := rw.Write(f.data)
if err != nil {
log.Printf("fileResponse: ServeHttp write failed: %v", err)
}
}