snix_castore_http/
lib.rs

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
pub mod app_state;
pub mod cli;
pub mod router;
pub mod routes;

use std::path;

use snix_castore::{
    blobservice::BlobService,
    directoryservice::{descend_to, DirectoryService},
    B3Digest, Directory, Node, Path, SymlinkTarget,
};

use axum::{
    body::Body,
    http::{header, StatusCode},
    response::{AppendHeaders, IntoResponse, Redirect, Response},
};
use axum_extra::{headers::Range, response::Html, TypedHeader};
use axum_range::{KnownSize, Ranged};
use path_clean::PathClean;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use tokio_util::io::ReaderStream;
use tracing::{debug, error, instrument, warn};

/// Helper function, descending from the given `root_node` to the `requested_path` specified.
/// Returns HTTP Responses or Status Codes.
/// If the path points to a regular file, it serves its contents.
/// If the path points to a symlink, it sends a redirect to the target (pretending `base_path`, if relative)
/// If the path points to a directory, files of `index_names` are tried,
/// if no files matched then a directory listing is returned if `auto_index` is enabled.
///
/// Uses the passed [BlobService] and [DirectoryService]
#[allow(clippy::too_many_arguments)]
#[instrument(level = "trace", skip_all, fields(base_path, requested_path), err)]
pub async fn get_root_node_contents<BS: BlobService, DS: DirectoryService, S: AsRef<str>>(
    blob_service: BS,
    directory_service: DS,
    base_path: &path::Path,
    root_node: &Node,
    requested_path: &Path,
    range_header: Option<TypedHeader<Range>>,
    index_names: &[S],
    auto_index: bool,
) -> Result<Response, StatusCode> {
    match root_node {
        Node::Directory { .. } => {
            let requested_node = descend_to(&directory_service, root_node.clone(), requested_path)
                .await
                .map_err(|err| {
                    error!(err=%err, "an error occured descending");
                    StatusCode::INTERNAL_SERVER_ERROR
                })?
                .ok_or_else(|| {
                    error!("requested path doesn't exist");
                    StatusCode::NOT_FOUND
                })?;
            match requested_node {
                Node::Directory { digest, .. } => {
                    let requested_directory = directory_service
                        .get(&digest)
                        .await
                        .map_err(|err| {
                            error!(err=%err, "an error occured getting the directory");
                            StatusCode::INTERNAL_SERVER_ERROR
                        })?
                        .ok_or_else(|| {
                            error!("directory doesn't exist");
                            StatusCode::NOT_FOUND
                        })?;

                    // If there was one or more index configured, try to find it
                    // in the directory requested by the client, by comparing the bytes
                    // of each directories immediate child's path with the bytes of the
                    // configured index name
                    for index_name in index_names {
                        if let Some((found_index_file_path, found_index_node)) = requested_directory
                            .nodes()
                            .find(|(path, _node)| index_name.as_ref().as_bytes() == path.as_ref())
                        {
                            match found_index_node {
                                Node::File { digest, size, .. } => {
                                    let extension = found_index_file_path
                                        .extension()
                                        .and_then(|b| std::str::from_utf8(b).ok());

                                    return respond_file(
                                        blob_service,
                                        extension,
                                        range_header,
                                        digest,
                                        *size,
                                    )
                                    .await;
                                }
                                _ => {
                                    debug!(
                                        path = %found_index_file_path,
                                        "One of the configured index names matched with a
                                        node located in the root node's directory which is
                                        not a file"
                                    );
                                }
                            }
                        }
                    }
                    if auto_index {
                        return respond_directory_list(&requested_directory, requested_path).await;
                    }
                    Err(StatusCode::FORBIDDEN)
                }
                Node::File { digest, size, .. } => {
                    respond_file(
                        blob_service,
                        requested_path
                            .extension()
                            .and_then(|b| std::str::from_utf8(b).ok()),
                        range_header,
                        &digest,
                        size,
                    )
                    .await
                }
                Node::Symlink { target } => {
                    let requested_path =
                        path::Path::new(OsStr::from_bytes(requested_path.as_bytes()));
                    respond_symlink(base_path, &target, Some(requested_path)).await
                }
            }
        }
        Node::File { digest, size, .. } => {
            if requested_path.to_string() == "" {
                respond_file(blob_service, None, range_header, digest, *size).await
            } else {
                warn!(
                    "The client requested a path but the configured root
                    node being served is a file"
                );
                Err(StatusCode::BAD_REQUEST)
            }
        }
        Node::Symlink { target } => {
            if requested_path.to_string() == "" {
                respond_symlink(base_path, target, None).await
            } else {
                warn!(
                    "The client requested a path but the configured root
                    node being served is a symlink"
                );
                Err(StatusCode::BAD_REQUEST)
            }
        }
    }
}

#[instrument(level = "trace", skip_all)]
pub async fn respond_symlink(
    base_path: &path::Path,
    symlink_target: &SymlinkTarget,
    requested_path: Option<&path::Path>,
) -> Result<Response, StatusCode> {
    if symlink_target.as_ref() == b"." {
        error!("There was a symlink with target '.'");
        return Err(StatusCode::INTERNAL_SERVER_ERROR);
    }

    let symlink_target_path = match std::str::from_utf8(symlink_target.as_ref()) {
        Ok(s) => path::Path::new(s),
        Err(_) => {
            error!("Symlink target contains invalid UTF-8");
            return Err(StatusCode::INTERNAL_SERVER_ERROR);
        }
    };

    let symlink_target_path = if symlink_target_path.is_absolute() {
        symlink_target_path.to_path_buf()
    } else if let Some(requested_path) = requested_path {
        let requested_path_parent = requested_path.parent().ok_or_else(|| {
            error!("failed to retrieve parent path for requested path");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;
        base_path
            .join(requested_path_parent)
            .join(symlink_target_path)
    } else {
        base_path.join(symlink_target_path)
    };

    let symlink_target_path = symlink_target_path.clean();

    if symlink_target_path.starts_with(path::Component::ParentDir) {
        error!("the symlink's target path points to a non-existing path");
        return Err(StatusCode::INTERNAL_SERVER_ERROR);
    }

    let symlink_target_path_str = symlink_target_path.to_str().ok_or(StatusCode::NOT_FOUND)?;
    Ok(Redirect::temporary(symlink_target_path_str).into_response())
}

#[instrument(level = "trace", skip_all, fields(directory_path, directory))]
pub async fn respond_directory_list(
    directory: &Directory,
    directory_path: &Path,
) -> Result<Response, StatusCode> {
    let mut directory_list_html = String::new();
    for (path_component, _node) in directory.nodes() {
        let directory_path = directory_path
            .try_join(path_component.as_ref())
            .expect("Join path");
        directory_list_html.push_str(&format!(
            "<li><a href=\"/{directory_path}\">{path_component}</a></li>"
        ))
    }
    Ok(Html(format!(
        "<!DOCTYPE html><html><body>{directory_list_html}</body></html>"
    ))
    .into_response())
}

#[instrument(level = "trace", skip_all, fields(digest, size))]
pub async fn respond_file<BS: BlobService>(
    blob_service: BS,
    extension: Option<&str>,
    range_header: Option<TypedHeader<Range>>,
    digest: &B3Digest,
    size: u64,
) -> Result<Response, StatusCode> {
    let blob_reader = blob_service
        .open_read(digest)
        .await
        .map_err(|err| {
            error!(err=%err, "failed to read blob");
            StatusCode::INTERNAL_SERVER_ERROR
        })?
        .ok_or_else(|| {
            error!("blob doesn't exist");
            StatusCode::NOT_FOUND
        })?;

    let mime_type = extension
        .and_then(|extension| mime_guess::from_ext(extension).first())
        .unwrap_or(mime::APPLICATION_OCTET_STREAM);
    match range_header {
        None => Ok((
            StatusCode::OK,
            AppendHeaders([
                (header::CONTENT_TYPE, mime_type.to_string()),
                (header::CONTENT_LENGTH, size.to_string()),
            ]),
            Body::from_stream(ReaderStream::new(blob_reader)),
        )
            .into_response()),
        Some(TypedHeader(range)) => Ok((
            StatusCode::OK,
            AppendHeaders([
                (header::CONTENT_TYPE, mime_type.to_string()),
                (header::CONTENT_LENGTH, size.to_string()),
            ]),
            Ranged::new(Some(range), KnownSize::sized(blob_reader, size)).into_response(),
        )
            .into_response()),
    }
}