/
response.go
52 lines (43 loc) · 1017 Bytes
/
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
package fasthttp
import (
"bufio"
"net"
"net/http"
"github.com/valyala/fasthttp"
)
type Response struct {
ctx *fasthttp.RequestCtx
header http.Header
headerSet bool
}
func (r *Response) Header() http.Header {
return r.header
}
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.ctx.Conn(), bufio.NewReadWriter(bufio.NewReader(r.ctx.Conn()), bufio.NewWriter(r.ctx.Conn())), nil
}
func (r *Response) Write(body []byte) (int, error) {
if !r.headerSet {
r.setHeaders(body)
r.headerSet = true
}
return r.ctx.Write(body)
}
func (r *Response) WriteHeader(status int) {
r.ctx.SetStatusCode(status)
}
func (r *Response) setHeaders(body []byte) {
// Because of special header handling in fasthttp, we need to manually
// set the header here.
for k, vv := range r.header {
for _, v := range vv {
r.ctx.Response.Header.Add(k, v)
}
}
}
func NewResponse(ctx *fasthttp.RequestCtx) *Response {
return &Response{
ctx: ctx,
header: make(http.Header),
}
}