-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfrom.go
117 lines (107 loc) · 2.46 KB
/
from.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
111
112
113
114
115
116
117
// 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 (
"fmt"
)
type parseFromStateFn func(f *From) parseFromStateFn
// From holds a parsed header that has a format like:
// "NAME" <sip:user@hostinfo>;param=val
// and is used for the parsing of the from, to, and
// contact header in the parser program
// From holds the following public fields:
// -- Error is an error
// -- Val is the raw value
// -- Name is the name value
// -- Tag is the tag value
// -- URI is a parsed uri
// -- Params are for any generic params that are part of
// the header
type From struct {
Error error
Val string
Name string
Tag string
URI *URI
//Params []*Param
endName int
rightBrack int
leftBrack int
brackChk bool
}
func (f *From) parse() {
for state := parseFromState; state != nil; {
state = state(f)
}
}
/*
func (f *From) addParam(s string) {
p := getParam(s)
switch {
case p.Param == "tag":
f.Tag = p.Val
default:
if f.Params == nil {
f.Params = make([]*Param, 0)
}
f.Params = append(f.Params, p)
}
}
*/
func parseFromState(f *From) parseFromStateFn {
if f.Error != nil {
return nil
}
f.Name, f.endName = getName(f.Val)
return parseFromGetURI
}
func parseFromGetURI(f *From) parseFromStateFn {
f.leftBrack, f.rightBrack, f.brackChk = getBracks(f.Val)
if f.brackChk == false {
f.URI = ParseURI(f.Val)
if f.URI.Error != nil {
f.Error = fmt.Errorf("parseFromGetURI err: rcvd err parsing uri: %v", f.URI.Error)
return nil
}
/*
if f.URI.UriParams != nil {
for i := range f.URI.UriParams {
if f.URI.UriParams[i].Param == "tag" {
f.Tag = f.URI.UriParams[i].Val
}
}
}
*/
return nil
}
if f.brackChk == true {
f.URI = ParseURI(f.Val[f.leftBrack+1 : f.rightBrack])
if f.URI.Error != nil {
f.Error = fmt.Errorf("parseFromGetURI err: rcvd err parsing uri: %v", f.URI.Error)
return nil
}
return parseFromGetParams
}
return nil
}
func parseFromGetParams(f *From) parseFromStateFn {
/*
if f.brackChk == true && len(f.Val) > f.rightBrack+1 {
pms := strings.Split(f.Val[f.rightBrack+1:], ";")
for i := range pms {
f.addParam(pms[i])
}
return nil
}
*/
return nil
}
func getFrom(s string) *From {
f := &From{Val: s}
f.Tag = extractParam("tag=", s)
f.parse()
return f
}