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

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