فهرست منبع

feat:增加URL传RPCX头信息

Cheng Jian 2 سال پیش
والد
کامیت
18b0a9216e
1فایلهای تغییر یافته به همراه32 افزوده شده و 7 حذف شده
  1. 32 7
      rpcx-gateway/converter.go

+ 32 - 7
rpcx-gateway/converter.go

@@ -31,7 +31,7 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
 	req.SetMessageType(protocol.Request)
 
 	h := r.Header
-	seq := h.Get(XMessageID)
+	seq := getRpcxHeader(r, XMessageID)
 	if seq != "" {
 		id, err := strconv.ParseUint(seq, 10, 64)
 		if err != nil {
@@ -40,12 +40,12 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
 		req.SetSeq(id)
 	}
 
-	heartbeat := h.Get(XHeartbeat)
+	heartbeat := getRpcxHeader(r, XHeartbeat)
 	if heartbeat != "" {
 		req.SetHeartbeat(true)
 	}
 
-	oneway := h.Get(XOneway)
+	oneway := getRpcxHeader(r, XOneway)
 	if oneway != "" {
 		req.SetOneway(true)
 	}
@@ -54,7 +54,7 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
 		req.SetCompressType(protocol.Gzip)
 	}
 
-	st := h.Get(XSerializeType)
+	st := getRpcxHeader(r, XSerializeType)
 	if st != "" {
 		rst, err := strconv.Atoi(st)
 		if err != nil {
@@ -65,7 +65,7 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
 		return nil, errors.New("empty serialized type")
 	}
 
-	meta := h.Get(XMeta)
+	meta := getRpcxHeader(r, XMeta)
 	if meta != "" {
 		metadata, err := url.ParseQuery(meta)
 		if err != nil {
@@ -80,14 +80,16 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
 		req.Metadata = mm
 	}
 
-	sp := h.Get(XServicePath)
+	req.Metadata = getUrlParams(r, req.Metadata)
+
+	sp := getRpcxHeader(r, XServicePath)
 	if sp != "" {
 		req.ServicePath = sp
 	} else {
 		return nil, errors.New("empty servicepath")
 	}
 
-	sm := h.Get(XServiceMethod)
+	sm := getRpcxHeader(r, XServiceMethod)
 	if sm != "" {
 		req.ServiceMethod = sm
 	} else {
@@ -152,6 +154,29 @@ func MultipartRequest2RpcxRequest(r *http.Request) (map[string]string, *multipar
 	return formValues, form.File["file"][0], nil
 }
 
+func getRpcxHeader(r *http.Request, key string) string {
+	val := r.Header.Get(key)
+	if val != "" {
+		return val
+	} else {
+		if values, ok := r.URL.Query()[key]; ok && len(values) > 0 {
+			return values[0]
+		} else {
+			return ""
+		}
+	}
+}
+
+func getUrlParams(r *http.Request, metadata map[string]string) map[string]string {
+	query := r.URL.Query()
+	for k, v := range query {
+		if k != XMeta {
+			metadata[k] = v[0]
+		}
+	}
+	return metadata
+}
+
 // func RpcxResponse2HttpResponse(res *protocol.Message) (url.Values, []byte, error) {
 // 	m := make(url.Values)
 // 	m.Set(XVersion, strconv.Itoa(int(res.Version())))