Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

URIish.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. + "(?:" // start non capturing group for host //$NON-NLS-1$
  128. // separator or end of line
  129. + "[\\\\/])|$" //$NON-NLS-1$
  130. + ")" // close non capturing group for the host//$NON-NLS-1$
  131. // separator or end of line
  132. + ")?" // close the optional group containing hostname //$NON-NLS-1$
  133. + "(.+)?" //$NON-NLS-1$
  134. + "$"); //$NON-NLS-1$
  135. /**
  136. * A pattern matching the reference to a local file. This may be an absolute
  137. * path (maybe even containing windows drive-letters) or a relative path.
  138. */
  139. private static final Pattern LOCAL_FILE = Pattern.compile("^" // //$NON-NLS-1$
  140. + "([\\\\/]?" + PATH_P + ")" // //$NON-NLS-1$ //$NON-NLS-2$
  141. + "$"); //$NON-NLS-1$
  142. /**
  143. * A pattern matching a URI for the scheme 'file' which has only ':/' as
  144. * separator between scheme and path. Standard file URIs have '://' as
  145. * separator, but java.io.File.toURI() constructs those URIs.
  146. */
  147. private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" // //$NON-NLS-1$
  148. + "(file):([\\\\/](?![\\\\/])" // //$NON-NLS-1$
  149. + PATH_P //
  150. + ")$"); //$NON-NLS-1$
  151. /**
  152. * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
  153. */
  154. private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  155. + OPT_USER_PWD_P //
  156. + HOST_P //
  157. + ":(" // //$NON-NLS-1$
  158. + ("(?:" + USER_HOME_P + "[\\\\/])?") // //$NON-NLS-1$ //$NON-NLS-2$
  159. + RELATIVE_PATH_P //
  160. + ")$"); //$NON-NLS-1$
  161. /**
  162. * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
  163. */
  164. private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  165. + OPT_USER_PWD_P //
  166. + "([^\\\\/:]{2,})" // //$NON-NLS-1$
  167. + ":(" // //$NON-NLS-1$
  168. + "[\\\\/]" + RELATIVE_PATH_P // //$NON-NLS-1$
  169. + ")$"); //$NON-NLS-1$
  170. private String scheme;
  171. private String path;
  172. private String rawPath;
  173. private String user;
  174. private String pass;
  175. private int port = -1;
  176. private String host;
  177. /**
  178. * Parse and construct an {@link URIish} from a string
  179. *
  180. * @param s
  181. * @throws URISyntaxException
  182. */
  183. public URIish(String s) throws URISyntaxException {
  184. if (StringUtils.isEmptyOrNull(s)) {
  185. throw new URISyntaxException("The uri was empty or null", //$NON-NLS-1$
  186. JGitText.get().cannotParseGitURIish);
  187. }
  188. Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
  189. if (matcher.matches()) {
  190. scheme = matcher.group(1);
  191. rawPath = cleanLeadingSlashes(matcher.group(2), scheme);
  192. path = unescape(rawPath);
  193. return;
  194. }
  195. matcher = FULL_URI.matcher(s);
  196. if (matcher.matches()) {
  197. scheme = matcher.group(1);
  198. user = unescape(matcher.group(2));
  199. pass = unescape(matcher.group(3));
  200. host = unescape(matcher.group(4));
  201. if (matcher.group(5) != null)
  202. port = Integer.parseInt(matcher.group(5));
  203. rawPath = cleanLeadingSlashes(
  204. n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
  205. path = unescape(rawPath);
  206. return;
  207. }
  208. matcher = RELATIVE_SCP_URI.matcher(s);
  209. if (matcher.matches()) {
  210. user = matcher.group(1);
  211. pass = matcher.group(2);
  212. host = matcher.group(3);
  213. rawPath = matcher.group(4);
  214. path = rawPath;
  215. return;
  216. }
  217. matcher = ABSOLUTE_SCP_URI.matcher(s);
  218. if (matcher.matches()) {
  219. user = matcher.group(1);
  220. pass = matcher.group(2);
  221. host = matcher.group(3);
  222. rawPath = matcher.group(4);
  223. path = rawPath;
  224. return;
  225. }
  226. matcher = LOCAL_FILE.matcher(s);
  227. if (matcher.matches()) {
  228. rawPath = matcher.group(1);
  229. path = rawPath;
  230. return;
  231. }
  232. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  233. }
  234. private static int parseHexByte(byte c1, byte c2) {
  235. return ((RawParseUtils.parseHexInt4(c1) << 4)
  236. | RawParseUtils.parseHexInt4(c2));
  237. }
  238. private static String unescape(String s) throws URISyntaxException {
  239. if (s == null)
  240. return null;
  241. if (s.indexOf('%') < 0)
  242. return s;
  243. byte[] bytes;
  244. try {
  245. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  246. } catch (UnsupportedEncodingException e) {
  247. throw new RuntimeException(e); // can't happen
  248. }
  249. byte[] os = new byte[bytes.length];
  250. int j = 0;
  251. for (int i = 0; i < bytes.length; ++i) {
  252. byte c = bytes[i];
  253. if (c == '%') {
  254. if (i + 2 >= bytes.length)
  255. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  256. byte c1 = bytes[i + 1];
  257. byte c2 = bytes[i + 2];
  258. int val;
  259. try {
  260. val = parseHexByte(c1, c2);
  261. } catch (ArrayIndexOutOfBoundsException e) {
  262. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  263. }
  264. os[j++] = (byte) val;
  265. i += 2;
  266. } else
  267. os[j++] = c;
  268. }
  269. return RawParseUtils.decode(os, 0, j);
  270. }
  271. private static final BitSet reservedChars = new BitSet(127);
  272. static {
  273. for (byte b : Constants.encodeASCII("!*'();:@&=+$,/?#[]")) //$NON-NLS-1$
  274. reservedChars.set(b);
  275. }
  276. /**
  277. * Escape unprintable characters optionally URI-reserved characters
  278. *
  279. * @param s
  280. * The Java String to encode (may contain any character)
  281. * @param escapeReservedChars
  282. * true to escape URI reserved characters
  283. * @param encodeNonAscii
  284. * encode any non-ASCII characters
  285. * @return a URI-encoded string
  286. */
  287. private static String escape(String s, boolean escapeReservedChars,
  288. boolean encodeNonAscii) {
  289. if (s == null)
  290. return null;
  291. ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
  292. byte[] bytes;
  293. try {
  294. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  295. } catch (UnsupportedEncodingException e) {
  296. throw new RuntimeException(e); // cannot happen
  297. }
  298. for (int i = 0; i < bytes.length; ++i) {
  299. int b = bytes[i] & 0xFF;
  300. if (b <= 32 || (encodeNonAscii && b > 127) || b == '%'
  301. || (escapeReservedChars && reservedChars.get(b))) {
  302. os.write('%');
  303. byte[] tmp = Constants.encodeASCII(String.format("%02x", //$NON-NLS-1$
  304. Integer.valueOf(b)));
  305. os.write(tmp[0]);
  306. os.write(tmp[1]);
  307. } else {
  308. os.write(b);
  309. }
  310. }
  311. byte[] buf = os.toByteArray();
  312. return RawParseUtils.decode(buf, 0, buf.length);
  313. }
  314. private String n2e(String s) {
  315. if (s == null)
  316. return ""; //$NON-NLS-1$
  317. else
  318. return s;
  319. }
  320. // takes care to cut of a leading slash if a windows drive letter or a
  321. // user-home-dir specifications are
  322. private String cleanLeadingSlashes(String p, String s) {
  323. if (p.length() >= 3
  324. && p.charAt(0) == '/'
  325. && p.charAt(2) == ':'
  326. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  327. && p.charAt(1) <= 'z'))
  328. return p.substring(1);
  329. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  330. && p.charAt(1) == '~')
  331. return p.substring(1);
  332. else
  333. return p;
  334. }
  335. /**
  336. * Construct a URIish from a standard URL.
  337. *
  338. * @param u
  339. * the source URL to convert from.
  340. */
  341. public URIish(final URL u) {
  342. scheme = u.getProtocol();
  343. path = u.getPath();
  344. path = cleanLeadingSlashes(path, scheme);
  345. try {
  346. rawPath = u.toURI().getRawPath();
  347. rawPath = cleanLeadingSlashes(rawPath, scheme);
  348. } catch (URISyntaxException e) {
  349. throw new RuntimeException(e); // Impossible
  350. }
  351. final String ui = u.getUserInfo();
  352. if (ui != null) {
  353. final int d = ui.indexOf(':');
  354. user = d < 0 ? ui : ui.substring(0, d);
  355. pass = d < 0 ? null : ui.substring(d + 1);
  356. }
  357. port = u.getPort();
  358. host = u.getHost();
  359. }
  360. /** Create an empty, non-configured URI. */
  361. public URIish() {
  362. // Configure nothing.
  363. }
  364. private URIish(final URIish u) {
  365. this.scheme = u.scheme;
  366. this.rawPath = u.rawPath;
  367. this.path = u.path;
  368. this.user = u.user;
  369. this.pass = u.pass;
  370. this.port = u.port;
  371. this.host = u.host;
  372. }
  373. /**
  374. * @return true if this URI references a repository on another system.
  375. */
  376. public boolean isRemote() {
  377. return getHost() != null;
  378. }
  379. /**
  380. * @return host name part or null
  381. */
  382. public String getHost() {
  383. return host;
  384. }
  385. /**
  386. * Return a new URI matching this one, but with a different host.
  387. *
  388. * @param n
  389. * the new value for host.
  390. * @return a new URI with the updated value.
  391. */
  392. public URIish setHost(final String n) {
  393. final URIish r = new URIish(this);
  394. r.host = n;
  395. return r;
  396. }
  397. /**
  398. * @return protocol name or null for local references
  399. */
  400. public String getScheme() {
  401. return scheme;
  402. }
  403. /**
  404. * Return a new URI matching this one, but with a different scheme.
  405. *
  406. * @param n
  407. * the new value for scheme.
  408. * @return a new URI with the updated value.
  409. */
  410. public URIish setScheme(final String n) {
  411. final URIish r = new URIish(this);
  412. r.scheme = n;
  413. return r;
  414. }
  415. /**
  416. * @return path name component
  417. */
  418. public String getPath() {
  419. return path;
  420. }
  421. /**
  422. * @return path name component
  423. */
  424. public String getRawPath() {
  425. return rawPath;
  426. }
  427. /**
  428. * Return a new URI matching this one, but with a different path.
  429. *
  430. * @param n
  431. * the new value for path.
  432. * @return a new URI with the updated value.
  433. */
  434. public URIish setPath(final String n) {
  435. final URIish r = new URIish(this);
  436. r.path = n;
  437. r.rawPath = n;
  438. return r;
  439. }
  440. /**
  441. * Return a new URI matching this one, but with a different (raw) path.
  442. *
  443. * @param n
  444. * the new value for path.
  445. * @return a new URI with the updated value.
  446. * @throws URISyntaxException
  447. */
  448. public URIish setRawPath(final String n) throws URISyntaxException {
  449. final URIish r = new URIish(this);
  450. r.path = unescape(n);
  451. r.rawPath = n;
  452. return r;
  453. }
  454. /**
  455. * @return user name requested for transfer or null
  456. */
  457. public String getUser() {
  458. return user;
  459. }
  460. /**
  461. * Return a new URI matching this one, but with a different user.
  462. *
  463. * @param n
  464. * the new value for user.
  465. * @return a new URI with the updated value.
  466. */
  467. public URIish setUser(final String n) {
  468. final URIish r = new URIish(this);
  469. r.user = n;
  470. return r;
  471. }
  472. /**
  473. * @return password requested for transfer or null
  474. */
  475. public String getPass() {
  476. return pass;
  477. }
  478. /**
  479. * Return a new URI matching this one, but with a different password.
  480. *
  481. * @param n
  482. * the new value for password.
  483. * @return a new URI with the updated value.
  484. */
  485. public URIish setPass(final String n) {
  486. final URIish r = new URIish(this);
  487. r.pass = n;
  488. return r;
  489. }
  490. /**
  491. * @return port number requested for transfer or -1 if not explicit
  492. */
  493. public int getPort() {
  494. return port;
  495. }
  496. /**
  497. * Return a new URI matching this one, but with a different port.
  498. *
  499. * @param n
  500. * the new value for port.
  501. * @return a new URI with the updated value.
  502. */
  503. public URIish setPort(final int n) {
  504. final URIish r = new URIish(this);
  505. r.port = n > 0 ? n : -1;
  506. return r;
  507. }
  508. public int hashCode() {
  509. int hc = 0;
  510. if (getScheme() != null)
  511. hc = hc * 31 + getScheme().hashCode();
  512. if (getUser() != null)
  513. hc = hc * 31 + getUser().hashCode();
  514. if (getPass() != null)
  515. hc = hc * 31 + getPass().hashCode();
  516. if (getHost() != null)
  517. hc = hc * 31 + getHost().hashCode();
  518. if (getPort() > 0)
  519. hc = hc * 31 + getPort();
  520. if (getPath() != null)
  521. hc = hc * 31 + getPath().hashCode();
  522. return hc;
  523. }
  524. public boolean equals(final Object obj) {
  525. if (!(obj instanceof URIish))
  526. return false;
  527. final URIish b = (URIish) obj;
  528. if (!eq(getScheme(), b.getScheme()))
  529. return false;
  530. if (!eq(getUser(), b.getUser()))
  531. return false;
  532. if (!eq(getPass(), b.getPass()))
  533. return false;
  534. if (!eq(getHost(), b.getHost()))
  535. return false;
  536. if (getPort() != b.getPort())
  537. return false;
  538. if (!eq(getPath(), b.getPath()))
  539. return false;
  540. return true;
  541. }
  542. private static boolean eq(final String a, final String b) {
  543. if (a == b)
  544. return true;
  545. if (StringUtils.isEmptyOrNull(a) && StringUtils.isEmptyOrNull(b))
  546. return true;
  547. if (a == null || b == null)
  548. return false;
  549. return a.equals(b);
  550. }
  551. /**
  552. * Obtain the string form of the URI, with the password included.
  553. *
  554. * @return the URI, including its password field, if any.
  555. */
  556. public String toPrivateString() {
  557. return format(true, false);
  558. }
  559. public String toString() {
  560. return format(false, false);
  561. }
  562. private String format(final boolean includePassword, boolean escapeNonAscii) {
  563. final StringBuilder r = new StringBuilder();
  564. if (getScheme() != null) {
  565. r.append(getScheme());
  566. r.append("://"); //$NON-NLS-1$
  567. }
  568. if (getUser() != null) {
  569. r.append(escape(getUser(), true, escapeNonAscii));
  570. if (includePassword && getPass() != null) {
  571. r.append(':');
  572. r.append(escape(getPass(), true, escapeNonAscii));
  573. }
  574. }
  575. if (getHost() != null) {
  576. if (getUser() != null && getUser().length() > 0)
  577. r.append('@');
  578. r.append(escape(getHost(), false, escapeNonAscii));
  579. if (getScheme() != null && getPort() > 0) {
  580. r.append(':');
  581. r.append(getPort());
  582. }
  583. }
  584. if (getPath() != null) {
  585. if (getScheme() != null) {
  586. if (!getPath().startsWith("/") && !getPath().isEmpty()) //$NON-NLS-1$
  587. r.append('/');
  588. } else if (getHost() != null)
  589. r.append(':');
  590. if (getScheme() != null)
  591. if (escapeNonAscii)
  592. r.append(escape(getPath(), false, escapeNonAscii));
  593. else
  594. r.append(getRawPath());
  595. else
  596. r.append(getPath());
  597. }
  598. return r.toString();
  599. }
  600. /**
  601. * @return the URI as an ASCII string. Password is not included.
  602. */
  603. public String toASCIIString() {
  604. return format(false, true);
  605. }
  606. /**
  607. * @return the URI including password, formatted with only ASCII characters
  608. * such that it will be valid for use over the network.
  609. */
  610. public String toPrivateASCIIString() {
  611. return format(true, true);
  612. }
  613. /**
  614. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  615. * for a full path:
  616. * <table summary="path vs humanish path" border="1">
  617. * <tr>
  618. * <th>Path</th>
  619. * <th>Humanish part</th>
  620. * </tr>
  621. * <tr>
  622. * <td><code>/path/to/repo.git</code></td>
  623. * <td rowspan="4"><code>repo</code></td>
  624. * </tr>
  625. * <tr>
  626. * <td><code>/path/to/repo.git/</code></td>
  627. * </tr>
  628. * <tr>
  629. * <td><code>/path/to/repo/.git</code></td>
  630. * </tr>
  631. * <tr>
  632. * <td><code>/path/to/repo/</code></td>
  633. * </tr>
  634. * <tr>
  635. * <td><code>localhost</code></td>
  636. * <td><code>ssh://localhost/</code></td>
  637. * </tr>
  638. * <tr>
  639. * <td><code>/path//to</code></td>
  640. * <td>an empty string</td>
  641. * </tr>
  642. * </table>
  643. *
  644. * @return the "humanish" part of the path. May be an empty string. Never
  645. * {@code null}.
  646. * @throws IllegalArgumentException
  647. * if it's impossible to determine a humanish part, or path is
  648. * {@code null} or empty
  649. * @see #getPath
  650. */
  651. public String getHumanishName() throws IllegalArgumentException {
  652. String s = getPath();
  653. if ("/".equals(s) || "".equals(s)) //$NON-NLS-1$ //$NON-NLS-2$
  654. s = getHost();
  655. if (s == null) // $NON-NLS-1$
  656. throw new IllegalArgumentException();
  657. String[] elements;
  658. if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
  659. elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
  660. else
  661. elements = s.split("/+"); //$NON-NLS-1$
  662. if (elements.length == 0)
  663. throw new IllegalArgumentException();
  664. String result = elements[elements.length - 1];
  665. if (Constants.DOT_GIT.equals(result))
  666. result = elements[elements.length - 2];
  667. else if (result.endsWith(Constants.DOT_GIT_EXT))
  668. result = result.substring(0, result.length()
  669. - Constants.DOT_GIT_EXT.length());
  670. return result;
  671. }
  672. }