github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/transport/internet/headers/http/config.go (about)

     1  package http
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/xtls/xray-core/common/dice"
     7  )
     8  
     9  func pickString(arr []string) string {
    10  	n := len(arr)
    11  	switch n {
    12  	case 0:
    13  		return ""
    14  	case 1:
    15  		return arr[0]
    16  	default:
    17  		return arr[dice.Roll(n)]
    18  	}
    19  }
    20  
    21  func (v *RequestConfig) PickURI() string {
    22  	return pickString(v.Uri)
    23  }
    24  
    25  func (v *RequestConfig) PickHeaders() []string {
    26  	n := len(v.Header)
    27  	if n == 0 {
    28  		return nil
    29  	}
    30  	headers := make([]string, n)
    31  	for idx, headerConfig := range v.Header {
    32  		headerName := headerConfig.Name
    33  		headerValue := pickString(headerConfig.Value)
    34  		headers[idx] = headerName + ": " + headerValue
    35  	}
    36  	return headers
    37  }
    38  
    39  func (v *RequestConfig) GetVersionValue() string {
    40  	if v == nil || v.Version == nil {
    41  		return "1.1"
    42  	}
    43  	return v.Version.Value
    44  }
    45  
    46  func (v *RequestConfig) GetMethodValue() string {
    47  	if v == nil || v.Method == nil {
    48  		return "GET"
    49  	}
    50  	return v.Method.Value
    51  }
    52  
    53  func (v *RequestConfig) GetFullVersion() string {
    54  	return "HTTP/" + v.GetVersionValue()
    55  }
    56  
    57  func (v *ResponseConfig) HasHeader(header string) bool {
    58  	cHeader := strings.ToLower(header)
    59  	for _, tHeader := range v.Header {
    60  		if strings.EqualFold(tHeader.Name, cHeader) {
    61  			return true
    62  		}
    63  	}
    64  	return false
    65  }
    66  
    67  func (v *ResponseConfig) PickHeaders() []string {
    68  	n := len(v.Header)
    69  	if n == 0 {
    70  		return nil
    71  	}
    72  	headers := make([]string, n)
    73  	for idx, headerConfig := range v.Header {
    74  		headerName := headerConfig.Name
    75  		headerValue := pickString(headerConfig.Value)
    76  		headers[idx] = headerName + ": " + headerValue
    77  	}
    78  	return headers
    79  }
    80  
    81  func (v *ResponseConfig) GetVersionValue() string {
    82  	if v == nil || v.Version == nil {
    83  		return "1.1"
    84  	}
    85  	return v.Version.Value
    86  }
    87  
    88  func (v *ResponseConfig) GetFullVersion() string {
    89  	return "HTTP/" + v.GetVersionValue()
    90  }
    91  
    92  func (v *ResponseConfig) GetStatusValue() *Status {
    93  	if v == nil || v.Status == nil {
    94  		return &Status{
    95  			Code:   "200",
    96  			Reason: "OK",
    97  		}
    98  	}
    99  	return v.Status
   100  }