2020-03-10 15:21:43 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2020-04-02 12:13:04 +03:00
|
|
|
"bytes"
|
2020-03-10 15:21:43 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-04-02 12:13:04 +03:00
|
|
|
"io"
|
2020-03-10 15:21:43 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-04-02 12:13:04 +03:00
|
|
|
"time"
|
2020-03-10 15:21:43 +00:00
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
"github.com/gobwas/httphead"
|
|
|
|
"github.com/gobwas/ws"
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2020-03-10 15:21:43 +00:00
|
|
|
"github.com/micro/go-micro/v2/api"
|
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/client/selector"
|
2020-04-02 12:13:04 +03:00
|
|
|
raw "github.com/micro/go-micro/v2/codec/bytes"
|
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-03-10 15:21:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// serveWebsocket will stream rpc back over websockets assuming json
|
|
|
|
func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *api.Service, c client.Client) {
|
2020-04-02 12:13:04 +03:00
|
|
|
var op ws.OpCode
|
|
|
|
|
|
|
|
ct := r.Header.Get("Content-Type")
|
|
|
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
|
|
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
|
|
ct = ct[:idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// check proto from request
|
|
|
|
switch ct {
|
|
|
|
case "application/json":
|
|
|
|
op = ws.OpText
|
|
|
|
default:
|
|
|
|
op = ws.OpBinary
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := make(http.Header)
|
|
|
|
if proto, ok := r.Header["Sec-WebSocket-Protocol"]; ok {
|
|
|
|
for _, p := range proto {
|
|
|
|
switch p {
|
|
|
|
case "binary":
|
|
|
|
hdr["Sec-WebSocket-Protocol"] = []string{"binary"}
|
|
|
|
op = ws.OpBinary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
payload, err := requestPayload(r)
|
2020-03-10 15:21:43 +00:00
|
|
|
if err != nil {
|
2020-04-02 12:13:04 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
upgrader := ws.HTTPUpgrader{Timeout: 5 * time.Second,
|
|
|
|
Protocol: func(proto string) bool {
|
|
|
|
if strings.Contains(proto, "binary") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// fallback to support all protocols now
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
Extension: func(httphead.Option) bool {
|
|
|
|
// disable extensions for compatibility
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
Header: hdr,
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, rw, _, err := upgrader.Upgrade(r, w)
|
2020-03-10 15:21:43 +00:00
|
|
|
if err != nil {
|
2020-04-02 12:13:04 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var request interface{}
|
|
|
|
if !bytes.Equal(payload, []byte(`{}`)) {
|
|
|
|
switch ct {
|
|
|
|
case "application/json", "":
|
|
|
|
m := json.RawMessage(payload)
|
|
|
|
request = &m
|
|
|
|
default:
|
|
|
|
request = &raw.Frame{Data: payload}
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
// we always need to set content type for message
|
|
|
|
if ct == "" {
|
|
|
|
ct = "application/json"
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
req := c.NewRequest(
|
|
|
|
service.Name,
|
|
|
|
service.Endpoint.Name,
|
2020-04-02 12:13:04 +03:00
|
|
|
request,
|
|
|
|
client.WithContentType(ct),
|
|
|
|
client.StreamingRequest(),
|
2020-03-10 15:21:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
so := selector.WithStrategy(strategy(service.Services))
|
|
|
|
// create a new stream
|
|
|
|
stream, err := c.Stream(ctx, req, client.WithSelectOption(so))
|
|
|
|
if err != nil {
|
2020-04-02 12:13:04 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
if request != nil {
|
|
|
|
if err = stream.Send(request); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
go writeLoop(rw, stream)
|
2020-03-10 15:21:43 +00:00
|
|
|
|
2020-04-02 12:13:04 +03:00
|
|
|
rsp := stream.Response()
|
2020-03-10 15:21:43 +00:00
|
|
|
|
|
|
|
// receive from stream and send to client
|
|
|
|
for {
|
2020-04-04 00:37:18 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-03-10 15:21:43 +00:00
|
|
|
return
|
2020-04-04 00:37:18 +03:00
|
|
|
case <-stream.Context().Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// read backend response body
|
|
|
|
buf, err := rsp.Read()
|
|
|
|
if err != nil {
|
|
|
|
// wants to avoid import grpc/status.Status
|
|
|
|
if strings.Contains(err.Error(), "context canceled") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
|
2020-04-04 00:37:18 +03:00
|
|
|
// write the response
|
|
|
|
if err := wsutil.WriteServerMessage(rw, op, buf); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
2020-04-02 12:13:04 +03:00
|
|
|
}
|
2020-04-04 00:37:18 +03:00
|
|
|
if err = rw.Flush(); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
2020-04-02 12:13:04 +03:00
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeLoop
|
2020-04-02 12:13:04 +03:00
|
|
|
func writeLoop(rw io.ReadWriter, stream client.Stream) {
|
2020-03-10 15:21:43 +00:00
|
|
|
// close stream when done
|
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
for {
|
2020-04-04 00:37:18 +03:00
|
|
|
select {
|
|
|
|
case <-stream.Context().Done():
|
2020-03-10 15:21:43 +00:00
|
|
|
return
|
2020-04-02 12:13:04 +03:00
|
|
|
default:
|
2020-04-04 00:37:18 +03:00
|
|
|
buf, op, err := wsutil.ReadClientData(rw)
|
|
|
|
if err != nil {
|
2020-04-04 00:55:15 +03:00
|
|
|
if wserr, ok := err.(wsutil.ClosedError); ok {
|
|
|
|
switch wserr.Code {
|
|
|
|
case ws.StatusNormalClosure, ws.StatusNoStatusRcvd:
|
|
|
|
return
|
2020-04-04 00:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 00:55:15 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
2020-04-04 00:37:18 +03:00
|
|
|
}
|
|
|
|
switch op {
|
|
|
|
default:
|
|
|
|
// not relevant
|
|
|
|
continue
|
|
|
|
case ws.OpText, ws.OpBinary:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// send to backend
|
|
|
|
// default to trying json
|
|
|
|
// if the extracted payload isn't empty lets use it
|
|
|
|
request := &raw.Frame{Data: buf}
|
|
|
|
if err := stream.Send(request); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
return
|
2020-04-02 12:13:04 +03:00
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isStream(r *http.Request, srv *api.Service) bool {
|
|
|
|
// check if it's a web socket
|
|
|
|
if !isWebSocket(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// check if the endpoint supports streaming
|
|
|
|
for _, service := range srv.Services {
|
|
|
|
for _, ep := range service.Endpoints {
|
|
|
|
// skip if it doesn't match the name
|
|
|
|
if ep.Name != srv.Endpoint.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// matched if the name
|
|
|
|
if v := ep.Metadata["stream"]; v == "true" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWebSocket(r *http.Request) bool {
|
|
|
|
contains := func(key, val string) bool {
|
|
|
|
vv := strings.Split(r.Header.Get(key), ",")
|
|
|
|
for _, v := range vv {
|
|
|
|
if val == strings.ToLower(strings.TrimSpace(v)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if contains("Connection", "upgrade") && contains("Upgrade", "websocket") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|