dweb 0.13.3

Decentralised web and storage library for Autonomi
Documentation
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
/*
 Copyright (c) 2025 Mark Hughes

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use actix_web::{
    HttpRequest, HttpResponse, HttpResponseBuilder,
    http::{StatusCode, header},
};
use color_eyre::eyre::{Result, eyre};
use mime;
use url::Url;

use autonomi::client::files::archive_public::ArchiveAddress;

use crate::client::DwebClient;
use crate::files::directory::{Tree, get_content_using_hex};
use crate::history::{History, HistoryAddress};
use crate::web::name::DwebHost;
use crate::web::name::decode_dweb_host;
use crate::{
    cache::directory_with_name::{
        DIRECTORY_VERSIONS_WITH_NAME, DirectoryVersionWithName, HISTORY_NAMES,
    },
    cache::directory_with_port::{
        DIRECTORY_VERSIONS_WITH_PORT, DirectoryVersionWithPort,
        key_for_directory_versions_with_port,
    },
    helpers::convert::address_tuple_from_address,
};

/// Fetch the requested resource from Autonomi or from cached data if available.
/// Assumes a dweb URL.
///
/// If as_website is false the URL is handled as an exact file path.
///
/// When as_website is true website specific handling such as redirecting
/// a directory path to an index.html etc is enabled.
///
/// TODO update to use response_with_body() instead of reason()
pub async fn fetch(client: &DwebClient, url: Url, as_website: bool) -> HttpResponse {
    println!("DEBUG fetch({url:?})...");
    let host = match url.host_str() {
        Some(host) => host,
        None => {
            return HttpResponseBuilder::new(StatusCode::BAD_REQUEST)
                .reason("bad host in URL")
                .finish();
        }
    };

    let dweb_host = match decode_dweb_host(host) {
        Ok(dweb_host) => dweb_host,
        Err(_e) => {
            return HttpResponseBuilder::new(StatusCode::NOT_FOUND)
                .reason("failed to decode web name")
                .finish();
        }
    };

    let mut reason: &'static str = "";
    let response = match directory_version_get(client, &dweb_host).await {
        // TODO cache function that wraps fetching the History/Tree
        Ok((_version, cache_version_entry)) => {
            match cache_version_entry
                .directory_tree
                .unwrap() // Guaranteed to be Some() by directory_version_get()
                .lookup_file(&url.path().to_string(), as_website)
            {
                Ok((datamap_chunk, data_address, content_type)) => {
                    let content_type = if content_type.is_some() {
                        content_type.unwrap().clone()
                    } else {
                        String::from("text/plain")
                    };

                    match get_content_using_hex(client, datamap_chunk, data_address).await {
                        Ok(bytes) => Some(
                            HttpResponseBuilder::new(StatusCode::OK)
                                .insert_header((header::CONTENT_TYPE, content_type.as_str()))
                                .body(bytes),
                        ),
                        Err(_e) => {
                            reason = "Failed to get file from network";
                            None
                        }
                    }
                }
                Err(_e) => {
                    reason = "Failed at lookup_or_fetch_file()";
                    None
                }
            }
        }
        Err(_e) => {
            reason = "Failed to get website version";
            None
        }
    };

    if response.is_some() {
        response.unwrap()
    } else {
        HttpResponseBuilder::new(StatusCode::NOT_FOUND)
            .reason(reason)
            .finish()
    }
}

/// Retrieve a given DirectoryVersionWithName from the cache, or if not access the network and
/// create a new DirectoryVersionWithName based on the DwebHost.
/// If the return is Ok(version, DirectoryVersionWithName), the DirectoryVersionWithName will have Some(Tree).
/// The version returned is the version retrieved, which is useful if dweb_host.version is None.
//
// Notes:
//   1) ensures that cache locks are released ASAP, and not held during network access.
//   2) may return an error, but still update the cache with an incomplete DirectoryVersionWithName
//      if it obtains the DirectoryVersionWithName.archive_address but not the directory_tree. A subsequent call
//      using the same DwebHost can then skip getting the archive_address and will just retry getting
//      the directory_tree.
// TODO refactor directory_version_get() to reduce complexity
pub async fn directory_version_get(
    client: &DwebClient,
    dweb_host: &DwebHost,
) -> Result<(u64, DirectoryVersionWithName)> {
    println!(
        "DEBUG directory_version_get([ {}, {}, {:?} ])...",
        dweb_host.dweb_host_string, dweb_host.dweb_name, dweb_host.version
    );

    // If the cache has all the info we return, or if it has an entry but no Tree we can use the addresses
    let (history_address, archive_address) =
        if let Ok(lock) = &mut DIRECTORY_VERSIONS_WITH_NAME.lock() {
            if let Some(cached_directory_version) = lock.get(&dweb_host.dweb_host_string) {
                if cached_directory_version.directory_tree.is_some() {
                    // Version 0 is ok here because if we have the tree we will already have cached the version
                    return Ok((0, cached_directory_version.clone()));
                } else {
                    (
                        Some(cached_directory_version.history_address),
                        Some(cached_directory_version.archive_address),
                    )
                }
            } else {
                (None, None)
            }
        } else {
            (None, None)
        };

    let history_address = if history_address.is_none() {
        // We need the history to get either the ArchiveAddress and/or the Tree
        if let Ok(lock) = &mut HISTORY_NAMES.lock() {
            if let Some(history_address) = lock.get(&dweb_host.dweb_name).copied() {
                history_address
            } else {
                return Err(eyre!(format!(
                    "unknown DWEB-NAME '{}'",
                    dweb_host.dweb_name
                )));
            }
        } else {
            return Err(eyre!(format!("failed to access DWEB-NAME cache",)));
        }
    } else {
        history_address.unwrap()
    };

    // At this point we have at least a history address
    if archive_address.is_some() {
        let archive_address = archive_address.unwrap();
        let directory_tree =
            match History::<Tree>::raw_trove_download(client, archive_address).await {
                Ok(directory_tree) => directory_tree,
                Err(e) => return Err(eyre!("failed to download directory from network: {e}")),
            };
        return update_cached_directory_version_with_name(
            &dweb_host,
            history_address,
            archive_address,
            Some(directory_tree),
        );
    } else {
        // TODO using dweb_host.version.is_none() for ignore pointer would ensures all versions
        // TODO are available even if the pointer is out of date, but this takes more than 20s.
        // TODO If apps cache the pointer counter, provide a way they can pass that for minimum_entry_index
        // TODO so that from_history_address() never has to wait while walking the graph, and
        // TODO can know the pointer is up-to-date from the minimum_entry_index

        // TODO this avoids issue where pointer is not up-to-date but makes the first load take a while
        let (ignore_pointer, minimum_entry_index) = if dweb_host.version.is_some() {
            (false, dweb_host.version.unwrap() + 1)
        } else {
            (true, 0)
        };

        // TODO this will load fast but may be missing later updates if the pointer
        // TODO isn't up-to-date on the network
        let (ignore_pointer, minimum_entry_index) = (false, 0);

        let mut history = match History::<Tree>::from_history_address(
            client.clone(),
            history_address,
            ignore_pointer,
            minimum_entry_index,
        )
        .await
        {
            Ok(history) => history,
            Err(e) => {
                return Err(eyre!(
                    "failed to get History for DWEB-NAME '{}': {e}",
                    dweb_host.dweb_name,
                ));
            }
        };

        if let Some(version) = dweb_host.version {
            if let Ok(history_versions) = history.num_versions() {
                if history_versions == 0 {
                    return Err(eyre!("History is empty - no website to display"));
                } else if version > history_versions {
                    return Err(eyre!(
                        "Invalid version {version}, highest version is {history_versions}"
                    ));
                } else if version < 1 {
                    return Err(eyre!("Invalid version {version}, lowest version is 1"));
                };
            }
        }

        let (archive_address, directory_tree, version) =
            match history.fetch_version_trove(dweb_host.version).await {
                Some(directory_tree) => match history.get_cached_version() {
                    Some(cached_version) => (
                        cached_version.trove_address(),
                        directory_tree,
                        cached_version.version,
                    ),
                    None => return Err(eyre!("History failed to get_cached_version()")),
                },
                None => return Err(eyre!("History failed to fetch_version_metadata()")),
            };

        // When retrieving the most recent version, ensure that the corresponding versioned DwebHost is cached
        let default_result = update_cached_directory_version_with_name(
            &dweb_host,
            history_address,
            archive_address,
            Some(directory_tree.clone()),
        );

        // When retrieving the most recent version, ensure that the corresponding versioned DwebHost is cached
        if dweb_host.version.is_none() {
            let versioned_host = format!("v{version}.{}", dweb_host.dweb_host_string);
            let versioned_dweb_host = DwebHost {
                dweb_host_string: versioned_host,
                dweb_name: dweb_host.dweb_name.clone(),
                version: Some(version),

                #[cfg(feature = "fixed-dweb-hosts")]
                // Development build feature for non-versioned Tree references
                is_fixed_dweb_host: false,
            };

            return update_cached_directory_version_with_name(
                &versioned_dweb_host,
                history_address,
                archive_address,
                Some(directory_tree),
            );
        }

        return default_result;
    };
}

/// Get a Tree from the network using the address and if a history, the optional version
pub async fn get_directory_tree_for_address_string(
    client: &DwebClient,
    // The hex representation of either a HistoryAddress or an ArchiveAddress
    address: &String,
    // Optional version when the address is a HistoryAddress
    version: Option<u64>,
) -> Result<(Option<HistoryAddress>, ArchiveAddress, Option<u64>, Tree)> {
    println!("DEBUG get_directory_tree_for_address_string({address}, {version:?})...");

    let (history_address, archive_address) = address_tuple_from_address(address);
    if history_address.is_none() && archive_address.is_none() {
        let msg = format!("Not a history or archive address: {address}");
        return Err(eyre!(msg));
    };

    if archive_address.is_some() {
        return Ok((
            None,
            archive_address.unwrap(),
            version,
            Tree::from_archive_address(client, archive_address.unwrap()).await?,
        ));
    }

    let ignore_pointer = false; // Fast but may not get most recent version when version is None
    let minimum_entry_index = version.unwrap_or(0);
    match History::<Tree>::from_history_address(
        client.clone(),
        history_address.unwrap(),
        ignore_pointer,
        minimum_entry_index,
    )
    .await
    {
        Ok(mut history) => {
            let (archive_address, directory_tree, version) =
                match history.fetch_version_trove(version).await {
                    Some(directory_tree) => match history.get_cached_version() {
                        Some(cached_version) => (
                            cached_version.trove_address(),
                            directory_tree,
                            cached_version.version,
                        ),
                        None => return Err(eyre!("History failed to get_cached_version()")),
                    },
                    None => return Err(eyre!("History failed to fetch_version_metadata()")),
                };
            Ok((
                history_address,
                archive_address,
                Some(version),
                directory_tree,
            ))
        }
        Err(e) => Err(e),
    }
}

pub fn update_cached_directory_version_with_name(
    dweb_host: &DwebHost,
    history_address: HistoryAddress,
    archive_address: ArchiveAddress,
    directory_tree: Option<Tree>,
) -> Result<(u64, DirectoryVersionWithName)> {
    // TODO may need both version_retrieved and version_requested in DirectoryVersionWithName
    let new_directory_version =
        DirectoryVersionWithName::new(&dweb_host, history_address, archive_address, directory_tree);

    match &mut DIRECTORY_VERSIONS_WITH_NAME.lock() {
        Ok(lock) => {
            #[cfg(feature = "development")]
            println!(
                "DEBUG directory version (v {:?}) added to cache for host: {}",
                dweb_host.version, dweb_host.dweb_host_string
            );

            lock.insert(
                dweb_host.dweb_host_string.clone(),
                new_directory_version.clone(),
            );
        }
        Err(e) => {
            return Err(eyre!(
                "Failed to store DirectoryVersionWithName in cache for DWEB-NAME '{}': {e}",
                dweb_host.dweb_name
            ));
        }
    }

    Ok((dweb_host.version.unwrap_or(0), new_directory_version))
}

pub fn update_cached_directory_version_with_port(
    port: u16,
    history_address: Option<HistoryAddress>,
    archive_address: ArchiveAddress,
    version: Option<u64>,
    directory_tree: Tree,
) -> Result<(u64, DirectoryVersionWithPort)> {
    // TODO may need both version_retrieved and version_requested in DirectoryVersionWithName
    let new_directory_version = DirectoryVersionWithPort::new(
        port,
        history_address,
        version,
        archive_address,
        directory_tree,
    );

    match &mut DIRECTORY_VERSIONS_WITH_PORT.lock() {
        Ok(lock) => {
            #[cfg(feature = "development")]
            println!(
                "DEBUG directory version with port (v {version:?}) added to cache for port: {port}",
            );

            let key = key_for_directory_versions_with_port(archive_address);
            lock.insert(key, new_directory_version.clone());
        }
        Err(e) => {
            return Err(eyre!(
                "Failed to store DirectoryVersionWithPort in cache for PORT '{port}': {e}",
            ));
        }
    }

    Ok((version.unwrap_or(0), new_directory_version))
}

#[cfg(not(feature = "development"))]
const NO_REASON: &str = "";

pub fn response_with_body(status: StatusCode, reason: Option<String>) -> HttpResponse {
    if let Some(reason) = reason {
        let html = format!(
            "<html>
            <head><title>{status}</title></head>
            <body>
            <p>{status}</p>
            <p>{reason}</p>
            </body>
            </html>",
        );
        #[cfg(feature = "development")]
        let reason = Box::leak(reason.into_boxed_str()); // This memory is leaked, hence development only

        #[cfg(not(feature = "development"))]
        let reason = NO_REASON;

        return HttpResponseBuilder::new(status)
            .append_header(header::ContentType(mime::TEXT_HTML))
            .reason(reason)
            .body(html);
    } else {
        HttpResponseBuilder::new(status).finish()
    }
}

pub fn response_redirect(
    req: &HttpRequest,
    host: &str,
    port: Option<u16>,
    path: Option<String>,
) -> HttpResponse {
    let scheme = &String::from(req.full_url().scheme());
    let port_str = if let Some(port) = port {
        &format!(":{port}")
    } else {
        if let Some(port) = req.full_url().port() {
            &format!(":{port}")
        } else {
            ""
        }
    };

    #[cfg(feature = "development")]
    println!("DEBUG req.full_url(): {}", req.full_url());
    println!("DEBUG scheme   : {scheme}");
    println!("DEBUG port     : {port_str}");

    let mut redirect_url = String::from(scheme) + "://" + host;
    redirect_url = redirect_url + port_str;
    if let Some(path) = path {
        redirect_url = redirect_url + &path;
    }

    #[cfg(feature = "development")]
    println!("DEBUG response_redirect() redirecting to {redirect_url}");
    HttpResponseBuilder::new(StatusCode::SEE_OTHER)
        .insert_header((header::LOCATION, redirect_url))
        .finish()
}