request.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package h2quic
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "golang.org/x/net/http2/hpack"
  10. )
  11. func requestFromHeaders(headers []hpack.HeaderField) (*http.Request, error) {
  12. var path, authority, method, contentLengthStr string
  13. httpHeaders := http.Header{}
  14. for _, h := range headers {
  15. switch h.Name {
  16. case ":path":
  17. path = h.Value
  18. case ":method":
  19. method = h.Value
  20. case ":authority":
  21. authority = h.Value
  22. case "content-length":
  23. contentLengthStr = h.Value
  24. default:
  25. if !h.IsPseudo() {
  26. httpHeaders.Add(h.Name, h.Value)
  27. }
  28. }
  29. }
  30. // concatenate cookie headers, see https://tools.ietf.org/html/rfc6265#section-5.4
  31. if len(httpHeaders["Cookie"]) > 0 {
  32. httpHeaders.Set("Cookie", strings.Join(httpHeaders["Cookie"], "; "))
  33. }
  34. if len(path) == 0 || len(authority) == 0 || len(method) == 0 {
  35. return nil, errors.New(":path, :authority and :method must not be empty")
  36. }
  37. u, err := url.Parse(path)
  38. if err != nil {
  39. return nil, err
  40. }
  41. var contentLength int64
  42. if len(contentLengthStr) > 0 {
  43. contentLength, err = strconv.ParseInt(contentLengthStr, 10, 64)
  44. if err != nil {
  45. return nil, err
  46. }
  47. }
  48. return &http.Request{
  49. Method: method,
  50. URL: u,
  51. Proto: "HTTP/2.0",
  52. ProtoMajor: 2,
  53. ProtoMinor: 0,
  54. Header: httpHeaders,
  55. Body: nil,
  56. ContentLength: contentLength,
  57. Host: authority,
  58. RequestURI: path,
  59. TLS: &tls.ConnectionState{},
  60. }, nil
  61. }
  62. func hostnameFromRequest(req *http.Request) string {
  63. if req.URL != nil {
  64. return req.URL.Host
  65. }
  66. return ""
  67. }