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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
  7. * Copyright (C) 2015, Patrick Steinhardt <ps@pks.im>
  8. * and other copyright owners as documented in the project's IP log.
  9. *
  10. * This program and the accompanying materials are made available
  11. * under the terms of the Eclipse Distribution License v1.0 which
  12. * accompanies this distribution, is reproduced below, and is
  13. * available at http://www.eclipse.org/org/documents/edl-v10.php
  14. *
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or
  18. * without modification, are permitted provided that the following
  19. * conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials provided
  27. * with the distribution.
  28. *
  29. * - Neither the name of the Eclipse Foundation, Inc. nor the
  30. * names of its contributors may be used to endorse or promote
  31. * products derived from this software without specific prior
  32. * written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  35. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  36. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  39. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  46. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. package org.eclipse.jgit.transport;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.Serializable;
  52. import java.io.UnsupportedEncodingException;
  53. import java.net.URISyntaxException;
  54. import java.net.URL;
  55. import java.util.BitSet;
  56. import java.util.regex.Matcher;
  57. import java.util.regex.Pattern;
  58. import org.eclipse.jgit.internal.JGitText;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.util.RawParseUtils;
  61. import org.eclipse.jgit.util.StringUtils;
  62. /**
  63. * This URI like construct used for referencing Git archives over the net, as
  64. * well as locally stored archives. It is similar to RFC 2396 URI's, but also
  65. * support SCP and the malformed file://&lt;path&gt; syntax (as opposed to the correct
  66. * file:&lt;path&gt; syntax.
  67. */
  68. public class URIish implements Serializable {
  69. /**
  70. * Part of a pattern which matches the scheme part (git, http, ...) of an
  71. * URI. Defines one capturing group containing the scheme without the
  72. * trailing colon and slashes
  73. */
  74. private static final String SCHEME_P = "([a-z][a-z0-9+-]+)://"; //$NON-NLS-1$
  75. /**
  76. * Part of a pattern which matches the optional user/password part (e.g.
  77. * root:pwd@ in git://root:pwd@host.xyz/a.git) of URIs. Defines two
  78. * capturing groups: the first containing the user and the second containing
  79. * the password
  80. */
  81. private static final String OPT_USER_PWD_P = "(?:([^/:@]+)(?::([^\\\\/]+))?@)?"; //$NON-NLS-1$
  82. /**
  83. * Part of a pattern which matches the host part of URIs. Defines one
  84. * capturing group containing the host name.
  85. */
  86. private static final String HOST_P = "((?:[^\\\\/:]+)|(?:\\[[0-9a-f:]+\\]))"; //$NON-NLS-1$
  87. /**
  88. * Part of a pattern which matches the optional port part of URIs. Defines
  89. * one capturing group containing the port without the preceding colon.
  90. */
  91. private static final String OPT_PORT_P = "(?::(\\d+))?"; //$NON-NLS-1$
  92. /**
  93. * Part of a pattern which matches the ~username part (e.g. /~root in
  94. * git://host.xyz/~root/a.git) of URIs. Defines no capturing group.
  95. */
  96. private static final String USER_HOME_P = "(?:/~(?:[^\\\\/]+))"; //$NON-NLS-1$
  97. /**
  98. * Part of a pattern which matches the optional drive letter in paths (e.g.
  99. * D: in file:///D:/a.txt). Defines no capturing group.
  100. */
  101. private static final String OPT_DRIVE_LETTER_P = "(?:[A-Za-z]:)?"; //$NON-NLS-1$
  102. /**
  103. * Part of a pattern which matches a relative path. Relative paths don't
  104. * start with slash or drive letters. Defines no capturing group.
  105. */
  106. private static final String RELATIVE_PATH_P = "(?:(?:[^\\\\/]+[\\\\/]+)*[^\\\\/]+[\\\\/]*)"; //$NON-NLS-1$
  107. /**
  108. * Part of a pattern which matches a relative or absolute path. Defines no
  109. * capturing group.
  110. */
  111. private static final String PATH_P = "(" + OPT_DRIVE_LETTER_P + "[\\\\/]?" //$NON-NLS-1$ //$NON-NLS-2$
  112. + RELATIVE_PATH_P + ")"; //$NON-NLS-1$
  113. private static final long serialVersionUID = 1L;
  114. /**
  115. * A pattern matching standard URI: </br>
  116. * <code>scheme "://" user_password? hostname? portnumber? path</code>
  117. */
  118. private static final Pattern FULL_URI = Pattern.compile("^" // //$NON-NLS-1$
  119. + SCHEME_P //
  120. + "(?:" // start a group containing hostname and all options only //$NON-NLS-1$
  121. // availabe when a hostname is there
  122. + OPT_USER_PWD_P //
  123. + HOST_P //
  124. + OPT_PORT_P //
  125. + "(" // open a group capturing the user-home-dir-part //$NON-NLS-1$
  126. + (USER_HOME_P + "?") //$NON-NLS-1$
  127. + "[\\\\/])" //$NON-NLS-1$
  128. + ")?" // close the optional group containing hostname //$NON-NLS-1$
  129. + "(.+)?" //$NON-NLS-1$
  130. + "$"); //$NON-NLS-1$
  131. /**
  132. * A pattern matching the reference to a local file. This may be an absolute
  133. * path (maybe even containing windows drive-letters) or a relative path.
  134. */
  135. private static final Pattern LOCAL_FILE = Pattern.compile("^" // //$NON-NLS-1$
  136. + "([\\\\/]?" + PATH_P + ")" // //$NON-NLS-1$ //$NON-NLS-2$
  137. + "$"); //$NON-NLS-1$
  138. /**
  139. * A pattern matching a URI for the scheme 'file' which has only ':/' as
  140. * separator between scheme and path. Standard file URIs have '://' as
  141. * separator, but java.io.File.toURI() constructs those URIs.
  142. */
  143. private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" // //$NON-NLS-1$
  144. + "(file):([\\\\/](?![\\\\/])" // //$NON-NLS-1$
  145. + PATH_P //
  146. + ")$"); //$NON-NLS-1$
  147. /**
  148. * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
  149. */
  150. private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  151. + OPT_USER_PWD_P //
  152. + HOST_P //
  153. + ":(" // //$NON-NLS-1$
  154. + ("(?:" + USER_HOME_P + "[\\\\/])?") // //$NON-NLS-1$ //$NON-NLS-2$
  155. + RELATIVE_PATH_P //
  156. + ")$"); //$NON-NLS-1$
  157. /**
  158. * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
  159. */
  160. private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  161. + OPT_USER_PWD_P //
  162. + "([^\\\\/:]{2,})" // //$NON-NLS-1$
  163. + ":(" // //$NON-NLS-1$
  164. + "[\\\\/]" + RELATIVE_PATH_P // //$NON-NLS-1$
  165. + ")$"); //$NON-NLS-1$
  166. private String scheme;
  167. private String path;
  168. private String rawPath;
  169. private String user;
  170. private String pass;
  171. private int port = -1;
  172. private String host;
  173. /**
  174. * Parse and construct an {@link URIish} from a string
  175. *
  176. * @param s
  177. * @throws URISyntaxException
  178. */
  179. public URIish(String s) throws URISyntaxException {
  180. if (StringUtils.isEmptyOrNull(s)) {
  181. throw new URISyntaxException("The uri was empty or null", //$NON-NLS-1$
  182. JGitText.get().cannotParseGitURIish);
  183. }
  184. Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
  185. if (matcher.matches()) {
  186. scheme = matcher.group(1);
  187. rawPath = cleanLeadingSlashes(matcher.group(2), scheme);
  188. path = unescape(rawPath);
  189. return;
  190. }
  191. matcher = FULL_URI.matcher(s);
  192. if (matcher.matches()) {
  193. scheme = matcher.group(1);
  194. user = unescape(matcher.group(2));
  195. pass = unescape(matcher.group(3));
  196. host = unescape(matcher.group(4));
  197. if (matcher.group(5) != null)
  198. port = Integer.parseInt(matcher.group(5));
  199. rawPath = cleanLeadingSlashes(
  200. n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
  201. path = unescape(rawPath);
  202. return;
  203. }
  204. matcher = RELATIVE_SCP_URI.matcher(s);
  205. if (matcher.matches()) {
  206. user = matcher.group(1);
  207. pass = matcher.group(2);
  208. host = matcher.group(3);
  209. rawPath = matcher.group(4);
  210. path = rawPath;
  211. return;
  212. }
  213. matcher = ABSOLUTE_SCP_URI.matcher(s);
  214. if (matcher.matches()) {
  215. user = matcher.group(1);
  216. pass = matcher.group(2);
  217. host = matcher.group(3);
  218. rawPath = matcher.group(4);
  219. path = rawPath;
  220. return;
  221. }
  222. matcher = LOCAL_FILE.matcher(s);
  223. if (matcher.matches()) {
  224. rawPath = matcher.group(1);
  225. path = rawPath;
  226. return;
  227. }
  228. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  229. }
  230. private static int parseHexByte(byte c1, byte c2) {
  231. return ((RawParseUtils.parseHexInt4(c1) << 4)
  232. | RawParseUtils.parseHexInt4(c2));
  233. }
  234. private static String unescape(String s) throws URISyntaxException {
  235. if (s == null)
  236. return null;
  237. if (s.indexOf('%') < 0)
  238. return s;
  239. byte[] bytes;
  240. try {
  241. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  242. } catch (UnsupportedEncodingException e) {
  243. throw new RuntimeException(e); // can't happen
  244. }
  245. byte[] os = new byte[bytes.length];
  246. int j = 0;
  247. for (int i = 0; i < bytes.length; ++i) {
  248. byte c = bytes[i];
  249. if (c == '%') {
  250. if (i + 2 >= bytes.length)
  251. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  252. byte c1 = bytes[i + 1];
  253. byte c2 = bytes[i + 2];
  254. int val;
  255. try {
  256. val = parseHexByte(c1, c2);
  257. } catch (ArrayIndexOutOfBoundsException e) {
  258. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  259. }
  260. os[j++] = (byte) val;
  261. i += 2;
  262. } else
  263. os[j++] = c;
  264. }
  265. return RawParseUtils.decode(os, 0, j);
  266. }
  267. private static final BitSet reservedChars = new BitSet(127);
  268. static {
  269. for (byte b : Constants.encodeASCII("!*'();:@&=+$,/?#[]")) //$NON-NLS-1$
  270. reservedChars.set(b);
  271. }
  272. /**
  273. * Escape unprintable characters optionally URI-reserved characters
  274. *
  275. * @param s
  276. * The Java String to encode (may contain any character)
  277. * @param escapeReservedChars
  278. * true to escape URI reserved characters
  279. * @param encodeNonAscii
  280. * encode any non-ASCII characters
  281. * @return a URI-encoded string
  282. */
  283. private static String escape(String s, boolean escapeReservedChars,
  284. boolean encodeNonAscii) {
  285. if (s == null)
  286. return null;
  287. ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
  288. byte[] bytes;
  289. try {
  290. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  291. } catch (UnsupportedEncodingException e) {
  292. throw new RuntimeException(e); // cannot happen
  293. }
  294. for (int i = 0; i < bytes.length; ++i) {
  295. int b = bytes[i] & 0xFF;
  296. if (b <= 32 || (encodeNonAscii && b > 127) || b == '%'
  297. || (escapeReservedChars && reservedChars.get(b))) {
  298. os.write('%');
  299. byte[] tmp = Constants.encodeASCII(String.format("%02x", //$NON-NLS-1$
  300. Integer.valueOf(b)));
  301. os.write(tmp[0]);
  302. os.write(tmp[1]);
  303. } else {
  304. os.write(b);
  305. }
  306. }
  307. byte[] buf = os.toByteArray();
  308. return RawParseUtils.decode(buf, 0, buf.length);
  309. }
  310. private String n2e(String s) {
  311. if (s == null)
  312. return ""; //$NON-NLS-1$
  313. else
  314. return s;
  315. }
  316. // takes care to cut of a leading slash if a windows drive letter or a
  317. // user-home-dir specifications are
  318. private String cleanLeadingSlashes(String p, String s) {
  319. if (p.length() >= 3
  320. && p.charAt(0) == '/'
  321. && p.charAt(2) == ':'
  322. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  323. && p.charAt(1) <= 'z'))
  324. return p.substring(1);
  325. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  326. && p.charAt(1) == '~')
  327. return p.substring(1);
  328. else
  329. return p;
  330. }
  331. /**
  332. * Construct a URIish from a standard URL.
  333. *
  334. * @param u
  335. * the source URL to convert from.
  336. */
  337. public URIish(final URL u) {
  338. scheme = u.getProtocol();
  339. path = u.getPath();
  340. try {
  341. rawPath = u.toURI().getRawPath();
  342. } catch (URISyntaxException e) {
  343. throw new RuntimeException(e); // Impossible
  344. }
  345. final String ui = u.getUserInfo();
  346. if (ui != null) {
  347. final int d = ui.indexOf(':');
  348. user = d < 0 ? ui : ui.substring(0, d);
  349. pass = d < 0 ? null : ui.substring(d + 1);
  350. }
  351. port = u.getPort();
  352. host = u.getHost();
  353. }
  354. /** Create an empty, non-configured URI. */
  355. public URIish() {
  356. // Configure nothing.
  357. }
  358. private URIish(final URIish u) {
  359. this.scheme = u.scheme;
  360. this.rawPath = u.rawPath;
  361. this.path = u.path;
  362. this.user = u.user;
  363. this.pass = u.pass;
  364. this.port = u.port;
  365. this.host = u.host;
  366. }
  367. /**
  368. * @return true if this URI references a repository on another system.
  369. */
  370. public boolean isRemote() {
  371. return getHost() != null;
  372. }
  373. /**
  374. * @return host name part or null
  375. */
  376. public String getHost() {
  377. return host;
  378. }
  379. /**
  380. * Return a new URI matching this one, but with a different host.
  381. *
  382. * @param n
  383. * the new value for host.
  384. * @return a new URI with the updated value.
  385. */
  386. public URIish setHost(final String n) {
  387. final URIish r = new URIish(this);
  388. r.host = n;
  389. return r;
  390. }
  391. /**
  392. * @return protocol name or null for local references
  393. */
  394. public String getScheme() {
  395. return scheme;
  396. }
  397. /**
  398. * Return a new URI matching this one, but with a different scheme.
  399. *
  400. * @param n
  401. * the new value for scheme.
  402. * @return a new URI with the updated value.
  403. */
  404. public URIish setScheme(final String n) {
  405. final URIish r = new URIish(this);
  406. r.scheme = n;
  407. return r;
  408. }
  409. /**
  410. * @return path name component
  411. */
  412. public String getPath() {
  413. return path;
  414. }
  415. /**
  416. * @return path name component
  417. */
  418. public String getRawPath() {
  419. return rawPath;
  420. }
  421. /**
  422. * Return a new URI matching this one, but with a different path.
  423. *
  424. * @param n
  425. * the new value for path.
  426. * @return a new URI with the updated value.
  427. */
  428. public URIish setPath(final String n) {
  429. final URIish r = new URIish(this);
  430. r.path = n;
  431. r.rawPath = n;
  432. return r;
  433. }
  434. /**
  435. * Return a new URI matching this one, but with a different (raw) path.
  436. *
  437. * @param n
  438. * the new value for path.
  439. * @return a new URI with the updated value.
  440. * @throws URISyntaxException
  441. */
  442. public URIish setRawPath(final String n) throws URISyntaxException {
  443. final URIish r = new URIish(this);
  444. r.path = unescape(n);
  445. r.rawPath = n;
  446. return r;
  447. }
  448. /**
  449. * @return user name requested for transfer or null
  450. */
  451. public String getUser() {
  452. return user;
  453. }
  454. /**
  455. * Return a new URI matching this one, but with a different user.
  456. *
  457. * @param n
  458. * the new value for user.
  459. * @return a new URI with the updated value.
  460. */
  461. public URIish setUser(final String n) {
  462. final URIish r = new URIish(this);
  463. r.user = n;
  464. return r;
  465. }
  466. /**
  467. * @return password requested for transfer or null
  468. */
  469. public String getPass() {
  470. return pass;
  471. }
  472. /**
  473. * Return a new URI matching this one, but with a different password.
  474. *
  475. * @param n
  476. * the new value for password.
  477. * @return a new URI with the updated value.
  478. */
  479. public URIish setPass(final String n) {
  480. final URIish r = new URIish(this);
  481. r.pass = n;
  482. return r;
  483. }
  484. /**
  485. * @return port number requested for transfer or -1 if not explicit
  486. */
  487. public int getPort() {
  488. return port;
  489. }
  490. /**
  491. * Return a new URI matching this one, but with a different port.
  492. *
  493. * @param n
  494. * the new value for port.
  495. * @return a new URI with the updated value.
  496. */
  497. public URIish setPort(final int n) {
  498. final URIish r = new URIish(this);
  499. r.port = n > 0 ? n : -1;
  500. return r;
  501. }
  502. public int hashCode() {
  503. int hc = 0;
  504. if (getScheme() != null)
  505. hc = hc * 31 + getScheme().hashCode();
  506. if (getUser() != null)
  507. hc = hc * 31 + getUser().hashCode();
  508. if (getPass() != null)
  509. hc = hc * 31 + getPass().hashCode();
  510. if (getHost() != null)
  511. hc = hc * 31 + getHost().hashCode();
  512. if (getPort() > 0)
  513. hc = hc * 31 + getPort();
  514. if (getPath() != null)
  515. hc = hc * 31 + getPath().hashCode();
  516. return hc;
  517. }
  518. public boolean equals(final Object obj) {
  519. if (!(obj instanceof URIish))
  520. return false;
  521. final URIish b = (URIish) obj;
  522. if (!eq(getScheme(), b.getScheme()))
  523. return false;
  524. if (!eq(getUser(), b.getUser()))
  525. return false;
  526. if (!eq(getPass(), b.getPass()))
  527. return false;
  528. if (!eq(getHost(), b.getHost()))
  529. return false;
  530. if (getPort() != b.getPort())
  531. return false;
  532. if (!eq(getPath(), b.getPath()))
  533. return false;
  534. return true;
  535. }
  536. private static boolean eq(final String a, final String b) {
  537. if (a == b)
  538. return true;
  539. if (a == null || b == null)
  540. return false;
  541. return a.equals(b);
  542. }
  543. /**
  544. * Obtain the string form of the URI, with the password included.
  545. *
  546. * @return the URI, including its password field, if any.
  547. */
  548. public String toPrivateString() {
  549. return format(true, false);
  550. }
  551. public String toString() {
  552. return format(false, false);
  553. }
  554. private String format(final boolean includePassword, boolean escapeNonAscii) {
  555. final StringBuilder r = new StringBuilder();
  556. if (getScheme() != null) {
  557. r.append(getScheme());
  558. r.append("://"); //$NON-NLS-1$
  559. }
  560. if (getUser() != null) {
  561. r.append(escape(getUser(), true, escapeNonAscii));
  562. if (includePassword && getPass() != null) {
  563. r.append(':');
  564. r.append(escape(getPass(), true, escapeNonAscii));
  565. }
  566. }
  567. if (getHost() != null) {
  568. if (getUser() != null && getUser().length() > 0)
  569. r.append('@');
  570. r.append(escape(getHost(), false, escapeNonAscii));
  571. if (getScheme() != null && getPort() > 0) {
  572. r.append(':');
  573. r.append(getPort());
  574. }
  575. }
  576. if (getPath() != null) {
  577. if (getScheme() != null) {
  578. if (!getPath().startsWith("/")) //$NON-NLS-1$
  579. r.append('/');
  580. } else if (getHost() != null)
  581. r.append(':');
  582. if (getScheme() != null)
  583. if (escapeNonAscii)
  584. r.append(escape(getPath(), false, escapeNonAscii));
  585. else
  586. r.append(getRawPath());
  587. else
  588. r.append(getPath());
  589. }
  590. return r.toString();
  591. }
  592. /**
  593. * @return the URI as an ASCII string. Password is not included.
  594. */
  595. public String toASCIIString() {
  596. return format(false, true);
  597. }
  598. /**
  599. * @return the URI including password, formatted with only ASCII characters
  600. * such that it will be valid for use over the network.
  601. */
  602. public String toPrivateASCIIString() {
  603. return format(true, true);
  604. }
  605. /**
  606. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  607. * for a full path:
  608. * <table summary="path vs humanish path" border="1">
  609. * <tr>
  610. * <th>Path</th>
  611. * <th>Humanish part</th>
  612. * </tr>
  613. * <tr>
  614. * <td><code>/path/to/repo.git</code></td>
  615. * <td rowspan="4"><code>repo</code></td>
  616. * </tr>
  617. * <tr>
  618. * <td><code>/path/to/repo.git/</code></td>
  619. * </tr>
  620. * <tr>
  621. * <td><code>/path/to/repo/.git</code></td>
  622. * </tr>
  623. * <tr>
  624. * <td><code>/path/to/repo/</code></td>
  625. * </tr>
  626. * <tr>
  627. * <td><code>localhost</code></td>
  628. * <td><code>ssh://localhost/</code></td>
  629. * </tr>
  630. * <tr>
  631. * <td><code>/path//to</code></td>
  632. * <td>an empty string</td>
  633. * </tr>
  634. * </table>
  635. *
  636. * @return the "humanish" part of the path. May be an empty string. Never
  637. * {@code null}.
  638. * @throws IllegalArgumentException
  639. * if it's impossible to determine a humanish part, or path is
  640. * {@code null} or empty
  641. * @see #getPath
  642. */
  643. public String getHumanishName() throws IllegalArgumentException {
  644. String s = getPath();
  645. if ("/".equals(s)) //$NON-NLS-1$
  646. s = getHost();
  647. if ("".equals(s) || s == null) //$NON-NLS-1$
  648. throw new IllegalArgumentException();
  649. String[] elements;
  650. if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
  651. elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
  652. else
  653. elements = s.split("/+"); //$NON-NLS-1$
  654. if (elements.length == 0)
  655. throw new IllegalArgumentException();
  656. String result = elements[elements.length - 1];
  657. if (Constants.DOT_GIT.equals(result))
  658. result = elements[elements.length - 2];
  659. else if (result.endsWith(Constants.DOT_GIT_EXT))
  660. result = result.substring(0, result.length()
  661. - Constants.DOT_GIT_EXT.length());
  662. return result;
  663. }
  664. }