package http

import (
	
	
	
	
)

// ValidateEndpointHost validates that the host string passed in is a valid RFC
// 3986 host. Returns error if the host is not valid.
func ( string) error {
	var  strings.Builder
	var  string
	var  string
	var  error

	if strings.Contains(, ":") {
		, ,  = net.SplitHostPort()
		if  != nil {
			.WriteString(fmt.Sprintf("\n endpoint %v, failed to parse, got ", ))
			.WriteString(.Error())
		}

		if !ValidPortNumber() {
			.WriteString(fmt.Sprintf("port number should be in range [0-65535], got %v", ))
		}
	} else {
		 = 
	}

	 := strings.Split(, ".")
	for ,  := range  {
		if  == len()-1 && len() == 0 {
			// Allow trailing dot for FQDN hosts.
			continue
		}

		if !ValidHostLabel() {
			.WriteString("\nendpoint host domain labels must match \"[a-zA-Z0-9-]{1,63}\", but found: ")
			.WriteString()
		}
	}

	if len() == 0 && len() != 0 {
		.WriteString("\nendpoint host with port must not be empty")
	}

	if len() > 255 {
		.WriteString(fmt.Sprintf("\nendpoint host must be less than 255 characters, but was %d", len()))
	}

	if len(.String()) > 0 {
		return fmt.Errorf("invalid endpoint host%s", .String())
	}
	return nil
}

// ValidPortNumber returns whether the port is valid RFC 3986 port.
func ( string) bool {
	,  := strconv.Atoi()
	if  != nil {
		return false
	}

	if  < 0 ||  > 65535 {
		return false
	}
	return true
}

// ValidHostLabel returns whether the label is a valid RFC 3986 host abel.
func ( string) bool {
	if  := len();  == 0 ||  > 63 {
		return false
	}
	for ,  := range  {
		switch {
		case  >= '0' &&  <= '9':
		case  >= 'A' &&  <= 'Z':
		case  >= 'a' &&  <= 'z':
		case  == '-':
		default:
			return false
		}
	}

	return true
}