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