1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21
22
23
24
25
26
27 public class HttpVersion implements Comparable<HttpVersion> {
28
29 private static final Pattern VERSION_PATTERN =
30 Pattern.compile("(\\S+)/(\\d+)\\.(\\d+)");
31
32
33
34
35 public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0, false);
36
37
38
39
40 public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1, true);
41
42
43
44
45
46
47
48
49
50 public static HttpVersion valueOf(String text) {
51 if (text == null) {
52 throw new NullPointerException("text");
53 }
54
55 text = text.trim().toUpperCase();
56 if ("HTTP/1.1".equals(text)) {
57 return HTTP_1_1;
58 }
59 if ("HTTP/1.0".equals(text)) {
60 return HTTP_1_0;
61 }
62 return new HttpVersion(text, true);
63 }
64
65 private final String protocolName;
66 private final int majorVersion;
67 private final int minorVersion;
68 private final String text;
69 private final boolean keepAliveDefault;
70
71
72
73
74
75
76
77
78
79
80
81
82 public HttpVersion(String text, boolean keepAliveDefault) {
83 if (text == null) {
84 throw new NullPointerException("text");
85 }
86
87 text = text.trim().toUpperCase();
88 if (text.length() == 0) {
89 throw new IllegalArgumentException("empty text");
90 }
91
92 Matcher m = VERSION_PATTERN.matcher(text);
93 if (!m.matches()) {
94 throw new IllegalArgumentException("invalid version format: " + text);
95 }
96
97 protocolName = m.group(1);
98 majorVersion = Integer.parseInt(m.group(2));
99 minorVersion = Integer.parseInt(m.group(3));
100 this.text = protocolName + '/' + majorVersion + '.' + minorVersion;
101 this.keepAliveDefault = keepAliveDefault;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 public HttpVersion(
116 String protocolName, int majorVersion, int minorVersion,
117 boolean keepAliveDefault) {
118 if (protocolName == null) {
119 throw new NullPointerException("protocolName");
120 }
121
122 protocolName = protocolName.trim().toUpperCase();
123 if (protocolName.length() == 0) {
124 throw new IllegalArgumentException("empty protocolName");
125 }
126
127 for (int i = 0; i < protocolName.length(); i ++) {
128 if (Character.isISOControl(protocolName.charAt(i)) ||
129 Character.isWhitespace(protocolName.charAt(i))) {
130 throw new IllegalArgumentException("invalid character in protocolName");
131 }
132 }
133
134 if (majorVersion < 0) {
135 throw new IllegalArgumentException("negative majorVersion");
136 }
137 if (minorVersion < 0) {
138 throw new IllegalArgumentException("negative minorVersion");
139 }
140
141 this.protocolName = protocolName;
142 this.majorVersion = majorVersion;
143 this.minorVersion = minorVersion;
144 text = protocolName + '/' + majorVersion + '.' + minorVersion;
145 this.keepAliveDefault = keepAliveDefault;
146 }
147
148
149
150
151 public String getProtocolName() {
152 return protocolName;
153 }
154
155
156
157
158 public int getMajorVersion() {
159 return majorVersion;
160 }
161
162
163
164
165 public int getMinorVersion() {
166 return minorVersion;
167 }
168
169
170
171
172 public String getText() {
173 return text;
174 }
175
176
177
178
179
180 public boolean isKeepAliveDefault() {
181 return keepAliveDefault;
182 }
183
184
185
186
187 @Override
188 public String toString() {
189 return getText();
190 }
191
192 @Override
193 public int hashCode() {
194 return (getProtocolName().hashCode() * 31 + getMajorVersion()) * 31 +
195 getMinorVersion();
196 }
197
198 @Override
199 public boolean equals(Object o) {
200 if (!(o instanceof HttpVersion)) {
201 return false;
202 }
203
204 HttpVersion that = (HttpVersion) o;
205 return getMinorVersion() == that.getMinorVersion() &&
206 getMajorVersion() == that.getMajorVersion() &&
207 getProtocolName().equals(that.getProtocolName());
208 }
209
210 public int compareTo(HttpVersion o) {
211 int v = getProtocolName().compareTo(o.getProtocolName());
212 if (v != 0) {
213 return v;
214 }
215
216 v = getMajorVersion() - o.getMajorVersion();
217 if (v != 0) {
218 return v;
219 }
220
221 return getMinorVersion() - o.getMinorVersion();
222 }
223 }