1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package resolver
import (
"bufio"
"bytes"
"context"
"net/http"
"strings"
"github.com/go-chassis/go-chassis/core/invocation"
"k8s.io/klog"
"github.com/kubeedge/kubeedge/edgemesh/pkg/config"
)
type Resolver interface {
Resolve(chan []byte, chan interface{}, func(string, invocation.Invocation)) (invocation.Invocation, bool)
}
type MyResolver struct {
Name string
}
func httpMethods() (methods []string) {
methods = []string{"GET", "HEAD", "POST", "OPTIONS", "PUT", "DELETE", "TRACE", "CONNECT"}
return
}
func isHTTPRequest(s string) bool {
methods := httpMethods()
for _, method := range methods {
if strings.HasPrefix(s, method) {
return true
}
}
return false
}
func (resolver *MyResolver) Resolve(data chan []byte, stop chan interface{}, invCallback func(string, invocation.Invocation)) (invocation.Invocation, bool) {
content := ""
protocol := ""
for {
select {
case d := <-data:
strData := string(d[:])
if protocol == "" {
if isHTTPRequest(strData) {
protocol = "http"
} else {
return invocation.Invocation{}, false
}
}
content += strData
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader([]byte(content))))
if err == nil {
content = ""
req.RequestURI = ""
i := invocation.New(context.Background())
i.MicroServiceName = req.Host
i.SourceServiceID = ""
i.Protocol = "rest"
i.Args = req
i.Strategy = config.Get().LBStrategy
i.Reply = &http.Response{}
invCallback("http", *i)
}
case <-stop:
i := invocation.Invocation{MicroServiceName: resolver.Name, Args: content}
invCallback(protocol, i)
return i, true
}
klog.Infof("content: %s\n", content)
}
}
|