request.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package http3
  2. import (
  3. "errors"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "github.com/quic-go/qpack"
  9. )
  10. func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {
  11. var path, authority, method, protocol, scheme, contentLengthStr string
  12. httpHeaders := http.Header{}
  13. for _, h := range headers {
  14. switch h.Name {
  15. case ":path":
  16. path = h.Value
  17. case ":method":
  18. method = h.Value
  19. case ":authority":
  20. authority = h.Value
  21. case ":protocol":
  22. protocol = h.Value
  23. case ":scheme":
  24. scheme = h.Value
  25. case "content-length":
  26. contentLengthStr = h.Value
  27. default:
  28. if !h.IsPseudo() {
  29. httpHeaders.Add(h.Name, h.Value)
  30. }
  31. }
  32. }
  33. // concatenate cookie headers, see https://tools.ietf.org/html/rfc6265#section-5.4
  34. if len(httpHeaders["Cookie"]) > 0 {
  35. httpHeaders.Set("Cookie", strings.Join(httpHeaders["Cookie"], "; "))
  36. }
  37. isConnect := method == http.MethodConnect
  38. // Extended CONNECT, see https://datatracker.ietf.org/doc/html/rfc8441#section-4
  39. isExtendedConnected := isConnect && protocol != ""
  40. if isExtendedConnected {
  41. if scheme == "" || path == "" || authority == "" {
  42. return nil, errors.New("extended CONNECT: :scheme, :path and :authority must not be empty")
  43. }
  44. } else if isConnect {
  45. if path != "" || authority == "" { // normal CONNECT
  46. return nil, errors.New(":path must be empty and :authority must not be empty")
  47. }
  48. } else if len(path) == 0 || len(authority) == 0 || len(method) == 0 {
  49. return nil, errors.New(":path, :authority and :method must not be empty")
  50. }
  51. var u *url.URL
  52. var requestURI string
  53. var err error
  54. if isConnect {
  55. u = &url.URL{}
  56. if isExtendedConnected {
  57. u, err = url.ParseRequestURI(path)
  58. if err != nil {
  59. return nil, err
  60. }
  61. } else {
  62. u.Path = path
  63. }
  64. u.Scheme = scheme
  65. u.Host = authority
  66. requestURI = authority
  67. } else {
  68. protocol = "HTTP/3.0"
  69. u, err = url.ParseRequestURI(path)
  70. if err != nil {
  71. return nil, err
  72. }
  73. requestURI = path
  74. }
  75. var contentLength int64
  76. if len(contentLengthStr) > 0 {
  77. contentLength, err = strconv.ParseInt(contentLengthStr, 10, 64)
  78. if err != nil {
  79. return nil, err
  80. }
  81. }
  82. return &http.Request{
  83. Method: method,
  84. URL: u,
  85. Proto: protocol,
  86. ProtoMajor: 3,
  87. ProtoMinor: 0,
  88. Header: httpHeaders,
  89. Body: nil,
  90. ContentLength: contentLength,
  91. Host: authority,
  92. RequestURI: requestURI,
  93. }, nil
  94. }
  95. func hostnameFromRequest(req *http.Request) string {
  96. if req.URL != nil {
  97. return req.URL.Host
  98. }
  99. return ""
  100. }