Show website title in 'server sent a website' error (#7629)

This commit is contained in:
ByteHamster 2025-01-28 22:11:03 +01:00 committed by GitHub
parent bbc098aba9
commit 9ac85bb6e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 40 deletions

View File

@ -318,13 +318,13 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
if ("html".equalsIgnoreCase(e.getRootElement())) {
boolean dialogShown = showFeedDiscoveryDialog(destinationFile, selectedDownloadUrl);
if (dialogShown) {
return null; // Should not display an error message
return null; // We handled the problem
} else {
throw new UnsupportedFeedtypeException(getString(R.string.download_error_unsupported_type_html));
throw new UnsupportedFeedtypeException(
getString(R.string.download_error_unsupported_type_html) + "\n" + e.getMessage());
}
} else {
throw e;
}
throw e;
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
throw e;

View File

@ -1,31 +1,17 @@
package de.danoeh.antennapod.parser.feed;
import de.danoeh.antennapod.parser.feed.util.TypeGetter;
import de.danoeh.antennapod.parser.feed.util.TypeGetter.Type;
public class UnsupportedFeedtypeException extends Exception {
private static final long serialVersionUID = 9105878964928170669L;
private final TypeGetter.Type type;
private String rootElement;
private String message = null;
private String message;
public UnsupportedFeedtypeException(Type type) {
super();
this.type = type;
}
public UnsupportedFeedtypeException(Type type, String rootElement) {
this.type = type;
public UnsupportedFeedtypeException(String rootElement, String message) {
this.rootElement = rootElement;
this.message = message;
}
public UnsupportedFeedtypeException(String message) {
this.message = message;
type = Type.INVALID;
}
public TypeGetter.Type getType() {
return type;
}
public String getRootElement() {
@ -36,10 +22,10 @@ public class UnsupportedFeedtypeException extends Exception {
public String getMessage() {
if (message != null) {
return message;
} else if (type == TypeGetter.Type.INVALID) {
return "Invalid type";
} else if (rootElement != null) {
return "Server returned " + rootElement;
} else {
return "Type " + type + " not supported";
return "Unknown type";
}
}
}

View File

@ -5,12 +5,13 @@ import android.util.Log;
import de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException;
import org.apache.commons.io.input.XmlStreamReader;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
@ -69,8 +70,9 @@ public class TypeGetter {
}
throw new UnsupportedFeedtypeException("Unsupported rss version");
default:
Log.d(TAG, "Type is invalid");
throw new UnsupportedFeedtypeException(Type.INVALID, tag);
Log.d(TAG, "Type is invalid: " + tag);
throwExceptionIfWebsite(feed);
throw new UnsupportedFeedtypeException(tag, null);
}
} else {
try {
@ -83,15 +85,8 @@ public class TypeGetter {
}
} catch (XmlPullParserException e) {
e.printStackTrace();
// XML document might actually be a HTML document -> try to parse as HTML
String rootElement = null;
try {
Jsoup.parse(new File(feed.getLocalFileUrl()));
rootElement = "html";
} catch (IOException e1) {
e1.printStackTrace();
}
throw new UnsupportedFeedtypeException(Type.INVALID, rootElement);
throwExceptionIfWebsite(feed);
throw new UnsupportedFeedtypeException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
@ -106,20 +101,31 @@ public class TypeGetter {
}
}
Log.d(TAG, "Type is invalid");
throw new UnsupportedFeedtypeException(Type.INVALID);
throw new UnsupportedFeedtypeException("Unknown problem when trying to determine feed type");
}
private Reader createReader(Feed feed) {
Reader reader;
try {
reader = new XmlStreamReader(new File(feed.getLocalFileUrl()));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return reader;
}
private void throwExceptionIfWebsite(Feed feed) throws UnsupportedFeedtypeException {
try {
Document document = Jsoup.parse(new File(feed.getLocalFileUrl()));
Element titleElement = document.head().getElementsByTag("title").first();
if (titleElement != null) {
throw new UnsupportedFeedtypeException("html", "Website title: \"" + titleElement.text() + "\"");
}
Element firstChild = document.children().first();
throw new UnsupportedFeedtypeException(firstChild != null ? firstChild.tagName() : "?", null);
} catch (IOException e) {
e.printStackTrace();
}
}
}