istio

Форк
0
/
parse.go 
157 строк · 4.3 Кб
1
//  Copyright Istio Authors
2
//
3
//  Licensed under the Apache License, Version 2.0 (the "License");
4
//  you may not use this file except in compliance with the License.
5
//  You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//  Unless required by applicable law or agreed to in writing, software
10
//  distributed under the License is distributed on an "AS IS" BASIS,
11
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//  See the License for the specific language governing permissions and
13
//  limitations under the License.
14

15
package echo
16

17
import (
18
	"net/http"
19
	"regexp"
20
	"strings"
21

22
	"istio.io/istio/pkg/test/echo/proto"
23
)
24

25
var (
26
	requestIDFieldRegex      = regexp.MustCompile("(?i)" + string(RequestIDField) + "=(.*)")
27
	serviceVersionFieldRegex = regexp.MustCompile(string(ServiceVersionField) + "=(.*)")
28
	servicePortFieldRegex    = regexp.MustCompile(string(ServicePortField) + "=(.*)")
29
	statusCodeFieldRegex     = regexp.MustCompile(string(StatusCodeField) + "=(.*)")
30
	hostFieldRegex           = regexp.MustCompile(string(HostField) + "=(.*)")
31
	hostnameFieldRegex       = regexp.MustCompile(string(HostnameField) + "=(.*)")
32
	requestHeaderFieldRegex  = regexp.MustCompile(string(RequestHeaderField) + "=(.*)")
33
	responseHeaderFieldRegex = regexp.MustCompile(string(ResponseHeaderField) + "=(.*)")
34
	URLFieldRegex            = regexp.MustCompile(string(URLField) + "=(.*)")
35
	ClusterFieldRegex        = regexp.MustCompile(string(ClusterField) + "=(.*)")
36
	IstioVersionFieldRegex   = regexp.MustCompile(string(IstioVersionField) + "=(.*)")
37
	IPFieldRegex             = regexp.MustCompile(string(IPField) + "=(.*)")
38
	methodFieldRegex         = regexp.MustCompile(string(MethodField) + "=(.*)")
39
	protocolFieldRegex       = regexp.MustCompile(string(ProtocolField) + "=(.*)")
40
	alpnFieldRegex           = regexp.MustCompile(string(AlpnField) + "=(.*)")
41
)
42

43
func ParseResponses(req *proto.ForwardEchoRequest, resp *proto.ForwardEchoResponse) Responses {
44
	responses := make([]Response, len(resp.Output))
45
	for i, output := range resp.Output {
46
		responses[i] = parseResponse(output)
47
		responses[i].RequestURL = req.Url
48
	}
49
	return responses
50
}
51

52
func parseResponse(output string) Response {
53
	out := Response{
54
		RawContent:      output,
55
		RequestHeaders:  make(http.Header),
56
		ResponseHeaders: make(http.Header),
57
	}
58

59
	match := requestIDFieldRegex.FindStringSubmatch(output)
60
	if match != nil {
61
		out.ID = match[1]
62
	}
63

64
	match = methodFieldRegex.FindStringSubmatch(output)
65
	if match != nil {
66
		out.Method = match[1]
67
	}
68

69
	match = protocolFieldRegex.FindStringSubmatch(output)
70
	if match != nil {
71
		out.Protocol = match[1]
72
	}
73

74
	match = alpnFieldRegex.FindStringSubmatch(output)
75
	if match != nil {
76
		out.Alpn = match[1]
77
	}
78

79
	match = serviceVersionFieldRegex.FindStringSubmatch(output)
80
	if match != nil {
81
		out.Version = match[1]
82
	}
83

84
	match = servicePortFieldRegex.FindStringSubmatch(output)
85
	if match != nil {
86
		out.Port = match[1]
87
	}
88

89
	match = statusCodeFieldRegex.FindStringSubmatch(output)
90
	if match != nil {
91
		out.Code = match[1]
92
	}
93

94
	match = hostFieldRegex.FindStringSubmatch(output)
95
	if match != nil {
96
		out.Host = match[1]
97
	}
98

99
	match = hostnameFieldRegex.FindStringSubmatch(output)
100
	if match != nil {
101
		out.Hostname = match[1]
102
	}
103

104
	match = URLFieldRegex.FindStringSubmatch(output)
105
	if match != nil {
106
		out.URL = match[1]
107
	}
108

109
	match = ClusterFieldRegex.FindStringSubmatch(output)
110
	if match != nil {
111
		out.Cluster = match[1]
112
	}
113

114
	match = IstioVersionFieldRegex.FindStringSubmatch(output)
115
	if match != nil {
116
		out.IstioVersion = match[1]
117
	}
118

119
	match = IPFieldRegex.FindStringSubmatch(output)
120
	if match != nil {
121
		out.IP = match[1]
122
	}
123

124
	out.rawBody = map[string]string{}
125

126
	matches := requestHeaderFieldRegex.FindAllStringSubmatch(output, -1)
127
	for _, kv := range matches {
128
		sl := strings.SplitN(kv[1], ":", 2)
129
		if len(sl) != 2 {
130
			continue
131
		}
132
		out.RequestHeaders.Set(sl[0], sl[1])
133
	}
134

135
	matches = responseHeaderFieldRegex.FindAllStringSubmatch(output, -1)
136
	for _, kv := range matches {
137
		sl := strings.SplitN(kv[1], ":", 2)
138
		if len(sl) != 2 {
139
			continue
140
		}
141
		out.ResponseHeaders.Set(sl[0], sl[1])
142
	}
143

144
	for _, l := range strings.Split(output, "\n") {
145
		prefixSplit := strings.Split(l, "body] ")
146
		if len(prefixSplit) != 2 {
147
			continue
148
		}
149
		kv := strings.SplitN(prefixSplit[1], "=", 2)
150
		if len(kv) != 2 {
151
			continue
152
		}
153
		out.rawBody[kv[0]] = kv[1]
154
	}
155

156
	return out
157
}
158

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.