package req

import (
	
	
	
	
	
	

	
	
	
)

// maxInt64 is the effective "infinite" value for the Server and
// Transport's byte-limiting readers.
const maxInt64 = 1<<63 - 1

// incomparable is a zero-width, non-comparable type. Adding it to a struct
// makes that struct also non-comparable, and generally doesn't add
// any size (as long as it's first).
type incomparable [0]func()

// bodyIsWritable reports whether the Body supports writing. The
// Transport returns Writable bodies for 101 Switching Protocols
// responses.
// The Transport uses this method to determine whether a persistent
// connection is done being managed from its perspective. Once we
// return a writable response body to a user, the net/http package is
// done managing that connection.
func bodyIsWritable( *http.Response) bool {
	,  := .Body.(io.Writer)
	return 
}

// isProtocolSwitch reports whether the response code and header
// indicate a successful protocol upgrade response.
func isProtocolSwitch( *http.Response) bool {
	return isProtocolSwitchResponse(.StatusCode, .Header)
}

// isProtocolSwitchResponse reports whether the response code and
// response header indicate a successful protocol upgrade response.
func isProtocolSwitchResponse( int,  http.Header) bool {
	return  == http.StatusSwitchingProtocols && isProtocolSwitchHeader()
}

// isProtocolSwitchHeader reports whether the request or response header
// is for a protocol switch.
func isProtocolSwitchHeader( http.Header) bool {
	return .Get("Upgrade") != "" &&
		httpguts.HeaderValuesContainsToken(["Connection"], "Upgrade")
}

// NoBody is an io.ReadCloser with no bytes. Read always returns EOF
// and Close always returns nil. It can be used in an outgoing client
// request to explicitly signal that a request has zero bytes.
// An alternative, however, is to simply set Request.Body to nil.
var NoBody = noBody{}

type noBody struct{}

func (noBody) ([]byte) (int, error)         { return 0, io.EOF }
func (noBody) () error                     { return nil }
func (noBody) (io.Writer) (int64, error) { return 0, nil }

var (
	// verify that an io.Copy from NoBody won't require a buffer:
	_ io.WriterTo   = NoBody
	_ io.ReadCloser = NoBody
)

type readResult struct {
	_   incomparable
	n   int
	err error
	b   byte // byte read, if n == 1
}

// hasToken reports whether token appears with v, ASCII
// case-insensitive, with space or comma boundaries.
// token must be all lowercase.
// v may contain mixed cased.
func hasToken(,  string) bool {
	if len() > len() ||  == "" {
		return false
	}
	if  ==  {
		return true
	}
	for  := 0;  <= len()-len(); ++ {
		// Check that first character is good.
		// The token is ASCII, so checking only a single byte
		// is sufficient. We skip this potential starting
		// position if both the first byte and its potential
		// ASCII uppercase equivalent (b|0x20) don't match.
		// False positives ('^' => '~') are caught by EqualFold.
		if  := [];  != [0] && |0x20 != [0] {
			continue
		}
		// Check that start pos is on a valid token boundary.
		if  > 0 && !isTokenBoundary([-1]) {
			continue
		}
		// Check that end pos is on a valid token boundary.
		if  :=  + len();  != len() && !isTokenBoundary([]) {
			continue
		}
		if ascii.EqualFold([:+len()], ) {
			return true
		}
	}
	return false
}

func isTokenBoundary( byte) bool {
	return  == ' ' ||  == ',' ||  == '\t'
}

func badStringError(,  string) error { return fmt.Errorf("%s %q", , ) }

// foreachHeaderElement splits v according to the "#rule" construction
// in RFC 7230 section 7 and calls fn for each non-empty element.
func foreachHeaderElement( string,  func(string)) {
	 = textproto.TrimString()
	if  == "" {
		return
	}
	if !strings.Contains(, ",") {
		()
		return
	}
	for ,  := range strings.Split(, ",") {
		if  = textproto.TrimString();  != "" {
			()
		}
	}
}

// maxPostHandlerReadBytes is the max number of Request.Body bytes not
// consumed by a handler that the server will read from the client
// in order to keep a connection alive. If there are more bytes than
// this then the server to be paranoid instead sends a "Connection:
// close" response.
//
// This number is approximately what a typical machine's TCP buffer
// size is anyway.  (if we have the bytes on the machine, we might as
// well read them)
const maxPostHandlerReadBytes = 256 << 10

func idnaASCII( string) (string, error) {
	// TODO: Consider removing this check after verifying performance is okay.
	// Right now punycode verification, length checks, context checks, and the
	// permissible character tests are all omitted. It also prevents the ToASCII
	// call from salvaging an invalid IDN, when possible. As a result it may be
	// possible to have two IDNs that appear identical to the user where the
	// ASCII-only version causes an error downstream whereas the non-ASCII
	// version does not.
	// Note that for correct ASCII IDNs ToASCII will only do considerably more
	// work, but it will not cause an allocation.
	if ascii.Is() {
		return , nil
	}
	return idna.Lookup.ToASCII()
}

// removeZone removes IPv6 zone identifier from host.
// E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
func removeZone( string) string {
	if !strings.HasPrefix(, "[") {
		return 
	}
	 := strings.LastIndex(, "]")
	if  < 0 {
		return 
	}
	 := strings.LastIndex([:], "%")
	if  < 0 {
		return 
	}
	return [:] + [:]
}

// stringContainsCTLByte reports whether s contains any ASCII control character.
func stringContainsCTLByte( string) bool {
	for  := 0;  < len(); ++ {
		 := []
		if  < ' ' ||  == 0x7f {
			return true
		}
	}
	return false
}

// See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt
// "To receive authorization, the client sends the userid and password,
// separated by a single colon (":") character, within a base64
// encoded string in the credentials."
// It is not meant to be urlencoded.
func basicAuth(,  string) string {
	 :=  + ":" + 
	return base64.StdEncoding.EncodeToString([]byte())
}