Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
 /*
  *
  * IPv6 Support added by Emil Ivov (emil_ivov@yahoo.com)<br/>
  * Network Research Team (http://www-r2.u-strasbg.fr))<br/>
  * Louis Pasteur University - Strasbourg - France<br/>
  *
  *Bug fixes for corner cases were contributed by Thomas Froment.
  */
 package gov.nist.core;
 
 
Parser for host names.

Author(s):
M. Ranganathan
Version:
1.2
 
 
 public class HostNameParser extends ParserCore {

    
Determines whether or not we should tolerate and strip address scope zones from IPv6 addresses. Address scope zones are sometimes returned at the end of IPv6 addresses generated by InetAddress.getHostAddress(). They are however not part of the SIP semantics so basically this method determines whether or not the parser should be stripping them (as opposed simply being blunt and throwing an exception).
 
     private boolean stripAddressScopeZones = false;
 
     public HostNameParser(String hname) {
         this. = new LexerCore("charLexer"hname);
 
         
             = Boolean.getBoolean("gov.nist.core.STRIP_ADDR_SCOPES");
     }

    
The lexer is initialized with the buffer.
 
     public HostNameParser(LexerCore lexer) {
         this. = lexer;
         lexer.selectLexer("charLexer");
 
         
             = Boolean.getBoolean("gov.nist.core.STRIP_ADDR_SCOPES");
     }
 
     private static final char[] VALID_DOMAIN_LABEL_CHAR =
         new char[] {.'-''.'};
     protected void consumeDomainLabel() throws ParseException {
         if ()
             dbg_enter("domainLabel");
         try {
         } finally {
             if ()
                 dbg_leave("domainLabel");
         }
     }
 
     protected String ipv6Reference() throws ParseException {
         StringBuffer retval = new StringBuffer();
         if ()
             dbg_enter("ipv6Reference");
 
         try {
 
             if(){
                 while (.hasMoreChars()) {
                    char la = .lookAhead(0);
                    //'%' is ipv6 address scope zone. see detail at
                    //java.sun.com/j2se/1.5.0/docs/api/java/net/Inet6Address.html
                    if (LexerCore.isHexDigit(la) || la == '.' || la == ':'
                            || la == '[' ) {
                        .consume(1);
                        retval.append(la);
                    } else if (la == ']') {
                        .consume(1);
                        retval.append(la);
                        return retval.toString();
                    } else if (la == '%'){
                        //we need to strip the address scope zone.
                        .consume(1);
                        String rest = .getRest();
                        if(rest == null || rest.length() == 0){
                            //head for the parse exception
                            break;
                        }
                        //we strip everything until either the end of the string
                        //or a closing square bracket (])
                        int stripLen = rest.indexOf(']');
                        if (stripLen == -1){
                            //no square bracket -> not a valid ipv6 reference
                            break;
                        }
                        .consume(stripLen+1);
                        retval.append("]");
                        return retval.toString();
                    } else
                        break;
                }
            }
            else
            {
                while (.hasMoreChars())
                {
                    char la = .lookAhead(0);
                    if (LexerCore.isHexDigit(la) || la == '.'
                            || la == ':' || la == '[') {
                        .consume(1);
                        retval.append(la);
                    } else if (la == ']') {
                        .consume(1);
                        retval.append(la);
                        return retval.toString();
                    } else
                    break;
                }
            }
            throw new ParseException(
                .getBuffer() + ": Illegal Host name ",
                .getPtr());
        } finally {
            if ()
                dbg_leave("ipv6Reference");
        }
    }
    public Host host() throws ParseException {
        if ()
            dbg_enter("host");
        try {
            String hostname;
            //IPv6 referene
            if (.lookAhead(0) == '[') {
                hostname = ipv6Reference();
            }
            //IPv6 address (i.e. missing square brackets)
            else ifisIPv6Address(.getRest()) )
            {
                int startPtr = .getPtr();
                .consumeValidChars(
                        new char[] {.':'});
                hostname
                    = new StringBuffer("[").append(
                        .getBuffer().substring(startPtr.getPtr()))
                        .append("]").toString();
            }
            //IPv4 address or hostname
            else {
                int startPtr = .getPtr();
                consumeDomainLabel();
                hostname = .getBuffer().substring(startPtr.getPtr());
            }
            if (hostname.length() == 0)
                throw new ParseException(
                    .getBuffer() + ": Missing host name",
                    .getPtr());
            else
                return new Host(hostname);
        } finally {
            if ()
                dbg_leave("host");
        }
    }

    
Tries to determine whether the address in uriHeader could be an IPv6 address by counting the number of colons that appear in it.

Parameters:
uriHeader the string (supposedly the value of a URI header) that we have received for parsing.
Returns:
true if the host part of uriHeader could be an IPv6 address (i.e. contains at least two colons) and false otherwise.
    private boolean isIPv6Address(String uriHeader)
    {    	
    	// Issue 275 https://jain-sip.dev.java.net/issues/show_bug.cgi?id=275 
    	// we check if the uriHeader includes a comma, if that's the case we are potentially parsing
    	// a multi header so strip everything after it
    	String hostName = uriHeader;
    	int indexOfComma = uriHeader.indexOf(",");
    	if(indexOfComma != -1) {
    		hostName  = uriHeader.substring(0, indexOfComma);
    	}
        // approximately detect the end the host part.
        //first check if we have an uri param
        int hostEnd = hostName.indexOf(.);
        //if not or if it appears after a semi-colon then the end of the
        //address would be a header param.
        int semiColonIndex = hostName.indexOf(.);
        if ( hostEnd == -1
            || (semiColonIndex!= -1 && hostEnd > semiColonIndex) )
            hostEnd = semiColonIndex;
        //if there was no header param either the address
        //continues until the end of the string
        if ( hostEnd == -1 )
            hostEnd = hostName.length();
        //extract the address
        String host = hostName.substring(0, hostEnd);
        int firstColonIndex = host.indexOf(.);
        if(firstColonIndex == -1)
            return false;
        int secondColonIndex = host.indexOf(.firstColonIndex + 1);
        if(secondColonIndex == -1)
            return false;
        return true;
    }
    
Parses a host:port string

Parameters:
allowWS - whether whitespace is allowed around ':', only true for Via headers
Returns:
Throws:
java.text.ParseException
    public HostPort hostPortboolean allowWS ) throws ParseException {
        if ()
            dbg_enter("hostPort");
        try {
            Host host = this.host();
            HostPort hp = new HostPort();
            hp.setHost(host);
            // Has a port?
            if (allowWS.SPorHT(); // white space before ":port" should be accepted
            if (.hasMoreChars()) {
                char la = .lookAhead(0);
                switch (la)
                {
                case ':':
                    .consume(1);
                    if (allowWS.SPorHT(); // white space before port number should be accepted
                    try {
                        String port = .number();
                        hp.setPort(Integer.parseInt(port));
                    } catch (NumberFormatException nfe) {
                        throw new ParseException(
                            .getBuffer() + " :Error parsing port ",
                            .getPtr());
                    }
                    break;
                case ',':	// allowed in case of multi-headers, e.g. Route
                			// Could check that current header is a multi hdr
                    
                case ';':   // OK, can appear in URIs (parameters)
                case '?':   // same, header parameters
                case '>':   // OK, can appear in headers
                case ' ':   // OK, allow whitespace
                case '\t':
                case '\r':
                case '\n':
                case '/':   // e.g. http://[::1]/xyz.html
                    break;
                case '%':
                    if(){
                        break;//OK,allow IPv6 address scope zone
                    }
                    
                default:
                    if (!allowWS) {
                        throw new ParseException.getBuffer() +
                                " Illegal character in hostname:" + .lookAhead(0),
                                .getPtr() );
                    }
                }
            }
            return hp;
        } finally {
            if ()
                dbg_leave("hostPort");
        }
    }
    public static void main(String args[]) throws ParseException {
        String hostNames[] =
            {
                "foo.bar.com:1234",
                "proxima.chaplin.bt.co.uk",
                "129.6.55.181:2345",
                ":1234",
                "foo.bar.com:         1234",
                "foo.bar.com     :      1234   ",
                "MIK_S:1234"
            };
        for (int i = 0; i < hostNames.lengthi++) {
            try {
                HostNameParser hnp = new HostNameParser(hostNames[i]);
                HostPort hp = hnp.hostPort(true);
                ..println("["+hp.encode()+"]");
            } catch (ParseException ex) {
                ..println("exception text = " + ex.getMessage());
            }
        }
    }
New to GrepCode? Check out our FAQ X