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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 static org.eclipse.jgit.http.server.ServletUtils.isChunked;
  45. import javax.servlet.http.HttpServletRequest;
  46. /**
  47. * Parses Git client User-Agent strings.
  48. */
  49. public class ClientVersionUtil {
  50. private static final int[] v1_7_5 = { 1, 7, 5 };
  51. private static final int[] v1_7_8_6 = { 1, 7, 8, 6 };
  52. private static final int[] v1_7_9 = { 1, 7, 9 };
  53. /**
  54. * An invalid version of Git
  55. *
  56. * @return maximum version array, indicating an invalid version of Git.
  57. */
  58. public static int[] invalidVersion() {
  59. return new int[] { Integer.MAX_VALUE };
  60. }
  61. /**
  62. * Parse a Git client User-Agent header value.
  63. *
  64. * @param version
  65. * git client version string, of the form "git/1.7.9".
  66. * @return components of the version string. {@link #invalidVersion()} if
  67. * the version string cannot be parsed.
  68. */
  69. public static int[] parseVersion(String version) {
  70. if (version != null && version.startsWith("git/"))
  71. return splitVersion(version.substring("git/".length()));
  72. return invalidVersion();
  73. }
  74. private static int[] splitVersion(String versionString) {
  75. char[] str = versionString.toCharArray();
  76. int[] ver = new int[4];
  77. int end = 0;
  78. int acc = 0;
  79. for (int i = 0; i < str.length; i++) {
  80. char c = str[i];
  81. if ('0' <= c && c <= '9') {
  82. acc *= 10;
  83. acc += c - '0';
  84. } else if (c == '.') {
  85. if (end == ver.length)
  86. ver = grow(ver);
  87. ver[end++] = acc;
  88. acc = 0;
  89. } else if (c == 'g' && 0 < i && str[i - 1] == '.' && 0 < end) {
  90. // Non-tagged builds may contain a mangled git describe output.
  91. // "1.7.6.1.45.gbe0cc". The 45 isn't a valid component. Drop it.
  92. ver[end - 1] = 0;
  93. acc = 0;
  94. break;
  95. } else if (c == '-' && (i + 2) < str.length
  96. && str[i + 1] == 'r' && str[i + 2] == 'c') {
  97. // Release candidates aren't the same as a final release.
  98. if (acc > 0)
  99. acc--;
  100. break;
  101. } else
  102. break;
  103. }
  104. if (acc != 0) {
  105. if (end == ver.length)
  106. ver = grow(ver);
  107. ver[end++] = acc;
  108. } else {
  109. while (0 < end && ver[end - 1] == 0)
  110. end--;
  111. }
  112. if (end < ver.length) {
  113. int[] n = new int[end];
  114. System.arraycopy(ver, 0, n, 0, end);
  115. ver = n;
  116. }
  117. return ver;
  118. }
  119. private static int[] grow(int[] tmp) {
  120. int[] n = new int[tmp.length + 1];
  121. System.arraycopy(tmp, 0, n, 0, tmp.length);
  122. return n;
  123. }
  124. /**
  125. * Compare two version strings for natural ordering.
  126. *
  127. * @param a
  128. * first parsed version string.
  129. * @param b
  130. * second parsed version string.
  131. * @return &lt; 0 if a is before b; 0 if a equals b; &gt;0 if a is after b.
  132. */
  133. public static int compare(int[] a, int[] b) {
  134. for (int i = 0; i < a.length && i < b.length; i++) {
  135. int cmp = a[i] - b[i];
  136. if (cmp != 0)
  137. return cmp;
  138. }
  139. return a.length - b.length;
  140. }
  141. /**
  142. * Convert a parsed version back to a string.
  143. *
  144. * @param ver
  145. * the parsed version array.
  146. * @return a string, e.g. "1.6.6.0".
  147. */
  148. public static String toString(int[] ver) {
  149. StringBuilder b = new StringBuilder();
  150. for (int v : ver) {
  151. if (b.length() > 0)
  152. b.append('.');
  153. b.append(v);
  154. }
  155. return b.toString();
  156. }
  157. /**
  158. * Check if a Git client has the known push status bug.
  159. * <p>
  160. * These buggy clients do not display the status report from a failed push
  161. * over HTTP.
  162. *
  163. * @param version
  164. * parsed version of the Git client software.
  165. * @return true if the bug is present.
  166. */
  167. public static boolean hasPushStatusBug(int[] version) {
  168. int cmp = compare(version, v1_7_8_6);
  169. if (cmp < 0)
  170. return true; // Everything before 1.7.8.6 is known broken.
  171. else if (cmp == 0)
  172. return false; // 1.7.8.6 contained the bug fix.
  173. if (compare(version, v1_7_9) <= 0)
  174. return true; // 1.7.9 shipped before 1.7.8.6 and has the bug.
  175. return false; // 1.7.9.1 and later are fixed.
  176. }
  177. /**
  178. * Check if a Git client has the known chunked request body encoding bug.
  179. * <p>
  180. * Git 1.7.5 contains a unique bug where chunked requests are malformed.
  181. * This applies to both fetch and push.
  182. *
  183. * @param version
  184. * parsed version of the Git client software.
  185. * @param request
  186. * incoming HTTP request.
  187. * @return true if the client has the chunked encoding bug.
  188. */
  189. public static boolean hasChunkedEncodingRequestBug(
  190. int[] version, HttpServletRequest request) {
  191. return compare(version, v1_7_5) == 0 && isChunked(request);
  192. }
  193. private ClientVersionUtil() {
  194. }
  195. }