v2ray-core/transport/internet/headers/http/config.go

101 lines
1.9 KiB
Go
Raw Normal View History

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