1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 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
20 package org.eclipse.jetty.http;
21
22 import org.eclipse.jetty.util.StringUtil;
23
24
25
26 /* ------------------------------------------------------------ */
27 /**
28 */
29 public class HostPortHttpField extends HttpField
30 {
31 private final String _host;
32 private final int _port;
33
34 public HostPortHttpField(String authority)
35 {
36 this(HttpHeader.HOST,HttpHeader.HOST.asString(),authority);
37 }
38
39 public HostPortHttpField(HttpHeader header, String name, String authority)
40 {
41 super(header,name,authority);
42 if (authority==null || authority.length()==0)
43 throw new IllegalArgumentException("No Authority");
44 try
45 {
46 if (authority.charAt(0)=='[')
47 {
48 // ipv6reference
49 int close=authority.lastIndexOf(']');
50 if (close<0)
51 throw new BadMessageException(HttpStatus.BAD_REQUEST_400,"Bad ipv6");
52 _host=authority.substring(0,close+1);
53
54 if (authority.length()>close+1)
55 {
56 if (authority.charAt(close+1)!=':')
57 throw new BadMessageException(HttpStatus.BAD_REQUEST_400,"Bad ipv6 port");
58 _port=StringUtil.toInt(authority,close+2);
59 }
60 else
61 _port=0;
62 }
63 else
64 {
65 // ipv4address or hostname
66 int c = authority.lastIndexOf(':');
67 if (c>=0)
68 {
69 _host=authority.substring(0,c);
70 _port=StringUtil.toInt(authority,c+1);
71 }
72 else
73 {
74 _host=authority;
75 _port=0;
76 }
77 }
78 }
79 catch (BadMessageException bm)
80 {
81 throw bm;
82 }
83 catch(Exception e)
84 {
85 throw new BadMessageException(HttpStatus.BAD_REQUEST_400,"Bad HostPort",e);
86 }
87 }
88
89 /* ------------------------------------------------------------ */
90 /** Get the host.
91 * @return the host
92 */
93 public String getHost()
94 {
95 return _host;
96 }
97
98 /* ------------------------------------------------------------ */
99 /** Get the port.
100 * @return the port
101 */
102 public int getPort()
103 {
104 return _port;
105 }
106 }