-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpassertedid.go
110 lines (101 loc) · 2.44 KB
/
passertedid.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2011, Shelby Ramsey. All rights reserved.
// Copyright 2018, Eugen Biegler. All rights reserved.
// Use of this code is governed by a BSD license that can be
// found in the LICENSE.txt file.
package sipparser
// Imports from the go standard library
import (
"errors"
"fmt"
)
// pAssertedIdStateFn is just a fn type
type pAssertedIdStateFn func(p *PAssertedId) pAssertedIdStateFn
// PAssertedId is a struct that holds:
// -- Error is just an os.Error
// -- Val is the raw value
// -- Name is the name value from the p-asserted-id hdr
// -- URI is the parsed uri from the p-asserted-id hdr
// -- Params is a slice of the *Params from the p-asserted-id hdr
type PAssertedId struct {
Error error
Val string
Name string
URI *URI
Params []*Param
nameInt int
}
// addParam adds the *Param to the Params field
func (p *PAssertedId) addParam(s string) {
np := getParam(s)
if p.Params == nil {
p.Params = []*Param{np}
return
}
p.Params = append(p.Params, np)
}
// parse actually parsed the .Val field of the PAssertedId struct
func (p *PAssertedId) parse() {
for state := parsePAssertedId; state != nil; {
state = state(p)
}
}
func parsePAssertedId(p *PAssertedId) pAssertedIdStateFn {
if p.Error != nil {
return nil
}
p.Name, p.nameInt = getName(p.Val)
return parsePAssertedIdGetUri
}
func parsePAssertedIdGetUri(p *PAssertedId) pAssertedIdStateFn {
left := 0
right := 0
for i := range p.Val {
if p.Val[i] == '<' && left == 0 {
left = i
}
if p.Val[i] == '>' && right == 0 {
right = i
}
}
if left < right {
p.URI = ParseURI(p.Val[left+1 : right])
if p.URI.Error != nil {
p.Error = fmt.Errorf("parsePAssertedIdGetUri err: received err getting uri: %v", p.URI.Error)
return nil
}
return parsePAssertedIdGetParams
}
p.Error = errors.New("parsePAssertedIdGetUri err: could not locate bracks in uri")
return nil
}
func parsePAssertedIdGetParams(p *PAssertedId) pAssertedIdStateFn {
var pos []int
right := 0
for i := range p.Val {
if p.Val[i] == '>' && right == 0 {
right = i
}
}
if len(p.Val) > right+1 {
pos = make([]int, 0)
for i := range p.Val[right+1:] {
if p.Val[right+1:][i] == ';' {
pos = append(pos, i)
}
}
}
if pos == nil {
return nil
}
for i := range pos {
if len(pos)-1 == i {
if len(p.Val[right+1:])-1 > pos[i]+1 {
p.addParam(p.Val[right+1:][pos[i]+1:])
}
}
if len(pos)-1 > i {
p.addParam(p.Val[right+1:][pos[i]+1 : pos[i+1]])
}
}
return nil
}