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