From e1cc4f0d9f2afe706e9918b0bb248706c3e109d1 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 23 Sep 2017 15:07:13 +0300 Subject: [PATCH] Broken implementation of etag/last_mod request. --- Cargo.toml | 1 + src/index_feed.rs | 31 ++++++++++++++++++++++++++----- src/lib.rs | 6 ++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39e60e4..32f7478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ structopt-derive = "0.1.0" log = "0.3.8" loggerv = "0.3.0" reqwest = "0.7.3" +hyper = "0.11.2" diesel = { version = "0.16.0", features = ["sqlite", "deprecated-time", "chrono"] } diesel_codegen = { version = "0.16.0", features = ["sqlite"] } time = "0.1.38" diff --git a/src/index_feed.rs b/src/index_feed.rs index cc8687d..631467f 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -119,19 +119,40 @@ pub fn index_loop(db: &SqliteConnection) -> Result<()> { } // TODO: make it into an iterator that yields reqwest::response +// TODO: After fixing etag/lmod, add sent_etag:bool arg and logic to bypass it. pub fn fetch_feeds(connection: &SqliteConnection) -> Result> { + use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified}; + let mut results = Vec::new(); let mut feeds = dbqueries::get_sources(connection)?; for feed in feeds.iter_mut() { - // TODO sent etag headers - let req = reqwest::get(feed.uri())?; + let client = reqwest::Client::new()?; + let mut headers = Headers::new(); - // TODO match on status() - if req.status() == reqwest::StatusCode::NotModified { - continue; + if let Some(foo) = feed.http_etag() { + headers.set(ETag(EntityTag::new(true, foo.to_owned()))); } + + if let Some(foo) = feed.last_modified() { + headers.set(LastModified(foo.parse::()?)); + } + + info!("{:?}", headers); + // FIXME: I have fucked up something here. + // Getting back 200 codes even though I supposedly sent etags. + let req = client.get(feed.uri())?.headers(headers).send()?; + info!("{}", req.status()); + + // TODO match on more stuff + match req.status() { + reqwest::StatusCode::NotModified => { + continue; + } + _ => (), + }; + feed.update_etag(connection, &req)?; results.push((req, feed.clone())); } diff --git a/src/lib.rs b/src/lib.rs index 5ad32c7..3068b6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,10 @@ extern crate diesel; extern crate diesel_codegen; extern crate chrono; +extern crate hyper; extern crate reqwest; extern crate rss; +extern crate time; extern crate xdg; pub mod cli; @@ -37,6 +39,8 @@ pub mod errors { use std::io; use rss; use chrono; + use hyper; + use time; use diesel::migrations::RunMigrationsError; use diesel::result; @@ -49,6 +53,8 @@ pub mod errors { RSSError(rss::Error); DieselResultError(result::Error); ChronoError(chrono::ParseError); + DurationError(time::OutOfRangeError); + HyperError(hyper::error::Error); } } }