config.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package http
  2. import (
  3. "strings"
  4. "v2ray.com/core/common/dice"
  5. )
  6. func (this *Version) GetValue() string {
  7. if this == nil {
  8. return "1.1"
  9. }
  10. return this.Value
  11. }
  12. func (this *Method) GetValue() string {
  13. if this == nil {
  14. return "GET"
  15. }
  16. return this.Value
  17. }
  18. func (this *Status) GetCode() string {
  19. if this == nil {
  20. return "200"
  21. }
  22. return this.Code
  23. }
  24. func (this *Status) GetReason() string {
  25. if this == nil {
  26. return "OK"
  27. }
  28. return this.Reason
  29. }
  30. func pickString(arr []string) string {
  31. n := len(arr)
  32. if n == 0 {
  33. return ""
  34. }
  35. return arr[dice.Roll(n)]
  36. }
  37. func (this *RequestConfig) PickUri() string {
  38. return pickString(this.Uri)
  39. }
  40. func (this *RequestConfig) PickHeaders() []string {
  41. n := len(this.Header)
  42. if n == 0 {
  43. return nil
  44. }
  45. headers := make([]string, n)
  46. for idx, headerConfig := range this.Header {
  47. headerName := headerConfig.Name
  48. headerValue := pickString(headerConfig.Value)
  49. headers[idx] = headerName + ": " + headerValue
  50. }
  51. return headers
  52. }
  53. func (this *RequestConfig) GetFullVersion() string {
  54. return "HTTP/" + this.Version.GetValue()
  55. }
  56. func (this *ResponseConfig) HasHeader(header string) bool {
  57. cHeader := strings.ToLower(header)
  58. for _, tHeader := range this.Header {
  59. if strings.ToLower(tHeader.Name) == cHeader {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. func (this *ResponseConfig) PickHeaders() []string {
  66. n := len(this.Header)
  67. if n == 0 {
  68. return nil
  69. }
  70. headers := make([]string, n)
  71. for idx, headerConfig := range this.Header {
  72. headerName := headerConfig.Name
  73. headerValue := pickString(headerConfig.Value)
  74. headers[idx] = headerName + ": " + headerValue
  75. }
  76. return headers
  77. }
  78. func (this *ResponseConfig) GetFullVersion() string {
  79. return "HTTP/" + this.Version.GetValue()
  80. }