1 Star 0 Fork 0

hellooooo/rpcx-gateway

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
converter.go 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
鸟窝 提交于 2018-10-29 18:11 . check empty headers
package gateway
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/smallnest/rpcx/protocol"
)
const (
XVersion = "X-RPCX-Version"
XMessageType = "X-RPCX-MesssageType"
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"
)
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)
}
if h.Get("Content-Encoding") == "gzip" {
req.SetCompressType(protocol.Gzip)
}
st := h.Get(XSerializeType)
if st != "" {
rst, err := strconv.Atoi(st)
if err != nil {
return nil, err
}
req.SetSerializeType(protocol.SerializeType(rst))
} else {
return nil, errors.New("empty serialized type")
}
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
}
sp := h.Get(XServicePath)
if sp != "" {
req.ServicePath = sp
} else {
return nil, errors.New("empty servicepath")
}
sm := h.Get(XServiceMethod)
if sm != "" {
req.ServiceMethod = sm
} else {
return nil, errors.New("empty servicemethod")
}
payload, err := ioutil.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
// }
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kmxcl/rpcx-gateway.git
git@gitee.com:kmxcl/rpcx-gateway.git
kmxcl
rpcx-gateway
rpcx-gateway
master

搜索帮助