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