Check redirect

This commit is contained in:
tom79 2019-11-29 15:26:49 +01:00
parent fd0832c88d
commit f98ae2002e
4 changed files with 89 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Html;
@ -64,6 +65,8 @@ import com.github.penfeizhou.animation.apng.decode.APNGParser;
import com.github.penfeizhou.animation.gif.GifDrawable;
import com.github.penfeizhou.animation.gif.decode.GifParser;
import net.gotev.uploadservice.http.HttpConnection;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
@ -86,6 +89,7 @@ import app.fedilab.android.activities.PeertubeActivity;
import app.fedilab.android.activities.ShowAccountActivity;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.android.client.HttpsConnection;
import app.fedilab.android.fragments.TabLayoutNotificationsFragment;
import app.fedilab.android.helper.CrossActions;
import app.fedilab.android.helper.CustomQuoteSpan;
@ -667,19 +671,20 @@ public class Status implements Parcelable {
PopupMenu popup = new PopupMenu(context, textView);
popup.getMenuInflater()
.inflate(R.menu.links_popup, popup.getMenu());
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_show_link:
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
builder.setMessage(url);
builder.setTitle(context.getString(R.string.display_full_link));
@ -716,6 +721,38 @@ public class Status implements Parcelable {
Toasty.info(context, context.getString(R.string.clipboard_url), Toast.LENGTH_LONG).show();
}
break;
case R.id.action_unshorten:
Thread thread = new Thread() {
@Override
public void run() {
String response = new HttpsConnection(context, null).checkUrl(url);
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
if( response != null ) {
builder.setMessage(response);
}else{
builder.setMessage(R.string.no_redirect);
}
builder.setTitle(context.getString(R.string.check_redirect));
builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
};
mainHandler.post(myRunnable);
}
};
thread.start();
break;
}
return true;
}

View File

@ -43,6 +43,7 @@ import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -76,6 +77,8 @@ import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static app.fedilab.android.helper.Helper.urlPattern;
/**
* Created by Thomas on 17/11/2017.
@ -253,6 +256,41 @@ public class HttpsConnection {
public String checkUrl(String urlConnection){
URL url;
String redirect = null;
try {
url = new URL(urlConnection);
if (proxy != null)
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setInstanceFollowRedirects(false);
httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
httpsURLConnection.setRequestMethod("GET");
if( httpsURLConnection.getResponseCode() == 301) {
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
if (entry.toString().startsWith("Location") || entry.toString().startsWith("Location")) {
Matcher matcher = urlPattern.matcher(entry.toString());
if (matcher.find()) {
redirect = matcher.group(1);
}
}
}
}
httpsURLConnection.getInputStream().close();
return redirect != null && redirect.compareTo(urlConnection)!=0?redirect:null;
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
return null;
}
public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
if (Build.VERSION.SDK_INT >= 21) {

View File

@ -17,4 +17,8 @@
android:id="@+id/action_copy_link"
android:title="@string/copy_link"
/>
<item
android:id="@+id/action_unshorten"
android:title="@string/check_redirect"
/>
</menu>

View File

@ -1194,4 +1194,6 @@
<string name="share_link">Share link</string>
<string name="link_copy">The URL has been copied to the clipboard</string>
<string name="open_other_app">Open with another app</string>
<string name="check_redirect">Check redirect</string>
<string name="no_redirect">This URL does not redirect</string>
</resources>