This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
196 lines (162 loc) · 3.73 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
package h1
import (
"io"
"strconv"
"sync"
)
var ResponsePool = sync.Pool{
New: func() any {
return &Response{
upstream: nil,
buf: make([]byte, 0, 8192),
itoaBuf: make([]byte, 0, 32),
n: 0,
ContentLength: -1,
//Connection: ConnectionKeepAlive,
}
},
}
func GetResponse(upstream io.Writer) *Response {
r := ResponsePool.Get().(*Response)
r.upstream = upstream
return r
}
func PutResponse(r *Response) {
r.Reset()
r.upstream = nil
ResponsePool.Put(r)
}
type Response struct {
upstream io.Writer
buf []byte // Note: Do not use append() to add bytes to this buffer. Use Write() instead. This is to avoid unnecessary memory allocations.
n int
// Itoa Buffer
itoaBuf []byte // buffer for itoa
// Standard Hop-by-Hop response headers.
ContentLength int
//Connection Connection
}
func (r *Response) Reset() {
r.n = 0
r.buf = r.buf[:0]
r.ContentLength = -1
}
var DefaultFastDateServer = NewFastDateServer("h1")
var _ = func() int {
go DefaultFastDateServer.Start()
return 0
}()
var DateServerHeaderFunc = func() []byte {
return DefaultFastDateServer.GetDate()
}
func (r *Response) Flush() error {
if r.upstream == nil || r.n == 0 {
return nil
}
_, err := r.upstream.Write(r.buf[:r.n])
if err != nil {
return err
}
r.Reset()
return nil
}
func (r *Response) Write(b []byte) (int, error) {
n := copy(r.buf[r.n:cap(r.buf)], b) // copy to buffer
r.n += n
if len(r.buf) < cap(r.buf) {
return n, nil
}
// buffer is full, flush it
err := r.Flush()
if err != nil {
return 0, err
}
// If b is bigger than buffer, write it directly
if len(b)-n > cap(r.buf) {
_, err = r.upstream.Write(b[n:])
return len(b), err
}
// copy b to buffer
copy(r.buf, b[n:])
r.n = len(b) - n
return n, nil
}
func (r *Response) WriteString(b string) (int, error) {
n := copy(r.buf[r.n:cap(r.buf)], b) // copy to buffer
r.n += n
if len(r.buf) < cap(r.buf) {
return n, nil
}
// buffer is full, flush it
err := r.Flush()
if err != nil {
return 0, err
}
// If b is bigger than buffer, write it directly
if len(b)-n > cap(r.buf) {
_, err = r.upstream.Write(stringToBytes(b[n:]))
return len(b), err
}
// copy b to buffer
copy(r.buf, b[n:])
r.n = len(b) - n
return n, nil
}
func (r *Response) WriteInt(i int) (int, error) {
r.itoaBuf = r.itoaBuf[:0]
r.itoaBuf = strconv.AppendInt(r.itoaBuf, int64(i), 10)
return r.Write(r.itoaBuf)
}
func (r *Response) WriteUint(u uint) (int, error) {
r.itoaBuf = r.itoaBuf[:0]
r.itoaBuf = strconv.AppendUint(r.itoaBuf, uint64(u), 10)
return r.Write(r.itoaBuf)
}
func (r *Response) WriteInt64(i int64) (int, error) {
r.itoaBuf = r.itoaBuf[:0]
r.itoaBuf = strconv.AppendInt(r.itoaBuf, i, 10)
return r.Write(r.itoaBuf)
}
func (r *Response) WriteUint64(u uint64) (int, error) {
r.itoaBuf = r.itoaBuf[:0]
r.itoaBuf = strconv.AppendUint(r.itoaBuf, u, 10)
return r.Write(r.itoaBuf)
}
func (r *Response) WriteUint64Hex(u uint64) (int, error) {
r.itoaBuf = r.itoaBuf[:0]
r.itoaBuf = strconv.AppendUint(r.itoaBuf, u, 16)
return r.Write(r.itoaBuf)
}
func (r *Response) WriteStatusLine(status int) error {
_, err := r.Write(GetStatusLine(status))
return err
}
var contentLengthHeader = []byte("Content-Length: ")
var crlf = []byte("\r\n")
func (r *Response) WriteHeader(status int) error {
err := r.WriteStatusLine(status)
if err != nil {
return err
}
// Write standard hop-by-hop response headers
_, err = r.Write(DateServerHeaderFunc())
if err != nil {
return err
}
// Content-Length
if r.ContentLength >= 0 {
_, err = r.Write(contentLengthHeader)
if err != nil {
return err
}
_, err = r.WriteInt(r.ContentLength)
if err != nil {
return err
}
_, err = r.Write(crlf)
if err != nil {
return err
}
}
return nil
}