2016-10-31 17:26:46 -04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2016-11-06 07:38:32 -05:00
|
|
|
"strings"
|
2016-10-31 17:26:46 -04:00
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Version) GetValue() string {
|
|
|
|
if v == nil {
|
2016-11-02 17:26:21 -04:00
|
|
|
return "1.1"
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.Value
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Method) GetValue() string {
|
|
|
|
if v == nil {
|
2016-11-02 17:26:21 -04:00
|
|
|
return "GET"
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.Value
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Status) GetCode() string {
|
|
|
|
if v == nil {
|
2016-11-02 17:26:21 -04:00
|
|
|
return "200"
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.Code
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Status) GetReason() string {
|
|
|
|
if v == nil {
|
2016-11-02 17:26:21 -04:00
|
|
|
return "OK"
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.Reason
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:26:46 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RequestConfig) PickUri() string {
|
|
|
|
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-11-27 15:39:09 -05:00
|
|
|
func (v *RequestConfig) GetFullVersion() string {
|
|
|
|
return "HTTP/" + v.Version.GetValue()
|
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 {
|
2016-11-06 07:38:32 -05:00
|
|
|
if strings.ToLower(tHeader.Name) == cHeader {
|
|
|
|
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-11-27 15:39:09 -05:00
|
|
|
func (v *ResponseConfig) GetFullVersion() string {
|
|
|
|
return "HTTP/" + v.Version.GetValue()
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|