From 63497bd0493fc3db1763e81786b1d2dd240894d9 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 4 Oct 2020 22:17:52 +0200 Subject: [PATCH] Always parse feed and items in the same time to detect malformed feeds --- .../api/localfeed/LocalRSSDataSourceTest.kt | 16 ++++++++-------- .../readrops/api/localfeed/LocalRSSDataSource.kt | 7 +++---- .../app/repositories/LocalFeedRepository.java | 4 ++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt b/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt index 880471b5..9e90506d 100644 --- a/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt +++ b/api/src/androidTest/java/com/readrops/api/localfeed/LocalRSSDataSourceTest.kt @@ -51,7 +51,7 @@ class LocalRSSDataSourceTest { .addHeader(LibUtils.LAST_MODIFIED_HEADER, "Last-Modified") .setBody(Buffer().readFrom(stream))) - val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true) + val pair = localRSSDataSource.queryRSSResource(url.toString(), null) val feed = pair?.first!! assertEquals(feed.name, "Hacker News") @@ -74,7 +74,7 @@ class LocalRSSDataSourceTest { .setBody(Buffer().readFrom(stream))) val headers = Headers.headersOf(LibUtils.ETAG_HEADER, "ETag", LibUtils.LAST_MODIFIED_HEADER, "Last-Modified") - localRSSDataSource.queryRSSResource(url.toString(), headers, false) + localRSSDataSource.queryRSSResource(url.toString(), headers) val request = mockServer.takeRequest() @@ -90,7 +90,7 @@ class LocalRSSDataSourceTest { .addHeader(LibUtils.CONTENT_TYPE_HEADER, "application/feed+json") .setBody(Buffer().readFrom(stream))) - val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true)!! + val pair = localRSSDataSource.queryRSSResource(url.toString(), null)!! assertEquals(pair.first.name, "News from Flying Meat") assertEquals(pair.second.size, 10) @@ -100,7 +100,7 @@ class LocalRSSDataSourceTest { fun response304Test() { mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)) - val pair = localRSSDataSource.queryRSSResource(url.toString(), null, false) + val pair = localRSSDataSource.queryRSSResource(url.toString(), null) assertNull(pair) } @@ -109,14 +109,14 @@ class LocalRSSDataSourceTest { fun response404Test() { mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)) - localRSSDataSource.queryRSSResource(url.toString(), null, false) + localRSSDataSource.queryRSSResource(url.toString(), null) } @Test(expected = ParseException::class) fun noContentTypeTest() { mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)) - localRSSDataSource.queryRSSResource(url.toString(), null, false) + localRSSDataSource.queryRSSResource(url.toString(), null) } @Test(expected = ParseException::class) @@ -124,7 +124,7 @@ class LocalRSSDataSourceTest { mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) .addHeader("Content-Type", "")) - localRSSDataSource.queryRSSResource(url.toString(), null, false) + localRSSDataSource.queryRSSResource(url.toString(), null) } @Test(expected = UnknownFormatException::class) @@ -133,7 +133,7 @@ class LocalRSSDataSourceTest { .addHeader("Content-Type", "application/xml") .setBody(" ")) - localRSSDataSource.queryRSSResource(url.toString(), null, false) + localRSSDataSource.queryRSSResource(url.toString(), null) } @Test diff --git a/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt b/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt index 7104b40e..2a6b5af7 100644 --- a/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt +++ b/api/src/main/java/com/readrops/api/localfeed/LocalRSSDataSource.kt @@ -27,12 +27,11 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) { * Query RSS url * @param url url to query * @param headers request headers - * @param withItems parse items with their feed - * @return a Feed object with its items if specified by [withItems] + * @return a Feed object with its items */ @Throws(ParseException::class, UnknownFormatException::class, NetworkErrorException::class, IOException::class) @WorkerThread - fun queryRSSResource(url: String, headers: Headers?, withItems: Boolean): Pair>? { + fun queryRSSResource(url: String, headers: Headers?): Pair>? { val response = queryUrl(url, headers) return when { @@ -54,7 +53,7 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) { if (type == LocalRSSHelper.RSSType.UNKNOWN) throw UnknownFormatException("Unable to guess $url RSS type") val feed = parseFeed(ByteArrayInputStream(bodyArray), type, response) - val items = if (withItems) parseItems(ByteArrayInputStream(bodyArray), type) else listOf() + val items = parseItems(ByteArrayInputStream(bodyArray), type) response.body?.close() Pair(feed, items) diff --git a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java index dc21cb85..2167dd40 100644 --- a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java @@ -81,7 +81,7 @@ public class LocalFeedRepository extends ARepository { headers.add(LibUtils.IF_MODIFIED_HEADER, feed.getLastModified()); } - Pair> pair = dataSource.queryRSSResource(feed.getUrl(), headers.build(), true); + Pair> pair = dataSource.queryRSSResource(feed.getUrl(), headers.build()); if (pair != null) { insertNewItems(feed, pair.getSecond()); @@ -105,7 +105,7 @@ public class LocalFeedRepository extends ARepository { try { Pair> pair = dataSource.queryRSSResource(parsingResult.getUrl(), - null, false); + null); Feed feed = insertFeed(pair.getFirst(), parsingResult); if (feed != null) {