config.go 1.5 KB

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