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
pub mod archivableurl;
pub mod errors;

pub use crate::archivableurl::ArchivableUrl;
pub use crate::errors::Error;
use chrono::{NaiveDateTime, TimeDelta, Utc};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use serde::Deserialize;
use url::Url;

/// Maximum number of allowed request retries attempts.
const DEFAULT_MAX_REQUEST_RETRIES: u32 = 10;

/// Default threshold for considering an archive as recent, in days.
/// URLs with archives older than this threshold will be re-archived.
const DEFAULT_ARCHIVE_THRESHOLD_DAYS: i64 = 30;

/// User-agent to make requests from
const DEFAULT_USER_AGENT: &str =
    "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0";

/// Endpoint for the Wayback Machine archiving service
pub const WAYBACK_MACHINE_ARCHIVE_ENDPOINT: &str = "https://web.archive.org/save/";
/// Endpoint to check if an archive is present in the Wayback Machine
pub const WAYBACK_MACHINE_CHECK_ENDPOINT: &str =
    "https://web.archive.org/cdx/search/cdx?fl=timestamp&limit=-1&output=json&url=";

#[derive(Debug, Deserialize)]
struct WaybackCheckResponse(Vec<Vec<String>>);

/// Configuration for the Wayback Machine client
pub struct ClientConfig {
    archive_endpoint: String,
    check_endpoint: String,
    retry_policy: ExponentialBackoff,
    archive_threshold_timestamp: NaiveDateTime,
    user_agent: String,
}

/// Status of the archive request
pub enum ArchiveResult {
    Archived(String),
    RecentArchiveExists,
}

impl ClientConfig {
    /// Constructs a new `ClientConfig` with custom retry policy and user agent
    pub fn new(
        archive_endpoint: String,
        check_endpoint: String,
        max_request_retries: u32,
        archive_threshold_days: i64,
        user_agent: String,
    ) -> Self {
        ClientConfig {
            archive_endpoint: Url::parse(&archive_endpoint)
                .unwrap_or_else(|_| panic!("Invalid archive_endpoint URL: {}", archive_endpoint))
                .to_string(),
            check_endpoint: Url::parse(&check_endpoint)
                .unwrap_or_else(|_| panic!("Invalid check_endpoint URL: {}", check_endpoint))
                .to_string(),
            retry_policy: ExponentialBackoff::builder().build_with_max_retries(max_request_retries),
            archive_threshold_timestamp: (Utc::now()
                - TimeDelta::try_days(archive_threshold_days).unwrap())
            .naive_utc(),
            user_agent,
        }
    }
}
impl Default for ClientConfig {
    /// Constructs a default `ClientConfig` with default retry policy and user agent
    fn default() -> Self {
        ClientConfig {
            archive_endpoint: WAYBACK_MACHINE_ARCHIVE_ENDPOINT.into(),
            check_endpoint: WAYBACK_MACHINE_CHECK_ENDPOINT.into(),
            retry_policy: ExponentialBackoff::builder()
                .build_with_max_retries(DEFAULT_MAX_REQUEST_RETRIES),
            archive_threshold_timestamp: (Utc::now()
                - TimeDelta::try_days(DEFAULT_ARCHIVE_THRESHOLD_DAYS).unwrap())
            .naive_utc(),
            user_agent: DEFAULT_USER_AGENT.into(),
        }
    }
}

/// Wayback Machine client for archiving URLs
pub struct WaybackMachineClient {
    http_client: ClientWithMiddleware,
    client_config: ClientConfig,
}

impl WaybackMachineClient {
    /// Constructs a new `WaybackMachineClient` with the given configuration
    pub fn new(client_config: ClientConfig) -> Self {
        let http_client = ClientBuilder::new(
            reqwest::Client::builder()
                .user_agent(client_config.user_agent.clone())
                .build()
                .unwrap(),
        )
        .with(RetryTransientMiddleware::new_with_policy(
            client_config.retry_policy,
        ))
        .build();
        WaybackMachineClient {
            http_client,
            client_config,
        }
    }

    /// Checks if a recent archive exists for the given URL.
    ///
    /// If an archive exists, and it is newer than the configured archive threshold,
    /// the function returns Ok(()), indicating that the URL is considered recently archived.
    /// If no recent archive is found or the found archive is older than the threshold,
    /// it returns Err(Error::NoRecentArchive).
    ///
    /// https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server
    ///
    async fn check_recent_archive_exists(&self, url: &str) -> Result<(), Error> {
        let to_check = ArchivableUrl::parse(url)?;
        let response = self
            .http_client
            .get(format!("{}{}", self.client_config.check_endpoint, to_check))
            .send()
            .await
            .map_err(|err| Error::CannotCheckArchive(err.to_string()))?
            .json::<WaybackCheckResponse>()
            .await
            .map_err(|e| Error::CannotCheckArchive(e.to_string()))?;

        match &response.0[..] {
            [_, timestamp] if timestamp.len() == 1 => {
                let snapshot_timestamp =
                    NaiveDateTime::parse_from_str(&timestamp[0], "%Y%m%d%H%M%S")?;
                if snapshot_timestamp > self.client_config.archive_threshold_timestamp {
                    Ok(())
                } else {
                    Err(Error::NoRecentArchive(url.to_string()))
                }
            }
            _ => Err(Error::NoRecentArchive(url.to_string())),
        }
    }

    /// Checks if a recent Wayback Machine archive exists for the given URL
    /// and archives it if necessary.
    ///
    /// This function first checks if a recent archive exists for the URL by calling
    /// `check_recent_archive_exists`. If an archive does not exist or is older than the
    /// configured archive threshold, it proceeds to archive the URL.
    ///
    /// It returns an `ArchiveResult::Archived` if it archives the URL,
    /// or an `ArchiveResult::RecentArchiveExists` if a recent archive already exists.
    ///
    /// # Errors
    ///
    /// This method fails if the `url` provided is not well formatted
    /// of if there was an error while sending the request.
    ///
    /// # Example
    /// ```
    /// use waybackmachine_client::{ClientConfig, Error, WaybackMachineClient};
    ///
    /// # async fn run() -> Result<(), Error> {
    /// let wayback_client = WaybackMachineClient::new(ClientConfig::default());
    /// wayback_client.archive_url("https://www.openbookpublishers.com/").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn archive_url(&self, url: &str) -> Result<ArchiveResult, Error> {
        let to_archive = ArchivableUrl::parse(url)?;
        // get the latest location in case of a redirect
        let to_check = self
            .http_client
            .get(to_archive.as_str())
            .send()
            .await
            .map_or(to_archive.url.clone(), |response| response.url().clone());

        if self
            .check_recent_archive_exists(to_check.as_str())
            .await
            .is_ok()
        {
            return Ok(ArchiveResult::RecentArchiveExists);
        }

        let response = self
            .http_client
            .get(format!(
                "{}{}",
                self.client_config.archive_endpoint, to_archive
            ))
            .send()
            .await?;
        if !response.status().is_success() {
            // check just in case the request returns a false negative
            if self.check_recent_archive_exists(url).await.is_err() {
                return Err(Error::CannotArchive(
                    response.status().to_string(),
                    url.to_string(),
                ));
            }
        }
        Ok(ArchiveResult::Archived(response.url().to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockito::ServerGuard;
    use serde_json::{json, Value};

    const ARCHIVE_ROOT_PATH: &str = "/save/";
    const CHECK_ROOT_PATH: &str = "/cdx/search/cdx?fl=timestamp&limit=-1&output=json&url=";
    const MAX_REQUEST_RETRIES: u32 = 3;

    async fn mock_server() -> (ServerGuard, WaybackMachineClient) {
        let server = mockito::Server::new_async().await;
        let client_config = ClientConfig::new(
            format!("{}{}", server.url(), ARCHIVE_ROOT_PATH),
            format!("{}{}", server.url(), CHECK_ROOT_PATH),
            MAX_REQUEST_RETRIES,
            30,
            "TestUserAgent".to_string(),
        );
        let wayback_client = WaybackMachineClient::new(client_config);
        (server, wayback_client)
    }

    #[tokio::test]
    async fn test_archive_url_success() {
        let to_archive = "https://example.com/";
        let snapshot_timestamp = "20230227054528";
        let (mut server, wayback_client) = mock_server().await;

        let snapshot: Value = json!({
            "url": to_archive,
            "archived_snapshots": {
                "closest": {
                    "status": "200",
                    "available": true,
                    "url": format!("http://web.archive.org/web/{}/{}", snapshot_timestamp, to_archive),
                    "timestamp": snapshot_timestamp
                }
            }
        });
        let mock1 = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;
        let mock2 = server
            .mock("GET", &format!("{}{}", ARCHIVE_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .create_async()
            .await;

        assert!(wayback_client.archive_url(to_archive).await.is_ok());
        mock1.assert_async().await;
        mock2.assert_async().await;
    }

    #[tokio::test]
    async fn test_archive_url_no_scheme() {
        let to_archive = "example.com";
        let wayback_client = WaybackMachineClient::new(ClientConfig::default());

        assert_eq!(
            wayback_client.archive_url(to_archive).await.err().unwrap(),
            Error::InvalidUrl(to_archive.to_string())
        );
    }

    #[tokio::test]
    async fn test_archive_url_local_url() {
        let to_archive = "http://localhost/page";
        let wayback_client = WaybackMachineClient::new(ClientConfig::default());

        assert_eq!(
            wayback_client.archive_url(to_archive).await.err().unwrap(),
            Error::InvalidUrl(to_archive.to_string())
        );
    }

    #[tokio::test]
    async fn test_archive_url_failure() {
        let to_archive = "https://example.com/";
        let snapshot_timestamp = "20230227054528";
        let (mut server, wayback_client) = mock_server().await;

        let snapshot: Value = json!({
            "url": to_archive,
            "archived_snapshots": {
                "closest": {
                    "status": "200",
                    "available": true,
                    "url": format!("http://web.archive.org/web/{}/{}", snapshot_timestamp, to_archive),
                    "timestamp": snapshot_timestamp
                }
            }
        });
        let mock1 = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;
        let mock2 = server
            .mock("GET", &format!("{}{}", ARCHIVE_ROOT_PATH, to_archive)[..])
            .with_status(520)
            .expect_at_least(MAX_REQUEST_RETRIES as usize)
            .create_async()
            .await;
        // checking if it actually was archived after receiving an archiving error
        let mock3 = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;

        assert!(wayback_client.archive_url(to_archive).await.is_err());
        mock1.assert_async().await;
        mock2.assert_async().await;
        mock3.assert_async().await;
    }

    #[tokio::test]
    async fn test_check_recent_archive_exists_success() {
        let to_archive = "https://example.com/";
        let snapshot_timestamp = (Utc::now() - TimeDelta::try_days(1).unwrap())
            .format("%Y%m%d%H%M%S")
            .to_string();
        let (mut server, wayback_client) = mock_server().await;

        let snapshot: Value = json!([["timestamp"], [snapshot_timestamp]]);
        let mock = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;

        assert!(wayback_client
            .check_recent_archive_exists(to_archive)
            .await
            .is_ok());
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn test_check_recent_archive_exists_old_snapshot() {
        let to_archive = "https://example.com/";
        let snapshot_timestamp = (Utc::now() - TimeDelta::try_days(100).unwrap())
            .format("%Y%m%d%H%M%S")
            .to_string();
        let (mut server, wayback_client) = mock_server().await;

        let snapshot: Value = json!([["timestamp"], [snapshot_timestamp]]);
        let mock = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;

        assert!(wayback_client
            .check_recent_archive_exists(to_archive)
            .await
            .is_err());
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn test_check_recent_archive_exists_no_snapshot() {
        let to_archive = "https://example.com/";
        let (mut server, wayback_client) = mock_server().await;

        let snapshot: Value = json!([]);
        let mock = server
            .mock("GET", &format!("{}{}", CHECK_ROOT_PATH, to_archive)[..])
            .with_status(200)
            .with_body(snapshot.to_string())
            .create_async()
            .await;

        assert!(wayback_client
            .check_recent_archive_exists(to_archive)
            .await
            .is_err());
        mock.assert_async().await;
    }
}