View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  /**
22   * Parse an authority string into Host and Port
23   * <p>Parse a string in the form "host:port", handling IPv4 an IPv6 hosts</p>
24   *
25   */
26  public class HostPort
27  {
28      private final String _host;
29      private final int _port;
30  
31      public HostPort(String authority) throws IllegalArgumentException
32      {
33          if (authority==null || authority.length()==0)
34              throw new IllegalArgumentException("No Authority");
35          try
36          {
37              if (authority.charAt(0)=='[')
38              {
39                  // ipv6reference
40                  int close=authority.lastIndexOf(']');
41                  if (close<0)
42                      throw new IllegalArgumentException("Bad IPv6 host");
43                  _host=authority.substring(0,close+1);
44  
45                  if (authority.length()>close+1)
46                  {
47                      if (authority.charAt(close+1)!=':')
48                          throw new IllegalArgumentException("Bad IPv6 port");
49                      _port=StringUtil.toInt(authority,close+2);
50                  }
51                  else
52                      _port=0;
53              }
54              else
55              {
56                  // ipv4address or hostname
57                  int c = authority.lastIndexOf(':');
58                  if (c>=0)
59                  {
60                      _host=authority.substring(0,c);
61                      _port=StringUtil.toInt(authority,c+1);
62                  }
63                  else
64                  {
65                      _host=authority;
66                      _port=0;
67                  }
68              }
69          }
70          catch (IllegalArgumentException iae)
71          {
72              throw iae;
73          }
74          catch(final Exception ex)
75          {
76              throw new IllegalArgumentException("Bad HostPort")
77              {
78                  {initCause(ex);}
79              };
80          }
81          if(_host.isEmpty())
82              throw new IllegalArgumentException("Bad host");
83          if(_port<0)
84              throw new IllegalArgumentException("Bad port");
85      }
86  
87      /* ------------------------------------------------------------ */
88      /** Get the host.
89       * @return the host
90       */
91      public String getHost()
92      {
93          return _host;
94      }
95  
96      /* ------------------------------------------------------------ */
97      /** Get the port.
98       * @return the port
99       */
100     public int getPort()
101     {
102         return _port;
103     }
104     
105     /* ------------------------------------------------------------ */
106     /** Get the port.
107      * @param defaultPort, the default port to return if a port is not specified
108      * @return the port
109      */
110     public int getPort(int defaultPort)
111     {
112         return _port>0?_port:defaultPort;
113     }
114 
115     /* ------------------------------------------------------------ */
116     /** Normalize IPv6 address as per https://www.ietf.org/rfc/rfc2732.txt
117      * @param host A host name
118      * @return Host name surrounded by '[' and ']' as needed.
119      */
120     public static String normalizeHost(String host)
121     {
122         // if it is normalized IPv6 or could not be IPv6, return
123         if (host.isEmpty() || host.charAt(0)=='[' || host.indexOf(':')<0)
124             return host;
125         
126         // normalize with [ ]
127         return "["+host+"]";
128     }
129 }