1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpVersion.java,v 1.5 2004/05/13 04:03:25 mbecke Exp $
  3. * $Revision: 1.5 $
  4. * $Date: 2004/05/13 04:03:25 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. /**
  31. * <p>HTTP version, as specified in RFC 2616.</p>
  32. * <p>
  33. * HTTP uses a "<major>.<minor>" numbering scheme to indicate versions
  34. * of the protocol. The protocol versioning policy is intended to allow
  35. * the sender to indicate the format of a message and its capacity for
  36. * understanding further HTTP communication, rather than the features
  37. * obtained via that communication. No change is made to the version
  38. * number for the addition of message components which do not affect
  39. * communication behavior or which only add to extensible field values.
  40. * The <minor> number is incremented when the changes made to the
  41. * protocol add features which do not change the general message parsing
  42. * algorithm, but which may add to the message semantics and imply
  43. * additional capabilities of the sender. The <major> number is
  44. * incremented when the format of a message within the protocol is
  45. * changed. See RFC 2145 [36] for a fuller explanation.
  46. * </p>
  47. * <p>
  48. * The version of an HTTP message is indicated by an HTTP-Version field
  49. * in the first line of the message.
  50. * </p>
  51. * <pre>
  52. * HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
  53. * </pre>
  54. * <p>
  55. * Note that the major and minor numbers MUST be treated as separate
  56. * integers and that each MAY be incremented higher than a single digit.
  57. * Thus, HTTP/2.4 is a lower version than HTTP/2.13, which in turn is
  58. * lower than HTTP/12.3. Leading zeros MUST be ignored by recipients and
  59. * MUST NOT be sent.
  60. * </p>
  61. *
  62. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  63. *
  64. * @version $Revision: 1.5 $ $Date: 2004/05/13 04:03:25 $
  65. *
  66. * @since 3.0
  67. */
  68. public class HttpVersion implements Comparable {
  69. /** Major version number of the HTTP protocol */
  70. private int major = 0;
  71. /** Minor version number of the HTTP protocol */
  72. private int minor = 0;
  73. /** HTTP protocol version 0.9 */
  74. public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
  75. /** HTTP protocol version 1.0 */
  76. public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
  77. /** HTTP protocol version 1.1 */
  78. public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
  79. /**
  80. * Create an HTTP protocol version designator.
  81. *
  82. * @param major the major version number of the HTTP protocol
  83. * @param minor the minor version number of the HTTP protocol
  84. *
  85. * @throws IllegalArgumentException if either major or minor version number is negative
  86. */
  87. public HttpVersion(int major, int minor) {
  88. if (major < 0) {
  89. throw new IllegalArgumentException("HTTP major version number may not be negative");
  90. }
  91. this.major = major;
  92. if (minor < 0) {
  93. throw new IllegalArgumentException("HTTP minor version number may not be negative");
  94. }
  95. this.minor = minor;
  96. }
  97. /**
  98. * Returns the major version number of the HTTP protocol.
  99. *
  100. * @return the major version number.
  101. */
  102. public int getMajor() {
  103. return major;
  104. }
  105. /**
  106. * Returns the minor version number of the HTTP protocol.
  107. *
  108. * @return the minor version number.
  109. */
  110. public int getMinor() {
  111. return minor;
  112. }
  113. /**
  114. * @see java.lang.Object#hashCode()
  115. */
  116. public int hashCode() {
  117. return this.major * 100000 + this.minor;
  118. }
  119. /**
  120. * @see java.lang.Object#equals(java.lang.Object)
  121. */
  122. public boolean equals(Object obj) {
  123. if (this == obj) {
  124. return true;
  125. }
  126. if (!(obj instanceof HttpVersion)) {
  127. return false;
  128. }
  129. return equals((HttpVersion)obj);
  130. }
  131. /**
  132. * Compares this HTTP protocol version with another one.
  133. *
  134. * @param anotherVer the version to be compared with.
  135. *
  136. * @return a negative integer, zero, or a positive integer as this version is less than,
  137. * equal to, or greater than the specified version.
  138. */
  139. public int compareTo(HttpVersion anotherVer) {
  140. if (anotherVer == null) {
  141. throw new IllegalArgumentException("Version parameter may not be null");
  142. }
  143. int delta = getMajor() - anotherVer.getMajor();
  144. if (delta == 0) {
  145. delta = getMinor() - anotherVer.getMinor();
  146. }
  147. return delta;
  148. }
  149. /**
  150. * @see java.lang.Comparable#compareTo(java.lang.Object)
  151. */
  152. public int compareTo(Object o) {
  153. return compareTo((HttpVersion)o);
  154. }
  155. /**
  156. * Test if the HTTP protocol version is equal to the given number.
  157. *
  158. * @return <tt>true</tt> if HTTP protocol version is given to the given number,
  159. * <tt>false</tt> otherwise.
  160. */
  161. public boolean equals(HttpVersion version) {
  162. return compareTo(version) == 0;
  163. }
  164. /**
  165. * Test if the HTTP protocol version is greater or equal to the given number.
  166. *
  167. * @return <tt>true</tt> if HTTP protocol version is greater or equal given to the
  168. * given number, <tt>false</tt> otherwise.
  169. */
  170. public boolean greaterEquals(HttpVersion version) {
  171. return compareTo(version) >= 0;
  172. }
  173. /**
  174. * Test if the HTTP protocol version is less or equal to the given number.
  175. *
  176. * @return <tt>true</tt> if HTTP protocol version is less or equal to given to the
  177. * given number, <tt>false</tt> otherwise.
  178. */
  179. public boolean lessEquals(HttpVersion version) {
  180. return compareTo(version) <= 0;
  181. }
  182. /**
  183. * @see java.lang.Object#toString()
  184. */
  185. public String toString() {
  186. StringBuffer buffer = new StringBuffer();
  187. buffer.append("HTTP/");
  188. buffer.append(this.major);
  189. buffer.append('.');
  190. buffer.append(this.minor);
  191. return buffer.toString();
  192. }
  193. /**
  194. * Parses the textual representation of the given HTTP protocol version.
  195. *
  196. * @return HTTP protocol version.
  197. *
  198. * @throws ProtocolException if the string is not a valid HTTP protocol version.
  199. */
  200. public static HttpVersion parse(final String s) throws ProtocolException {
  201. if (s == null) {
  202. throw new IllegalArgumentException("String may not be null");
  203. }
  204. if (!s.startsWith("HTTP/")) {
  205. throw new ProtocolException("Invalid HTTP version string: " + s);
  206. }
  207. int major, minor;
  208. int i1 = "HTTP/".length();
  209. int i2 = s.indexOf(".", i1);
  210. if (i2 == -1) {
  211. throw new ProtocolException("Invalid HTTP version number: " + s);
  212. }
  213. try {
  214. major = Integer.parseInt(s.substring(i1, i2));
  215. } catch (NumberFormatException e) {
  216. throw new ProtocolException("Invalid HTTP major version number: " + s);
  217. }
  218. i1 = i2 + 1;
  219. i2 = s.length();
  220. try {
  221. minor = Integer.parseInt(s.substring(i1, i2));
  222. } catch (NumberFormatException e) {
  223. throw new ProtocolException("Invalid HTTP minor version number: " + s);
  224. }
  225. return new HttpVersion(major, minor);
  226. }
  227. }