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 21KB

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