/
conn_http.go
125 lines (96 loc) · 2.3 KB
/
conn_http.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
package fasthttp
import (
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/utils"
"github.com/clubpay/ronykit/std/gateways/fasthttp/internal/realip"
"github.com/valyala/fasthttp"
)
var strLocation = []byte(fasthttp.HeaderLocation)
type httpConn struct {
utils.SpinLock
ctx *fasthttp.RequestCtx
rd *routeData
}
var _ kit.RESTConn = (*httpConn)(nil)
func (c *httpConn) Walk(f func(key string, val string) bool) {
stopCall := false
c.ctx.Request.Header.VisitAll(
func(key, value []byte) {
if stopCall {
return
}
stopCall = !f(utils.B2S(key), utils.B2S(value))
},
)
}
func (c *httpConn) WalkQueryParams(f func(key string, val string) bool) {
stopCall := false
c.ctx.QueryArgs().VisitAll(
func(key, value []byte) {
if stopCall {
return
}
stopCall = !f(utils.B2S(key), utils.B2S(value))
},
)
}
func (c *httpConn) Get(key string) string {
return utils.B2S(c.ctx.Request.Header.Peek(key))
}
func (c *httpConn) Set(key string, val string) {
c.ctx.Response.Header.Set(key, val)
}
func (c *httpConn) SetStatusCode(code int) {
c.ctx.Response.SetStatusCode(code)
}
func (c *httpConn) ConnID() uint64 {
return c.ctx.ConnID()
}
func (c *httpConn) ClientIP() string {
return realip.FromRequest(c.ctx)
}
func (c *httpConn) Write(data []byte) (int, error) {
c.ctx.Response.AppendBody(data)
return len(data), nil
}
func (c *httpConn) WriteEnvelope(e *kit.Envelope) error {
var (
data []byte
err error
)
data, err = kit.MarshalMessage(e.GetMsg())
if err != nil {
return err
}
e.WalkHdr(
func(key string, val string) bool {
c.ctx.Response.Header.Set(key, val)
return true
},
)
c.ctx.SetBody(data)
return nil
}
func (c *httpConn) Stream() bool {
return false
}
func (c *httpConn) GetHost() string {
return utils.B2S(c.ctx.Host())
}
func (c *httpConn) GetRequestURI() string {
c.ctx.URI().RequestURI()
return utils.B2S(c.ctx.Request.RequestURI())
}
func (c *httpConn) GetMethod() string {
return utils.B2S(c.ctx.Method())
}
func (c *httpConn) GetPath() string {
return utils.B2S(c.ctx.URI().Path())
}
func (c *httpConn) Redirect(statusCode int, url string) {
u := fasthttp.AcquireURI()
_ = u.Parse(nil, utils.S2B(url))
c.ctx.Response.Header.SetCanonical(strLocation, u.FullURI())
c.ctx.Response.SetStatusCode(statusCode)
fasthttp.ReleaseURI(u)
}