UriUtil { public static String resolve(@Nullable String baseUri, @Nullable String referenceUri) { StringBuilder uri = new StringBuilder(); baseUri = baseUri == null ? "" : baseUri; referenceUri = referenceUri == null ? "" : referenceUri; int[] refIndices = getUriIndices(referenceUri); if (refIndices[SCHEME_COLON] != -1) { uri.append(referenceUri); removeDotSegments(uri, refIndices[PATH], refIndices[QUERY]); return uri.toString(); } int[] baseIndices = getUriIndices(baseUri); if (refIndices[FRAGMENT] == 0) { return uri.append(baseUri, 0, baseIndices[FRAGMENT]).append(referenceUri).toString(); } if (refIndices[QUERY] == 0) { return uri.append(baseUri, 0, baseIndices[QUERY]).append(referenceUri).toString(); } if (refIndices[PATH] != 0) { int baseLimit = baseIndices[SCHEME_COLON] + 1; uri.append(baseUri, 0, baseLimit).append(referenceUri); return removeDotSegments(uri, baseLimit + refIndices[PATH], baseLimit + refIndices[QUERY]); } if (referenceUri.charAt(refIndices[PATH]) == '/') { uri.append(baseUri, 0, baseIndices[PATH]).append(referenceUri); return removeDotSegments(uri, baseIndices[PATH], baseIndices[PATH] + refIndices[QUERY]); } if (baseIndices[SCHEME_COLON] + 2 < baseIndices[PATH] && baseIndices[PATH] == baseIndices[QUERY]) { uri.append(baseUri, 0, baseIndices[PATH]).append('/').append(referenceUri); return removeDotSegments(uri, baseIndices[PATH], baseIndices[PATH] + refIndices[QUERY] + 1); } else { int lastSlashIndex = baseUri.lastIndexOf('/', baseIndices[QUERY] - 1); int baseLimit = lastSlashIndex == -1 ? baseIndices[PATH] : lastSlashIndex + 1; uri.append(baseUri, 0, baseLimit).append(referenceUri); return removeDotSegments(uri, baseIndices[PATH], baseLimit + refIndices[QUERY]); } } private  UriUtil(); static Uri resolveToUri(@Nullable String baseUri, @Nullable String referenceUri); static String resolve(@Nullable String baseUri, @Nullable String referenceUri); static Uri removeQueryParameter(Uri uri, String queryParameterName);  }
@Test public void testResolveNormal() { String base = "http: assertThat(resolve(base, "g:h")).isEqualTo("g:h"); assertThat(resolve(base, "g")).isEqualTo("http: assertThat(resolve(base, "g/")).isEqualTo("http: assertThat(resolve(base, "/g")).isEqualTo("http: assertThat(resolve(base, " assertThat(resolve(base, "?y")).isEqualTo("http: assertThat(resolve(base, "g?y")).isEqualTo("http: assertThat(resolve(base, "#s")).isEqualTo("http: assertThat(resolve(base, "g#s")).isEqualTo("http: assertThat(resolve(base, "g?y#s")).isEqualTo("http: assertThat(resolve(base, ";x")).isEqualTo("http: assertThat(resolve(base, "g;x")).isEqualTo("http: assertThat(resolve(base, "g;x?y#s")).isEqualTo("http: assertThat(resolve(base, "")).isEqualTo("http: assertThat(resolve(base, ".")).isEqualTo("http: assertThat(resolve(base, "./")).isEqualTo("http: assertThat(resolve(base, "..")).isEqualTo("http: assertThat(resolve(base, "../")).isEqualTo("http: assertThat(resolve(base, "../g")).isEqualTo("http: assertThat(resolve(base, "../..")).isEqualTo("http: assertThat(resolve(base, "../../")).isEqualTo("http: assertThat(resolve(base, "../../g")).isEqualTo("http: }
@Test public void testResolveAbnormal() { String base = "http: assertThat(resolve(base, "../../../g")).isEqualTo("http: assertThat(resolve(base, "../../../../g")).isEqualTo("http: assertThat(resolve(base, "/./g")).isEqualTo("http: assertThat(resolve(base, "/../g")).isEqualTo("http: assertThat(resolve(base, "g.")).isEqualTo("http: assertThat(resolve(base, ".g")).isEqualTo("http: assertThat(resolve(base, "g..")).isEqualTo("http: assertThat(resolve(base, "..g")).isEqualTo("http: assertThat(resolve(base, "./../g")).isEqualTo("http: assertThat(resolve(base, "./g/.")).isEqualTo("http: assertThat(resolve(base, "g/./h")).isEqualTo("http: assertThat(resolve(base, "g/../h")).isEqualTo("http: assertThat(resolve(base, "g;x=1/./y")).isEqualTo("http: assertThat(resolve(base, "g;x=1/../y")).isEqualTo("http: assertThat(resolve(base, "g?y/./x")).isEqualTo("http: assertThat(resolve(base, "g?y/../x")).isEqualTo("http: assertThat(resolve(base, "g#s/./x")).isEqualTo("http: assertThat(resolve(base, "g#s/../x")).isEqualTo("http: assertThat(resolve(base, "http:g")).isEqualTo("http:g"); }
@Test public void testResolveAbnormalAdditional() { assertThat(resolve("http: assertThat(resolve("a:b", "../c")).isEqualTo("a:c"); }