This repository was archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_buffer.go
More file actions
63 lines (58 loc) · 1.54 KB
/
response_buffer.go
File metadata and controls
63 lines (58 loc) · 1.54 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
package detour
import (
"bytes"
"io"
"net/http"
)
type responseBuffer struct {
statusCode int
headers http.Header
body *bytes.Buffer
}
func newResponseBuffer() *responseBuffer {
buffer := new(responseBuffer)
buffer.initialize()
return buffer
}
func (this *responseBuffer) StatusCode() int { return this.statusCode }
func (this *responseBuffer) Header() http.Header { return this.headers }
func (this *responseBuffer) Write(p []byte) (int, error) { return this.body.Write(p) }
func (this *responseBuffer) WriteHeader(statusCode int) { this.statusCode = statusCode }
func (this *responseBuffer) flush(response http.ResponseWriter) {
copyHeaders(this.headers, response.Header())
response.WriteHeader(this.statusCode)
_, _ = io.Copy(response, this.body)
this.initialize()
}
func copyHeaders(source, destination http.Header) {
for key, value := range source {
destination[key] = append(destination[key], value...)
}
}
func (this *responseBuffer) initialize() {
this.initializeStatusCode()
this.initializeHeaders()
this.initializeBody()
}
func (this *responseBuffer) initializeStatusCode() {
this.statusCode = http.StatusOK
}
func (this *responseBuffer) initializeBody() {
if this.body == nil {
this.body = new(bytes.Buffer)
} else {
this.body.Reset()
}
}
func (this *responseBuffer) initializeHeaders() {
if this.headers == nil {
this.headers = make(http.Header)
} else {
this.resetHeaders()
}
}
func (this *responseBuffer) resetHeaders() {
for key := range this.headers {
delete(this.headers, key)
}
}