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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import java.io.File;
  48. import java.io.Serializable;
  49. import java.net.URISyntaxException;
  50. import java.net.URL;
  51. import java.util.regex.Matcher;
  52. import java.util.regex.Pattern;
  53. import org.eclipse.jgit.JGitText;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.util.StringUtils;
  56. /**
  57. * This URI like construct used for referencing Git archives over the net, as
  58. * well as locally stored archives. The most important difference compared to
  59. * RFC 2396 URI's is that no URI encoding/decoding ever takes place. A space or
  60. * any special character is written as-is.
  61. */
  62. public class URIish implements Serializable {
  63. /**
  64. * Part of a pattern which matches the scheme part (git, http, ...) of an
  65. * URI. Defines one capturing group containing the scheme without the
  66. * trailing colon and slashes
  67. */
  68. private static final String SCHEME_P = "([a-z][a-z0-9+-]+)://";
  69. /**
  70. * Part of a pattern which matches the optional user/password part (e.g.
  71. * root:pwd@ in git://root:pwd@host.xyz/a.git) of URIs. Defines two
  72. * capturing groups: the first containing the user and the second containing
  73. * the password
  74. */
  75. private static final String OPT_USER_PWD_P = "(?:([^/:@]+)(?::([^\\\\/]+))?@)?";
  76. /**
  77. * Part of a pattern which matches the host part of URIs. Defines one
  78. * capturing group containing the host name.
  79. */
  80. private static final String HOST_P = "([^\\\\/:]+)";
  81. /**
  82. * Part of a pattern which matches the optional port part of URIs. Defines
  83. * one capturing group containing the port without the preceding colon.
  84. */
  85. private static final String OPT_PORT_P = "(?::(\\d+))?";
  86. /**
  87. * Part of a pattern which matches the ~username part (e.g. /~root in
  88. * git://host.xyz/~root/a.git) of URIs. Defines no capturing group.
  89. */
  90. private static final String USER_HOME_P = "(?:/~(?:[^\\\\/]+))";
  91. /**
  92. * Part of a pattern which matches the optional drive letter in paths (e.g.
  93. * D: in file:///D:/a.txt). Defines no capturing group.
  94. */
  95. private static final String OPT_DRIVE_LETTER_P = "(?:[A-Za-z]:)?";
  96. /**
  97. * Part of a pattern which matches a relative path. Relative paths don't
  98. * start with slash or drive letters. Defines no capturing group.
  99. */
  100. private static final String RELATIVE_PATH_P = "(?:(?:[^\\\\/]+[\\\\/])*[^\\\\/]+[\\\\/]?)";
  101. /**
  102. * Part of a pattern which matches a relative or absolute path. Defines no
  103. * capturing group.
  104. */
  105. private static final String PATH_P = "(" + OPT_DRIVE_LETTER_P + "[\\\\/]?"
  106. + RELATIVE_PATH_P + ")";
  107. private static final long serialVersionUID = 1L;
  108. /**
  109. * A pattern matching standard URI: </br>
  110. * <code>scheme "://" user_password? hostname? portnumber? path</code>
  111. */
  112. private static final Pattern FULL_URI = Pattern.compile("^" //
  113. + SCHEME_P //
  114. + "(?:" // start a group containing hostname and all options only
  115. // availabe when a hostname is there
  116. + OPT_USER_PWD_P //
  117. + HOST_P //
  118. + OPT_PORT_P //
  119. + "(" // open a catpuring group the the user-home-dir part
  120. + (USER_HOME_P + "?") //
  121. + "[\\\\/])" //
  122. + ")?" // close the optional group containing hostname
  123. + "(.+)?" //
  124. + "$");
  125. /**
  126. * A pattern matching the reference to a local file. This may be an absolute
  127. * path (maybe even containing windows drive-letters) or a relative path.
  128. */
  129. private static final Pattern LOCAL_FILE = Pattern.compile("^" //
  130. + "([\\\\/]?" + PATH_P + ")" //
  131. + "$");
  132. /**
  133. * A pattern matching a URI for the scheme 'file' which has only ':/' as
  134. * separator between scheme and path. Standard file URIs have '://' as
  135. * separator, but java.io.File.toURI() constructs those URIs.
  136. */
  137. private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" //
  138. + "(file):([\\\\/](?![\\\\/])" //
  139. + PATH_P //
  140. + ")$");
  141. /**
  142. * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
  143. */
  144. private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" //
  145. + OPT_USER_PWD_P //
  146. + HOST_P //
  147. + ":(" //
  148. + ("(?:" + USER_HOME_P + "[\\\\/])?") //
  149. + RELATIVE_PATH_P //
  150. + ")$");
  151. /**
  152. * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
  153. */
  154. private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" //
  155. + OPT_USER_PWD_P //
  156. + "([^\\\\/:]{2,})" //
  157. + ":(" //
  158. + "[\\\\/]" + RELATIVE_PATH_P //
  159. + ")$");
  160. private String scheme;
  161. private String path;
  162. private String user;
  163. private String pass;
  164. private int port = -1;
  165. private String host;
  166. /**
  167. * Parse and construct an {@link URIish} from a string
  168. *
  169. * @param s
  170. * @throws URISyntaxException
  171. */
  172. public URIish(String s) throws URISyntaxException {
  173. if (StringUtils.isEmptyOrNull(s)) {
  174. throw new URISyntaxException("The uri was empty or null",
  175. JGitText.get().cannotParseGitURIish);
  176. }
  177. Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
  178. if (matcher.matches()) {
  179. scheme = matcher.group(1);
  180. path = cleanLeadingSlashes(matcher.group(2), scheme);
  181. return;
  182. }
  183. matcher = FULL_URI.matcher(s);
  184. if (matcher.matches()) {
  185. scheme = matcher.group(1);
  186. user = matcher.group(2);
  187. pass = matcher.group(3);
  188. host = matcher.group(4);
  189. if (matcher.group(5) != null)
  190. port = Integer.parseInt(matcher.group(5));
  191. path = cleanLeadingSlashes(
  192. n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
  193. return;
  194. }
  195. matcher = RELATIVE_SCP_URI.matcher(s);
  196. if (matcher.matches()) {
  197. user = matcher.group(1);
  198. pass = matcher.group(2);
  199. host = matcher.group(3);
  200. path = matcher.group(4);
  201. return;
  202. }
  203. matcher = ABSOLUTE_SCP_URI.matcher(s);
  204. if (matcher.matches()) {
  205. user = matcher.group(1);
  206. pass = matcher.group(2);
  207. host = matcher.group(3);
  208. path = matcher.group(4);
  209. return;
  210. }
  211. matcher = LOCAL_FILE.matcher(s);
  212. if (matcher.matches()) {
  213. path = matcher.group(1);
  214. return;
  215. }
  216. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  217. }
  218. private String n2e(String s) {
  219. if (s == null)
  220. return "";
  221. else
  222. return s;
  223. }
  224. // takes care to cut of a leading slash if a windows drive letter or a
  225. // user-home-dir specifications are
  226. private String cleanLeadingSlashes(String p, String s) {
  227. if (p.length() >= 3
  228. && p.charAt(0) == '/'
  229. && p.charAt(2) == ':'
  230. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  231. && p.charAt(1) <= 'z'))
  232. return p.substring(1);
  233. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  234. && p.charAt(1) == '~')
  235. return p.substring(1);
  236. else
  237. return p;
  238. }
  239. /**
  240. * Construct a URIish from a standard URL.
  241. *
  242. * @param u
  243. * the source URL to convert from.
  244. */
  245. public URIish(final URL u) {
  246. scheme = u.getProtocol();
  247. path = u.getPath();
  248. final String ui = u.getUserInfo();
  249. if (ui != null) {
  250. final int d = ui.indexOf(':');
  251. user = d < 0 ? ui : ui.substring(0, d);
  252. pass = d < 0 ? null : ui.substring(d + 1);
  253. }
  254. port = u.getPort();
  255. host = u.getHost();
  256. }
  257. /** Create an empty, non-configured URI. */
  258. public URIish() {
  259. // Configure nothing.
  260. }
  261. private URIish(final URIish u) {
  262. this.scheme = u.scheme;
  263. this.path = u.path;
  264. this.user = u.user;
  265. this.pass = u.pass;
  266. this.port = u.port;
  267. this.host = u.host;
  268. }
  269. /**
  270. * @return true if this URI references a repository on another system.
  271. */
  272. public boolean isRemote() {
  273. return getHost() != null;
  274. }
  275. /**
  276. * @return host name part or null
  277. */
  278. public String getHost() {
  279. return host;
  280. }
  281. /**
  282. * Return a new URI matching this one, but with a different host.
  283. *
  284. * @param n
  285. * the new value for host.
  286. * @return a new URI with the updated value.
  287. */
  288. public URIish setHost(final String n) {
  289. final URIish r = new URIish(this);
  290. r.host = n;
  291. return r;
  292. }
  293. /**
  294. * @return protocol name or null for local references
  295. */
  296. public String getScheme() {
  297. return scheme;
  298. }
  299. /**
  300. * Return a new URI matching this one, but with a different scheme.
  301. *
  302. * @param n
  303. * the new value for scheme.
  304. * @return a new URI with the updated value.
  305. */
  306. public URIish setScheme(final String n) {
  307. final URIish r = new URIish(this);
  308. r.scheme = n;
  309. return r;
  310. }
  311. /**
  312. * @return path name component
  313. */
  314. public String getPath() {
  315. return path;
  316. }
  317. /**
  318. * Return a new URI matching this one, but with a different path.
  319. *
  320. * @param n
  321. * the new value for path.
  322. * @return a new URI with the updated value.
  323. */
  324. public URIish setPath(final String n) {
  325. final URIish r = new URIish(this);
  326. r.path = n;
  327. return r;
  328. }
  329. /**
  330. * @return user name requested for transfer or null
  331. */
  332. public String getUser() {
  333. return user;
  334. }
  335. /**
  336. * Return a new URI matching this one, but with a different user.
  337. *
  338. * @param n
  339. * the new value for user.
  340. * @return a new URI with the updated value.
  341. */
  342. public URIish setUser(final String n) {
  343. final URIish r = new URIish(this);
  344. r.user = n;
  345. return r;
  346. }
  347. /**
  348. * @return password requested for transfer or null
  349. */
  350. public String getPass() {
  351. return pass;
  352. }
  353. /**
  354. * Return a new URI matching this one, but with a different password.
  355. *
  356. * @param n
  357. * the new value for password.
  358. * @return a new URI with the updated value.
  359. */
  360. public URIish setPass(final String n) {
  361. final URIish r = new URIish(this);
  362. r.pass = n;
  363. return r;
  364. }
  365. /**
  366. * @return port number requested for transfer or -1 if not explicit
  367. */
  368. public int getPort() {
  369. return port;
  370. }
  371. /**
  372. * Return a new URI matching this one, but with a different port.
  373. *
  374. * @param n
  375. * the new value for port.
  376. * @return a new URI with the updated value.
  377. */
  378. public URIish setPort(final int n) {
  379. final URIish r = new URIish(this);
  380. r.port = n > 0 ? n : -1;
  381. return r;
  382. }
  383. public int hashCode() {
  384. int hc = 0;
  385. if (getScheme() != null)
  386. hc = hc * 31 + getScheme().hashCode();
  387. if (getUser() != null)
  388. hc = hc * 31 + getUser().hashCode();
  389. if (getPass() != null)
  390. hc = hc * 31 + getPass().hashCode();
  391. if (getHost() != null)
  392. hc = hc * 31 + getHost().hashCode();
  393. if (getPort() > 0)
  394. hc = hc * 31 + getPort();
  395. if (getPath() != null)
  396. hc = hc * 31 + getPath().hashCode();
  397. return hc;
  398. }
  399. public boolean equals(final Object obj) {
  400. if (!(obj instanceof URIish))
  401. return false;
  402. final URIish b = (URIish) obj;
  403. if (!eq(getScheme(), b.getScheme()))
  404. return false;
  405. if (!eq(getUser(), b.getUser()))
  406. return false;
  407. if (!eq(getPass(), b.getPass()))
  408. return false;
  409. if (!eq(getHost(), b.getHost()))
  410. return false;
  411. if (getPort() != b.getPort())
  412. return false;
  413. if (!eq(getPath(), b.getPath()))
  414. return false;
  415. return true;
  416. }
  417. private static boolean eq(final String a, final String b) {
  418. if (a == b)
  419. return true;
  420. if (a == null || b == null)
  421. return false;
  422. return a.equals(b);
  423. }
  424. /**
  425. * Obtain the string form of the URI, with the password included.
  426. *
  427. * @return the URI, including its password field, if any.
  428. */
  429. public String toPrivateString() {
  430. return format(true);
  431. }
  432. public String toString() {
  433. return format(false);
  434. }
  435. private String format(final boolean includePassword) {
  436. final StringBuilder r = new StringBuilder();
  437. if (getScheme() != null) {
  438. r.append(getScheme());
  439. r.append("://");
  440. }
  441. if (getUser() != null) {
  442. r.append(getUser());
  443. if (includePassword && getPass() != null) {
  444. r.append(':');
  445. r.append(getPass());
  446. }
  447. }
  448. if (getHost() != null) {
  449. if (getUser() != null)
  450. r.append('@');
  451. r.append(getHost());
  452. if (getScheme() != null && getPort() > 0) {
  453. r.append(':');
  454. r.append(getPort());
  455. }
  456. }
  457. if (getPath() != null) {
  458. if (getScheme() != null) {
  459. if (!getPath().startsWith("/"))
  460. r.append('/');
  461. } else if (getHost() != null)
  462. r.append(':');
  463. r.append(getPath());
  464. }
  465. return r.toString();
  466. }
  467. /**
  468. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  469. * for a full path:
  470. * <table>
  471. * <tr>
  472. * <th>Path</th>
  473. * <th>Humanish part</th>
  474. * </tr>
  475. * <tr>
  476. * <td><code>/path/to/repo.git</code></td>
  477. * <td rowspan="4"><code>repo</code></td>
  478. * </tr>
  479. * <tr>
  480. * <td><code>/path/to/repo.git/</code></td>
  481. * </tr>
  482. * <tr>
  483. * <td><code>/path/to/repo/.git</code></td>
  484. * </tr>
  485. * <tr>
  486. * <td><code>/path/to/repo/</code></td>
  487. * </tr>
  488. * <tr>
  489. * <td><code>/path//to</code></td>
  490. * <td>an empty string</td>
  491. * </tr>
  492. * </table>
  493. *
  494. * @return the "humanish" part of the path. May be an empty string. Never
  495. * {@code null}.
  496. * @throws IllegalArgumentException
  497. * if it's impossible to determine a humanish part, or path is
  498. * {@code null} or empty
  499. * @see #getPath
  500. */
  501. public String getHumanishName() throws IllegalArgumentException {
  502. if ("".equals(getPath()) || getPath() == null)
  503. throw new IllegalArgumentException();
  504. String s = getPath();
  505. String[] elements;
  506. if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches())
  507. elements = s.split("[\\" + File.separatorChar + "/]");
  508. else
  509. elements = s.split("/");
  510. if (elements.length == 0)
  511. throw new IllegalArgumentException();
  512. String result = elements[elements.length - 1];
  513. if (Constants.DOT_GIT.equals(result))
  514. result = elements[elements.length - 2];
  515. else if (result.endsWith(Constants.DOT_GIT_EXT))
  516. result = result.substring(0, result.length()
  517. - Constants.DOT_GIT_EXT.length());
  518. return result;
  519. }
  520. }