Skip to main content

edgestore_repl/
http_server.rs

1//! HTTP replication server.
2//!
3//! Endpoints:
4//!   GET /merkle              → MessagePack `{root: Vec<u8>}`
5//!   GET /segments            → MessagePack `[{segment_id, segment_hash}]`
6//!   GET /segments/{hash_hex} → raw segment bytes (application/octet-stream)
7//!   GET /watermark           → MessagePack `{confirmed_lsn, wtoken}`
8//!
9//! All endpoints support `?debug=json` for human inspection.
10//! Engine access is serialized via `Arc<Mutex<Engine>>` — correct for single-writer.
11
12use std::sync::{Arc, Mutex};
13
14const BODY_DRAIN_BUF: usize = 4096;
15const BODY_DRAIN_MAX: usize = 64 * 1024;
16
17use edgestore::WatermarkResponse;
18use edgestore::EdgestoreError;
19use edgestore::Engine;
20
21use serde::Serialize;
22
23use crate::wire::{MerkleResponse, SegmentEntry};
24
25/// HTTP replication server wrapping an `Arc<Mutex<Engine>>`.
26///
27/// # Security
28///
29/// **No authentication is performed.** All endpoints are open to any client
30/// that can reach the bound address. Bind to a loopback address (`127.0.0.1`)
31/// or a trusted private network interface — never to `0.0.0.0` in production
32/// unless protected by a network-layer firewall or mTLS proxy.
33///
34/// Call `start(bind_addr)` to spawn the server loop in a background thread.
35pub struct HttpReplicationServer {
36    engine: Arc<Mutex<Engine>>,
37}
38
39impl HttpReplicationServer {
40    /// Create a new server wrapping the provided engine.
41    pub fn new(engine: Arc<Mutex<Engine>>) -> Self {
42        HttpReplicationServer { engine }
43    }
44
45    /// Bind to `bind_addr` and start the request loop in a background thread.
46    ///
47    /// Returns `(JoinHandle, bound_port)`. The bound port is useful when `bind_addr`
48    /// uses port 0 (OS-assigned ephemeral port). The server runs until the process exits.
49    pub fn start(
50        &self,
51        bind_addr: &str,
52    ) -> Result<(std::thread::JoinHandle<()>, u16), EdgestoreError> {
53        let server = tiny_http::Server::http(bind_addr)
54            .map_err(|e| EdgestoreError::ReplicationError(format!("bind error: {}", e)))?;
55
56        // Extract the bound port before moving `server` into the thread.
57        let bound_port = match server.server_addr() {
58            tiny_http::ListenAddr::IP(addr) => addr.port(),
59            #[cfg(unix)]
60            tiny_http::ListenAddr::Unix(_) => {
61                return Err(EdgestoreError::ReplicationError(
62                    "Unix socket bind not supported for replication server".into(),
63                ))
64            }
65        };
66
67        let engine = Arc::clone(&self.engine);
68
69        let handle = std::thread::spawn(move || {
70            for request in server.incoming_requests() {
71                handle_request(request, &engine);
72            }
73        });
74
75        Ok((handle, bound_port))
76    }
77}
78
79/// Parse path and query string from a request URL.
80///
81/// Returns `(path_parts, debug_json)` where `path_parts` is the non-empty path
82/// components and `debug_json` is `true` when `?debug=json` is present.
83fn parse_url(url: &str) -> (Vec<&str>, bool) {
84    let (path_part, query_part) = match url.split_once('?') {
85        Some((p, q)) => (p, q),
86        None => (url, ""),
87    };
88
89    let debug_json = query_part.split('&').any(|kv| kv == "debug=json");
90
91    let parts: Vec<&str> = path_part.split('/').filter(|s| !s.is_empty()).collect();
92
93    (parts, debug_json)
94}
95
96/// Respond with a MessagePack-serialized value, or JSON if `debug_json` is set.
97fn respond_msgpack<T: Serialize>(request: tiny_http::Request, value: &T, debug_json: bool) {
98    if debug_json {
99        match serde_json::to_vec(value) {
100            Ok(json_bytes) => {
101                let response = tiny_http::Response::from_data(json_bytes).with_header(
102                    tiny_http::Header::from_bytes("Content-Type", "application/json").unwrap(),
103                );
104                let _ = request.respond(response);
105            }
106            Err(e) => {
107                respond_error(request, 500, &format!("json serialization error: {}", e));
108            }
109        }
110    } else {
111        match rmp_serde::to_vec_named(value) {
112            Ok(msgpack_bytes) => {
113                let response = tiny_http::Response::from_data(msgpack_bytes).with_header(
114                    tiny_http::Header::from_bytes("Content-Type", "application/msgpack").unwrap(),
115                );
116                let _ = request.respond(response);
117            }
118            Err(e) => {
119                respond_error(request, 500, &format!("msgpack serialization error: {}", e));
120            }
121        }
122    }
123}
124
125/// Respond with raw bytes and `application/octet-stream`.
126fn respond_raw(request: tiny_http::Request, data: Vec<u8>) {
127    let response = tiny_http::Response::from_data(data).with_header(
128        tiny_http::Header::from_bytes("Content-Type", "application/octet-stream").unwrap(),
129    );
130    let _ = request.respond(response);
131}
132
133/// Respond with an error status and plain-text body.
134fn respond_error(request: tiny_http::Request, status: u16, msg: &str) {
135    let response = tiny_http::Response::from_string(msg.to_string())
136        .with_status_code(tiny_http::StatusCode(status));
137    let _ = request.respond(response);
138}
139
140/// Dispatch a single HTTP request.
141fn handle_request(mut request: tiny_http::Request, engine: &Arc<Mutex<Engine>>) {
142    // Drain up to BODY_DRAIN_MAX to unblock the client; GET endpoints never use the body.
143    {
144        let mut drain_buf = [0u8; BODY_DRAIN_BUF];
145        let mut drained = 0usize;
146        while drained < BODY_DRAIN_MAX {
147            let n = request.as_reader().read(&mut drain_buf).unwrap_or(0);
148            if n == 0 {
149                break;
150            }
151            drained += n;
152        }
153    }
154
155    let method = request.method().clone();
156    let url = request.url().to_string();
157
158    let (parts, debug_json) = parse_url(&url);
159
160    match (method, parts.as_slice()) {
161        (tiny_http::Method::Get, ["merkle"]) => {
162            // GET /merkle — return Merkle root as MessagePack.
163            match engine.lock() {
164                Ok(eng) => match eng.range_merkle_root() {
165                    Ok(root) => {
166                        let resp = MerkleResponse {
167                            root: root.to_vec(),
168                        };
169                        respond_msgpack(request, &resp, debug_json);
170                    }
171                    Err(e) => {
172                        eprintln!("[http_server] merkle root error: {e}");
173                        respond_error(request, 500, "internal server error");
174                    }
175                },
176                Err(_) => {
177                    respond_error(request, 500, "engine lock poisoned");
178                }
179            }
180        }
181
182        (tiny_http::Method::Get, ["segments"]) => {
183            // GET /segments — return full manifest as MessagePack.
184            match engine.lock() {
185                Ok(eng) => match eng.export_manifest() {
186                    Ok(refs) => {
187                        let entries: Vec<SegmentEntry> = refs
188                            .into_iter()
189                            .map(|sr| SegmentEntry {
190                                segment_id: sr.segment_id,
191                                segment_hash: sr.segment_hash.to_vec(),
192                            })
193                            .collect();
194                        respond_msgpack(request, &entries, debug_json);
195                    }
196                    Err(e) => {
197                        eprintln!("[http_server] export manifest error: {e}");
198                        respond_error(request, 500, "internal server error");
199                    }
200                },
201                Err(_) => {
202                    respond_error(request, 500, "engine lock poisoned");
203                }
204            }
205        }
206
207        (tiny_http::Method::Get, ["segments", hash_hex]) => {
208            // GET /segments/{hash_hex} — return raw segment bytes.
209            // Parse 64-char hex string into 32 bytes.
210            let hash_hex = *hash_hex;
211            if hash_hex.len() != 64 {
212                respond_error(request, 400, "hash_hex must be 64 hex characters");
213                return;
214            }
215
216            // Decode hex.
217            let mut hash_bytes = [0u8; 32];
218            let mut valid = true;
219            for i in 0..32 {
220                match u8::from_str_radix(&hash_hex[i * 2..i * 2 + 2], 16) {
221                    Ok(b) => hash_bytes[i] = b,
222                    Err(_) => {
223                        valid = false;
224                        break;
225                    }
226                }
227            }
228            if !valid {
229                respond_error(request, 400, "invalid hex encoding in hash");
230                return;
231            }
232
233            // Determine the segment file path from the engine's db path.
234            // The canonical segment filename is segment-{id:08}.dat, but we need to find it
235            // by content hash. Look up via export_manifest to find the segment_id, then
236            // read the canonical .dat file. If not found, fall back to {hash_hex}.dat.
237            let dat_bytes = {
238                let eng_guard = match engine.lock() {
239                    Ok(g) => g,
240                    Err(_) => {
241                        respond_error(request, 500, "engine lock poisoned");
242                        return;
243                    }
244                };
245
246                // Find the segment_id for this hash.
247                let manifest = match eng_guard.export_manifest() {
248                    Ok(m) => m,
249                    Err(e) => {
250                        eprintln!("[http_server] manifest error: {e}");
251                        respond_error(request, 500, "internal server error");
252                        return;
253                    }
254                };
255
256                let segment_id = manifest
257                    .iter()
258                    .find(|sr| sr.segment_hash == hash_bytes)
259                    .map(|sr| sr.segment_id);
260
261                let base_path = eng_guard.db_path().to_path_buf();
262
263                drop(eng_guard); // release lock before file I/O
264
265                match segment_id {
266                    Some(sid) => {
267                        // Read the canonical segment-{id:08}.dat file.
268                        let dat_path = base_path.join(format!("segment-{:08}.dat", sid));
269                        match std::fs::read(&dat_path) {
270                            Ok(bytes) => bytes,
271                            Err(_) => {
272                                // Fall back to hash-named file.
273                                let fallback = base_path.join(format!("{}.dat", hash_hex));
274                                match std::fs::read(&fallback) {
275                                    Ok(b) => b,
276                                    Err(e) => {
277                                        eprintln!("[http_server] segment read error: {e}");
278                                        respond_error(request, 404, "segment not found");
279                                        return;
280                                    }
281                                }
282                            }
283                        }
284                    }
285                    None => {
286                        // Hash not in manifest; try direct hash-named file.
287                        let dat_path = base_path.join(format!("{}.dat", hash_hex));
288                        match std::fs::read(&dat_path) {
289                            Ok(b) => b,
290                            Err(_) => {
291                                respond_error(request, 404, "segment not found");
292                                return;
293                            }
294                        }
295                    }
296                }
297            };
298
299            respond_raw(request, dat_bytes);
300        }
301
302        (tiny_http::Method::Get, ["watermark"]) => {
303            // GET /watermark — return confirmed_lsn + wtoken as MessagePack.
304            match engine.lock() {
305                Ok(eng) => {
306                    let resp = WatermarkResponse {
307                        confirmed_lsn: eng.confirmed_lsn(),
308                        wtoken: eng.write_token(),
309                    };
310                    respond_msgpack(request, &resp, debug_json);
311                }
312                Err(_) => {
313                    respond_error(request, 500, "engine lock poisoned");
314                }
315            }
316        }
317
318        _ => {
319            respond_error(request, 404, "not found");
320        }
321    }
322}