1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use humansize::{file_size_opts, FileSize};
use thiserror::{self, Error};

#[allow(unused_imports)]
use crate::internal_prelude::*;
use crate::{config, load_filters, status_filters, task_expanders, task_filters};

pub type TaskFilters<JS, TS> = Vec<Box<dyn task_filters::Filter<JS, TS> + Send + Sync>>;
pub type StatusFilters<JS, TS> = Vec<Box<dyn status_filters::Filter<JS, TS> + Send + Sync>>;
pub type LoadFilters<JS, TS> = Vec<Box<dyn load_filters::Filter<JS, TS> + Send + Sync>>;
pub type TaskExpanders<JS, TS> = Vec<Box<dyn task_expanders::Expander<JS, TS> + Send + Sync>>;

pub type BoxedFn<T> = Box<dyn Fn() -> Box<T> + Send + Sync>;
pub type BoxedTaskFilter<JS, TS> = BoxedFn<dyn task_filters::Filter<JS, TS> + Send + Sync>;
pub type BoxedStatusFilter<JS, TS> = BoxedFn<dyn status_filters::Filter<JS, TS> + Send + Sync>;
pub type BoxedLoadFilter<JS, TS> = BoxedFn<dyn load_filters::Filter<JS, TS> + Send + Sync>;
pub type BoxedTaskExpander<JS, TS> = BoxedFn<dyn task_expanders::Expander<JS, TS> + Send + Sync>;

pub struct Job<JS: JobStateValues, TS: TaskStateValues> {
	pub url:       url::Url,
	pub settings:  config::CrawlingSettings,
	pub rules:     Box<dyn JobRules<JS, TS>>,
	pub job_state: JS,
}

impl<JS: JobStateValues, TS: TaskStateValues> Job<JS, TS> {
	pub fn new<R: JobRules<JS, TS>>(
		url: &str,
		settings: config::CrawlingSettings,
		rules: R,
		job_state: JS,
	) -> anyhow::Result<Job<JS, TS>> {
		let url = Url::parse(url).context("cannot parse url")?;

		Ok(Self { url, settings, rules: Box::new(rules), job_state })
	}
}

#[derive(Clone)]
pub struct ResolvedJob<JS: JobStateValues, TS: TaskStateValues> {
	pub url:      url::Url,
	pub settings: Arc<config::CrawlingSettings>,
	pub rules:    Arc<Box<dyn JobRules<JS, TS>>>,
	pub ctx:      JobCtx<JS, TS>,
}

impl<JS: JobStateValues, TS: TaskStateValues> From<Job<JS, TS>> for ResolvedJob<JS, TS> {
	fn from(mut job: Job<JS, TS>) -> ResolvedJob<JS, TS> {
		job.settings.build();
		let settings = Arc::new(job.settings);
		ResolvedJob {
			url:      job.url.clone(),
			settings: Arc::clone(&settings),
			rules:    Arc::new(job.rules),
			ctx:      JobCtx::new(job.url, settings, job.job_state, TS::default()),
		}
	}
}

pub trait JobRules<JS: JobStateValues, TS: TaskStateValues>: Send + Sync + 'static {
	fn task_filters(&self) -> TaskFilters<JS, TS>;
	fn status_filters(&self) -> StatusFilters<JS, TS>;
	fn load_filters(&self) -> LoadFilters<JS, TS>;
	fn task_expanders(&self) -> TaskExpanders<JS, TS>;
}

pub type BoxedJobRules<JS, TS> = Box<dyn JobRules<JS, TS>>;

#[derive(Error, Debug)]
pub enum Error {
	#[error(transparent)]
	Other(#[from] anyhow::Error),
	#[error("timeout during loading")]
	LoadTimeout,
	#[error("terminated by status filter {name:?}")]
	StatusFilterTerm { name: String },
	#[error("terminated by error in status filter(term_by_error=true) {name:?}")]
	StatusFilterTermByError {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
	#[error("terminated by panic in status filter {name:?}")]
	StatusFilterTermByPanic {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
	#[error("terminated by load filter {name:?}")]
	LoadFilterTerm { name: String },
	#[error("terminated by error in load filter(term_by_error=true) {name:?}")]
	LoadFilterTermByError {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
	#[error("terminated by panic in load filter {name:?}")]
	LoadFilterTermByPanic {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
	#[error("terminated by task expander {name:?}")]
	TaskExpanderTerm { name: String },
	#[error("terminated by error in task expander(term_by_error=true) {name:?}")]
	TaskExpanderTermByError {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
	#[error("terminated by panic in task expander {name:?}")]
	TaskExpanderTermByPanic {
		name:   String,
		#[source]
		source: anyhow::Error,
	},
}

pub type Result<T> = anyhow::Result<T, Error>;

#[derive(Error, Debug)]
pub enum ExtError {
	#[error(transparent)]
	Other(#[from] anyhow::Error),
	#[error("terminated")]
	Term,
}
pub type ExtResult<T> = std::result::Result<T, ExtError>;

#[derive(Error, Debug)]
pub enum JobError {
	#[error("job finished by soft timeout")]
	JobFinishedBySoftTimeout,
	#[error("job finished by hard timeout")]
	JobFinishedByHardTimeout,
}

#[derive(Clone, PartialEq, Debug, Copy)]
pub enum LinkTarget {
	JustResolveDNS = 0,
	Head           = 1,
	Load           = 2,
	HeadLoad       = 3,
	Follow         = 4,
	HeadFollow     = 5,
}

impl fmt::Display for LinkTarget {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{:?}", self)
	}
}

pub type LinkIter<'a> = Box<dyn Iterator<Item = &'a Link> + Send + 'a>;

#[derive(Clone)]
pub struct Link {
	pub url:             Url,
	pub rel:             String,
	pub alt:             String,
	pub text:            String,
	pub redirect:        usize,
	pub target:          LinkTarget,
	pub(crate) is_waker: bool,
}

impl Link {
	pub fn host(&self) -> Option<String> {
		Some(self.url.host()?.to_string().trim().to_lowercase())
	}
}

#[derive(Clone, Default)]
pub struct ResolveMetrics {
	pub duration: Duration,
}

#[derive(Clone)]
pub struct ResolveData {
	pub addrs:   Vec<SocketAddr>,
	pub metrics: ResolveMetrics,
}

#[derive(Clone)]
pub struct HttpStatus {
	pub started_at: Instant,
	pub code:       i32,
	pub headers:    http::HeaderMap<http::HeaderValue>,
	pub metrics:    StatusMetrics,
}

#[derive(Clone, Default)]
pub struct StatusMetrics {
	pub wait_duration: Duration,
	pub duration:      Duration,
}

#[derive(Clone, Default)]
pub struct LoadMetrics {
	pub wait_duration: Duration,
	pub duration:      Duration,
	pub read_size:     usize,
	pub write_size:    usize,
}

#[derive(Clone, Default)]
pub struct FollowMetrics {
	pub duration: Duration,
}

pub enum StatusResult {
	None,
	Ok(HttpStatus),
	Err(Error),
}

pub enum FollowResult {
	None,
	Ok(FollowData),
	Err(Error),
}

pub enum LoadResult {
	None,
	Ok(LoadData),
	Err(Error),
}

pub struct LoadData {
	pub metrics: LoadMetrics,
}

pub struct JobProcessing {
	pub resolve_data: ResolveData,
	pub head_status:  StatusResult,
	pub status:       StatusResult,
	pub load:         LoadResult,
	pub follow:       FollowResult,
	pub links:        Vec<Arc<Link>>,
}

impl JobProcessing {
	pub fn new(resolve_data: ResolveData) -> Self {
		Self {
			resolve_data,
			head_status: StatusResult::None,
			status: StatusResult::None,
			load: LoadResult::None,
			follow: FollowResult::None,
			links: vec![],
		}
	}
}

pub struct FollowData {
	pub metrics: FollowMetrics,
}

pub struct JobFinished {}

pub enum JobStatus {
	Processing(Result<JobProcessing>),
	Finished(std::result::Result<JobFinished, JobError>),
}

#[derive(Clone)]
pub struct Task {
	pub queued_at: Instant,
	pub link:      Arc<Link>,
	pub level:     usize,
}

pub struct JobUpdate<JS: JobStateValues, TS: TaskStateValues> {
	pub task:   Arc<Task>,
	pub status: JobStatus,
	pub ctx:    JobCtx<JS, TS>,
}

pub struct ParserTask {
	pub payload: Box<dyn FnOnce() -> Result<FollowData> + Send + 'static>,
	pub time:    Instant,
	pub res_tx:  Sender<ParserResponse>,
}

pub struct ParserResponse {
	pub payload:       Result<FollowData>,
	pub wait_duration: Duration,
	pub work_duration: Duration,
}

pub trait JobStateValues: Send + Sync + Clone + 'static {}

impl<T: Send + Sync + Clone + 'static> JobStateValues for T {}

pub trait TaskStateValues: Send + Sync + Clone + Default + 'static {}

impl<T: Send + Sync + Clone + Default + 'static> TaskStateValues for T {}

pub type JobSharedState = Arc<Mutex<HashMap<String, Box<dyn std::any::Any + Send + Sync>>>>;

#[derive(Clone)]
pub struct JobCtx<JS, TS> {
	pub settings:   Arc<config::CrawlingSettings>,
	pub started_at: Instant,
	pub root_url:   Url,
	pub shared:     JobSharedState,
	pub job_state:  Arc<Mutex<JS>>,
	pub task_state: TS,
	links:          Vec<Link>,
}

impl<JS: JobStateValues, TS: TaskStateValues> JobCtx<JS, TS> {
	pub fn new(root_url: Url, settings: Arc<config::CrawlingSettings>, job_state: JS, task_state: TS) -> Self {
		Self {
			started_at: Instant::now(),
			root_url,
			settings,
			shared: Arc::new(Mutex::new(HashMap::new())),
			job_state: Arc::new(Mutex::new(job_state)),
			task_state,
			links: vec![],
		}
	}

	pub fn timeout_remaining(&self, t: Duration) -> PinnedFut<()> {
		let elapsed = self.started_at.elapsed();

		Box::pin(async move {
			if t <= elapsed {
				return
			}
			tokio::time::sleep(t - elapsed).await;
		})
	}

	pub fn push_links(&mut self, links: Vec<Link>) {
		self.links.extend(links.into_iter())
	}

	pub fn consume_links(&mut self) -> Vec<Arc<Link>> {
		self.links.drain(0..).map(Arc::new).collect()
	}

	pub(crate) fn _consume_links(&mut self) -> Vec<Link> {
		self.links.drain(0..).collect()
	}
}

impl Link {
	pub(crate) fn new(
		href: &str,
		rel: &str,
		alt: &str,
		text: &str,
		redirect: usize,
		target: LinkTarget,
		parent: &Link,
	) -> Result<Self> {
		let link_str = href.splitn(2, '#').next().context("cannot prepare link")?;

		let url = Url::parse(link_str).unwrap_or(
			parent
				.url
				.join(link_str)
				.with_context(|| format!("cannot join relative href {} to {}", href, &parent.url))?,
		);

		Ok(Self {
			url,
			rel: String::from(rel),
			alt: alt.trim().to_string(),
			text: text.trim().to_string(),
			redirect,
			target,
			is_waker: false,
		})
	}

	pub(crate) fn new_abs(href: Url, rel: &str, alt: &str, text: &str, redirect: usize, target: LinkTarget) -> Self {
		Self {
			url: href,
			rel: String::from(rel),
			alt: alt.trim().to_string(),
			text: text.trim().to_string(),
			redirect,
			target,
			is_waker: false,
		}
	}
}

impl Task {
	pub(crate) fn new_root(url: &Url) -> Result<Task> {
		let link = Arc::new(Link::new_abs(url.clone(), "", "", "", 0, LinkTarget::Follow));

		Ok(Task { queued_at: Instant::now(), link, level: 0 })
	}

	pub(crate) fn new(link: Arc<Link>, parent: &Task) -> Result<Task> {
		let scheme = link.url.scheme();
		if scheme != "http" && scheme != "https" {
			return Err(anyhow!("invalid scheme {:#?} in {:#?}", scheme, link.url.as_str()).into())
		}

		Ok(Task { queued_at: Instant::now(), link, level: parent.level + 1 })
	}

	pub fn is_root(&self) -> bool {
		self.level == 0
	}
}

impl fmt::Display for ResolveData {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let addrs_str = self.addrs.iter().map(|a| a.ip().to_string()).collect::<Vec<String>>().join(", ");
		write!(f, "[{}] resolve {}ms", addrs_str, self.metrics.duration.as_millis())
	}
}

impl fmt::Display for StatusResult {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			StatusResult::Ok(ref r) => {
				write!(
					f,
					"[{}] wait {}ms / status {}ms",
					r.code,
					r.metrics.wait_duration.as_millis(),
					r.metrics.duration.as_millis()
				)
			}
			StatusResult::Err(ref err) => {
				write!(f, "[err]: {:#}", err)
			}
			_ => {
				write!(f, "")
			}
		}
	}
}

impl fmt::Display for LoadResult {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			LoadResult::Ok(ref r) => {
				let m = &r.metrics;
				write!(
					f,
					"loaded {}ms / write {} / read {}",
					m.duration.as_millis(),
					m.write_size.file_size(file_size_opts::CONVENTIONAL).unwrap(),
					m.read_size.file_size(file_size_opts::CONVENTIONAL).unwrap()
				)
			}
			LoadResult::Err(ref err) => {
				write!(f, "[err loading]: {:#}", err)
			}
			_ => {
				write!(f, "none")
			}
		}
	}
}

impl fmt::Display for FollowResult {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			FollowResult::Ok(ref r) => {
				let m = &r.metrics;
				write!(f, "parsed {}ms", m.duration.as_millis())
			}
			FollowResult::Err(ref err) => {
				write!(f, "[err following]: {:#}", err)
			}
			_ => {
				write!(f, "none")
			}
		}
	}
}

impl<JS: JobStateValues, TS: TaskStateValues> fmt::Display for JobUpdate<JS, TS> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self.status {
			JobStatus::Processing(Ok(ref r)) => {
				write!(
					f,
					"{} {} {} (load: {}) | (follow: {}) {}",
					r.resolve_data, r.head_status, r.status, r.load, r.follow, self.task
				)
			}
			JobStatus::Processing(Err(ref err)) => {
				write!(f, "[dns error : {:#}] {}", err, self.task)
			}
			JobStatus::Finished(Ok(ref r)) => {
				write!(f, "[finished] {} {}", self.task, r)
			}
			JobStatus::Finished(Err(ref err)) => {
				write!(f, "[finished : {:?}] {}", err, self.task)
			}
		}
	}
}

impl fmt::Display for Link {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{} {:?}", &self.url, self.target)
	}
}

impl fmt::Display for JobFinished {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "")
	}
}

impl fmt::Display for Task {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if self.level < 1 {
			write!(f, "{} (root)", &self.link.url)
		} else {
			write!(f, "{} ({}) {}", &self.link.url, self.level, &self.link.text[..32])
		}
	}
}