http.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate bundle -o=h2_bundle.go -prefix=http2 -tags=!nethttpomithttp2 golang.org/x/net/http2
  5. package http
  6. import (
  7. "io"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "unicode/utf8"
  12. "golang.org/x/net/http/httpguts"
  13. )
  14. // incomparable is a zero-width, non-comparable type. Adding it to a struct
  15. // makes that struct also non-comparable, and generally doesn't add
  16. // any size (as long as it's first).
  17. type incomparable [0]func()
  18. // maxInt64 is the effective "infinite" value for the Server and
  19. // Transport's byte-limiting readers.
  20. const maxInt64 = 1<<63 - 1
  21. // aLongTimeAgo is a non-zero time, far in the past, used for
  22. // immediate cancellation of network operations.
  23. var aLongTimeAgo = time.Unix(1, 0)
  24. // omitBundledHTTP2 is set by omithttp2.go when the nethttpomithttp2
  25. // build tag is set. That means h2_bundle.go isn't compiled in and we
  26. // shouldn't try to use it.
  27. var omitBundledHTTP2 bool
  28. // TODO(bradfitz): move common stuff here. The other files have accumulated
  29. // generic http stuff in random places.
  30. // contextKey is a value for use with context.WithValue. It's used as
  31. // a pointer so it fits in an interface{} without allocation.
  32. type contextKey struct {
  33. name string
  34. }
  35. func (k *contextKey) String() string { return "net/http context value " + k.name }
  36. // Given a string of the form "host", "host:port", or "[ipv6::address]:port",
  37. // return true if the string includes a port.
  38. func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
  39. // removeEmptyPort strips the empty port in ":port" to ""
  40. // as mandated by RFC 3986 Section 6.2.3.
  41. func removeEmptyPort(host string) string {
  42. if hasPort(host) {
  43. return strings.TrimSuffix(host, ":")
  44. }
  45. return host
  46. }
  47. func isNotToken(r rune) bool {
  48. return !httpguts.IsTokenRune(r)
  49. }
  50. // stringContainsCTLByte reports whether s contains any ASCII control character.
  51. func stringContainsCTLByte(s string) bool {
  52. for i := 0; i < len(s); i++ {
  53. b := s[i]
  54. if b < ' ' || b == 0x7f {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. func hexEscapeNonASCII(s string) string {
  61. newLen := 0
  62. for i := 0; i < len(s); i++ {
  63. if s[i] >= utf8.RuneSelf {
  64. newLen += 3
  65. } else {
  66. newLen++
  67. }
  68. }
  69. if newLen == len(s) {
  70. return s
  71. }
  72. b := make([]byte, 0, newLen)
  73. for i := 0; i < len(s); i++ {
  74. if s[i] >= utf8.RuneSelf {
  75. b = append(b, '%')
  76. b = strconv.AppendInt(b, int64(s[i]), 16)
  77. } else {
  78. b = append(b, s[i])
  79. }
  80. }
  81. return string(b)
  82. }
  83. // NoBody is an io.ReadCloser with no bytes. Read always returns EOF
  84. // and Close always returns nil. It can be used in an outgoing client
  85. // request to explicitly signal that a request has zero bytes.
  86. // An alternative, however, is to simply set Request.Body to nil.
  87. var NoBody = noBody{}
  88. type noBody struct{}
  89. func (noBody) Read([]byte) (int, error) { return 0, io.EOF }
  90. func (noBody) Close() error { return nil }
  91. func (noBody) WriteTo(io.Writer) (int64, error) { return 0, nil }
  92. var (
  93. // verify that an io.Copy from NoBody won't require a buffer:
  94. _ io.WriterTo = NoBody
  95. _ io.ReadCloser = NoBody
  96. )
  97. // PushOptions describes options for Pusher.Push.
  98. type PushOptions struct {
  99. // Method specifies the HTTP method for the promised request.
  100. // If set, it must be "GET" or "HEAD". Empty means "GET".
  101. Method string
  102. // Header specifies additional promised request headers. This cannot
  103. // include HTTP/2 pseudo header fields like ":path" and ":scheme",
  104. // which will be added automatically.
  105. Header Header
  106. }
  107. // Pusher is the interface implemented by ResponseWriters that support
  108. // HTTP/2 server push. For more background, see
  109. // https://tools.ietf.org/html/rfc7540#section-8.2.
  110. type Pusher interface {
  111. // Push initiates an HTTP/2 server push. This constructs a synthetic
  112. // request using the given target and options, serializes that request
  113. // into a PUSH_PROMISE frame, then dispatches that request using the
  114. // server's request handler. If opts is nil, default options are used.
  115. //
  116. // The target must either be an absolute path (like "/path") or an absolute
  117. // URL that contains a valid host and the same scheme as the parent request.
  118. // If the target is a path, it will inherit the scheme and host of the
  119. // parent request.
  120. //
  121. // The HTTP/2 spec disallows recursive pushes and cross-authority pushes.
  122. // Push may or may not detect these invalid pushes; however, invalid
  123. // pushes will be detected and canceled by conforming clients.
  124. //
  125. // Handlers that wish to push URL X should call Push before sending any
  126. // data that may trigger a request for URL X. This avoids a race where the
  127. // client issues requests for X before receiving the PUSH_PROMISE for X.
  128. //
  129. // Push will run in a separate goroutine making the order of arrival
  130. // non-deterministic. Any required synchronization needs to be implemented
  131. // by the caller.
  132. //
  133. // Push returns ErrNotSupported if the client has disabled push or if push
  134. // is not supported on the underlying connection.
  135. Push(target string, opts *PushOptions) error
  136. }