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.

URIish.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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.Serializable;
  48. import java.net.URISyntaxException;
  49. import java.net.URL;
  50. import java.util.regex.Matcher;
  51. import java.util.regex.Pattern;
  52. import org.eclipse.jgit.JGitText;
  53. import org.eclipse.jgit.lib.Constants;
  54. /**
  55. * This URI like construct used for referencing Git archives over the net, as
  56. * well as locally stored archives. The most important difference compared to
  57. * RFC 2396 URI's is that no URI encoding/decoding ever takes place. A space or
  58. * any special character is written as-is.
  59. */
  60. public class URIish implements Serializable {
  61. /**
  62. * Part of a pattern which matches the scheme part (git, http, ...) of an
  63. * URI. Defines one capturing group containing the scheme without the
  64. * trailing colon and slashes
  65. */
  66. private static final String SCHEME_P = "([a-z][a-z0-9+-]+)://";
  67. /**
  68. * Part of a pattern which matches the optional user/password part (e.g.
  69. * root:pwd@ in git://root:pwd@host.xyz/a.git) of URIs. Defines two
  70. * capturing groups: the first containing the user and the second containing
  71. * the password
  72. */
  73. private static final String OPT_USER_PWD_P = "(?:([^/:@]+)(?::([^/]+))?@)?";
  74. /**
  75. * Part of a pattern which matches the host part of URIs. Defines one
  76. * capturing group containing the host name.
  77. */
  78. private static final String HOST_P = "([^/:]+)";
  79. /**
  80. * Part of a pattern which matches the optional port part of URIs. Defines
  81. * one capturing group containing the port without the preceding colon.
  82. */
  83. private static final String OPT_PORT_P = "(?::(\\d+))?";
  84. /**
  85. * Part of a pattern which matches the ~username part (e.g. /~root in
  86. * git://host.xyz/~root/a.git) of URIs. Defines no capturing group.
  87. */
  88. private static final String USER_HOME_P = "(?:/~(?:[^/]+))";
  89. /**
  90. * Part of a pattern which matches the optional drive letter in paths (e.g.
  91. * D: in file:///D:/a.txt). Defines no capturing group.
  92. */
  93. private static final String OPT_DRIVE_LETTER_P = "(?:[A-Za-z]:)?";
  94. /**
  95. * Part of a pattern which matches a relative path. Relative paths don't
  96. * start with slash or drive letters. Defines no capturing group.
  97. */
  98. private static final String RELATIVE_PATH_P = "(?:(?:[^/]+/)*[^/]+/?)";
  99. /**
  100. * Part of a pattern which matches a relative or absolute path. Defines no
  101. * capturing group.
  102. */
  103. private static final String PATH_P = "(" + OPT_DRIVE_LETTER_P + "/?"
  104. + RELATIVE_PATH_P + ")";
  105. private static final long serialVersionUID = 1L;
  106. /**
  107. * A pattern matching standard URI: </br>
  108. * <code>scheme "://" user_password? hostname? portnumber? path</code>
  109. */
  110. private static final Pattern FULL_URI = Pattern.compile("^" //
  111. + SCHEME_P //
  112. + "(?:" // start a group containing hostname and all options only
  113. // availabe when a hostname is there
  114. + OPT_USER_PWD_P //
  115. + HOST_P //
  116. + OPT_PORT_P //
  117. + "(" // open a catpuring group the the user-home-dir part
  118. + (USER_HOME_P + "?") //
  119. + "/)" //
  120. + ")?" // close the optional group containing hostname
  121. + "(.+)?" //
  122. + "$");
  123. /**
  124. * A pattern matching the reference to a local file. This may be an absolute
  125. * path (maybe even containing windows drive-letters) or a relative path.
  126. */
  127. private static final Pattern LOCAL_FILE = Pattern.compile("^" //
  128. + "(/?" + PATH_P + ")" //
  129. + "$");
  130. /**
  131. * A pattern matching a URI for the scheme 'file' which has only ':/' as
  132. * separator between scheme and path. Standard file URIs have '://' as
  133. * separator, but java.io.File.toURI() constructs those URIs.
  134. */
  135. private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" //
  136. + "(file):(/(?!/)" //
  137. + PATH_P //
  138. + ")$");
  139. /**
  140. * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
  141. */
  142. private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" //
  143. + OPT_USER_PWD_P //
  144. + HOST_P //
  145. + ":(" //
  146. + ("(?:" + USER_HOME_P + "/)?") //
  147. + RELATIVE_PATH_P //
  148. + ")$");
  149. /**
  150. * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
  151. */
  152. private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" //
  153. + OPT_USER_PWD_P //
  154. + "([^/:]{2,})" //
  155. + ":(" //
  156. + "/" + RELATIVE_PATH_P //
  157. + ")$");
  158. private String scheme;
  159. private String path;
  160. private String user;
  161. private String pass;
  162. private int port = -1;
  163. private String host;
  164. /**
  165. * Parse and construct an {@link URIish} from a string
  166. *
  167. * @param s
  168. * @throws URISyntaxException
  169. */
  170. public URIish(String s) throws URISyntaxException {
  171. s = s.replace('\\', '/');
  172. Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
  173. if (matcher.matches()) {
  174. scheme = matcher.group(1);
  175. path = cleanLeadingSlashes(matcher.group(2), scheme);
  176. } else {
  177. matcher = FULL_URI.matcher(s);
  178. if (matcher.matches()) {
  179. scheme = matcher.group(1);
  180. user = matcher.group(2);
  181. pass = matcher.group(3);
  182. host = matcher.group(4);
  183. if (matcher.group(5) != null)
  184. port = Integer.parseInt(matcher.group(5));
  185. path = cleanLeadingSlashes(
  186. n2e(matcher.group(6)) + n2e(matcher.group(7)),
  187. scheme);
  188. } else {
  189. matcher = RELATIVE_SCP_URI.matcher(s);
  190. if (matcher.matches()) {
  191. user = matcher.group(1);
  192. pass = matcher.group(2);
  193. host = matcher.group(3);
  194. path = matcher.group(4);
  195. } else {
  196. matcher = ABSOLUTE_SCP_URI.matcher(s);
  197. if (matcher.matches()) {
  198. user = matcher.group(1);
  199. pass = matcher.group(2);
  200. host = matcher.group(3);
  201. path = matcher.group(4);
  202. } else {
  203. matcher = LOCAL_FILE.matcher(s);
  204. if (matcher.matches()) {
  205. path = matcher.group(1);
  206. } else
  207. throw new URISyntaxException(s,
  208. JGitText.get().cannotParseGitURIish);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. private String n2e(String s) {
  215. if (s == null)
  216. return "";
  217. else
  218. return s;
  219. }
  220. // takes care to cut of a leading slash if a windows drive letter or a
  221. // user-home-dir specifications are
  222. private String cleanLeadingSlashes(String p, String s) {
  223. if (p.length() >= 3
  224. && p.charAt(0) == '/'
  225. && p.charAt(2) == ':'
  226. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  227. && p.charAt(1) <= 'z'))
  228. return p.substring(1);
  229. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  230. && p.charAt(1) == '~')
  231. return p.substring(1);
  232. else
  233. return p;
  234. }
  235. /**
  236. * Construct a URIish from a standard URL.
  237. *
  238. * @param u
  239. * the source URL to convert from.
  240. */
  241. public URIish(final URL u) {
  242. scheme = u.getProtocol();
  243. path = u.getPath();
  244. final String ui = u.getUserInfo();
  245. if (ui != null) {
  246. final int d = ui.indexOf(':');
  247. user = d < 0 ? ui : ui.substring(0, d);
  248. pass = d < 0 ? null : ui.substring(d + 1);
  249. }
  250. port = u.getPort();
  251. host = u.getHost();
  252. }
  253. /** Create an empty, non-configured URI. */
  254. public URIish() {
  255. // Configure nothing.
  256. }
  257. private URIish(final URIish u) {
  258. this.scheme = u.scheme;
  259. this.path = u.path;
  260. this.user = u.user;
  261. this.pass = u.pass;
  262. this.port = u.port;
  263. this.host = u.host;
  264. }
  265. /**
  266. * @return true if this URI references a repository on another system.
  267. */
  268. public boolean isRemote() {
  269. return getHost() != null;
  270. }
  271. /**
  272. * @return host name part or null
  273. */
  274. public String getHost() {
  275. return host;
  276. }
  277. /**
  278. * Return a new URI matching this one, but with a different host.
  279. *
  280. * @param n
  281. * the new value for host.
  282. * @return a new URI with the updated value.
  283. */
  284. public URIish setHost(final String n) {
  285. final URIish r = new URIish(this);
  286. r.host = n;
  287. return r;
  288. }
  289. /**
  290. * @return protocol name or null for local references
  291. */
  292. public String getScheme() {
  293. return scheme;
  294. }
  295. /**
  296. * Return a new URI matching this one, but with a different scheme.
  297. *
  298. * @param n
  299. * the new value for scheme.
  300. * @return a new URI with the updated value.
  301. */
  302. public URIish setScheme(final String n) {
  303. final URIish r = new URIish(this);
  304. r.scheme = n;
  305. return r;
  306. }
  307. /**
  308. * @return path name component
  309. */
  310. public String getPath() {
  311. return path;
  312. }
  313. /**
  314. * Return a new URI matching this one, but with a different path.
  315. *
  316. * @param n
  317. * the new value for path.
  318. * @return a new URI with the updated value.
  319. */
  320. public URIish setPath(final String n) {
  321. final URIish r = new URIish(this);
  322. r.path = n;
  323. return r;
  324. }
  325. /**
  326. * @return user name requested for transfer or null
  327. */
  328. public String getUser() {
  329. return user;
  330. }
  331. /**
  332. * Return a new URI matching this one, but with a different user.
  333. *
  334. * @param n
  335. * the new value for user.
  336. * @return a new URI with the updated value.
  337. */
  338. public URIish setUser(final String n) {
  339. final URIish r = new URIish(this);
  340. r.user = n;
  341. return r;
  342. }
  343. /**
  344. * @return password requested for transfer or null
  345. */
  346. public String getPass() {
  347. return pass;
  348. }
  349. /**
  350. * Return a new URI matching this one, but with a different password.
  351. *
  352. * @param n
  353. * the new value for password.
  354. * @return a new URI with the updated value.
  355. */
  356. public URIish setPass(final String n) {
  357. final URIish r = new URIish(this);
  358. r.pass = n;
  359. return r;
  360. }
  361. /**
  362. * @return port number requested for transfer or -1 if not explicit
  363. */
  364. public int getPort() {
  365. return port;
  366. }
  367. /**
  368. * Return a new URI matching this one, but with a different port.
  369. *
  370. * @param n
  371. * the new value for port.
  372. * @return a new URI with the updated value.
  373. */
  374. public URIish setPort(final int n) {
  375. final URIish r = new URIish(this);
  376. r.port = n > 0 ? n : -1;
  377. return r;
  378. }
  379. public int hashCode() {
  380. int hc = 0;
  381. if (getScheme() != null)
  382. hc = hc * 31 + getScheme().hashCode();
  383. if (getUser() != null)
  384. hc = hc * 31 + getUser().hashCode();
  385. if (getPass() != null)
  386. hc = hc * 31 + getPass().hashCode();
  387. if (getHost() != null)
  388. hc = hc * 31 + getHost().hashCode();
  389. if (getPort() > 0)
  390. hc = hc * 31 + getPort();
  391. if (getPath() != null)
  392. hc = hc * 31 + getPath().hashCode();
  393. return hc;
  394. }
  395. public boolean equals(final Object obj) {
  396. if (!(obj instanceof URIish))
  397. return false;
  398. final URIish b = (URIish) obj;
  399. if (!eq(getScheme(), b.getScheme()))
  400. return false;
  401. if (!eq(getUser(), b.getUser()))
  402. return false;
  403. if (!eq(getPass(), b.getPass()))
  404. return false;
  405. if (!eq(getHost(), b.getHost()))
  406. return false;
  407. if (getPort() != b.getPort())
  408. return false;
  409. if (!eq(getPath(), b.getPath()))
  410. return false;
  411. return true;
  412. }
  413. private static boolean eq(final String a, final String b) {
  414. if (a == b)
  415. return true;
  416. if (a == null || b == null)
  417. return false;
  418. return a.equals(b);
  419. }
  420. /**
  421. * Obtain the string form of the URI, with the password included.
  422. *
  423. * @return the URI, including its password field, if any.
  424. */
  425. public String toPrivateString() {
  426. return format(true);
  427. }
  428. public String toString() {
  429. return format(false);
  430. }
  431. private String format(final boolean includePassword) {
  432. final StringBuilder r = new StringBuilder();
  433. if (getScheme() != null) {
  434. r.append(getScheme());
  435. r.append("://");
  436. }
  437. if (getUser() != null) {
  438. r.append(getUser());
  439. if (includePassword && getPass() != null) {
  440. r.append(':');
  441. r.append(getPass());
  442. }
  443. }
  444. if (getHost() != null) {
  445. if (getUser() != null)
  446. r.append('@');
  447. r.append(getHost());
  448. if (getScheme() != null && getPort() > 0) {
  449. r.append(':');
  450. r.append(getPort());
  451. }
  452. }
  453. if (getPath() != null) {
  454. if (getScheme() != null) {
  455. if (!getPath().startsWith("/"))
  456. r.append('/');
  457. } else if (getHost() != null)
  458. r.append(':');
  459. r.append(getPath());
  460. }
  461. return r.toString();
  462. }
  463. /**
  464. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  465. * for a full path:
  466. * <table>
  467. * <tr>
  468. * <th>Path</th>
  469. * <th>Humanish part</th>
  470. * </tr>
  471. * <tr>
  472. * <td><code>/path/to/repo.git</code></td>
  473. * <td rowspan="4"><code>repo</code></td>
  474. * </tr>
  475. * <tr>
  476. * <td><code>/path/to/repo.git/</code></td>
  477. * </tr>
  478. * <tr>
  479. * <td><code>/path/to/repo/.git</code></td>
  480. * </tr>
  481. * <tr>
  482. * <td><code>/path/to/repo/</code></td>
  483. * </tr>
  484. * <tr>
  485. * <td><code>/path//to</code></td>
  486. * <td>an empty string</td>
  487. * </tr>
  488. * </table>
  489. *
  490. * @return the "humanish" part of the path. May be an empty string. Never
  491. * {@code null}.
  492. * @throws IllegalArgumentException
  493. * if it's impossible to determine a humanish part, or path is
  494. * {@code null} or empty
  495. * @see #getPath
  496. */
  497. public String getHumanishName() throws IllegalArgumentException {
  498. if ("".equals(getPath()) || getPath() == null)
  499. throw new IllegalArgumentException();
  500. String[] elements = getPath().split("/");
  501. if (elements.length == 0)
  502. throw new IllegalArgumentException();
  503. String result = elements[elements.length - 1];
  504. if (Constants.DOT_GIT.equals(result))
  505. result = elements[elements.length - 2];
  506. else if (result.endsWith(Constants.DOT_GIT_EXT))
  507. result = result.substring(0, result.length()
  508. - Constants.DOT_GIT_EXT.length());
  509. return result;
  510. }
  511. }