forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
123 lines (104 loc) ยท 2.65 KB
/
converter.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
package server
import (
"io"
"net/http"
"net/url"
"strconv"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/share"
)
const (
XVersion = "X-RPCX-Version"
XMessageType = "X-RPCX-MessageType"
XHeartbeat = "X-RPCX-Heartbeat"
XOneway = "X-RPCX-Oneway"
XMessageStatusType = "X-RPCX-MessageStatusType"
XSerializeType = "X-RPCX-SerializeType"
XMessageID = "X-RPCX-MessageID"
XServicePath = "X-RPCX-ServicePath"
XServiceMethod = "X-RPCX-ServiceMethod"
XMeta = "X-RPCX-Meta"
XErrorMessage = "X-RPCX-ErrorMessage"
)
// HTTPRequest2RpcxRequest converts a http request to a rpcx request.
func HTTPRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
req := protocol.NewMessage()
req.SetMessageType(protocol.Request)
h := r.Header
seq := h.Get(XMessageID)
if seq != "" {
id, err := strconv.ParseUint(seq, 10, 64)
if err != nil {
return nil, err
}
req.SetSeq(id)
}
heartbeat := h.Get(XHeartbeat)
if heartbeat != "" {
req.SetHeartbeat(true)
}
oneway := h.Get(XOneway)
if oneway != "" {
req.SetOneway(true)
}
st := h.Get(XSerializeType)
if st != "" {
rst, err := strconv.Atoi(st)
if err != nil {
return nil, err
}
req.SetSerializeType(protocol.SerializeType(rst))
}
meta := h.Get(XMeta)
if meta != "" {
metadata, err := url.ParseQuery(meta)
if err != nil {
return nil, err
}
mm := make(map[string]string)
for k, v := range metadata {
if len(v) > 0 {
mm[k] = v[0]
}
}
req.Metadata = mm
}
auth := h.Get("Authorization")
if auth != "" {
if req.Metadata == nil {
req.Metadata = make(map[string]string)
}
req.Metadata[share.AuthKey] = auth
}
req.ServicePath = h.Get(XServicePath)
req.ServiceMethod = h.Get(XServiceMethod)
payload, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
req.Payload = payload
return req, nil
}
// func RpcxResponse2HttpResponse(res *protocol.Message) (url.Values, []byte, error) {
// m := make(url.Values)
// m.Set(XVersion, strconv.Itoa(int(res.Version())))
// if res.IsHeartbeat() {
// m.Set(XHeartbeat, "true")
// }
// if res.IsOneway() {
// m.Set(XOneway, "true")
// }
// if res.MessageStatusType() == protocol.Error {
// m.Set(XMessageStatusType, "Error")
// } else {
// m.Set(XMessageStatusType, "Normal")
// }
// if res.CompressType() == protocol.Gzip {
// m.Set("Content-Encoding", "gzip")
// }
// m.Set(XSerializeType, strconv.Itoa(int(res.SerializeType())))
// m.Set(XMessageID, strconv.FormatUint(res.Seq(), 10))
// m.Set(XServicePath, res.ServicePath)
// m.Set(XServiceMethod, res.ServiceMethod)
// return m, res.Payload, nil
// }