@Test public void minimalEncodeChecksAuthority() { try { Urls.escape("http: fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("Invalid hostname:")); } }
@Nonnull public static String escape(@Nonnull String url) { String trim = Strings.sanitizeWhitespace(url); SplitUrl split = SplitUrl.split(trim); if (split.urlType() != Type.FULL) { throw new IllegalArgumentException( "Not a full URL: " + url); } StringBuilder sb = new StringBuilder(); if (split.scheme() != null) { String scheme = split.scheme().toLowerCase(Locale.US); if (scheme.equals("http") || scheme.equals("https")) { sb.append(scheme).append(':'); } else { throw new IllegalArgumentException( "Only http and https schemes are supported. Input: " + url); } } if (split.authority() != null) { sb.append(" } if (split.path() != null) { sb.append(PercentEncoder.reEncodePath(split.path()).replace('\\', '/')); } if (split.query() != null) { sb.append('?').append(PercentEncoder.reEncodeQuery(split.query())); } if (split.fragment() != null) { sb.append('#').append(PercentEncoder.reEncodeFragment(split.fragment())); } return sb.toString(); }
Urls { @Nonnull public static String escape(@Nonnull String url) { String trim = Strings.sanitizeWhitespace(url); SplitUrl split = SplitUrl.split(trim); if (split.urlType() != Type.FULL) { throw new IllegalArgumentException( "Not a full URL: " + url); } StringBuilder sb = new StringBuilder(); if (split.scheme() != null) { String scheme = split.scheme().toLowerCase(Locale.US); if (scheme.equals("http") || scheme.equals("https")) { sb.append(scheme).append(':'); } else { throw new IllegalArgumentException( "Only http and https schemes are supported. Input: " + url); } } if (split.authority() != null) { sb.append(" } if (split.path() != null) { sb.append(PercentEncoder.reEncodePath(split.path()).replace('\\', '/')); } if (split.query() != null) { sb.append('?').append(PercentEncoder.reEncodeQuery(split.query())); } if (split.fragment() != null) { sb.append('#').append(PercentEncoder.reEncodeFragment(split.fragment())); } return sb.toString(); } }
Urls { @Nonnull public static String escape(@Nonnull String url) { String trim = Strings.sanitizeWhitespace(url); SplitUrl split = SplitUrl.split(trim); if (split.urlType() != Type.FULL) { throw new IllegalArgumentException( "Not a full URL: " + url); } StringBuilder sb = new StringBuilder(); if (split.scheme() != null) { String scheme = split.scheme().toLowerCase(Locale.US); if (scheme.equals("http") || scheme.equals("https")) { sb.append(scheme).append(':'); } else { throw new IllegalArgumentException( "Only http and https schemes are supported. Input: " + url); } } if (split.authority() != null) { sb.append(" } if (split.path() != null) { sb.append(PercentEncoder.reEncodePath(split.path()).replace('\\', '/')); } if (split.query() != null) { sb.append('?').append(PercentEncoder.reEncodeQuery(split.query())); } if (split.fragment() != null) { sb.append('#').append(PercentEncoder.reEncodeFragment(split.fragment())); } return sb.toString(); } private  Urls(); }
Urls { @Nonnull public static String escape(@Nonnull String url) { String trim = Strings.sanitizeWhitespace(url); SplitUrl split = SplitUrl.split(trim); if (split.urlType() != Type.FULL) { throw new IllegalArgumentException( "Not a full URL: " + url); } StringBuilder sb = new StringBuilder(); if (split.scheme() != null) { String scheme = split.scheme().toLowerCase(Locale.US); if (scheme.equals("http") || scheme.equals("https")) { sb.append(scheme).append(':'); } else { throw new IllegalArgumentException( "Only http and https schemes are supported. Input: " + url); } } if (split.authority() != null) { sb.append(" } if (split.path() != null) { sb.append(PercentEncoder.reEncodePath(split.path()).replace('\\', '/')); } if (split.query() != null) { sb.append('?').append(PercentEncoder.reEncodeQuery(split.query())); } if (split.fragment() != null) { sb.append('#').append(PercentEncoder.reEncodeFragment(split.fragment())); } return sb.toString(); } private  Urls(); @Nonnull static URI createURI(@Nonnull String fullUrl); @Nonnull static String escape(@Nonnull String url); static UrlBuilder http(String host); static UrlBuilder https(String host); @Nonnull static Url parse(String url); }
Urls { @Nonnull public static String escape(@Nonnull String url) { String trim = Strings.sanitizeWhitespace(url); SplitUrl split = SplitUrl.split(trim); if (split.urlType() != Type.FULL) { throw new IllegalArgumentException( "Not a full URL: " + url); } StringBuilder sb = new StringBuilder(); if (split.scheme() != null) { String scheme = split.scheme().toLowerCase(Locale.US); if (scheme.equals("http") || scheme.equals("https")) { sb.append(scheme).append(':'); } else { throw new IllegalArgumentException( "Only http and https schemes are supported. Input: " + url); } } if (split.authority() != null) { sb.append(" } if (split.path() != null) { sb.append(PercentEncoder.reEncodePath(split.path()).replace('\\', '/')); } if (split.query() != null) { sb.append('?').append(PercentEncoder.reEncodeQuery(split.query())); } if (split.fragment() != null) { sb.append('#').append(PercentEncoder.reEncodeFragment(split.fragment())); } return sb.toString(); } private  Urls(); @Nonnull static URI createURI(@Nonnull String fullUrl); @Nonnull static String escape(@Nonnull String url); static UrlBuilder http(String host); static UrlBuilder https(String host); @Nonnull static Url parse(String url);  }