Broken implementation of etag/last_mod request.

This commit is contained in:
Jordan Petridis 2017-09-23 15:07:13 +03:00
parent d76d367a9c
commit e1cc4f0d9f
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 33 additions and 5 deletions

View File

@ -10,6 +10,7 @@ structopt-derive = "0.1.0"
log = "0.3.8" log = "0.3.8"
loggerv = "0.3.0" loggerv = "0.3.0"
reqwest = "0.7.3" reqwest = "0.7.3"
hyper = "0.11.2"
diesel = { version = "0.16.0", features = ["sqlite", "deprecated-time", "chrono"] } diesel = { version = "0.16.0", features = ["sqlite", "deprecated-time", "chrono"] }
diesel_codegen = { version = "0.16.0", features = ["sqlite"] } diesel_codegen = { version = "0.16.0", features = ["sqlite"] }
time = "0.1.38" time = "0.1.38"

View File

@ -119,19 +119,40 @@ pub fn index_loop(db: &SqliteConnection) -> Result<()> {
} }
// TODO: make it into an iterator that yields reqwest::response // 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<Vec<(reqwest::Response, Source)>> { pub fn fetch_feeds(connection: &SqliteConnection) -> Result<Vec<(reqwest::Response, Source)>> {
use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified};
let mut results = Vec::new(); let mut results = Vec::new();
let mut feeds = dbqueries::get_sources(connection)?; let mut feeds = dbqueries::get_sources(connection)?;
for feed in feeds.iter_mut() { for feed in feeds.iter_mut() {
// TODO sent etag headers let client = reqwest::Client::new()?;
let req = reqwest::get(feed.uri())?; let mut headers = Headers::new();
// TODO match on status() if let Some(foo) = feed.http_etag() {
if req.status() == reqwest::StatusCode::NotModified { headers.set(ETag(EntityTag::new(true, foo.to_owned())));
continue;
} }
if let Some(foo) = feed.last_modified() {
headers.set(LastModified(foo.parse::<HttpDate>()?));
}
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)?; feed.update_etag(connection, &req)?;
results.push((req, feed.clone())); results.push((req, feed.clone()));
} }

View File

@ -20,8 +20,10 @@ extern crate diesel;
extern crate diesel_codegen; extern crate diesel_codegen;
extern crate chrono; extern crate chrono;
extern crate hyper;
extern crate reqwest; extern crate reqwest;
extern crate rss; extern crate rss;
extern crate time;
extern crate xdg; extern crate xdg;
pub mod cli; pub mod cli;
@ -37,6 +39,8 @@ pub mod errors {
use std::io; use std::io;
use rss; use rss;
use chrono; use chrono;
use hyper;
use time;
use diesel::migrations::RunMigrationsError; use diesel::migrations::RunMigrationsError;
use diesel::result; use diesel::result;
@ -49,6 +53,8 @@ pub mod errors {
RSSError(rss::Error); RSSError(rss::Error);
DieselResultError(result::Error); DieselResultError(result::Error);
ChronoError(chrono::ParseError); ChronoError(chrono::ParseError);
DurationError(time::OutOfRangeError);
HyperError(hyper::error::Error);
} }
} }
} }