Exemplo n.º 1
0
func (this *SIPParser) SipVersion() (s string, ParseException error) {
	if core.Debug.ParserDebug {
		this.Dbg_enter("sipVersion")
		defer this.Dbg_leave("sipVersion")
	}

	var tok *core.Token
	if tok, ParseException = this.GetLexer().Match(TokenTypes_SIP); ParseException != nil {
		return "", this.CreateParseException("Expecting SIP")
	}
	if tok.GetTokenValue() != "SIP" {
		return "", this.CreateParseException("Expecting SIP")
	}
	this.GetLexer().Match('/')
	if tok, ParseException = this.GetLexer().Match(TokenTypes_ID); ParseException != nil {
		return "", this.CreateParseException("Expecting SIP/2.0")
	}
	if tok.GetTokenValue() != "2.0" {
		return "", this.CreateParseException("Expecting SIP/2.0")
	}

	return "SIP/2.0", nil
}
Exemplo n.º 2
0
/**  a parser for the essential part of the via header.
 */
func (this *ViaParser) ParseVia(v *header.Via) (ParseException error) {
	lexer := this.GetLexer()

	var protocolName, protocolVersion *core.Token
	// The protocol
	lexer.Match(TokenTypes_ID)

	if protocolName = lexer.GetNextToken(); protocolName.GetTokenValue() != "SIP" {
		return this.CreateParseException("Protcoal Not Supported error")
	}

	lexer.SPorHT()
	// consume the "/"
	lexer.Match('/')
	lexer.SPorHT()
	lexer.Match(TokenTypes_ID)
	lexer.SPorHT()
	if protocolVersion = lexer.GetNextToken(); protocolVersion.GetTokenValue() != "2.0" {
		return this.CreateParseException("Version Not Supported error")
	}

	lexer.SPorHT()

	// We consume the "/"
	lexer.Match('/')
	lexer.SPorHT()
	lexer.Match(TokenTypes_ID)
	lexer.SPorHT()

	transport := lexer.GetNextToken()
	lexer.SPorHT()

	protocol := header.NewProtocol()
	protocol.SetProtocolName(protocolName.GetTokenValue())
	protocol.SetProtocolVersion(protocolVersion.GetTokenValue())
	protocol.SetTransport(transport.GetTokenValue())
	v.SetSentProtocol(protocol)

	// sent-By
	hnp := core.NewHostNameParserFromLexer(this.GetLexer())

	var hostPort *core.HostPort
	if hostPort, ParseException = hnp.GetHostPort(); ParseException != nil {
		return ParseException
	}
	v.SetSentBy(hostPort)

	// Ignore blanks
	lexer.SPorHT()

	var la byte
	for la, _ = lexer.LookAheadK(0); la == ';'; la, _ = lexer.LookAheadK(0) {
		lexer.Match(';')
		lexer.SPorHT()

		var nameValue *core.NameValue
		if nameValue, ParseException = this.NameValue(); ParseException != nil {
			return ParseException
		}

		name := nameValue.GetName()
		nameValue.SetName(strings.ToLower(name))
		v.SetParameterFromNameValue(nameValue)
		lexer.SPorHT()
	}

	if la, _ = lexer.LookAheadK(0); la == '(' {
		lexer.SelectLexer("charLexer")
		lexer.ConsumeK(1)

		var comment bytes.Buffer
		for {
			ch, _ := lexer.LookAheadK(0)
			if ch == ')' {
				lexer.ConsumeK(1)
				break
			} else if ch == '\\' {
				// Escaped character
				tok := lexer.GetNextToken()
				comment.WriteString(tok.GetTokenValue())
				lexer.ConsumeK(1)
				tok = lexer.GetNextToken()
				comment.WriteString(tok.GetTokenValue())
				lexer.ConsumeK(1)
			} else if ch == '\n' {
				break
			} else {
				comment.WriteByte(ch)
				lexer.ConsumeK(1)
			}
		}
		v.SetComment(comment.String())
	}

	return nil

}