From df6510e2e78605a20f36233ef8055426e25138ba Mon Sep 17 00:00:00 2001 From: Christian Ruppert Date: Sat, 10 Oct 2015 00:12:21 +0200 Subject: [PATCH 1/3] Also allow ftp downloads Controller.getInstance().openConnection(url) already returns a URLConnection which can be HTTP(s) or FTP so lets stick with it. FTP URLs are also valid for the image cacher. --- .../org/ttrssreader/gui/view/ArticleWebViewClient.java | 8 +++----- .../main/java/org/ttrssreader/imageCache/ImageCacher.java | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java b/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java index eb9cd428..f0c53b8b 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java +++ b/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java @@ -178,7 +178,9 @@ protected Void doInBackground(URL... urls) { File file; try { URL url = urls[0]; - HttpURLConnection c = (HttpURLConnection) Controller.getInstance().openConnection(url); + URLConnection c; + + c = Controller.getInstance().openConnection(url); file = new File(folder, URLUtil.guessFileName(url.toString(), null, ".mp3")); if (file.exists()) { @@ -186,10 +188,6 @@ protected Void doInBackground(URL... urls) { c.setRequestProperty("Range", "bytes=" + size + "-"); // try to resume downloads } - c.setRequestMethod("GET"); - c.setDoInput(true); - c.setDoOutput(true); - in = new BufferedInputStream(c.getInputStream()); fos = (size == 0) ? new FileOutputStream(file) : new FileOutputStream(file, true); bout = new BufferedOutputStream(fos, BUFFER); diff --git a/ttrssreader/src/main/java/org/ttrssreader/imageCache/ImageCacher.java b/ttrssreader/src/main/java/org/ttrssreader/imageCache/ImageCacher.java index f31a4b06..94cfeb4e 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/imageCache/ImageCacher.java +++ b/ttrssreader/src/main/java/org/ttrssreader/imageCache/ImageCacher.java @@ -394,7 +394,8 @@ private static Set findAllImageUrls(String html) { while (m.find()) { String url = m.group(1); - if (url.startsWith("http")) ret.add(url); + + if (url.startsWith("http") || url.startsWith("ftp://")) ret.add(url); } return ret; } From 19d70bd9f5e7ead33e4c9710a44063f468c5fb0c Mon Sep 17 00:00:00 2001 From: Christian Ruppert Date: Sat, 10 Oct 2015 00:14:43 +0200 Subject: [PATCH 2/3] Extend findImageUrlsPattern Pattern There might be "img" or even "video" tags. --- ttrssreader/src/main/java/org/ttrssreader/utils/Utils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttrssreader/src/main/java/org/ttrssreader/utils/Utils.java b/ttrssreader/src/main/java/org/ttrssreader/utils/Utils.java index b6162624..abc50e43 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/utils/Utils.java +++ b/ttrssreader/src/main/java/org/ttrssreader/utils/Utils.java @@ -89,7 +89,7 @@ public class Utils { * The Pattern to match image-urls inside HTML img-tags. */ public static final Pattern findImageUrlsPattern = Pattern - .compile("]+?src=[\"']([^\"']*)", Pattern.CASE_INSENSITIVE); + .compile("<(?:img|video)[^>]+?src=[\"']([^\"']*)", Pattern.CASE_INSENSITIVE); private static final int ID_RUNNING = 4564561; private static final int ID_FINISHED = 7897891; From 2c634b19364bc6a33af74d57e9df3990425334d6 Mon Sep 17 00:00:00 2001 From: Christian Ruppert Date: Sat, 10 Oct 2015 00:20:54 +0200 Subject: [PATCH 3/3] Refactor schemeless URL handling Schemeless URLs are now translated as early as possible, so when fetching the data from the tt-rss instance. We now check attachments, article content, article url and comment url. Android treated some of those schemeless URLs as "file://" which caused some trouble. --- .../org/ttrssreader/gui/view/ArticleWebViewClient.java | 1 - .../main/java/org/ttrssreader/net/JSONConnector.java | 10 +++++++++- .../src/main/java/org/ttrssreader/utils/FileUtils.java | 4 ---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java b/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java index f0c53b8b..629de568 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java +++ b/ttrssreader/src/main/java/org/ttrssreader/gui/view/ArticleWebViewClient.java @@ -44,7 +44,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.lang.ref.WeakReference; -import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; diff --git a/ttrssreader/src/main/java/org/ttrssreader/net/JSONConnector.java b/ttrssreader/src/main/java/org/ttrssreader/net/JSONConnector.java index ac3d1e4f..48cb282b 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/net/JSONConnector.java +++ b/ttrssreader/src/main/java/org/ttrssreader/net/JSONConnector.java @@ -22,6 +22,7 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.MalformedJsonException; +import org.intellij.lang.annotations.RegExp; import org.json.JSONException; import org.json.JSONObject; import org.ttrssreader.MyApplication; @@ -49,6 +50,7 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; public abstract class JSONConnector { @@ -422,6 +424,8 @@ private Set parseAttachments(JsonReader reader) throws IOException { switch (reader.nextName()) { case CONTENT_URL: attUrl = reader.nextString(); + // Some URLs may start with // to indicate that both, http and https can be used + if (attUrl.startsWith("//")) attUrl = "https:" + attUrl; break; case ID: attId = reader.nextString(); @@ -525,13 +529,17 @@ private boolean parseArticle(final Article a, final JsonReader reader, final Set else a.feedId = reader.nextInt(); break; case content: - a.content = reader.nextString(); + a.content = reader.nextString().replaceAll("(<(?:img|video)[^>]+?src=[\"'])//([^\"']*)", "$1https://$2"); break; case link: a.url = reader.nextString(); + // Some URLs may start with // to indicate that both, http and https can be used + if (a.url.startsWith("//")) a.url = "https:" + a.url; break; case comments: a.commentUrl = reader.nextString(); + // Some URLs may start with // to indicate that both, http and https can be used + if (a.commentUrl.startsWith("//")) a.commentUrl = "https:" + a.commentUrl; break; case attachments: a.attachments = parseAttachments(reader); diff --git a/ttrssreader/src/main/java/org/ttrssreader/utils/FileUtils.java b/ttrssreader/src/main/java/org/ttrssreader/utils/FileUtils.java index 221cbffc..64760e23 100755 --- a/ttrssreader/src/main/java/org/ttrssreader/utils/FileUtils.java +++ b/ttrssreader/src/main/java/org/ttrssreader/utils/FileUtils.java @@ -90,10 +90,6 @@ public static long downloadToFile(String downloadUrl, File file, long maxSize, l InputStream is = null; try (FileOutputStream fos = new FileOutputStream(file)) { - // In case where http and https is supported we might get just // as URL prefix, so we use a secure default... - if(downloadUrl.startsWith("//")) - downloadUrl = "https:" + downloadUrl; - URL url = new URL(downloadUrl); URLConnection connection = Controller.getInstance().openConnection(url);