-
Notifications
You must be signed in to change notification settings - Fork 12
/
proto.go
60 lines (54 loc) · 1.38 KB
/
proto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package url
import (
"errors"
"fmt"
"strings"
)
type Proto struct {
Type string // "RTSP"
Major int // 1
Minor int // 0
}
func ParseProto(rawproto string) (*Proto, error) {
if rawproto == "" {
return nil, errors.New("empty url_")
}
proto := new(Proto)
var protoType string
var protoMajor, protoMinor int
protoType, protoVersion := split(rawproto, "/", true)
if protoType == "" {
return nil, errors.New("invalid PROTO for request : empty protoType")
}
n, err := fmt.Sscanf(protoVersion, "%d.%d", &protoMajor, &protoMinor)
if n == 2 && err == nil {
proto.Type = protoType
proto.Major = protoMajor
proto.Minor = protoMinor
return proto, nil
}
return nil, errors.New("invalid PROTO for request")
}
// Maybe s is of the form t c u.
// If so, return t, c u (or t, u if cutc == true).
// If not, return s, "".
func split(s string, c string, cutc bool) (string, string) {
i := strings.Index(s, c)
if i < 0 {
return s, ""
}
if cutc {
return s[:i], s[i+len(c):]
}
return s[:i], s[i:]
}
func (p *Proto) String() string {
return fmt.Sprintf("%s/%d.%d", p.Type, p.Major, p.Minor)
}
func (p *Proto) ProtoAtLeast(major, minor int) bool {
return p.Major > major ||
p.Major == major && p.Minor >= minor
}