/
conn_http.go
121 lines (94 loc) · 2.15 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
package silverhttp
import (
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/utils"
"github.com/clubpay/ronykit/std/gateways/silverhttp/realip"
"github.com/go-www/silverlining"
)
type httpConn struct {
utils.SpinLock
ctx *silverlining.Context
}
var _ kit.RESTConn = (*httpConn)(nil)
func (c *httpConn) Walk(f func(key string, val string) bool) {
stopCall := false
for _, h := range c.ctx.RequestHeaders().List() {
if !stopCall {
if !f(utils.B2S(h.Name), utils.B2S(h.RawValue)) {
stopCall = true
}
}
}
}
func (c *httpConn) WalkQueryParams(f func(key string, val string) bool) {
stopCall := false
for _, h := range c.ctx.QueryParams() {
if !stopCall {
if !f(utils.B2S(h.Key), utils.B2S(h.Value)) {
stopCall = true
}
}
}
}
func (c *httpConn) Get(key string) string {
v, ok := c.ctx.RequestHeaders().GetBytes(utils.S2B(key))
if ok {
return utils.B2S(v)
}
return ""
}
func (c *httpConn) Set(key string, val string) {
c.ctx.ResponseHeaders().Set(key, val)
}
func (c *httpConn) SetStatusCode(code int) {
c.ctx.WriteHeader(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.SetContentLength(len(data))
_, err := c.ctx.Write(data)
return len(data), err
}
func (c *httpConn) WriteEnvelope(e *kit.Envelope) error {
var (
data []byte
err error
)
data, err = kit.MarshalMessage(e.GetMsg())
if err != nil {
return err
}
resHdr := c.ctx.ResponseHeaders()
e.WalkHdr(
func(key string, val string) bool {
resHdr.Set(key, val)
return true
},
)
c.ctx.SetContentLength(len(data))
_, err = c.ctx.Write(data)
return err
}
func (c *httpConn) Stream() bool {
return false
}
func (c *httpConn) GetHost() string {
return c.ctx.Host()
}
func (c *httpConn) GetRequestURI() string {
return utils.B2S(c.ctx.RawURI())
}
func (c *httpConn) GetMethod() string {
return c.ctx.Method().String()
}
func (c *httpConn) GetPath() string {
return utils.B2S(c.ctx.Path())
}
func (c *httpConn) Redirect(statusCode int, url string) {
c.ctx.Redirect(statusCode, url)
}