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