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