-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreason.go
69 lines (64 loc) · 1.48 KB
/
reason.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
// Copyright 2011, Shelby Ramsey. 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 (
"strings"
)
// Reason is a struct that holds a parsed reason hdr
// Fields are as follows:
// -- Val is the raw value
// -- Proto is the protocol (i.e. SIP)
// -- Cause is the cause code (i.e. 41)
// -- Text is the actual text response
type Reason struct {
Val string
Proto string
Cause string
Text string
}
// addParam is a method for the Reason type that looks at the
// parameter passed into it
func (r *Reason) addParam(s string) {
np := getParam(s)
if np.Param == "cause" {
r.Cause = np.Val
}
if np.Param == "text" {
r.Text = np.Val
}
}
// parse is the method that actual parses the .Val of the Reason type
func (r *Reason) parse() {
pos := make([]int, 0)
for i := range r.Val {
if r.Val[i] == ';' {
pos = append(pos, i)
}
}
if len(pos) == 0 {
return
}
if len(pos) == 1 {
r.Proto = strings.TrimSpace(r.Val[0:pos[0]])
if len(r.Val)-1 > pos[0] {
r.addParam(strings.Replace(r.Val[pos[0]+1:], "\"", "", -1))
}
return
}
if len(pos) > 1 {
r.Proto = strings.TrimSpace(r.Val[0:pos[0]])
for i := range pos {
if len(pos)-1 == i {
if len(r.Val)-1 > pos[i] {
r.addParam(strings.Replace(r.Val[pos[i]+1:], "\"", "", -1))
}
return
}
r.addParam(strings.Replace(r.Val[pos[i]+1:pos[i+1]], "\"", "", -1))
}
return
}
return
}