forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HeaderParser.go
78 lines (64 loc) · 1.87 KB
/
HeaderParser.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
package parser
import (
"sip/core"
"sip/header"
"strings"
)
type Parser interface {
Parse() (sh header.Header, ParseException error)
}
/** Generic header parser class. The parsers for various headers extend this
* class. To create a parser for a new header, extend this class and change
* the createParser class.
*/
type HeaderParser struct {
SIPParser
}
/** Creates new HeaderParser
* @param String to parse.
*/
func NewHeaderParser(header string) *HeaderParser {
this := &HeaderParser{}
this.SIPParser.super(header)
this.SIPParser.GetLexer().SetLexerName("command_keywordLexer")
return this
}
func NewHeaderParserFromLexer(lexer core.Lexer) *HeaderParser {
this := &HeaderParser{}
this.SIPParser.SetLexer(lexer)
this.SIPParser.GetLexer().SetLexerName("command_keywordLexer")
return this
}
func (this *HeaderParser) super(header string) {
this.SIPParser.super(header)
this.SIPParser.GetLexer().SetLexerName("command_keywordLexer")
}
func (this *HeaderParser) superFromLexer(lexer core.Lexer) {
this.SetLexer(lexer)
this.SIPParser.GetLexer().SetLexerName("command_keywordLexer")
}
/** Parse the SIP header from the buffer and return a parsed
* structure.
*@throws ParseException if there was an error parsing.
*/
func (this *HeaderParser) Parse() (sh header.Header, ParseException error) {
lexer := this.GetLexer()
var name string
if name, ParseException = lexer.GetNextTokenByDelim(':'); ParseException != nil {
return nil, ParseException
}
lexer.ConsumeK(1)
body := strings.TrimSpace(lexer.GetLine())
// we dont set any fields because the header is ok
retval := header.NewExtension(name)
retval.SetValue(body)
return retval, nil
}
/** Parse the header name until the colon and chew WS after that.
*/
func (this *HeaderParser) HeaderName(tok int) {
this.GetLexer().Match(tok)
this.GetLexer().SPorHT()
this.GetLexer().Match(':')
this.GetLexer().SPorHT()
}