From ca06a16bd9f6766428c20fd2c2e7b6e0008ffce5 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 29 Dec 2017 02:45:02 +0200 Subject: [PATCH] Closes #2. Kudo to @jwykeham for the fix!. --- TODO.md | 3 --- hammond-data/src/feed.rs | 23 ++++++++++--------- hammond-data/src/models/queryables.rs | 32 +++++++++++++++------------ hammond-downloader/src/downloader.rs | 2 +- hammond-gtk/src/utils.rs | 2 +- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/TODO.md b/TODO.md index 6106680..5190a13 100644 --- a/TODO.md +++ b/TODO.md @@ -31,6 +31,3 @@ - [ ] Make Podcast cover fetchng and loading not block the execution of the program at startup. - [ ] Lazy evaluate episode loading based on the show_widget's scrolling. -**FIXME:** - -- [ ] Fix Etag/Last-modified implementation. [#2](https://gitlab.gnome.org/alatiera/Hammond/issues/2) diff --git a/hammond-data/src/feed.rs b/hammond-data/src/feed.rs index f613f16..efc77cb 100644 --- a/hammond-data/src/feed.rs +++ b/hammond-data/src/feed.rs @@ -26,7 +26,7 @@ pub struct Feed { impl Feed { /// Constructor that consumes a `Source` and returns the corresponding `Feed` struct. pub fn from_source(s: Source) -> Result { - s.into_feed() + s.into_feed(false) } /// Constructor that consumes a `Source` and a `rss::Channel` returns a `Feed` struct. @@ -112,19 +112,22 @@ pub fn index(feed: &Feed) { /// Consume a `Source` and return a `Feed`. fn fetch(source: Source) -> Result { - let uri = source.uri().to_owned(); - let feed = Feed::from_source(source); - if feed.is_err() { - error!("Error While trying to fetch from source url: {}.", uri); - } - feed + Feed::from_source(source) } /// Index a "list" of `Source`s. pub fn index_loop>(sources: S) { sources .into_par_iter() - .filter_map(|x| fetch(x).ok()) + .filter_map(|x| { + let foo = fetch(x); + if let Err(err) = foo { + error!("Error: {}", err); + None + } else { + foo.ok() + } + }) .for_each(|x| index(&x)); info!("Indexing done."); @@ -223,8 +226,8 @@ mod tests { assert_eq!(s1, s2); assert_eq!(s1.id(), s2.id()); - let f1 = s1.into_feed().unwrap(); - let f2 = s2.into_feed().unwrap(); + let f1 = s1.into_feed(false).unwrap(); + let f2 = s2.into_feed(false).unwrap(); let p1 = f1.get_podcast().unwrap(); let p2 = { diff --git a/hammond-data/src/models/queryables.rs b/hammond-data/src/models/queryables.rs index d4409cd..b83ba52 100644 --- a/hammond-data/src/models/queryables.rs +++ b/hammond-data/src/models/queryables.rs @@ -640,18 +640,22 @@ impl<'a> Source { /// /// Consumes `self` and Returns the corresponding `Feed` Object. // TODO: Refactor into TryInto once it lands on stable. - pub fn into_feed(mut self) -> Result { - use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified}; + pub fn into_feed(mut self, ignore_etags: bool) -> Result { + use reqwest::header::{EntityTag, Headers, HttpDate, IfModifiedSince, IfNoneMatch}; let mut headers = Headers::new(); - if let Some(foo) = self.http_etag() { - headers.set(ETag(EntityTag::new(true, foo.to_owned()))); - } + if !ignore_etags { + if let Some(foo) = self.http_etag() { + headers.set(IfNoneMatch::Items(vec![ + EntityTag::new(true, foo.to_owned()), + ])); + } - if let Some(foo) = self.last_modified() { - if let Ok(x) = foo.parse::() { - headers.set(LastModified(x)); + if let Some(foo) = self.last_modified() { + if let Ok(x) = foo.parse::() { + headers.set(IfModifiedSince(x)); + } } } @@ -663,17 +667,17 @@ impl<'a> Source { info!("GET to {} , returned: {}", self.uri(), req.status()); + self.update_etag(&req)?; + // TODO match on more stuff // 301: Permanent redirect of the url // 302: Temporary redirect of the url // 304: Up to date Feed, checked with the Etag // 410: Feed deleted - // match req.status() { - // reqwest::StatusCode::NotModified => (), - // _ => (), - // }; - - self.update_etag(&req)?; + match req.status() { + reqwest::StatusCode::NotModified => bail!("304, skipping.."), + _ => (), + }; let mut buf = String::new(); req.read_to_string(&mut buf)?; diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 2403cc9..7df62b6 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -214,7 +214,7 @@ mod tests { let sid = source.id().clone(); // Convert Source it into a Feed and index it - let feed = source.into_feed().unwrap(); + let feed = source.into_feed(true).unwrap(); index(&feed); // Get the Podcast diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index b99252d..cd77c1a 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -113,7 +113,7 @@ mod tests { let sid = source.id().clone(); // Convert Source it into a Feed and index it - let feed = source.into_feed().unwrap(); + let feed = source.into_feed(true).unwrap(); index(&feed); // Get the Podcast