github.com/searKing/golang/go@v1.2.74/net/url/proto.go (about) 1 // Copyright 2020 The searKing Author. 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 url 6 7 import ( 8 "errors" 9 "fmt" 10 "strings" 11 ) 12 13 type Proto struct { 14 Type string // "RTSP" 15 Major int // 1 16 Minor int // 0 17 } 18 19 func ParseProto(rawproto string) (*Proto, error) { 20 if rawproto == "" { 21 return nil, errors.New("empty url_") 22 } 23 proto := new(Proto) 24 var protoType string 25 var protoMajor, protoMinor int 26 27 protoType, protoVersion := split(rawproto, "/", true) 28 if protoType == "" { 29 return nil, errors.New("invalid PROTO for request : empty protoType") 30 } 31 n, err := fmt.Sscanf(protoVersion, "%d.%d", &protoMajor, &protoMinor) 32 if n == 2 && err == nil { 33 proto.Type = protoType 34 proto.Major = protoMajor 35 proto.Minor = protoMinor 36 return proto, nil 37 } 38 return nil, errors.New("invalid PROTO for request") 39 } 40 41 // Maybe s is of the form t c u. 42 // If so, return t, c u (or t, u if cutc == true). 43 // If not, return s, "". 44 func split(s string, c string, cutc bool) (string, string) { 45 i := strings.Index(s, c) 46 if i < 0 { 47 return s, "" 48 } 49 if cutc { 50 return s[:i], s[i+len(c):] 51 } 52 return s[:i], s[i:] 53 } 54 func (p *Proto) String() string { 55 return fmt.Sprintf("%s/%d.%d", p.Type, p.Major, p.Minor) 56 } 57 func (p *Proto) ProtoAtLeast(major, minor int) bool { 58 return p.Major > major || 59 p.Major == major && p.Minor >= minor 60 }