1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
91
92
93 public String getHost()
94 {
95 return _host;
96 }
97
98
99
100
101
102 public int getPort()
103 {
104 return _port;
105 }
106 }