Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.http;
 
 import java.net.URI;
 
 
 
 /* ------------------------------------------------------------ */
Http URI. Parse a HTTP URI from a string or byte array. Given a URI http://user@host:port/path/info;param?query#fragment this class will split it into the following undecoded optional elements:
 
 public class HttpURI
 {
     private static final byte[] __empty={};
     private final static int
     START=0,
     AUTH_OR_PATH=1,
     SCHEME_OR_PATH=2,
     AUTH=4,
     IPV6=5,
     PORT=6,
     PATH=7,
     PARAM=8,
     QUERY=9,
     ASTERISK=10;
 
     boolean _partial=false;
     byte[] _raw=;
     String _rawString;
     int _scheme;
     int _authority;
     int _host;
     int _port;
     int _portValue;
     int _path;
     int _param;
     int _query;
     int _fragment;
     int _end;
     boolean _encoded=false;
 
     final Utf8StringBuilder _utf8b = new Utf8StringBuilder(64);
 
     public HttpURI()
     {
 
     }
 
     /* ------------------------------------------------------------ */
    

Parameters:
parsePartialAuth If True, parse auth without prior scheme, else treat all URIs starting with / as paths
 
     public HttpURI(boolean parsePartialAuth)
     {
         =parsePartialAuth;
     }
 
     public HttpURI(String raw)
     {
         =raw;
         byte[] b;
         try
         {
            b = raw.getBytes(.);
        }
        catch (UnsupportedEncodingException e)
        {
           throw new RuntimeException(e.getMessage());
        }
        parse(b,0,b.length);
    }
    public HttpURI(byte[] raw,int offsetint length)
    {
        parse2(raw,offset,length);
    }
    
    public HttpURI(URI uri)
    {
        parse(uri.toASCIIString());
    }
    public void parse(String raw)
    {
        byte[] b = raw.getBytes();
        parse2(b,0,b.length);
        =raw;
    }
    public void parse(byte[] raw,int offsetint length)
    {
        =null;
        parse2(raw,offset,length);
    }
    public void parseConnect(byte[] raw,int offsetint length)
    {
        =null;
        =false;
        =raw;
        int i=offset;
        int e=offset+length;
        int state=;
        =offset+length;
        =offset;
        =offset;
        =offset;
        =;
        =-1;
        =;
        =;
        =;
        =;
        loop: while (i<e)
        {
            char c=(char)(0xff&[i]);
            int s=i++;
            switch (state)
            {
                case :
                {
                    switch (c)
                    {
                        case ':':
                        {
                             = s;
                            break loop;
                        }
                        case '[':
                        {
                            state = ;
                            break;
                        }
                    }
                    continue;
                }
                case :
                {
                    switch (c)
                    {
                        case '/':
                        {
                            throw new IllegalArgumentException("No closing ']' for " + StringUtil.toString(,offset,length,.));
                        }
                        case ']':
                        {
                            state = ;
                            break;
                        }
                    }
                    continue;
                }
            }
        }
        if (<)
            =TypeUtil.parseInt(+1, --1,10);
        else
            throw new IllegalArgumentException("No port");
        =offset;
    }
    private void parse2(byte[] raw,int offsetint length)
    {
        =false;
        =raw;
        int i=offset;
        int e=offset+length;
        int state=;
        int m=offset;
        =offset+length;
        =offset;
        =offset;
        =offset;
        =offset;
        =-1;
        =offset;
        =;
        =;
        =;
        while (i<e)
        {
            char c=(char)(0xff&[i]);
            int s=i++;
            state: switch (state)
            {
                case :
                {
                    m=s;
                    switch(c)
                    {
                        case '/':
                            state=;
                            break;
                        case ';':
                            =s;
                            state=;
                            break;
                        case '?':
                            =s;
                            =s;
                            state=;
                            break;
                        case '#':
                            =s;
                            =s;
                            =s;
                            break;
                        case '*':
                            =s;
                            state=;
                            break;
                        default:
                            state=;
                    }
                    continue;
                }
                case :
                {
                    if ((||!=) && c=='/')
                    {
                        =i;
                        =;
                        =;
                        state=;
                    }
                    else if (c==';' || c=='?' || c=='#')
                    {
                        i--;
                        state=;
                    }
                    else
                    {
                        =m;
                        =m;
                        state=;
                    }
                    continue;
                }
                case :
                {
                    // short cut for http and https
                    if (length>6 && c=='t')
                    {
                        if ([offset+3]==':')
                        {
                            s=offset+3;
                            i=offset+4;
                            c=':';
                        }
                        else if ([offset+4]==':')
                        {
                            s=offset+4;
                            i=offset+5;
                            c=':';
                        }
                        else if ([offset+5]==':')
                        {
                            s=offset+5;
                            i=offset+6;
                            c=':';
                        }
                    }
                    switch (c)
                    {
                        case ':':
                        {
                            m = i++;
                             = m;
                             = m;
                            c = (char)(0xff & [i]);
                            if (c == '/')
                                state = ;
                            else
                            {
                                 = m;
                                 = m;
                                state = ;
                            }
                            break;
                        }
                        case '/':
                        {
                            state = ;
                            break;
                        }
                        case ';':
                        {
                             = s;
                            state = ;
                            break;
                        }
                        case '?':
                        {
                             = s;
                             = s;
                            state = ;
                            break;
                        }
                        case '#':
                        {
                             = s;
                             = s;
                             = s;
                            break;
                        }
                    }
                    continue;
                }
                case :
                {
                    switch (c)
                    {
                        case '/':
                        {
                            m = s;
                             = m;
                             = ;
                            state = ;
                            break;
                        }
                        case '@':
                        {
                             = i;
                            break;
                        }
                        case ':':
                        {
                             = s;
                            state = ;
                            break;
                        }
                        case '[':
                        {
                            state = ;
                            break;
                        }
                    }
                    continue;
                }
                case :
                {
                    switch (c)
                    {
                        case '/':
                        {
                            throw new IllegalArgumentException("No closing ']' for " + StringUtil.toString(,offset,length,.));
                        }
                        case ']':
                        {
                            state = ;
                            break;
                        }
                    }
                    continue;
                }
                case :
                {
                    if (c=='/')
                    {
                        m=s;
                        =m;
                        if (<=)
                            =;
                        state=;
                    }
                    continue;
                }
                case :
                {
                    switch (c)
                    {
                        case ';':
                        {
                             = s;
                            state = ;
                            break;
                        }
                        case '?':
                        {
                             = s;
                             = s;
                            state = ;
                            break;
                        }
                        case '#':
                        {
                             = s;
                             = s;
                             = s;
                            break state;
                        }
                        case '%':
                        {
                            =true;
                        }
                    }
                    continue;
                }
                case :
                {
                    switch (c)
                    {
                        case '?':
                        {
                             = s;
                            state = ;
                            break;
                        }
                        case '#':
                        {
                             = s;
                             = s;
                            break state;
                        }
                    }
                    continue;
                }
                case :
                {
                    if (c=='#')
                    {
                        =s;
                        break state;
                    }
                    continue;
                }
                case :
                {
                    throw new IllegalArgumentException("only '*'");
                }
            }
        }
        if (<)
            =TypeUtil.parseInt(+1, --1,10);
    }
    private String toUtf8String(int offset,int length)
    {
        .reset();
        .append(,offset,length);
        return .toString();
    }
    public String getScheme()
    {
        if (==)
            return null;
        int l=-;
        if (l==5 &&
            []=='h' &&
            [+1]=='t' &&
            [+2]=='t' &&
            [+3]=='p' )
            return .;
        if (l==6 &&
            []=='h' &&
            [+1]=='t' &&
            [+2]=='t' &&
            [+3]=='p' &&
            [+4]=='s' )
            return .;
        return toUtf8String(,--1);
    }
    public String getAuthority()
    {
        if (==)
            return null;
        return toUtf8String(,-);
    }
    public String getHost()
    {
        if (==)
            return null;
        return toUtf8String(,-);
    }
    public int getPort()
    {
        return ;
    }
    public String getPath()
    {
        if (==)
            return null;
        return toUtf8String(,-);
    }
    public String getDecodedPath()
    {
        if (==)
            return null;
        int length = -;
        boolean decoding=false;
        for (int i=;i<;i++)
        {
            byte b = [i];
            if (b=='%')
            {
                if (!decoding)
                {
                    .reset();
                    .append(,,i-);
                    decoding=true;
                }
                
                if ((i+2)>=)
                    throw new IllegalArgumentException("Bad % encoding: "+this);
                if ([i+1]=='u')
                {
                    if ((i+5)>=)
                        throw new IllegalArgumentException("Bad %u encoding: "+this);
                    try
                    {
                        String unicode = new String(Character.toChars(TypeUtil.parseInt(,i+2,4,16)));
                        .getStringBuilder().append(unicode);
                        i+=5;
                    }
                    catch(Exception e)
                    {
                        throw new RuntimeException(e);
                    }
                }
                else
                {
                    b=(byte)(0xff&TypeUtil.parseInt(,i+1,2,16));
                    .append(b);
                    i+=2;
                }
                continue;
            }
            else if (decoding)
            {
                .append(b);
            }
        }
        if (!decoding)
            return toUtf8String(,length);
        return .toString();
    }
    
    public String getDecodedPath(String encoding)
    {
        if (==)
            return null;
        int length = -;
        byte[] bytes=null;
        int n=0;
        for (int i=;i<;i++)
        {
            byte b = [i];
            if (b=='%')
            {
                if (bytes==null)
                {
                    bytes=new byte[length];
                    System.arraycopy(,,bytes,0,n);
                }
                
                if ((i+2)>=)
                    throw new IllegalArgumentException("Bad % encoding: "+this);
                if ([i+1]=='u')
                {
                    if ((i+5)>=)
                        throw new IllegalArgumentException("Bad %u encoding: "+this);
                    try
                    {
                        String unicode = new String(Character.toChars(TypeUtil.parseInt(,i+2,4,16)));
                        byte[] encoded = unicode.getBytes(encoding);
                        System.arraycopy(encoded,0,bytes,n,encoded.length);
                        n+=encoded.length;
                        i+=5;
                    }
                    catch(Exception e)
                    {
                        throw new RuntimeException(e);
                    }
                }
                else
                {
                    b=(byte)(0xff&TypeUtil.parseInt(,i+1,2,16));
                    bytes[n++]=b;
                    i+=2;
                }
                continue;
            }
            else if (bytes==null)
            {
                n++;
                continue;
            }
            bytes[n++]=b;
        }
        if (bytes==null)
            return StringUtil.toString(,,-,encoding);
        return StringUtil.toString(bytes,0,n,encoding);
    }
    
    
    
    
    
    public String getPathAndParam()
    {
        if (==)
            return null;
        return toUtf8String(,-);
    }
    public String getCompletePath()
    {
        if (==)
            return null;
        return toUtf8String(,-);
    }
    public String getParam()
    {
        if (==)
            return null;
        return toUtf8String(+1,--1);
    }
    public String getQuery()
    {
        if (==)
            return null;
        return toUtf8String(+1,--1);
    }
    public String getQuery(String encoding)
    {
        if (==)
            return null;
        return StringUtil.toString(,+1,--1,encoding);
    }
    public boolean hasQuery()
    {
        return (>);
    }
    public String getFragment()
    {
        if (==)
            return null;
        return toUtf8String(+1,--1);
    }
    public void decodeQueryTo(MultiMap parameters)
    {
        if (==)
            return;
        .reset();
        UrlEncoded.decodeUtf8To(,+1,--1,parameters,);
    }
    public void decodeQueryTo(MultiMap parametersString encoding)
        throws UnsupportedEncodingException
    {
        if (==)
            return;
        if (encoding==null || StringUtil.isUTF8(encoding))
            UrlEncoded.decodeUtf8To(,+1,--1,parameters);
        else
            UrlEncoded.decodeTo(StringUtil.toString(,+1,--1,encoding),parameters,encoding);
    }
    public void clear()
    {
        =;
        ="";
        =false;
    }
    @Override
    public String toString()
    {
        if (==null)
            =toUtf8String(,-);
        return ;
    }
    public void writeTo(Utf8StringBuilder buf)
    {
        buf.append(,,-);
    }
New to GrepCode? Check out our FAQ X