| 1 |
use crate::{map_transport_err_to_py_err, Transport}; |
| 2 |
use dromedary::pathfilter::FilterFunc; |
| 3 |
use dromedary::pyo3::PyTransport; |
| 4 |
use pyo3::prelude::*; |
| 5 |
use std::sync::Arc; |
| 6 |
|
| 7 |
fn make_filter_func(filter_py: Option<Py<PyAny>>) -> Option<FilterFunc> { |
| 8 |
let f = filter_py?; |
| 9 |
Some(Arc::new(move |p: &str| -> dromedary::Result<String> { |
| 10 |
Python::attach(|py| match f.call1(py, (p,)) { |
| 11 |
Ok(r) => r |
| 12 |
.extract::<String>(py) |
| 13 |
.map_err(|e| dromedary::Error::from(e)), |
| 14 |
Err(e) => Err(dromedary::Error::from(e)), |
| 15 |
}) |
| 16 |
})) |
| 17 |
} |
| 18 |
|
| 19 |
fn build_rust_transport( |
| 20 |
py: Python, |
| 21 |
server: &Py<PyAny>, |
| 22 |
base: &str, |
| 23 |
) -> PyResult<dromedary::pathfilter::PathFilteringTransport> { |
| 24 |
let backing = server.getattr(py, "backing_transport")?; |
| 25 |
let scheme: String = server.getattr(py, "scheme")?.extract(py)?; |
| 26 |
let filter_func_py: Option<Py<PyAny>> = { |
| 27 |
let obj = server.getattr(py, "filter_func")?; |
| 28 |
if obj.is_none(py) { |
| 29 |
None |
| 30 |
} else { |
| 31 |
Some(obj) |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
let mut full_base = base.to_string(); |
| 36 |
if !full_base.ends_with('/') { |
| 37 |
full_base.push('/'); |
| 38 |
} |
| 39 |
// base_path is the path portion of the base URL, derived the same way |
| 40 |
// Python does: self.base[len(self.server.scheme) - 1:] |
| 41 |
let base_path = if full_base.len() + 1 >= scheme.len() { |
| 42 |
full_base[scheme.len() - 1..].to_string() |
| 43 |
} else { |
| 44 |
"/".to_string() |
| 45 |
}; |
| 46 |
|
| 47 |
let backing_rust: Box<dyn dromedary::Transport + Send + Sync> = |
| 48 |
Box::new(PyTransport::from(backing)); |
| 49 |
let filter = make_filter_func(filter_func_py); |
| 50 |
dromedary::pathfilter::PathFilteringTransport::new(backing_rust, scheme, base_path, filter) |
| 51 |
.map_err(|e| map_transport_err_to_py_err(e, None, None)) |
| 52 |
} |
| 53 |
|
| 54 |
#[pyclass(extends=Transport, subclass)] |
| 55 |
pub(crate) struct PathFilteringTransport { |
| 56 |
server: Py<PyAny>, |
| 57 |
base: String, |
| 58 |
} |
| 59 |
|
| 60 |
#[pymethods] |
| 61 |
impl PathFilteringTransport { |
| 62 |
#[new] |
| 63 |
fn new(py: Python, server: Py<PyAny>, base: String) -> PyResult<(Self, Transport)> { |
| 64 |
let rust = build_rust_transport(py, &server, &base)?; |
| 65 |
let mut stored_base = base.clone(); |
| 66 |
if !stored_base.ends_with('/') { |
| 67 |
stored_base.push('/'); |
| 68 |
} |
| 69 |
Ok(( |
| 70 |
PathFilteringTransport { |
| 71 |
server, |
| 72 |
base: stored_base, |
| 73 |
}, |
| 74 |
Transport(Box::new(rust)), |
| 75 |
)) |
| 76 |
} |
| 77 |
|
| 78 |
#[getter] |
| 79 |
fn server(&self, py: Python) -> Py<PyAny> { |
| 80 |
self.server.clone_ref(py) |
| 81 |
} |
| 82 |
|
| 83 |
#[getter] |
| 84 |
fn scheme(&self, py: Python) -> PyResult<String> { |
| 85 |
self.server.getattr(py, "scheme")?.extract(py) |
| 86 |
} |
| 87 |
|
| 88 |
#[getter] |
| 89 |
fn base_path(&self, py: Python) -> PyResult<String> { |
| 90 |
let scheme: String = self.server.getattr(py, "scheme")?.extract(py)?; |
| 91 |
if self.base.len() + 1 >= scheme.len() { |
| 92 |
Ok(self.base[scheme.len() - 1..].to_string()) |
| 93 |
} else { |
| 94 |
Ok("/".to_string()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
fn _relpath_from_server_root(&self, py: Python, relpath: &str) -> PyResult<String> { |
| 99 |
let base_path = self.base_path(py)?; |
| 100 |
let urlutils = py.import("dromedary.urlutils")?; |
| 101 |
let combined: String = urlutils |
| 102 |
.call_method1("combine_paths", (base_path, relpath))? |
| 103 |
.extract()?; |
| 104 |
if !combined.starts_with('/') { |
| 105 |
return Err(pyo3::exceptions::PyValueError::new_err(combined)); |
| 106 |
} |
| 107 |
Ok(combined[1..].to_string()) |
| 108 |
} |
| 109 |
|
| 110 |
fn _filter(&self, py: Python, relpath: &str) -> PyResult<String> { |
| 111 |
let rebased = self._relpath_from_server_root(py, relpath)?; |
| 112 |
let filter_func = self.server.getattr(py, "filter_func")?; |
| 113 |
if filter_func.is_none(py) { |
| 114 |
return Ok(rebased); |
| 115 |
} |
| 116 |
filter_func.call1(py, (rebased,))?.extract(py) |
| 117 |
} |
| 118 |
|
| 119 |
#[pyo3(signature = (offset=None))] |
| 120 |
fn clone<'a>( |
| 121 |
slf: PyRef<'a, Self>, |
| 122 |
py: Python<'a>, |
| 123 |
offset: Option<Py<PyAny>>, |
| 124 |
) -> PyResult<Bound<'a, PathFilteringTransport>> { |
| 125 |
let super_ = slf.as_ref(); |
| 126 |
let new_base_url = match offset { |
| 127 |
Some(o) => { |
| 128 |
let o_str: String = o.extract(py)?; |
| 129 |
super_ |
| 130 |
.0 |
| 131 |
.abspath(&o_str) |
| 132 |
.map_err(|e| map_transport_err_to_py_err(e, None, Some(&o_str)))? |
| 133 |
.to_string() |
| 134 |
} |
| 135 |
None => super_.0.base().to_string(), |
| 136 |
}; |
| 137 |
let server = slf.server.clone_ref(py); |
| 138 |
let rust = build_rust_transport(py, &server, &new_base_url)?; |
| 139 |
let mut stored_base = new_base_url; |
| 140 |
if !stored_base.ends_with('/') { |
| 141 |
stored_base.push('/'); |
| 142 |
} |
| 143 |
let init = PyClassInitializer::from(Transport(Box::new(rust))).add_subclass( |
| 144 |
PathFilteringTransport { |
| 145 |
server, |
| 146 |
base: stored_base, |
| 147 |
}, |
| 148 |
); |
| 149 |
Bound::new(py, init) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[pyclass(extends=PathFilteringTransport, subclass)] |
| 154 |
pub(crate) struct ChrootTransport {} |
| 155 |
|
| 156 |
#[pymethods] |
| 157 |
impl ChrootTransport { |
| 158 |
#[new] |
| 159 |
fn new(py: Python, server: Py<PyAny>, base: String) -> PyResult<PyClassInitializer<Self>> { |
| 160 |
let (parent, t) = PathFilteringTransport::new(py, server, base)?; |
| 161 |
let init = PyClassInitializer::from(t) |
| 162 |
.add_subclass(parent) |
| 163 |
.add_subclass(ChrootTransport {}); |
| 164 |
Ok(init) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
pub(crate) fn register(_py: Python, m: &Bound<PyModule>) -> PyResult<()> { |
| 169 |
m.add_class::<PathFilteringTransport>()?; |
| 170 |
m.add_class::<ChrootTransport>()?; |
| 171 |
Ok(()) |
| 172 |
} |