| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use base64::engine::general_purpose; |
| 21 |
use base64::prelude::*; |
| 22 |
use diesel::SaveChangesDsl; |
| 23 |
use http::StatusCode; |
| 24 |
use http::header::{ |
| 25 |
AUTHORIZATION, ETAG, HeaderValue, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, |
| 26 |
USER_AGENT as USER_AGENT_HEADER, |
| 27 |
}; |
| 28 |
use rss::Channel; |
| 29 |
use std::str::FromStr; |
| 30 |
use url::Url; |
| 31 |
|
| 32 |
use crate::USER_AGENT; |
| 33 |
use crate::database::connection; |
| 34 |
use crate::errors::*; |
| 35 |
use crate::feed::{Feed, FeedBuilder}; |
| 36 |
use crate::make_id_wrapper; |
| 37 |
use crate::models::{NewSource, Save}; |
| 38 |
use crate::schema::source; |
| 39 |
|
| 40 |
make_id_wrapper!(SourceId); |
| 41 |
|
| 42 |
#[derive(Queryable, Identifiable, AsChangeset, PartialEq, Selectable)] |
| 43 |
#[diesel(table_name = source)] |
| 44 |
#[diesel(treat_none_as_null = true)] |
| 45 |
#[derive(Debug, Clone)] |
| 46 |
|
| 47 |
pub struct Source { |
| 48 |
id: SourceId, |
| 49 |
uri: String, |
| 50 |
last_modified: Option<String>, |
| 51 |
http_etag: Option<String>, |
| 52 |
} |
| 53 |
|
| 54 |
impl Save<Source> for Source { |
| 55 |
type Error = DataError; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
fn save(&self) -> Result<Source, Self::Error> { |
| 60 |
let db = connection(); |
| 61 |
let mut con = db.get()?; |
| 62 |
|
| 63 |
self.save_changes::<Source>(&mut con).map_err(From::from) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl Source { |
| 68 |
|
| 69 |
pub fn id(&self) -> SourceId { |
| 70 |
self.id |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
pub fn uri(&self) -> &str { |
| 75 |
&self.uri |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
pub fn set_uri(&mut self, uri: String) { |
| 80 |
self.uri = uri; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub fn last_modified(&self) -> Option<&str> { |
| 87 |
self.last_modified.as_deref() |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
pub fn set_last_modified(&mut self, value: Option<String>) { |
| 92 |
|
| 93 |
self.last_modified = value; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pub fn http_etag(&self) -> Option<&str> { |
| 100 |
self.http_etag.as_deref() |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub fn set_http_etag(&mut self, value: Option<&str>) { |
| 105 |
self.http_etag = value.map(|x| x.to_string()); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
fn update_etag(mut self, res: &reqwest::Response) -> Result<Self, DataError> { |
| 111 |
let headers = res.headers(); |
| 112 |
|
| 113 |
let etag = headers.get(ETAG).and_then(|h| h.to_str().ok()); |
| 114 |
let lmod = headers |
| 115 |
.get(LAST_MODIFIED) |
| 116 |
.and_then(|h| h.to_str().ok()) |
| 117 |
.map(From::from); |
| 118 |
|
| 119 |
if (self.http_etag() != etag) || (self.last_modified != lmod) { |
| 120 |
self.set_http_etag(etag); |
| 121 |
self.set_last_modified(lmod); |
| 122 |
self = self.save()?; |
| 123 |
} |
| 124 |
|
| 125 |
Ok(self) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
fn clear_etags(&mut self) { |
| 132 |
debug!("Source etags before clear: {:#?}", &self); |
| 133 |
self.http_etag = None; |
| 134 |
self.last_modified = None; |
| 135 |
} |
| 136 |
|
| 137 |
fn make_err(self, context: &str, code: StatusCode) -> DataError { |
| 138 |
DataError::HttpStatusGeneral { |
| 139 |
url: self.uri, |
| 140 |
status_code: code, |
| 141 |
context: context.into(), |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
fn match_status(mut self, res: reqwest::Response) -> Result<reqwest::Response, DataError> { |
| 156 |
let code = res.status(); |
| 157 |
|
| 158 |
if code.is_success() { |
| 159 |
|
| 160 |
self = self.update_etag(&res)? |
| 161 |
} else { |
| 162 |
match code.as_u16() { |
| 163 |
|
| 164 |
304 => self = self.update_etag(&res)?, |
| 165 |
|
| 166 |
_ => { |
| 167 |
self.clear_etags(); |
| 168 |
self = self.save()?; |
| 169 |
} |
| 170 |
}; |
| 171 |
}; |
| 172 |
|
| 173 |
match code.as_u16() { |
| 174 |
304 => { |
| 175 |
info!("304: Source, {} is up to date", self.uri()); |
| 176 |
return Err(DataError::FeedNotModified(self)); |
| 177 |
} |
| 178 |
301 | 308 => { |
| 179 |
info!("Feed was moved permanently."); |
| 180 |
self = self.update_url(&res)?; |
| 181 |
return Err(DataError::FeedRedirect(self)); |
| 182 |
} |
| 183 |
302 | 307 => { |
| 184 |
info!("302/307: Temporary Redirect."); |
| 185 |
return Err(DataError::FeedRedirect(self)); |
| 186 |
} |
| 187 |
401 => return Err(self.make_err("401: Unauthorized.", code)), |
| 188 |
403 => return Err(self.make_err("403: Forbidden.", code)), |
| 189 |
404 => return Err(self.make_err("404: Not found.", code)), |
| 190 |
408 => return Err(self.make_err("408: Request Timeout.", code)), |
| 191 |
410 => return Err(self.make_err("410: Feed was deleted..", code)), |
| 192 |
_ => info!("HTTP StatusCode: {}", code), |
| 193 |
}; |
| 194 |
|
| 195 |
Ok(res) |
| 196 |
} |
| 197 |
|
| 198 |
fn update_url(mut self, res: &reqwest::Response) -> Result<Self, DataError> { |
| 199 |
let code = res.status(); |
| 200 |
let headers = res.headers(); |
| 201 |
info!("HTTP StatusCode: {}", code); |
| 202 |
debug!("Headers {:#?}", headers); |
| 203 |
|
| 204 |
if let Some(url) = headers.get(LOCATION) { |
| 205 |
debug!("Previous Source: {:#?}", &self); |
| 206 |
|
| 207 |
self.set_uri(url.to_str()?.into()); |
| 208 |
self.clear_etags(); |
| 209 |
self = self.save()?; |
| 210 |
|
| 211 |
debug!("Updated Source: {:#?}", &self); |
| 212 |
info!( |
| 213 |
"Feed url of Source {}, was updated successfully.", |
| 214 |
self.uri() |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
Ok(self) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
pub fn from_url(uri: &str) -> Result<Source, DataError> { |
| 225 |
let url = Url::parse(uri)?; |
| 226 |
|
| 227 |
NewSource::new(&url).to_source() |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
pub async fn into_feed(self, client: &reqwest::Client) -> Result<Feed, DataError> { |
| 239 |
let id = self.id(); |
| 240 |
|
| 241 |
let resp = self.get_response(client).await?; |
| 242 |
let chan = response_to_channel(resp).await?; |
| 243 |
|
| 244 |
FeedBuilder::default() |
| 245 |
.channel(chan) |
| 246 |
.source_id(id) |
| 247 |
.build() |
| 248 |
.map_err(|err| DataError::BuilderError(format!("{err}"))) |
| 249 |
} |
| 250 |
|
| 251 |
async fn get_response(self, client: &reqwest::Client) -> Result<reqwest::Response, DataError> { |
| 252 |
let mut source = self; |
| 253 |
loop { |
| 254 |
match source.request_constructor(client).await { |
| 255 |
Ok(response) => return Ok(response), |
| 256 |
Err(err) => match err { |
| 257 |
DataError::FeedRedirect(s) => { |
| 258 |
info!("Following redirect..."); |
| 259 |
source = s; |
| 260 |
} |
| 261 |
e => return Err(e), |
| 262 |
}, |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
async fn request_constructor( |
| 268 |
self, |
| 269 |
client: &reqwest::Client, |
| 270 |
) -> Result<reqwest::Response, DataError> { |
| 271 |
let uri = Url::from_str(self.uri())?; |
| 272 |
let mut req = client.get(uri); |
| 273 |
|
| 274 |
if let Ok(url) = Url::parse(self.uri()) { |
| 275 |
if let Some(password) = url.password() { |
| 276 |
let mut auth = "Basic ".to_owned(); |
| 277 |
auth.push_str(&general_purpose::URL_SAFE.encode( |
| 278 |
|
| 279 |
format!("{}:{}", url.username().replace("%40", "@"), password), |
| 280 |
)); |
| 281 |
req = req.header(AUTHORIZATION, HeaderValue::from_str(&auth).unwrap()); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
req = req.header(USER_AGENT_HEADER, HeaderValue::from_static(USER_AGENT)); |
| 287 |
|
| 288 |
if let Some(etag) = self.http_etag() { |
| 289 |
req = req.header(IF_NONE_MATCH, HeaderValue::from_str(etag).unwrap()); |
| 290 |
} |
| 291 |
|
| 292 |
if let Some(lmod) = self.last_modified() { |
| 293 |
req = req.header(IF_MODIFIED_SINCE, HeaderValue::from_str(lmod).unwrap()); |
| 294 |
} |
| 295 |
|
| 296 |
let res = req.send().await?; |
| 297 |
self.match_status(res) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
async fn response_to_channel(res: reqwest::Response) -> Result<Channel, DataError> { |
| 302 |
use bytes::buf::Buf; |
| 303 |
let chunk = res.bytes().await?; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
Channel::read_from(chunk.reader()).map_err(From::from) |
| 309 |
} |
| 310 |
|
| 311 |
#[cfg(test)] |
| 312 |
mod tests { |
| 313 |
use super::*; |
| 314 |
use anyhow::Result; |
| 315 |
|
| 316 |
use crate::database::truncate_db; |
| 317 |
use crate::dbqueries; |
| 318 |
use crate::downloader::client_builder; |
| 319 |
use crate::utils::get_feed; |
| 320 |
|
| •321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
\ |
| 364 |
" |
| 365 |
|
| 366 |
|
| 367 |
|
| •368 |
|
| 369 |
} |