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