forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerParser.go
70 lines (62 loc) · 1.61 KB
/
ServerParser.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
package parser
import (
"errors"
"github.com/tutuvss/sip/core"
"github.com/tutuvss/sip/header"
"strings"
)
/** SIPParser for Server header.
*/
type ServerParser struct {
HeaderParser
}
/** Creates a new instance of ServerParser
* @param server the header to parse
*/
func NewServerParser(server string) *ServerParser {
this := &ServerParser{}
this.HeaderParser.super(server)
return this
}
/** Constructor
* @param lexer the lexer to use to parse the header
*/
func NewServerParserFromLexer(lexer core.Lexer) *ServerParser {
this := &ServerParser{}
this.HeaderParser.superFromLexer(lexer)
return this
}
/** parse the String server
* @return SIPHeader (Server object)
* @throws SIPParseException if the message does not respect the spec.
*/
func (this *ServerParser) Parse() (sh header.Header, ParseException error) {
server := header.NewServer()
var ch byte
lexer := this.GetLexer()
this.HeaderName(TokenTypes_SERVER)
if ch, _ = lexer.LookAheadK(0); ch == '\n' {
return nil, errors.New("empty header")
}
// mandatory token: product[/product-version] | (comment)
for ch, ParseException = lexer.LookAheadK(0); ch != '\n' && ParseException == nil; ch, ParseException = lexer.LookAheadK(0) {
if ch == '(' {
comment, _ := lexer.Comment()
server.AddProductToken("(" + comment + ")")
} else {
var tok string
tok, ParseException = lexer.GetString('/')
if ParseException != nil {
tok = lexer.GetRest()
server.AddProductToken(tok)
break
} else {
if tok[len(tok)-1] == '\n' {
tok = strings.TrimSpace(tok)
}
server.AddProductToken(tok)
}
}
}
return server, nil
}