Repository navigation
Expand file tree
/
Copy pathresponse_builder.go
More file actions
108 lines (87 loc) · 2.76 KB
/
Copy pathresponse_builder.go
File metadata and controls
108 lines (87 loc) · 2.76 KB
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
package playground
import (
"bytes"
"fmt"
"maps"
"net/http"
"slices"
"sort"
"strings"
"time"
)
// responseBuilder is used to construct an HTTP response message with custom start lines, headers, and body content.
type responseBuilder struct {
startLine []byte
header http.Header
body bytes.Buffer
errored bool
}
func newResponseBuilder() *responseBuilder {
return &responseBuilder{
header: http.Header{},
}
}
// SetStartLine sets the start line of the HTTP response with the provided protocol version and status.
func (r *responseBuilder) SetStartLine(proto, status string) {
r.startLine = []byte(fmt.Sprintf("%s %s", strings.TrimSpace(proto), strings.TrimSpace(status)))
}
// SetHeaders copies the provided headers into the HTTP response headers.
func (r *responseBuilder) SetHeaders(h http.Header) {
maps.Copy(r.header, h)
}
// DefaultHeaders sets some default headers for the HTTP response, discarding any previous headers.
func (r *responseBuilder) DefaultHeaders() {
r.header = http.Header{}
r.header.Set("Content-Length", fmt.Sprintf("%d", r.body.Len()))
r.header.Set("Content-Type", "text/plain; charset=utf-8")
r.header.Set("Date", time.Now().Format(time.RFC1123))
r.header.Set("Server", "fontseca.dev/playground (v1.0)")
}
// Write appends the provided byte slice to the body of the HTTP response.
func (r *responseBuilder) Write(p []byte) (n int, err error) {
if r.errored {
return 0, nil
}
return r.body.Write(p)
}
// WriteError writes an error message to the HTTP response, discarding any previous written bytes to the body.
func (r *responseBuilder) WriteError(err error) {
r.errored = true
r.body.Reset()
r.body.WriteString(err.Error())
}
func (r *responseBuilder) build() *bytes.Buffer {
buffer := &bytes.Buffer{}
buffer.Grow(len(r.startLine) + 1 + len(r.header) + len(r.body.Bytes())) // approximate growth
if r.errored {
r.SetStartLine("HTTP/1.0", "503 Service Unavailable")
} else if len(r.startLine) == 0 {
r.SetStartLine("HTTP/1.0", "200 OK")
}
buffer.Write(r.startLine)
buffer.WriteRune('\n')
keys := make([]string, 0, len(r.header))
for key := range maps.Keys(r.header) {
keys = append(keys, key)
}
sort.Strings(keys)
for key := range slices.Values(keys) {
for kk := range slices.Values(r.header.Values(key)) {
buffer.WriteString(fmt.Sprintf("%s: %s\n", key, kk))
}
}
buffer.WriteRune('\n')
if r.errored {
buffer.WriteString("Playground server failed: ")
}
buffer.Write(r.body.Bytes())
return buffer
}
// Bytes returns the HTTP response as a byte slice.
func (r *responseBuilder) Bytes() []byte {
return r.build().Bytes()
}
// String returns the HTTP response as a string.
func (r *responseBuilder) String() string {
return r.build().String()
}