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())) { if ("html".equalsIgnoreCase(e.getRootElement())) {
boolean dialogShown = showFeedDiscoveryDialog(destinationFile, selectedDownloadUrl); boolean dialogShown = showFeedDiscoveryDialog(destinationFile, selectedDownloadUrl);
if (dialogShown) { if (dialogShown) {
return null; // Should not display an error message return null; // We handled the problem
} else { } 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) { } catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e)); Log.e(TAG, Log.getStackTraceString(e));
throw e; throw e;

View File

@ -1,31 +1,17 @@
package de.danoeh.antennapod.parser.feed; 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 { public class UnsupportedFeedtypeException extends Exception {
private static final long serialVersionUID = 9105878964928170669L; private static final long serialVersionUID = 9105878964928170669L;
private final TypeGetter.Type type;
private String rootElement; private String rootElement;
private String message = null; private String message;
public UnsupportedFeedtypeException(Type type) { public UnsupportedFeedtypeException(String rootElement, String message) {
super();
this.type = type;
}
public UnsupportedFeedtypeException(Type type, String rootElement) {
this.type = type;
this.rootElement = rootElement; this.rootElement = rootElement;
this.message = message;
} }
public UnsupportedFeedtypeException(String message) { public UnsupportedFeedtypeException(String message) {
this.message = message; this.message = message;
type = Type.INVALID;
}
public TypeGetter.Type getType() {
return type;
} }
public String getRootElement() { public String getRootElement() {
@ -36,10 +22,10 @@ public class UnsupportedFeedtypeException extends Exception {
public String getMessage() { public String getMessage() {
if (message != null) { if (message != null) {
return message; return message;
} else if (type == TypeGetter.Type.INVALID) { } else if (rootElement != null) {
return "Invalid type"; return "Server returned " + rootElement;
} else { } 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 de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException;
import org.apache.commons.io.input.XmlStreamReader; import org.apache.commons.io.input.XmlStreamReader;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -69,8 +70,9 @@ public class TypeGetter {
} }
throw new UnsupportedFeedtypeException("Unsupported rss version"); throw new UnsupportedFeedtypeException("Unsupported rss version");
default: default:
Log.d(TAG, "Type is invalid"); Log.d(TAG, "Type is invalid: " + tag);
throw new UnsupportedFeedtypeException(Type.INVALID, tag); throwExceptionIfWebsite(feed);
throw new UnsupportedFeedtypeException(tag, null);
} }
} else { } else {
try { try {
@ -83,15 +85,8 @@ public class TypeGetter {
} }
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
e.printStackTrace(); e.printStackTrace();
// XML document might actually be a HTML document -> try to parse as HTML throwExceptionIfWebsite(feed);
String rootElement = null; throw new UnsupportedFeedtypeException(e.getMessage());
try {
Jsoup.parse(new File(feed.getLocalFileUrl()));
rootElement = "html";
} catch (IOException e1) {
e1.printStackTrace();
}
throw new UnsupportedFeedtypeException(Type.INVALID, rootElement);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -106,20 +101,31 @@ public class TypeGetter {
} }
} }
Log.d(TAG, "Type is invalid"); 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) { private Reader createReader(Feed feed) {
Reader reader; Reader reader;
try { try {
reader = new XmlStreamReader(new File(feed.getLocalFileUrl())); reader = new XmlStreamReader(new File(feed.getLocalFileUrl()));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
return reader; 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();
}
}
} }