Skip to content

Commit

Permalink
#443 support pass metadata via jsonrpc 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Apr 10, 2020
1 parent 327a93b commit 5e3a8ca
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions server/jsonrpc2.go
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"

"github.com/rs/cors"
Expand Down Expand Up @@ -34,7 +35,7 @@ func (s *Server) jsonrpcHandler(w http.ResponseWriter, r *http.Request) {
return
}
conn := r.Context().Value(HttpConnContextKey).(net.Conn)

ctx := context.WithValue(r.Context(), RemoteConnContextKey, conn)

if req.ID != nil {
Expand All @@ -54,6 +55,9 @@ func (s *Server) handleJSONRPCRequest(ctx context.Context, r *jsonrpcRequest, he
res.ID = r.ID

req := protocol.GetPooledMsg()
if req.Metadata == nil {
req.Metadata = make(map[string]string)
}

if r.ID == nil {
req.SetOneway(true)
Expand All @@ -73,11 +77,19 @@ func (s *Server) handleJSONRPCRequest(ctx context.Context, r *jsonrpcRequest, he
req.ServiceMethod = pathAndMethod[1]
req.Payload = *r.Params

// meta
meta := header.Get(XMeta)
if meta != "" {
metadata, _ := url.ParseQuery(meta)
for k, v := range metadata {
if len(v) > 0 {
req.Metadata[k] = v[0]
}
}
}

auth := header.Get("Authorization")
if auth != "" {
if req.Metadata == nil {
req.Metadata = make(map[string]string)
}
req.Metadata[share.AuthKey] = auth
}

Expand Down Expand Up @@ -205,17 +217,17 @@ func (s *Server) SetCORS(options *CORSOptions) {
func (s *Server) startJSONRPC2(ln net.Listener) {
newServer := http.NewServeMux()
newServer.HandleFunc("/", s.jsonrpcHandler)
srv := http.Server{ConnContext: func(ctx context.Context, c net.Conn) context.Context{

srv := http.Server{ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, HttpConnContextKey, c)
}}

if s.corsOptions != nil {
opt := cors.Options(*s.corsOptions)
c := cors.New(opt)
mux := c.Handler(newServer)
srv.Handler = mux

go srv.Serve(ln)
} else {
srv.Handler = newServer
Expand Down

0 comments on commit 5e3a8ca

Please sign in to comment.