1
// Copyright Istio Authors
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
22
"istio.io/istio/pkg/test/echo/proto"
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) + "=(.*)")
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
52
func parseResponse(output string) Response {
55
RequestHeaders: make(http.Header),
56
ResponseHeaders: make(http.Header),
59
match := requestIDFieldRegex.FindStringSubmatch(output)
64
match = methodFieldRegex.FindStringSubmatch(output)
69
match = protocolFieldRegex.FindStringSubmatch(output)
71
out.Protocol = match[1]
74
match = alpnFieldRegex.FindStringSubmatch(output)
79
match = serviceVersionFieldRegex.FindStringSubmatch(output)
81
out.Version = match[1]
84
match = servicePortFieldRegex.FindStringSubmatch(output)
89
match = statusCodeFieldRegex.FindStringSubmatch(output)
94
match = hostFieldRegex.FindStringSubmatch(output)
99
match = hostnameFieldRegex.FindStringSubmatch(output)
101
out.Hostname = match[1]
104
match = URLFieldRegex.FindStringSubmatch(output)
109
match = ClusterFieldRegex.FindStringSubmatch(output)
111
out.Cluster = match[1]
114
match = IstioVersionFieldRegex.FindStringSubmatch(output)
116
out.IstioVersion = match[1]
119
match = IPFieldRegex.FindStringSubmatch(output)
124
out.rawBody = map[string]string{}
126
matches := requestHeaderFieldRegex.FindAllStringSubmatch(output, -1)
127
for _, kv := range matches {
128
sl := strings.SplitN(kv[1], ":", 2)
132
out.RequestHeaders.Set(sl[0], sl[1])
135
matches = responseHeaderFieldRegex.FindAllStringSubmatch(output, -1)
136
for _, kv := range matches {
137
sl := strings.SplitN(kv[1], ":", 2)
141
out.ResponseHeaders.Set(sl[0], sl[1])
144
for _, l := range strings.Split(output, "\n") {
145
prefixSplit := strings.Split(l, "body] ")
146
if len(prefixSplit) != 2 {
149
kv := strings.SplitN(prefixSplit[1], "=", 2)
153
out.rawBody[kv[0]] = kv[1]