You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClientVersionUtil.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2012, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.server;
  44. import javax.servlet.http.HttpServletRequest;
  45. /**
  46. * Parses Git client User-Agent strings.
  47. */
  48. public class ClientVersionUtil {
  49. /**
  50. * An invalid version of Git
  51. *
  52. * @return maximum version array, indicating an invalid version of Git.
  53. */
  54. public static int[] invalidVersion() {
  55. return new int[] { Integer.MAX_VALUE };
  56. }
  57. /**
  58. * Parse a Git client User-Agent header value.
  59. *
  60. * @param version
  61. * git client version string, of the form "git/1.7.9".
  62. * @return components of the version string. {@link #invalidVersion()} if
  63. * the version string cannot be parsed.
  64. */
  65. public static int[] parseVersion(String version) {
  66. if (version != null && version.startsWith("git/"))
  67. return splitVersion(version.substring("git/".length()));
  68. return invalidVersion();
  69. }
  70. private static int[] splitVersion(String versionString) {
  71. char[] str = versionString.toCharArray();
  72. int[] ver = new int[4];
  73. int end = 0;
  74. int acc = 0;
  75. for (int i = 0; i < str.length; i++) {
  76. char c = str[i];
  77. if ('0' <= c && c <= '9') {
  78. acc *= 10;
  79. acc += c - '0';
  80. } else if (c == '.') {
  81. if (end == ver.length)
  82. ver = grow(ver);
  83. ver[end++] = acc;
  84. acc = 0;
  85. } else if (c == 'g' && 0 < i && str[i - 1] == '.' && 0 < end) {
  86. // Non-tagged builds may contain a mangled git describe output.
  87. // "1.7.6.1.45.gbe0cc". The 45 isn't a valid component. Drop it.
  88. ver[end - 1] = 0;
  89. acc = 0;
  90. break;
  91. } else if (c == '-' && (i + 2) < str.length
  92. && str[i + 1] == 'r' && str[i + 2] == 'c') {
  93. // Release candidates aren't the same as a final release.
  94. if (acc > 0)
  95. acc--;
  96. break;
  97. } else
  98. break;
  99. }
  100. if (acc != 0) {
  101. if (end == ver.length)
  102. ver = grow(ver);
  103. ver[end++] = acc;
  104. } else {
  105. while (0 < end && ver[end - 1] == 0)
  106. end--;
  107. }
  108. if (end < ver.length) {
  109. int[] n = new int[end];
  110. System.arraycopy(ver, 0, n, 0, end);
  111. ver = n;
  112. }
  113. return ver;
  114. }
  115. private static int[] grow(int[] tmp) {
  116. int[] n = new int[tmp.length + 1];
  117. System.arraycopy(tmp, 0, n, 0, tmp.length);
  118. return n;
  119. }
  120. /**
  121. * Compare two version strings for natural ordering.
  122. *
  123. * @param a
  124. * first parsed version string.
  125. * @param b
  126. * second parsed version string.
  127. * @return &lt; 0 if a is before b; 0 if a equals b; &gt;0 if a is after b.
  128. */
  129. public static int compare(int[] a, int[] b) {
  130. for (int i = 0; i < a.length && i < b.length; i++) {
  131. int cmp = a[i] - b[i];
  132. if (cmp != 0)
  133. return cmp;
  134. }
  135. return a.length - b.length;
  136. }
  137. /**
  138. * Convert a parsed version back to a string.
  139. *
  140. * @param ver
  141. * the parsed version array.
  142. * @return a string, e.g. "1.6.6.0".
  143. */
  144. public static String toString(int[] ver) {
  145. StringBuilder b = new StringBuilder();
  146. for (int v : ver) {
  147. if (b.length() > 0)
  148. b.append('.');
  149. b.append(v);
  150. }
  151. return b.toString();
  152. }
  153. /**
  154. * Check if a Git client has the known push status bug.
  155. * <p>
  156. * These buggy clients do not display the status report from a failed push
  157. * over HTTP.
  158. *
  159. * @param version
  160. * parsed version of the Git client software.
  161. * @return true if the bug is present.
  162. * @deprecated no widely used Git versions need this any more
  163. */
  164. @Deprecated
  165. public static boolean hasPushStatusBug(int[] version) {
  166. return false;
  167. }
  168. /**
  169. * Check if a Git client has the known chunked request body encoding bug.
  170. * <p>
  171. * Git 1.7.5 contains a unique bug where chunked requests are malformed.
  172. * This applies to both fetch and push.
  173. *
  174. * @param version
  175. * parsed version of the Git client software.
  176. * @param request
  177. * incoming HTTP request.
  178. * @return true if the client has the chunked encoding bug.
  179. * @deprecated no widely used Git versions need this any more
  180. */
  181. @Deprecated
  182. public static boolean hasChunkedEncodingRequestBug(
  183. int[] version, HttpServletRequest request) {
  184. return false;
  185. }
  186. private ClientVersionUtil() {
  187. }
  188. }