generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathparse.go
273 lines (240 loc) · 7.95 KB
/
parse.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package echo
import (
"fmt"
"net/http"
"regexp"
"sort"
"strings"
)
// Field is a list of fields returned in responses from the Echo server.
type Field string
const (
RequestIDField Field = "X-Request-Id"
ServiceVersionField Field = "ServiceVersion"
ServicePortField Field = "ServicePort"
StatusCodeField Field = "StatusCode"
URLField Field = "URL"
ForwarderURLField Field = "Url"
ForwarderMessageField Field = "Echo"
ForwarderHeaderField Field = "Header"
HostField Field = "Host"
HostnameField Field = "Hostname"
MethodField Field = "Method"
ProtocolField Field = "Proto"
AlpnField Field = "Alpn"
RequestHeaderField Field = "RequestHeader"
ResponseHeaderField Field = "ResponseHeader"
ClusterField Field = "Cluster"
IPField Field = "IP" // The Requester’s IP Address.
LatencyField Field = "Latency"
ActiveRequestsField Field = "ActiveRequests"
DNSProtocolField Field = "Protocol"
DNSQueryField Field = "Query"
DNSServerField Field = "DnsServer"
CipherField Field = "Cipher"
TLSVersionField Field = "Version"
TLSServerName Field = "ServerName"
)
var (
requestIDFieldRegex = regexp.MustCompile("(?i)" + string(RequestIDField) + "=(.*)")
serviceVersionFieldRegex = regexp.MustCompile(string(ServiceVersionField) + "=(.*)")
servicePortFieldRegex = regexp.MustCompile(string(ServicePortField) + "=(.*)")
statusCodeFieldRegex = regexp.MustCompile(string(StatusCodeField) + "=(.*)")
hostFieldRegex = regexp.MustCompile(string(HostField) + "=(.*)")
hostnameFieldRegex = regexp.MustCompile(string(HostnameField) + "=(.*)")
requestHeaderFieldRegex = regexp.MustCompile(string(RequestHeaderField) + "=(.*)")
responseHeaderFieldRegex = regexp.MustCompile(string(ResponseHeaderField) + "=(.*)")
URLFieldRegex = regexp.MustCompile(string(URLField) + "=(.*)")
ClusterFieldRegex = regexp.MustCompile(string(ClusterField) + "=(.*)")
IPFieldRegex = regexp.MustCompile(string(IPField) + "=(.*)")
methodFieldRegex = regexp.MustCompile(string(MethodField) + "=(.*)")
protocolFieldRegex = regexp.MustCompile(string(ProtocolField) + "=(.*)")
alpnFieldRegex = regexp.MustCompile(string(AlpnField) + "=(.*)")
)
// Response represents a response to a single echo request.
type Response struct {
// RequestURL is the requested URL. This differs from URL, which is the just the path.
// For example, RequestURL=http://foo/bar, URL=/bar
RequestURL string
// Method used (for HTTP).
Method string
// Protocol used for the request.
Protocol string
// Alpn value (for HTTP).
Alpn string
// RawContent is the original unparsed content for this response
RawContent string
// ID is a unique identifier of the resource in the response
ID string
// URL is the url the request is sent to
URL string
// Version is the version of the resource in the response
Version string
// Port is the port of the resource in the response
Port string
// Code is the response code
Code string
// Host is the host called by the request
Host string
// Hostname is the host that responded to the request
Hostname string
// The cluster where the server is deployed.
Cluster string
// IP is the requester's ip address
IP string
// rawBody gives a map of all key/values in the body of the response.
rawBody map[string]string
RequestHeaders http.Header
ResponseHeaders http.Header
}
func ParseResponse(output string) Response {
out := Response{
RawContent: output,
RequestHeaders: make(http.Header),
ResponseHeaders: make(http.Header),
}
match := requestIDFieldRegex.FindStringSubmatch(output)
if match != nil {
out.ID = match[1]
}
match = methodFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Method = match[1]
}
match = protocolFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Protocol = match[1]
}
match = alpnFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Alpn = match[1]
}
match = serviceVersionFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Version = match[1]
}
match = servicePortFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Port = match[1]
}
match = statusCodeFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Code = match[1]
}
match = hostFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Host = match[1]
}
match = hostnameFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Hostname = match[1]
}
match = URLFieldRegex.FindStringSubmatch(output)
if match != nil {
out.URL = match[1]
}
match = ClusterFieldRegex.FindStringSubmatch(output)
if match != nil {
out.Cluster = match[1]
}
match = IPFieldRegex.FindStringSubmatch(output)
if match != nil {
out.IP = match[1]
}
out.rawBody = map[string]string{}
matches := requestHeaderFieldRegex.FindAllStringSubmatch(output, -1)
for _, kv := range matches {
sl := strings.SplitN(kv[1], ":", 2)
if len(sl) != 2 {
continue
}
out.RequestHeaders.Set(sl[0], sl[1])
}
matches = responseHeaderFieldRegex.FindAllStringSubmatch(output, -1)
for _, kv := range matches {
sl := strings.SplitN(kv[1], ":", 2)
if len(sl) != 2 {
continue
}
out.ResponseHeaders.Set(sl[0], sl[1])
}
for _, l := range strings.Split(output, "\n") {
prefixSplit := strings.Split(l, "body] ")
if len(prefixSplit) != 2 {
continue
}
kv := strings.SplitN(prefixSplit[1], "=", 2)
if len(kv) != 2 {
continue
}
out.rawBody[kv[0]] = kv[1]
}
return out
}
// HeaderType is a helper enum for retrieving Headers from a Response.
type HeaderType string
const (
RequestHeader HeaderType = "request"
ResponseHeader HeaderType = "response"
)
// GetHeaders returns the appropriate headers for the given type.
func (r Response) GetHeaders(hType HeaderType) http.Header {
switch hType {
case RequestHeader:
return r.RequestHeaders
case ResponseHeader:
return r.ResponseHeaders
default:
panic("invalid HeaderType enum: " + hType)
}
}
// Body returns the lines of the response body, in order
func (r Response) Body() []string {
type keyValue struct {
k, v string
}
var keyValues []keyValue
// rawBody is in random order, so get the order back via sorting.
for k, v := range r.rawBody {
keyValues = append(keyValues, keyValue{k, v})
}
sort.Slice(keyValues, func(i, j int) bool {
return keyValues[i].k < keyValues[j].k
})
var resp []string
for _, kv := range keyValues {
resp = append(resp, kv.v)
}
return resp
}
func (r Response) String() string {
out := ""
out += fmt.Sprintf("RawContent: %s\n", r.RawContent)
out += fmt.Sprintf("ID: %s\n", r.ID)
out += fmt.Sprintf("Method: %s\n", r.Method)
out += fmt.Sprintf("Protocol: %s\n", r.Protocol)
out += fmt.Sprintf("Alpn: %s\n", r.Alpn)
out += fmt.Sprintf("URL: %s\n", r.URL)
out += fmt.Sprintf("Version: %s\n", r.Version)
out += fmt.Sprintf("Port: %s\n", r.Port)
out += fmt.Sprintf("Code: %s\n", r.Code)
out += fmt.Sprintf("Host: %s\n", r.Host)
out += fmt.Sprintf("Hostname: %s\n", r.Hostname)
out += fmt.Sprintf("Cluster: %s\n", r.Cluster)
out += fmt.Sprintf("IP: %s\n", r.IP)
out += fmt.Sprintf("Request Headers: %v\n", r.RequestHeaders)
out += fmt.Sprintf("Response Headers: %v\n", r.ResponseHeaders)
return out
}