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