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 		if (s == null)
355 			return ""; //$NON-NLS-1$
356 		else
357 			return s;
358 	}
359 
360 	// takes care to cut of a leading slash if a windows drive letter or a
361 	// user-home-dir specifications are
362 	private String cleanLeadingSlashes(String p, String s) {
363 		if (p.length() >= 3
364 				&& p.charAt(0) == '/'
365 				&& p.charAt(2) == ':'
366 				&& ((p.charAt(1) >= 'A' && p.charAt(1) <= 'Z')
367 						|| (p.charAt(1) >= 'a' && p.charAt(1) <= 'z')))
368 			return p.substring(1);
369 		else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
370 				&& p.charAt(1) == '~')
371 			return p.substring(1);
372 		else
373 			return p;
374 	}
375 
376 	/**
377 	 * Construct a URIish from a standard URL.
378 	 *
379 	 * @param u
380 	 *            the source URL to convert from.
381 	 */
382 	public URIish(URL u) {
383 		scheme = u.getProtocol();
384 		path = u.getPath();
385 		path = cleanLeadingSlashes(path, scheme);
386 		try {
387 			rawPath = u.toURI().getRawPath();
388 			rawPath = cleanLeadingSlashes(rawPath, scheme);
389 		} catch (URISyntaxException e) {
390 			throw new RuntimeException(e); // Impossible
391 		}
392 
393 		final String ui = u.getUserInfo();
394 		if (ui != null) {
395 			final int d = ui.indexOf(':');
396 			user = d < 0 ? ui : ui.substring(0, d);
397 			pass = d < 0 ? null : ui.substring(d + 1);
398 		}
399 
400 		port = u.getPort();
401 		host = u.getHost();
402 	}
403 
404 	/**
405 	 * Create an empty, non-configured URI.
406 	 */
407 	public URIish() {
408 		// Configure nothing.
409 	}
410 
411 	private URIishsh" href="../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish(URIish u) {
412 		this.scheme = u.scheme;
413 		this.rawPath = u.rawPath;
414 		this.path = u.path;
415 		this.user = u.user;
416 		this.pass = u.pass;
417 		this.port = u.port;
418 		this.host = u.host;
419 	}
420 
421 	/**
422 	 * Whether this URI references a repository on another system.
423 	 *
424 	 * @return true if this URI references a repository on another system.
425 	 */
426 	public boolean isRemote() {
427 		return getHost() != null;
428 	}
429 
430 	/**
431 	 * Get host name part.
432 	 *
433 	 * @return host name part or null
434 	 */
435 	public String getHost() {
436 		return host;
437 	}
438 
439 	/**
440 	 * Return a new URI matching this one, but with a different host.
441 	 *
442 	 * @param n
443 	 *            the new value for host.
444 	 * @return a new URI with the updated value.
445 	 */
446 	public URIish setHost(String n) {
447 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
448 		r.host = n;
449 		return r;
450 	}
451 
452 	/**
453 	 * Get protocol name
454 	 *
455 	 * @return protocol name or null for local references
456 	 */
457 	public String getScheme() {
458 		return scheme;
459 	}
460 
461 	/**
462 	 * Return a new URI matching this one, but with a different scheme.
463 	 *
464 	 * @param n
465 	 *            the new value for scheme.
466 	 * @return a new URI with the updated value.
467 	 */
468 	public URIish setScheme(String n) {
469 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
470 		r.scheme = n;
471 		return r;
472 	}
473 
474 	/**
475 	 * Get path name component
476 	 *
477 	 * @return path name component
478 	 */
479 	public String getPath() {
480 		return path;
481 	}
482 
483 	/**
484 	 * Get path name component
485 	 *
486 	 * @return path name component
487 	 */
488 	public String getRawPath() {
489 		return rawPath;
490 	}
491 
492 	/**
493 	 * Return a new URI matching this one, but with a different path.
494 	 *
495 	 * @param n
496 	 *            the new value for path.
497 	 * @return a new URI with the updated value.
498 	 */
499 	public URIish setPath(String n) {
500 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
501 		r.path = n;
502 		r.rawPath = n;
503 		return r;
504 	}
505 
506 	/**
507 	 * Return a new URI matching this one, but with a different (raw) path.
508 	 *
509 	 * @param n
510 	 *            the new value for path.
511 	 * @return a new URI with the updated value.
512 	 * @throws java.net.URISyntaxException
513 	 */
514 	public URIish setRawPath(String n) throws URISyntaxException {
515 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
516 		r.path = unescape(n);
517 		r.rawPath = n;
518 		return r;
519 	}
520 
521 	/**
522 	 * Get user name requested for transfer
523 	 *
524 	 * @return user name requested for transfer or null
525 	 */
526 	public String getUser() {
527 		return user;
528 	}
529 
530 	/**
531 	 * Return a new URI matching this one, but with a different user.
532 	 *
533 	 * @param n
534 	 *            the new value for user.
535 	 * @return a new URI with the updated value.
536 	 */
537 	public URIish setUser(String n) {
538 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
539 		r.user = n;
540 		return r;
541 	}
542 
543 	/**
544 	 * Get password requested for transfer
545 	 *
546 	 * @return password requested for transfer or null
547 	 */
548 	public String getPass() {
549 		return pass;
550 	}
551 
552 	/**
553 	 * Return a new URI matching this one, but with a different password.
554 	 *
555 	 * @param n
556 	 *            the new value for password.
557 	 * @return a new URI with the updated value.
558 	 */
559 	public URIish setPass(String n) {
560 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
561 		r.pass = n;
562 		return r;
563 	}
564 
565 	/**
566 	 * Get port number requested for transfer or -1 if not explicit
567 	 *
568 	 * @return port number requested for transfer or -1 if not explicit
569 	 */
570 	public int getPort() {
571 		return port;
572 	}
573 
574 	/**
575 	 * Return a new URI matching this one, but with a different port.
576 	 *
577 	 * @param n
578 	 *            the new value for port.
579 	 * @return a new URI with the updated value.
580 	 */
581 	public URIish setPort(int n) {
582 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
583 		r.port = n > 0 ? n : -1;
584 		return r;
585 	}
586 
587 	/** {@inheritDoc} */
588 	@Override
589 	public int hashCode() {
590 		int hc = 0;
591 		if (getScheme() != null)
592 			hc = hc * 31 + getScheme().hashCode();
593 		if (getUser() != null)
594 			hc = hc * 31 + getUser().hashCode();
595 		if (getPass() != null)
596 			hc = hc * 31 + getPass().hashCode();
597 		if (getHost() != null)
598 			hc = hc * 31 + getHost().hashCode();
599 		if (getPort() > 0)
600 			hc = hc * 31 + getPort();
601 		if (getPath() != null)
602 			hc = hc * 31 + getPath().hashCode();
603 		return hc;
604 	}
605 
606 	/** {@inheritDoc} */
607 	@Override
608 	public boolean equals(Object obj) {
609 		if (!(obj instanceof URIish))
610 			return false;
611 		final URIishref="../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish b = (URIish) obj;
612 		if (!eq(getScheme(), b.getScheme()))
613 			return false;
614 		if (!eq(getUser(), b.getUser()))
615 			return false;
616 		if (!eq(getPass(), b.getPass()))
617 			return false;
618 		if (!eq(getHost(), b.getHost()))
619 			return false;
620 		if (getPort() != b.getPort())
621 			return false;
622 		if (!eq(getPath(), b.getPath()))
623 			return false;
624 		return true;
625 	}
626 
627 	private static boolean eq(String a, String b) {
628 		if (References.isSameObject(a, b)) {
629 			return true;
630 		}
631 		if (StringUtils.isEmptyOrNull(a) && StringUtils.isEmptyOrNull(b))
632 			return true;
633 		if (a == null || b == null)
634 			return false;
635 		return a.equals(b);
636 	}
637 
638 	/**
639 	 * Obtain the string form of the URI, with the password included.
640 	 *
641 	 * @return the URI, including its password field, if any.
642 	 */
643 	public String toPrivateString() {
644 		return format(true, false);
645 	}
646 
647 	/** {@inheritDoc} */
648 	@Override
649 	public String toString() {
650 		return format(false, false);
651 	}
652 
653 	private String format(boolean includePassword, boolean escapeNonAscii) {
654 		final StringBuilder r = new StringBuilder();
655 		if (getScheme() != null) {
656 			r.append(getScheme());
657 			r.append("://"); //$NON-NLS-1$
658 		}
659 
660 		if (getUser() != null) {
661 			r.append(escape(getUser(), true, escapeNonAscii));
662 			if (includePassword && getPass() != null) {
663 				r.append(':');
664 				r.append(escape(getPass(), true, escapeNonAscii));
665 			}
666 		}
667 
668 		if (getHost() != null) {
669 			if (getUser() != null && getUser().length() > 0)
670 				r.append('@');
671 			r.append(escape(getHost(), false, escapeNonAscii));
672 			if (getScheme() != null && getPort() > 0) {
673 				r.append(':');
674 				r.append(getPort());
675 			}
676 		}
677 
678 		if (getPath() != null) {
679 			if (getScheme() != null) {
680 				if (!getPath().startsWith("/") && !getPath().isEmpty()) //$NON-NLS-1$
681 					r.append('/');
682 			} else if (getHost() != null)
683 				r.append(':');
684 			if (getScheme() != null)
685 				if (escapeNonAscii)
686 					r.append(escape(getPath(), false, escapeNonAscii));
687 				else
688 					r.append(getRawPath());
689 			else
690 				r.append(getPath());
691 		}
692 
693 		return r.toString();
694 	}
695 
696 	/**
697 	 * Get the URI as an ASCII string.
698 	 *
699 	 * @return the URI as an ASCII string. Password is not included.
700 	 */
701 	public String toASCIIString() {
702 		return format(false, true);
703 	}
704 
705 	/**
706 	 * Convert the URI including password, formatted with only ASCII characters
707 	 * such that it will be valid for use over the network.
708 	 *
709 	 * @return the URI including password, formatted with only ASCII characters
710 	 *         such that it will be valid for use over the network.
711 	 */
712 	public String toPrivateASCIIString() {
713 		return format(true, true);
714 	}
715 
716 	/**
717 	 * Get the "humanish" part of the path. Some examples of a 'humanish' part
718 	 * for a full path:
719 	 * <table summary="path vs humanish path" border="1">
720 	 * <tr>
721 	 * <th>Path</th>
722 	 * <th>Humanish part</th>
723 	 * </tr>
724 	 * <tr>
725 	 * <td><code>/path/to/repo.git</code></td>
726 	 * <td rowspan="4"><code>repo</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/.git</code></td>
733 	 * </tr>
734 	 * <tr>
735 	 * <td><code>/path/to/repo/</code></td>
736 	 * </tr>
737 	 * <tr>
738 	 * <td><code>localhost</code></td>
739 	 * <td><code>ssh://localhost/</code></td>
740 	 * </tr>
741 	 * <tr>
742 	 * <td><code>/path//to</code></td>
743 	 * <td>an empty string</td>
744 	 * </tr>
745 	 * </table>
746 	 *
747 	 * @return the "humanish" part of the path. May be an empty string. Never
748 	 *         {@code null}.
749 	 * @throws java.lang.IllegalArgumentException
750 	 *             if it's impossible to determine a humanish part, or path is
751 	 *             {@code null} or empty
752 	 * @see #getPath
753 	 */
754 	public String getHumanishName() throws IllegalArgumentException {
755 		String s = getPath();
756 		if ("/".equals(s) || "".equals(s)) //$NON-NLS-1$ //$NON-NLS-2$
757 			s = getHost();
758 		if (s == null) // $NON-NLS-1$
759 			throw new IllegalArgumentException();
760 
761 		String[] elements;
762 		if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
763 			elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
764 		else
765 			elements = s.split("/+"); //$NON-NLS-1$
766 		if (elements.length == 0)
767 			throw new IllegalArgumentException();
768 		String result = elements[elements.length - 1];
769 		if (Constants.DOT_GIT.equals(result))
770 			result = elements[elements.length - 2];
771 		else if (result.endsWith(Constants.DOT_GIT_EXT))
772 			result = result.substring(0, result.length()
773 					- Constants.DOT_GIT_EXT.length());
774 		return result;
775 	}
776 
777 }