1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/transport/internet/headers/http/config.go

101 lines
1.9 KiB
Go
Raw Normal View History

2016-10-31 17:26:46 -04:00
package http
import (
2016-11-06 07:38:32 -05:00
"strings"
2016-12-23 06:42:25 -05:00
2016-10-31 17:26:46 -04:00
"v2ray.com/core/common/dice"
)
func pickString(arr []string) string {
n := len(arr)
2016-11-06 08:04:44 -05:00
switch n {
case 0:
2016-10-31 17:26:46 -04:00
return ""
2016-11-06 08:04:44 -05:00
case 1:
return arr[0]
default:
return arr[dice.Roll(n)]
2016-10-31 17:26:46 -04:00
}
}
func (v *RequestConfig) PickURI() string {
2016-11-27 15:39:09 -05:00
return pickString(v.Uri)
2016-10-31 17:26:46 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *RequestConfig) PickHeaders() []string {
n := len(v.Header)
2016-10-31 17:26:46 -04:00
if n == 0 {
return nil
}
headers := make([]string, n)
2016-11-27 15:39:09 -05:00
for idx, headerConfig := range v.Header {
2016-10-31 17:26:46 -04:00
headerName := headerConfig.Name
headerValue := pickString(headerConfig.Value)
headers[idx] = headerName + ": " + headerValue
}
return headers
}
2016-12-23 06:42:25 -05:00
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
}
2016-11-27 15:39:09 -05:00
func (v *RequestConfig) GetFullVersion() string {
2016-12-23 06:42:25 -05:00
return "HTTP/" + v.GetVersionValue()
2016-10-31 17:26:46 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *ResponseConfig) HasHeader(header string) bool {
2016-11-06 07:38:32 -05:00
cHeader := strings.ToLower(header)
2016-11-27 15:39:09 -05:00
for _, tHeader := range v.Header {
if strings.EqualFold(tHeader.Name, cHeader) {
2016-11-06 07:38:32 -05:00
return true
}
}
return false
}
2016-11-27 15:39:09 -05:00
func (v *ResponseConfig) PickHeaders() []string {
n := len(v.Header)
2016-10-31 17:26:46 -04:00
if n == 0 {
return nil
}
headers := make([]string, n)
2016-11-27 15:39:09 -05:00
for idx, headerConfig := range v.Header {
2016-10-31 17:26:46 -04:00
headerName := headerConfig.Name
headerValue := pickString(headerConfig.Value)
headers[idx] = headerName + ": " + headerValue
}
return headers
}
2016-12-23 06:42:25 -05:00
func (v *ResponseConfig) GetVersionValue() string {
if v == nil || v.Version == nil {
return "1.1"
}
return v.Version.Value
}
2016-11-27 15:39:09 -05:00
func (v *ResponseConfig) GetFullVersion() string {
2016-12-23 06:42:25 -05:00
return "HTTP/" + v.GetVersionValue()
}
func (v *ResponseConfig) GetStatusValue() *Status {
if v == nil || v.Status == nil {
return &Status{
Code: "200",
Reason: "OK",
}
}
return v.Status
2016-10-31 17:26:46 -04:00
}