(
State(state): State<Arc<Mutex<PeerState>>>,
method: Method,
uri: Uri,
headers: HeaderMap,
body: Bytes,
)
| 102 | } |
| 103 | |
| 104 | async fn peer( |
| 105 | State(state): State<Arc<Mutex<PeerState>>>, |
| 106 | method: Method, |
| 107 | uri: Uri, |
| 108 | headers: HeaderMap, |
| 109 | body: Bytes, |
| 110 | ) -> Response { |
| 111 | let mut state = state.lock().unwrap(); |
| 112 | state.requests += 1; |
| 113 | if let Some(identity) = &state.require_identity { |
| 114 | if !authenticates(&headers, identity, &state.audience) { |
| 115 | return (StatusCode::UNAUTHORIZED, "identity credential required").into_response(); |
| 116 | } |
| 117 | state.authenticated += 1; |
| 118 | } |
| 119 | if uri.query() == Some("project-identity=1") { |
| 120 | return ([("content-type", "application/json")], "{\"bound\":false}").into_response(); |
| 121 | } |
| 122 | if headers.get("Atomic-Protocol").and_then(|v| v.to_str().ok()) != Some("sync/1") { |
| 123 | return (StatusCode::BAD_REQUEST, "sync/1 required").into_response(); |
| 124 | } |
| 125 | if method == Method::GET { |
| 126 | let Ok(wants) = SyncWants::decode(&body) else { |
| 127 | return (StatusCode::BAD_REQUEST, "invalid wants").into_response(); |
| 128 | }; |
| 129 | let mut selected: std::collections::HashSet<_> = wants.refs.iter().cloned().collect(); |
| 130 | let mut pending = wants.refs.clone(); |
| 131 | while let Some(name) = pending.pop() { |
| 132 | let Some(key) = state.refs.get(&name) else { |
| 133 | continue; |
| 134 | }; |
| 135 | let Some(object) = state.objects.get(&(ObjectFamily::View, key.clone())) else { |
| 136 | continue; |
| 137 | }; |
| 138 | let Some(snapshot) = atomic_objects::ViewSnapshot::from_bytes(&object.bytes) else { |
| 139 | continue; |
| 140 | }; |
| 141 | if let Some(parent) = snapshot.parent_view { |
| 142 | if selected.insert(parent.clone()) { |
| 143 | pending.push(parent); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | let refs: Vec<_> = state |
| 148 | .refs |
| 149 | .iter() |
| 150 | .filter(|(name, _)| wants.refs.is_empty() || selected.contains(*name)) |
| 151 | .map(|(name, key)| RefRecord { |
| 152 | name: name.clone(), |
| 153 | expect_old: None, |
| 154 | new_target: key.clone(), |
| 155 | }) |
| 156 | .collect(); |
| 157 | let objects = state |
| 158 | .objects |
| 159 | .values() |
| 160 | .filter(|object| !wants.haves.contains(&object.key)) |
| 161 | .filter(|object| !wants.refs_only || object.family == ObjectFamily::View) |
nothing calls this directly
no test coverage detected