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
//! Configurable parallel web crawler
//! 
//! # Example
//! 
//! ```rust,no_run
//! extern crate url_crawler;
//! use url_crawler::*;
//! use std::sync::Arc;
//! 
//! /// Function for filtering content in the crawler before a HEAD request.
//! /// 
//! /// Only allow directory entries, and files that have the `deb` extension.
//! fn apt_filter(url: &Url) -> bool {
//!     let url = url.as_str();
//!     url.ends_with("/") || url.ends_with(".deb")
//! }
//! 
//! pub fn main() {
//!     // Create a crawler designed to crawl the given website.
//!     let crawler = Crawler::new("http://apt.pop-os.org/".to_owned())
//!         // Use four threads for fetching
//!         .threads(4)
//!         // Check if a URL matches this filter before performing a HEAD request on it.
//!         .pre_fetch(Arc::new(apt_filter))
//!         // Initialize the crawler and begin crawling. This returns immediately.
//!         .crawl();
//! 
//!     // Process url entries as they become available
//!     for file in crawler {
//!         println!("{:#?}", file);
//!     }
//! }
//! ```

#[macro_use]
extern crate bitflags;
extern crate chrono;
extern crate crossbeam_channel;
extern crate reqwest;
extern crate url_scraper;

mod scraper;

pub use reqwest::{Url, header};
use channel::Receiver;
use chrono::{DateTime, FixedOffset};
use crossbeam_channel as channel;
use reqwest::Client;
use reqwest::header::*;
use scraper::Scraper;
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;
use url_scraper::UrlScraper;

bitflags! {
    /// Flags for controlling the behavior of the crawler.
    pub struct Flags: u8 {
        /// Enable crawling across domains.
        const CROSS_DOMAIN = 1;
        /// Enable crawling outside of the specified directory.
        const CROSS_DIR = 2;
    }
}

pub type ErrorsCallback = Arc<Fn(Error) -> bool + Send + Sync>;
pub type PreFetchCallback = Arc<Fn(&Url) -> bool + Send + Sync>;
pub type PostFetchCallback = Arc<Fn(&Url, &HeaderMap) -> bool + Send + Sync>;

/// Defines whether to crawl from a single source, or from multiple sources.
/// 
/// Both the `From<String>` and `From<Vec<String>>` traits are implemented for this.
/// 
/// ```rust
/// use url_crawler::CrawlerSource;
/// 
/// let single: String = "url".into();
/// let multiple: Vec<String> = vec![
///     "url1".into(),
///     "url2".into()
/// ];
/// 
/// // Get a source from a `String`.
/// let source: CrawlerSource = single.into();
/// assert_eq!(CrawlerSource::Single("url".into()), source);
/// 
/// // Get a source from a `Vec<String>`.
/// let source: CrawlerSource = multiple.into();
/// assert_eq!(
///     CrawlerSource::Multiple(vec!["url1".into(), "url2".into()]),
///     source
/// );
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum CrawlerSource {
    Single(String),
    Multiple(Vec<String>)
}

impl From<String> for CrawlerSource {
    fn from(s: String) -> Self {
        CrawlerSource::Single(s)
    }
}

impl From<Vec<String>> for CrawlerSource {
    fn from(s: Vec<String>) -> Self {
        CrawlerSource::Multiple(s)
    }
}

/// A configurable parallel web crawler.
/// 
/// Crawling does not occur until this type is consumed by the `crawl` method.
pub struct Crawler {
    urls: CrawlerSource,
    threads: usize,
    flags: Flags,
    errors: ErrorsCallback,
    pre_fetch: PreFetchCallback,
    post_fetch: PostFetchCallback,
}

impl Crawler {
    /// Initializes a new crawler with a default thread count of `4`.
    pub fn new(source: impl Into<CrawlerSource>) -> Self {
        Crawler {
            urls: source.into(),
            threads: 4,
            flags: Flags::empty(),
            errors: Arc::new(|_| true),
            pre_fetch: Arc::new(|_| true),
            post_fetch: Arc::new(|_, _| true),
        }
    }

    /// Set flags for configuring the crawler.
    pub fn flags(mut self, flags: Flags) -> Self {
        self.flags = flags;
        self
    }

    /// Specifies the number of fetcher threads to use.
    /// 
    /// # Notes
    /// - If the input is `0`, `1` thread will be used.
    /// - The default thread count is `4` when not using this method.
    pub fn threads(mut self, threads: usize) -> Self {
        self.threads = if threads == 0 { 1 } else { threads };
        self
    }

    /// Allow the caller to handle errors.
    /// 
    /// # Notes
    /// Returning `false` will stop the crawler.
    pub fn errors(mut self, errors: ErrorsCallback) -> Self {
        self.errors = errors;
        self
    }

    /// Enables filtering items based on their filename.
    /// 
    /// # Notes
    /// Returning `false` will prevent the item from being fetched.
    pub fn pre_fetch(mut self, pre_fetch: PreFetchCallback) -> Self {
        self.pre_fetch = pre_fetch;
        self
    }

    /// Enables filtering items based on their filename and requested headers.
    /// 
    /// # Notes
    /// Returning `false` will prevent the item from being scraped / returned.
    pub fn post_fetch(mut self, post_fetch: PostFetchCallback) -> Self {
        self.post_fetch = post_fetch;
        self
    }

    /// Initializes the crawling, returning an iterator of discovered files.
    /// 
    /// The crawler will continue to crawl in background threads even while the iterator
    /// is not being pulled from.
    pub fn crawl(self) -> CrawlIter {
        let client_ = Arc::new(Client::new());

        let threads = self.threads;
        let pre_fetch = self.pre_fetch;
        let post_fetch = self.post_fetch;
        let errors = self.errors;
        let flags = self.flags;
        let (scraper_tx, scraper_rx) = channel::unbounded::<String>();
        let (fetcher_tx, fetcher_rx) = channel::bounded::<Url>(threads * 4);
        let (output_tx, output_rx) = channel::bounded::<UrlEntry>(threads * 4);
        let state = Arc::new(AtomicUsize::new(0));
        let kill = Arc::new(AtomicBool::new(false));

        match self.urls {
            CrawlerSource::Single(url) => scraper_tx.send(url),
            CrawlerSource::Multiple(urls) => for url in urls {
                scraper_tx.send(url);
            }
        }

        for _ in 0..threads {
            let fetcher = fetcher_rx.clone();
            let client = client_.clone();
            let scraper_tx = scraper_tx.clone();
            let output_tx = output_tx.clone();
            let status = state.clone();
            let kill = kill.clone();
            let pre_fetch = pre_fetch.clone();
            let post_fetch = post_fetch.clone();
            let errors = errors.clone();
            thread::spawn(move || {
                status.fetch_add(1, Ordering::SeqCst);
                for url in fetcher {
                    status.fetch_sub(1, Ordering::SeqCst);
                    if kill.load(Ordering::SeqCst) {
                        break
                    }

                    if ! (pre_fetch)(&url) {
                        status.fetch_add(1, Ordering::SeqCst);
                        continue
                    }

                    let head = match client.head(url.clone()).send() {
                        Ok(head) => head,
                        Err(why) => {
                            if ! errors(why.into()) {
                                kill.store(true, Ordering::SeqCst);
                            }
                            status.fetch_add(1, Ordering::SeqCst);
                            continue
                        }
                    };

                    let headers = head.headers();

                    if ! (post_fetch)(&url, head.headers()) {
                        status.fetch_add(1, Ordering::SeqCst);
                        continue
                    }

                    if let Some(content_type) = head.headers().get(CONTENT_TYPE).and_then(|c| c.to_str().ok()) {
                        if content_type.starts_with("text/html") {
                            scraper_tx.send(url.to_string());
                            output_tx.send(UrlEntry::Html { url });
                        } else {
                            let length: u64 = headers.get(CONTENT_LENGTH)
                                .and_then(|c| c.to_str().ok())
                                .and_then(|c| c.parse().ok())
                                .unwrap_or(0);

                            let modified = headers.get(LAST_MODIFIED)
                                .and_then(|c| c.to_str().ok())
                                .and_then(|c| DateTime::parse_from_rfc2822(c).ok());

                            output_tx.send(UrlEntry::File { url, length, modified, content_type: content_type.into() });
                        }
                    }

                    status.fetch_add(1, Ordering::SeqCst);
                }
            });
        }

        // Thread for scraping urls and avoiding repeats.
        let state_ = state.clone();
        let client = client_.clone();
        let kill_ = kill.clone();
        thread::spawn(move || {
            let mut visited = Vec::new();
            let jobs_complete = || {
                state_.load(Ordering::SeqCst) == threads
                    && scraper_rx.is_empty()
                    && fetcher_tx.is_empty()
            };

            while ! kill_.load(Ordering::SeqCst) {
                let url: String = match scraper_rx.try_recv() {
                    Some(url) => url,
                    None => {
                        if jobs_complete() { break }
                        thread::sleep(Duration::from_millis(1));
                        continue
                    }
                };

                match UrlScraper::new_with_client(&url, &client) {
                    Ok(scraper) => for url in Scraper::new(scraper.into_iter(), &url, &mut visited, flags) {
                        fetcher_tx.send(url);
                    }
                    Err(why) => if ! errors(why.into()) {
                        kill_.store(true, Ordering::SeqCst);
                    }
                }
            }
        });

        CrawlIter {
            recv: output_rx,
            kill
        }
    }
}

/// Iterator that returns fetched `UrlEntry` items.
/// 
/// On drop, the crawler's threads will be killed.
pub struct CrawlIter {
    recv: Receiver<UrlEntry>,
    kill: Arc<AtomicBool>,
}

impl Iterator for CrawlIter {
    type Item = UrlEntry;

    fn next(&mut self) -> Option<Self::Item> {
        self.recv.next()
    }
}

impl Drop for CrawlIter {
    fn drop(&mut self) {
        self.kill.store(true, Ordering::SeqCst);
    }
}

#[derive(Debug)]
/// URLs discovered found by the web crawler
pub enum UrlEntry {
    /// A URL with the "text/html" content type
    Html { url: Url },
    /// All other detected content.
    File { url: Url, content_type: String, length: u64, modified: Option<DateTime<FixedOffset>> }
}

/// Convenience function for getting the filename from a URL.
pub fn filename_from_url(url: &str) -> &str {
    if url.len() < 2 {
        url
    } else {
        &url[url.rfind('/').unwrap_or(0)+1..]
    }
}

#[derive(Debug)]
pub enum Error {
    Scraper { why: url_scraper::Error },
    Request { why: reqwest::Error }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "error while {}", match *self {
            Error::Scraper { ref why } => format!("scraping a page: {}", why),
            Error::Request { ref why } => format!("requesting content: {}", why),
        })
    }
}

impl From<url_scraper::Error> for Error {
    fn from(why: url_scraper::Error) -> Error {
        Error::Scraper { why }
    }
}

impl From<reqwest::Error> for Error {
    fn from(why: reqwest::Error) -> Error {
        Error::Request { why }
    }
}