forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChallengeParser.go
80 lines (70 loc) · 1.99 KB
/
ChallengeParser.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
package parser
import (
"github.com/tutuvss/sip/core"
"github.com/tutuvss/sip/header"
)
/** SIPParser for the challenge portion of the authentication header.
*/
type ChallengeParser struct {
HeaderParser
}
/** Constructor
* @param String challenge message to parse to set
*/
func NewChallengeParser(challenge string) *ChallengeParser {
this := &ChallengeParser{}
this.HeaderParser.super(challenge)
return this
}
/** Constructor
* @param String challenge message to parse to set
*/
func NewChallengeParserFromLexer(lexer core.Lexer) *ChallengeParser {
this := &ChallengeParser{}
this.HeaderParser.superFromLexer(lexer)
return this
}
func (this *ChallengeParser) super(buffer string) {
this.HeaderParser.super(buffer)
}
func (this *ChallengeParser) superFromLexer(lexer core.Lexer) {
this.HeaderParser.superFromLexer(lexer)
}
/** Get the parameter of the challenge string
* @return NameValue containing the parameter
*/
func (this *ChallengeParser) ParseParameter(h header.AuthorizationHeader) (ParseException error) {
var nv *core.NameValue
if nv, ParseException = this.NameValue('='); ParseException != nil {
return ParseException
}
ParseException = h.SetParameter(nv.GetName(), nv.GetValue().(string))
return ParseException
}
/** parser the String message
* @return Challenge object
* @throws ParseException if the message does not respect the spec.
*/
func (this *ChallengeParser) Parse(h header.AuthorizationHeader) (ParseException error) {
var ch byte
lexer := this.GetLexer()
// the Scheme:
lexer.SPorHT()
lexer.Match(TokenTypes_ID)
t := lexer.GetNextToken()
lexer.SPorHT()
h.SetScheme(t.GetTokenValue())
// The parameters:
for ch, _ = lexer.LookAheadK(0); ch != '\n'; ch, _ = lexer.LookAheadK(0) {
if ParseException = this.ParseParameter(h); ParseException != nil {
return ParseException
}
lexer.SPorHT()
if ch, ParseException = lexer.LookAheadK(0); ch == '\n' || ParseException != nil { //||ch=='\0'
break
}
lexer.Match(',')
lexer.SPorHT()
}
return nil
}