Skip to content

Instantly share code, notes, and snippets.

@webserveis
Forked from liangzai-cool/HttpUtils.java
Last active March 12, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webserveis/a8f595ffb795de4dc1b86cac6c410127 to your computer and use it in GitHub Desktop.
Save webserveis/a8f595ffb795de4dc1b86cac6c410127 to your computer and use it in GitHub Desktop.
convert relative url to absolute url and beautify the result in java.
```
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtils {
public static void main(String[] args) throws MalformedURLException {
String urlString = "https://www.google.com///images//branding/1234567890/..\\googlelogo/abcdefg/../1x/googlelogo_color_272x92dp.png";
System.out.println(absUrl(urlString));
System.out.println(absUrl(urlString, "/abc/../def.png"));
}
public static String absUrl(String baseUrlString, String urlString) throws MalformedURLException, URISyntaxException {
if (urlString == null || urlString.trim().length() == 0) urlString = "";
URL baseUrl = new URL(baseUrlString);
URL url = new URL(baseUrl, urlString);
urlString = url.toString().replaceAll("\\\\+", "/");
url = new URL(urlString);
String uri = url.getPath();
String uriString = uri.replaceAll("/+", "/");
urlString = url.toString().replaceAll(uri, uriString);
int index = urlString.indexOf("/..");
if (index < 0) return urlString;
String urlStringLeft = urlString.substring(0, index) + "/";
String urlStringRight = urlString.substring(index + 1);
return absUrl(urlStringLeft, urlStringRight);
}
public static String absUrl(String url) {
try {
return absUrl(url, null);
} catch (MalformedURLException | URISyntaxException e) {
log.error("convert url to absUrl error", e);
return url;
}
}
}
```
convert `https://www.google.com///images//branding/1234567890/..\\googlelogo/abcdefg/../1x/googlelogo_color_272x92dp.png`
to `https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png`
or
convert `https://www.google.com///images//branding/1234567890/..\\googlelogo/abcdefg/../1x/googlelogo_color_272x92dp.png` & `/abc/../def.png`
to `https://www.google.com/def.png`
package com.webserveis.httpredirectiontrace.utils;
import android.net.Uri;
import android.util.Log;
import android.util.Patterns;
import android.webkit.URLUtil;
import java.io.UnsupportedEncodingException;
import java.net.IDN;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class HttpUtils {
public static boolean isValidUrl(String mUrl) {
if (mUrl == null) return false;
if (!URLUtil.isNetworkUrl(mUrl)) return false;
return Patterns.WEB_URL.matcher(mUrl).matches();
}
public static String getAbsoluteUrl(@NonNull String baseUrl, @NonNull String relativeUrl) throws MalformedURLException {
URL url = new URL(new URL(baseUrl), relativeUrl);
return url.toString();
}
public static boolean isIDN(@Nullable String urlStr) {
return (urlStr != null) && !(urlStr.matches("\\A\\p{ASCII}*\\z"));
}
@Nullable
public static String getPunyCode(@Nullable String urlStr) {
return (urlStr != null) ? IDN.toASCII(urlStr).toLowerCase() : null;
}
public static boolean isPunyCode(@Nullable String urlStr) {
return (urlStr != null) && urlStr.toLowerCase().contains("xn--");
}
@Nullable
public static String sanitiseUrl(@Nullable String urlStr) {
if (urlStr == null) return null;
if (!(URLUtil.isHttpUrl(urlStr) || URLUtil.isHttpsUrl(urlStr))) {
urlStr = Uri.parse("http://" + urlStr).toString();
}
URL url = null;
try {
urlStr = URLDecoder.decode(urlStr, "UTF-8");
Log.d("sanitiseUrl", "decode: " + urlStr);
url = new URL(urlStr);
URI uri = new URI(
url.getProtocol(),
url.getUserInfo(),
url.getHost(),
url.getPort(),
url.getPath(),
url.getQuery(),
url.getRef());
url = uri.toURL();
Log.d("sanitiseUrl", "encode: " + url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return url != null ? url.toString() : null;
}
/*
https://www.programcreek.com/java-api-examples/?class=android.net.Uri&method=Builder
*/
public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
URI oldUri = new URI(uri);
String newQuery = oldUri.getQuery();
if (newQuery == null) {
newQuery = appendQuery;
} else {
newQuery += "&" + appendQuery;
}
return new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), newQuery, oldUri.getFragment());
}
public Uri buildURI(String url, Map<String, String> params) {
// build url with parameters.
Uri.Builder builder = Uri.parse(url).buildUpon();
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.appendQueryParameter(entry.getKey(), entry.getValue());
}
return builder.build();
}
}
/* if (HttpUtils.isIDN(argUrl)) {
Log.d(TAG, "onEditorAction: is a IDN you punycode " + HttpUtils.getPunyCode(argUrl));
} else {
Log.d(TAG, "onEditorAction: url is with ACE");
if (HttpUtils.isPunyCode(argUrl))
Log.d(TAG, "onEditorAction: is a punycode");
}*/
/* if (!(URLUtil.isHttpUrl(argUrl) || URLUtil.isHttpsUrl(argUrl))) {
argUrl = Uri.parse("http://" + argUrl).toString();
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment