-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
100 lines (87 loc) · 1.85 KB
/
config.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
package http
import (
"strings"
"v2ray.com/core/common/dice"
)
func pickString(arr []string) string {
n := len(arr)
switch n {
case 0:
return ""
case 1:
return arr[0]
default:
return arr[dice.Roll(n)]
}
}
func (v *RequestConfig) PickUri() string {
return pickString(v.Uri)
}
func (v *RequestConfig) PickHeaders() []string {
n := len(v.Header)
if n == 0 {
return nil
}
headers := make([]string, n)
for idx, headerConfig := range v.Header {
headerName := headerConfig.Name
headerValue := pickString(headerConfig.Value)
headers[idx] = headerName + ": " + headerValue
}
return headers
}
func (v *RequestConfig) GetVersionValue() string {
if v == nil || v.Version == nil {
return "1.1"
}
return v.Version.Value
}
func (v *RequestConfig) GetMethodValue() string {
if v == nil || v.Method == nil {
return "GET"
}
return v.Method.Value
}
func (v *RequestConfig) GetFullVersion() string {
return "HTTP/" + v.GetVersionValue()
}
func (v *ResponseConfig) HasHeader(header string) bool {
cHeader := strings.ToLower(header)
for _, tHeader := range v.Header {
if strings.ToLower(tHeader.Name) == cHeader {
return true
}
}
return false
}
func (v *ResponseConfig) PickHeaders() []string {
n := len(v.Header)
if n == 0 {
return nil
}
headers := make([]string, n)
for idx, headerConfig := range v.Header {
headerName := headerConfig.Name
headerValue := pickString(headerConfig.Value)
headers[idx] = headerName + ": " + headerValue
}
return headers
}
func (v *ResponseConfig) GetVersionValue() string {
if v == nil || v.Version == nil {
return "1.1"
}
return v.Version.Value
}
func (v *ResponseConfig) GetFullVersion() string {
return "HTTP/" + v.GetVersionValue()
}
func (v *ResponseConfig) GetStatusValue() *Status {
if v == nil || v.Status == nil {
return &Status{
Code: "200",
Reason: "OK",
}
}
return v.Status
}