Example #1
0
func (this *URLParser) SipURL() (sipurl *address.SipURIImpl, ParseException error) {
	retval := address.NewSipURIImpl()

	this.GetLexer().Match(TokenTypes_SIP)
	this.GetLexer().Match(':')
	retval.SetScheme(core.SIPTransportNames_SIP)

	buffer := this.GetLexer().GetRest()
	if n := strings.Index(buffer, "@"); n == -1 {
		// hostPort
		hnp := core.NewHostNameParserFromLexer(this.GetLexer())
		hp, _ := hnp.GetHostPort()
		retval.SetHostPort(hp)
	} else {
		var hp *core.HostPort
		if n = strings.Index(buffer, ":"); n == -1 {
			// name@hostPort
			user, _ := this.User()
			this.GetLexer().Match('@')
			hnp := core.NewHostNameParserFromLexer(this.GetLexer())
			if hp, ParseException = hnp.GetHostPort(); ParseException != nil {
				return nil, ParseException
			}
			retval.SetUser(user)
			retval.SetHostPort(hp)
		} else {
			user, _ := this.User()
			// name:password@hostPort
			this.GetLexer().Match(':')
			password, _ := this.Password()
			this.GetLexer().Match('@')
			hnp := core.NewHostNameParserFromLexer(this.GetLexer())
			if hp, ParseException = hnp.GetHostPort(); ParseException != nil {
				return nil, ParseException
			}
			retval.SetUser(user)
			retval.SetUserPassword(password)
			retval.SetHostPort(hp)
		}
	}
	this.GetLexer().SelectLexer("charLexer")
	for this.GetLexer().HasMoreChars() {
		if la, _ := this.GetLexer().LookAheadK(0); la != ';' {
			break
		}
		this.GetLexer().ConsumeK(1)
		var parms *core.NameValue
		if parms, ParseException = this.UriParam(); ParseException != nil {
			return nil, ParseException
		}
		retval.SetUriParameter(parms)
	}

	if la, _ := this.GetLexer().LookAheadK(0); this.GetLexer().HasMoreChars() && la == '?' {
		this.GetLexer().ConsumeK(1)
		for this.GetLexer().HasMoreChars() {
			parms, _ := this.Qheader()
			retval.SetQHeader(parms)
			if la, _ = this.GetLexer().LookAheadK(0); this.GetLexer().HasMoreChars() && la != '&' {
				break
			} else {
				this.GetLexer().ConsumeK(1)
			}
		}
	}
	return retval, nil
}
Example #2
0
/**
 * Parse a host name and return a parsed structure.
 * @param host is a String containing the host name to be parsed
 * @return a parsed address structure.
 * @exception throws a ParseException when the hostname is badly formatted.
 */
func (this *StringMsgParser) ParseHost(host string) (*core.Host, error) {
	lexer := NewSIPLexer("charLexer", host)
	return core.NewHostNameParserFromLexer(lexer).GetHost()
}
Example #3
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

}