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