-
Notifications
You must be signed in to change notification settings - Fork 3
/
fasthttp_adapter.go
145 lines (118 loc) · 3.17 KB
/
fasthttp_adapter.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
package fasthttp
import (
"bytes"
"github.com/valyala/fasthttp"
"github.com/xray-family/xray"
"io"
"reflect"
"unsafe"
)
type readCloser struct {
payload []byte
reader io.Reader
}
func (c *readCloser) Close() error { return nil }
func (c *readCloser) Read(p []byte) (n int, err error) { return c.reader.Read(p) }
func (c *readCloser) Bytes() []byte { return c.payload }
type responseWriter struct {
writer *fasthttp.Response
header xray.Header
}
func (c *responseWriter) Write(p []byte) (n int, err error) {
return c.writer.BodyWriter().Write(p)
}
func (c *responseWriter) Protocol() string {
return xray.ProtocolHTTP
}
func (c *responseWriter) Header() xray.Header {
return c.header
}
func (c *responseWriter) Code(code int) {
c.writer.SetStatusCode(code)
}
func (c *responseWriter) Raw() any {
return c.writer
}
func (c *responseWriter) Flush() error {
return nil
}
func NewAdapter(router *xray.Router) *Adapter {
return &Adapter{router: router}
}
// Adapter FastHTTP适配器
type Adapter struct {
router *xray.Router
}
// SetRouter 设置路由器
func (c *Adapter) SetRouter(r *xray.Router) *Adapter {
c.router = r
return c
}
// ServeFastHTTP 服务HTTP
func (c *Adapter) ServeFastHTTP(ctx *fasthttp.RequestCtx) {
var body = ctx.Request.Body()
var r = &xray.Request{
Raw: &ctx.Request,
Header: &requestHeader{RequestHeader: &ctx.Request.Header},
Method: b2s(ctx.Method()),
Body: &readCloser{payload: body, reader: bytes.NewReader(body)},
}
var uctx = xray.NewContext(c.router, r, &responseWriter{
writer: &ctx.Response,
header: &responseHeader{ResponseHeader: &ctx.Response.Header},
})
c.router.EmitEvent(r.Method, b2s(ctx.Request.URI().Path()), uctx)
}
// b2s converts byte slice to a string without memory allocation.
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func b2s(b []byte) string {
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&b))
}
// s2b converts string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func s2b(s string) (b []byte) {
/* #nosec G103 */
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
/* #nosec G103 */
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
type requestHeader struct {
*fasthttp.RequestHeader
}
func (c *requestHeader) New() xray.Header {
return &requestHeader{}
}
func (c *requestHeader) Get(key string) string { return b2s(c.Peek(key)) }
func (c *requestHeader) Range(f func(key string, value string) bool) {
next := true
c.VisitAll(func(key, value []byte) {
if !next {
return
}
next = f(b2s(key), b2s(value))
})
}
type responseHeader struct {
*fasthttp.ResponseHeader
}
func (c *responseHeader) New() xray.Header { return &responseHeader{} }
func (c *responseHeader) Get(key string) string { return b2s(c.Peek(key)) }
func (c *responseHeader) Range(f func(key string, value string) bool) {
next := true
c.VisitAll(func(key, value []byte) {
if !next {
return
}
next = f(b2s(key), b2s(value))
})
}