View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * The version of HTTP or its derived protocols, such as
23   * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
24   * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
25   * @apiviz.exclude
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       * HTTP/1.0
34       */
35      public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0, false);
36  
37      /**
38       * HTTP/1.1
39       */
40      public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1, true);
41  
42      /**
43       * Returns an existing or new {@link HttpVersion} instance which matches to
44       * the specified protocol version string.  If the specified {@code text} is
45       * equal to {@code "HTTP/1.0"}, {@link #HTTP_1_0} will be returned.  If the
46       * specified {@code text} is equal to {@code "HTTP/1.1"}, {@link #HTTP_1_1}
47       * will be returned.  Otherwise, a new {@link HttpVersion} instance will be
48       * returned.
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       * Creates a new HTTP version with the specified version string.  You will
73       * not need to create a new instance unless you are implementing a protocol
74       * derived from HTTP, such as
75       * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
76       * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
77       *
78       * @param keepAliveDefault
79       *        {@code true} if and only if the connection is kept alive unless
80       *        the {@code "Connection"} header is set to {@code "close"} explicitly.
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      * Creates a new HTTP version with the specified protocol name and version
106      * numbers.  You will not need to create a new instance unless you are
107      * implementing a protocol derived from HTTP, such as
108      * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
109      * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
110      *
111      * @param keepAliveDefault
112      *        {@code true} if and only if the connection is kept alive unless
113      *        the {@code "Connection"} header is set to {@code "close"} explicitly.
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      * Returns the name of the protocol such as {@code "HTTP"} in {@code "HTTP/1.0"}.
150      */
151     public String getProtocolName() {
152         return protocolName;
153     }
154 
155     /**
156      * Returns the name of the protocol such as {@code 1} in {@code "HTTP/1.0"}.
157      */
158     public int getMajorVersion() {
159         return majorVersion;
160     }
161 
162     /**
163      * Returns the name of the protocol such as {@code 0} in {@code "HTTP/1.0"}.
164      */
165     public int getMinorVersion() {
166         return minorVersion;
167     }
168 
169     /**
170      * Returns the full protocol version text such as {@code "HTTP/1.0"}.
171      */
172     public String getText() {
173         return text;
174     }
175 
176     /**
177      * Returns {@code true} if and only if the connection is kept alive unless
178      * the {@code "Connection"} header is set to {@code "close"} explicitly.
179      */
180     public boolean isKeepAliveDefault() {
181         return keepAliveDefault;
182     }
183 
184     /**
185      * Returns the full protocol version text such as {@code "HTTP/1.0"}.
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 }