-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstartline.go
122 lines (111 loc) · 3.21 KB
/
startline.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
118
119
120
121
122
// 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 (
"errors"
"strings"
)
const ()
var (
SIP_METHODS = []string{
SIP_METHOD_INVITE,
SIP_METHOD_ACK,
SIP_METHOD_OPTIONS,
SIP_METHOD_BYE,
SIP_METHOD_CANCEL,
SIP_METHOD_REGISTER,
SIP_METHOD_INFO,
SIP_METHOD_PRACK,
SIP_METHOD_SUBSCRIBE,
SIP_METHOD_NOTIFY,
SIP_METHOD_UPDATE,
SIP_METHOD_MESSAGE,
SIP_METHOD_REFER,
SIP_METHOD_PUBLISH,
}
)
type parseStartLineStateFn func(s *StartLine) parseStartLineStateFn
type StartLine struct {
Error error "err"
Val string "val" // Val is the raw value
Type string "type" // Type is the type of startline (i.e. request or response)
Method string "method" // Method is the method (if request)
URI *URI "uri" // URI is the *URI (if request)
Resp string "resp" // Resp is the response code (i.e. 183)
RespText string "resptext" // RespText is the response text (i.e. "Session Progress")
Proto string "proto" // Proto is the protocol (should be "SIP")
Version string "version" // Version is the version (should be "2.0")
}
func (s *StartLine) run() {
for state := parseStartLine; state != nil; {
state = state(s)
}
}
func parseStartLine(s *StartLine) parseStartLineStateFn {
if s.Error != nil {
return nil
}
if len(s.Val) < 3 {
s.Error = errors.New("parseStartLine err: length of s.Val is less than 3. Invalid start line.")
return nil
}
if s.Val[0:3] == "SIP" {
s.Type = SIP_RESPONSE
return parseStartLineResponse
}
s.Type = SIP_REQUEST
return parseStartLineRequest
}
func parseStartLineResponse(s *StartLine) parseStartLineStateFn {
parts := strings.SplitN(s.Val, " ", 3)
if len(parts) != 3 {
s.Error = errors.New("parseStartLineRespone err: err getting parts from LWS.")
return nil
}
charPos := strings.IndexRune(parts[0], '/')
if charPos == -1 {
s.Error = errors.New("parseStartLineRespone err: err getting proto char.")
return nil
}
s.Proto = parts[0][0:charPos]
if len(parts[0])-1 < charPos+1 {
s.Error = errors.New("parseStartLineResponse err: proto char appears to be at end of proto.")
return nil
}
s.Version = parts[0][charPos+1:]
s.Resp = parts[1]
s.RespText = parts[2]
return nil
}
func parseStartLineRequest(s *StartLine) parseStartLineStateFn {
parts := strings.SplitN(s.Val, " ", 3)
if len(parts) != 3 {
s.Error = errors.New("parseStartLineRequest err: request line did split on LWS correctly.")
return nil
}
s.Method = parts[0]
s.URI = ParseURI(parts[1])
if s.URI.Error != nil {
s.Error = errors.New("parseStartLineRequest err: err in URI: " + s.URI.Error.Error())
return nil
}
charPos := strings.IndexRune(parts[2], '/')
if charPos == -1 {
s.Error = errors.New("parseStartLineRequest err: could not get \"/\" pos in parts[2].")
return nil
}
if len(parts[2])-1 < charPos+1 {
s.Error = errors.New("parseStartLineRequest err: \"/\" char appears to be at end of line.")
return nil
}
s.Proto = parts[2][0:charPos]
s.Version = parts[2][charPos+1:]
return nil
}
func ParseStartLine(str string) *StartLine {
s := &StartLine{Val: str}
s.run()
return s
}