package altsvcutil

import (
	
	
	
	
	
	
	
	
	
	
)

type altAvcParser struct {
	*bytes.Buffer
}

// validOptionalPort reports whether port is either an empty string
// or matches /^:\d*$/
func validOptionalPort( string) bool {
	if  == "" {
		return true
	}
	if [0] != ':' {
		return false
	}
	for ,  := range [1:] {
		if  < '0' ||  > '9' {
			return false
		}
	}
	return true
}

// splitHostPort separates host and port. If the port is not valid, it returns
// the entire input as host, and it doesn't check the validity of the host.
// Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
func splitHostPort( string) (,  string) {
	 = 

	 := strings.LastIndexByte(, ':')
	if  != -1 && validOptionalPort([:]) {
		,  = [:], [+1:]
	}

	if strings.HasPrefix(, "[") && strings.HasSuffix(, "]") {
		 = [1 : len()-1]
	}
	return
}

// ParseHeader parses the AltSvc from header value.
func ( string) ([]*altsvc.AltSvc, error) {
	 := newAltSvcParser()
	return .Parse()
}

func newAltSvcParser( string) *altAvcParser {
	 := bytes.NewBufferString()
	return &altAvcParser{}
}

var endOfTime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)

func ( *altAvcParser) () ( []*altsvc.AltSvc,  error) {
	for {
		,  := .parseOne()
		if  != nil {
			 = append(, )
		}
		if  != nil {
			if  == io.EOF {
				return
			} else {
				 = 
				return
			}
		}
	}
}

func ( *altAvcParser) () (,  string,  bool,  error) {
	,  := .ReadBytes('=')
	if len() == 0 {
		return
	}
	 = strings.TrimSpace(string([:len()-1]))
	 := .Bytes()
	if len() == 0 {
		 = io.EOF
		return
	}
	if [0] == '"' {
		 := 0
		for  := 1;  < len(); ++ {
			if [] == '"' {
				 = 
				break
			}
		}
		if  == 0 {
			 = fmt.Errorf("quote in alt-svc is not complete: %s", )
			return
		}
		 = string([1:])
		.Next( + 1)
		if len() == +1 {
			 = io.EOF
			return
		}
		var  byte
		,  = .ReadByte()
		if  != nil {
			return
		}
		if  == ';' {
			 = true
		}
	} else {
		 := 0
	:
		for ,  := range  {
			switch  {
			case ',':
				 = 
				break 
			case ';':
				 = 
				 = true
				break 
			}
		}
		if  == 0 {
			 = io.EOF
			 = strings.TrimSpace(string())
			return
		}
		.Next( + 1)
		 = string([:])
	}
	return
}

func ( *altAvcParser) () ( *altsvc.AltSvc,  error) {
	, , ,  := .parseKv()
	if  == "" ||  == "" {
		return
	}
	,  := splitHostPort()

	 = &altsvc.AltSvc{
		Protocol: ,
		Host:     ,
		Port:     ,
		Expire:   endOfTime,
	}

	if ! {
		return
	}

	, , ,  := .parseKv()
	if  == "" ||  == "" {
		return
	}
	if  != "ma" {
		 = fmt.Errorf("expect ma field, got %s", )
		return
	}

	,  := strconv.ParseInt(, 10, 64)
	if  != nil {
		return
	}
	.Expire = time.Now().Add(time.Duration() * time.Second)

	if ! {
		return
	}

	// drain useless fields
	for {
		_, _, ,  = .parseKv()
		if  {
			continue
		} else {
			break
		}
	}
	return
}

// ConvertURL converts the raw request url to expected alt-svc's url.
func ( *altsvc.AltSvc,  *url.URL) *url.URL {
	,  := netutil.AuthorityHostPort(.Scheme, .Host)
	 := *
	 := false
	if .Host != "" && .Host !=  {
		 = .Host
		 = true
	}
	if .Port != "" && .Port !=  {
		 = .Port
		 = true
	}
	if  {
		.Host = net.JoinHostPort(, )
	}
	return &
}