summaryrefslogtreecommitdiff
path: root/support/pkgcheck/src/linkcheck.rs
blob: cc7aa36e318e8b105d7c50dc7e93e0c307a92640 (plain)
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
use threadpool::ThreadPool;

use colored::*;
use linkify::{LinkFinder, LinkKind};

use std::fs::File;
use std::io::prelude::*;


use reqwest::header;
use std::time::Duration;

use std::sync::Arc;
use std::sync::Mutex;

use std::sync::atomic::Ordering;

use rustc_hash::{FxHashMap, FxHashSet};

enum UrlStatus {
    Unknown,
    UrlOk,
    UrlError(String),
}

struct HashVal {
    paths: FxHashSet<String>,
    status: UrlStatus,
}

type UrlHash = FxHashMap<String, HashVal>;

pub struct LinkCheck {
    pool: Mutex<ThreadPool>,
    urlhash: Arc<Mutex<UrlHash>>,
    print_all: bool,
}

impl LinkCheck {
    pub fn new(num_threads: usize, print_all: bool) -> LinkCheck {
        openssl_probe::init_ssl_cert_env_vars();
        let pool = Mutex::new(ThreadPool::new(num_threads));
        LinkCheck {
            pool,
            urlhash: Arc::new(Mutex::new(FxHashMap::default())),
            print_all,
        }
    }

    pub fn check_urls(&self, fname: &str) {
        let print_all = self.print_all;
        if let Some(links) = get_links(fname) {
            for l in links {
                let urlhash = self.urlhash.clone();
                let fname_s = String::from(fname);
                self.pool.lock().unwrap().execute(move || {
                    check_link(&l, &fname_s, &urlhash, print_all);
                });
            }
        }
    }
}

impl Drop for LinkCheck {
    fn drop(&mut self) {
        //println!("Now dropping ...");
        let pool = self.pool.lock().unwrap();
        pool.join();
    }
}

fn check_link(url: &str, fname: &str, urlhash: &Arc<Mutex<UrlHash>>, print_all: bool) {
    let url = String::from(url);

    let mut run_check_link = false;

    // It is very important to keep the lock for the urlhash
    // only for a short period of time
    //
    // If we don't find the url in the urlhash then
    // we set `run_check_link` to `true` so that we will
    // check the url
    {
        let f = String::from(fname);

        let mut urlhash = urlhash.lock().unwrap();
        if !urlhash.contains_key(&url) {
            let mut hs = FxHashSet::default();
            hs.insert(f);
            let url1 = url.clone();

            urlhash.insert(
                url1,
                HashVal {
                    status: UrlStatus::Unknown,
                    paths: hs,
                },
            );
            run_check_link = true;
        } else if let Some(hs) = urlhash.get_mut(&url) {
            match &hs.status {
                UrlStatus::Unknown => {
                    hs.paths.insert(f);
                }
                UrlStatus::UrlOk => {
                    if print_all {
                        print_ok(no_colors!(), &url, &f);
                    };
                }
                UrlStatus::UrlError(e) => {
                    e0022!(f, e);
                }
            }
        }
    }

    //
    if run_check_link {
        match check_link_inner(&url, true) {
            UrlStatus::UrlOk => {
                let mut urlhash = urlhash.lock().unwrap();
                if let Some(mut hs) = urlhash.get_mut(&url) {
                    if print_all {
                        for p in hs.paths.iter() {
                            print_ok(no_colors!(), &url, p);
                        }
                    }
                    hs.status = UrlStatus::UrlOk;
                }
            }
            UrlStatus::UrlError(e) => {
                let mut urlhash = urlhash.lock().unwrap();
                if let Some(mut hs) = urlhash.get_mut(&url) {
                    for p in hs.paths.iter() {
                        e0022!(p, e);
                    }
                    hs.status = UrlStatus::UrlError(e);
                }
            }
            _ => (),
        }
    }
}

fn get_links(fname: &str) -> Option<Vec<String>> {
    let fhdl = File::open(fname);
    match fhdl {
        Ok(mut f) => {
            let mut buf = Vec::new();

            match f.read_to_end(&mut buf) {
                Ok(_bytes_read) => {
                    return get_links_inner(&String::from_utf8_lossy(&buf));
                }
                Err(e) => println!("Error reading file {}: {:?}", fname, e),
            }
        }
        Err(e) => println!("Error opening file {}: {}", fname, e),
    }

    None
}

// retrieves links in a string and then checks those links
fn get_links_inner(s: &str) -> Option<Vec<String>> {
    let mut finder = LinkFinder::new();
    finder.kinds(&[LinkKind::Url]);

    // finder.links() does the actual search for URLs
    let links: Vec<_> = finder.links(s).collect();
    let result: Vec<&str> = links.iter().map(|e| e.as_str()).collect();

    let mut links = vec![];
    for r in result {
        if !r.starts_with("http://") && !r.starts_with("https://") && !r.starts_with("ftp://") {
            continue;
        }

        // This is a workaround to prevent URLs ending with certain characters
        let url = r.trim_end_matches(|c| c == '。' || c == '`');

        links.push(String::from(url));
    }
    if !links.is_empty() {
        Some(links)
    } else {
        None
    }
}

fn check_link_inner(l: &str, head: bool) -> UrlStatus {
    let mut headers = header::HeaderMap::new();
    headers.insert(
        header::USER_AGENT,
        header::HeaderValue::from_static(
            "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0",
        ),
    );

    let default_policy = reqwest::redirect::Policy::default();
    let policy = reqwest::redirect::Policy::custom(move |attempt| {
        if attempt.url().host_str() == Some("127.0.0.1") {
            attempt.stop()
        } else {
            default_policy.redirect(attempt)
        }
    });

    let cb = reqwest::blocking::Client::builder()
        .gzip(true)
        .redirect(policy)
        .default_headers(headers)
        .timeout(Duration::from_secs(7))
        .build()
        .unwrap();
    // let url: Url =
    // match l.parse() {
    //    Ok(url) => url,
    //    Err(e) => {  println!("Error: {:?}", e); panic!("Scheiss"); }
    // };
    let resp = if head {
        cb.head(l).send()
    } else {
        cb.get(l).send()
    };
    match resp {
        Ok(s) => {
            if s.status().is_informational()
                || s.status().is_success()
                || s.status().is_redirection()
            {
                return UrlStatus::UrlOk;
            }

            if head {
                check_link_inner(l, false)
            } else {
                let e = format!("{}: {}", l, s.status());
                UrlStatus::UrlError(e)
            }
        }
        Err(e) => {
            let e = format!("{}", e);
            UrlStatus::UrlError(e)
        }
    }
}

fn print_ok(no_colors: bool, url: &str, f: &str) {
    if no_colors {
        println!("✔    {} in {}", &url, f);
    } else {
        //        println!("✔    {} in {}", &url, f);
        println!("{}    {} in {}", "✔".bright_green().bold(), url, f);
    }
}