api/handler/rpc: improvements and fixes (#1535)

* api/handler/rpc: fix empty body case
* api/handler/rpc: copy all request headers to metadata

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-15 01:37:15 +03:00
parent 2fafc81af7
commit 026212486a

10
rpc.go
View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"net/textproto"
"strconv" "strconv"
"strings" "strings"
@ -119,6 +120,13 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// fill contex with http headers // fill contex with http headers
md["Host"] = r.Host md["Host"] = r.Host
md["Method"] = r.Method
// get canonical headers
for k, _ := range r.Header {
// may be need to get all values for key like r.Header.Values() provide in go 1.14
md[textproto.CanonicalMIMEHeaderKey(k)] = r.Header.Get(k)
}
// merge context with overwrite // merge context with overwrite
cx = metadata.MergeContext(cx, md, true) cx = metadata.MergeContext(cx, md, true)
@ -384,8 +392,6 @@ func requestPayload(r *http.Request) ([]byte, error) {
} }
if b := buf.Bytes(); len(b) > 0 { if b := buf.Bytes(); len(b) > 0 {
bodybuf = b bodybuf = b
} else {
return []byte{}, nil
} }
if bodydst == "" || bodydst == "*" { if bodydst == "" || bodydst == "*" {
if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil { if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil {