forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rs
More file actions
323 lines (303 loc) · 12 KB
/
Copy pathserver.rs
File metadata and controls
323 lines (303 loc) · 12 KB
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
//! Minimal HTTP package registry server.
//!
//! Serves the registry API over HTTP/1.1 on a background thread, using
//! `std::net::TcpListener` + `httparse` (same pattern as
//! `crate::runtime::http_server`). Shutdown is controlled by an
//! `AtomicBool` flag; `stop()` sets it and joins the listener thread.
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use parking_lot::Mutex;
use std::time::Duration;
/// Package registry server.
///
/// Storage layout under `data_dir`:
/// ```text
/// <data-dir>/
/// <name>/
/// <version>.tar.gz
/// ```
///
/// The listener handle lives behind a `Mutex` so that `start(&self)` and
/// `stop(&self)` can manage the background thread through shared references.
pub struct RegistryServer {
data_dir: PathBuf,
auth_token: Option<String>,
running: Arc<AtomicBool>,
handle: Mutex<Option<thread::JoinHandle<()>>>,
}
impl RegistryServer {
/// Maximum accepted tarball size (64 MiB).
const MAX_BODY_SIZE: usize = 64 * 1024 * 1024;
/// Per-connection read timeout.
const READ_TIMEOUT: Duration = Duration::from_secs(10);
pub fn new(data_dir: PathBuf, auth_token: Option<String>) -> Self {
RegistryServer {
data_dir,
auth_token,
running: Arc::new(AtomicBool::new(false)),
handle: Mutex::new(None),
}
}
/// Bind to `bind_addr` and spawn the listener thread.
///
/// Fails with `ErrorKind::AlreadyExists` if the server is already running,
/// or with the bind error if the address cannot be bound.
pub fn start(&self, bind_addr: &str) -> std::io::Result<()> {
let mut handle_guard = self.handle.lock();
if handle_guard.is_some() {
return Err(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"registry server already running",
));
}
let listener = TcpListener::bind(bind_addr)?;
let data_dir = self.data_dir.clone();
let auth_token = self.auth_token.clone();
let running = self.running.clone();
let handle = thread::Builder::new()
.name("nulang-registry-listener".into())
.spawn(move || {
Self::listener_loop(listener, data_dir, auth_token, running);
})?;
*handle_guard = Some(handle);
Ok(())
}
/// Signal shutdown and join the listener thread. Safe to call multiple
/// times and from `Drop`.
pub fn stop(&self) {
self.running.store(true, Ordering::Relaxed);
if let Some(handle) = self.handle.lock().take() {
let _ = handle.join();
}
}
fn listener_loop(
listener: TcpListener,
data_dir: PathBuf,
auth_token: Option<String>,
running: Arc<AtomicBool>,
) {
listener.set_nonblocking(true).ok();
loop {
if running.load(Ordering::Relaxed) {
break;
}
match listener.accept() {
Ok((stream, _)) => {
let _ = stream.set_read_timeout(Some(Self::READ_TIMEOUT));
Self::handle_connection(stream, &data_dir, &auth_token);
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(10));
}
Err(_) => break,
}
}
}
fn handle_connection(mut stream: TcpStream, data_dir: &Path, auth_token: &Option<String>) {
// Read the request head (headers plus any body bytes already received).
let mut head: Vec<u8> = Vec::new();
let mut chunk = [0u8; 4096];
let (method, path, headers, body_offset) = loop {
match stream.read(&mut chunk) {
Ok(0) => return, // EOF before a complete request
Ok(n) => {
head.extend_from_slice(&chunk[..n]);
let mut httparse_headers = [httparse::EMPTY_HEADER; 64];
let mut req = httparse::Request::new(&mut httparse_headers);
match req.parse(&head) {
Ok(httparse::Status::Complete(body_offset)) => {
let method = req.method.unwrap_or("").to_string();
let path = req.path.unwrap_or("/").to_string();
let headers: Vec<(String, String)> = req
.headers
.iter()
.map(|h| {
(
h.name.to_string(),
String::from_utf8_lossy(h.value).to_string(),
)
})
.collect();
break (method, path, headers, body_offset);
}
Ok(httparse::Status::Partial) => continue, // need more bytes
Err(_) => {
Self::write_response(&mut stream, 400, "text/plain", b"Bad request");
return;
}
}
}
Err(_) => return,
}
};
// Read the remainder of the body according to Content-Length.
let content_length: usize = headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case("content-length"))
.and_then(|(_, v)| v.parse().ok())
.unwrap_or(0);
let mut body = head[body_offset..].to_vec();
while body.len() < content_length.min(Self::MAX_BODY_SIZE) {
let mut chunk = [0u8; 4096];
match stream.read(&mut chunk) {
Ok(0) => break,
Ok(n) => body.extend_from_slice(&chunk[..n]),
Err(_) => break,
}
}
if content_length > Self::MAX_BODY_SIZE {
Self::write_response(&mut stream, 413, "text/plain", b"Payload too large");
return;
}
Self::dispatch(
&mut stream,
&method,
&path,
&headers,
&body,
data_dir,
auth_token,
);
}
fn dispatch(
stream: &mut TcpStream,
method: &str,
path: &str,
headers: &[(String, String)],
body: &[u8],
data_dir: &Path,
auth_token: &Option<String>,
) {
let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
let route = match segments.as_slice() {
["api", "v1", "packages", name] => Some((*name, None)),
["api", "v1", "packages", name, version] => Some((*name, Some(*version))),
_ => None,
};
let Some((name, version)) = route else {
Self::write_response(stream, 404, "text/plain", b"Not found");
return;
};
if !valid_segment(name) || version.is_some_and(|v| !valid_segment(v)) {
Self::write_response(stream, 404, "text/plain", b"Not found");
return;
}
match (method, version) {
("PUT", Some(version)) => {
if !authorized(headers, auth_token) {
Self::write_response(stream, 401, "text/plain", b"Unauthorized");
return;
}
Self::handle_put(stream, data_dir, name, version, body);
}
("GET", Some(version)) => Self::handle_get_tarball(stream, data_dir, name, version),
("GET", None) => Self::handle_list_versions(stream, data_dir, name),
_ => Self::write_response(stream, 405, "text/plain", b"Method not allowed"),
}
}
/// PUT /api/v1/packages/<name>/<version> — store a tarball.
fn handle_put(stream: &mut TcpStream, data_dir: &Path, name: &str, version: &str, body: &[u8]) {
let dir = data_dir.join(name);
let file = dir.join(format!("{}.tar.gz", version));
if file.exists() {
Self::write_response(stream, 409, "text/plain", b"Version already exists");
return;
}
if let Err(_) = std::fs::create_dir_all(&dir) {
Self::write_response(stream, 500, "text/plain", b"Internal server error");
return;
}
match std::fs::write(&file, body) {
Ok(()) => Self::write_response(stream, 201, "text/plain", b"Created"),
Err(_) => Self::write_response(stream, 500, "text/plain", b"Internal server error"),
}
}
/// GET /api/v1/packages/<name>/<version> — return the stored tarball.
fn handle_get_tarball(stream: &mut TcpStream, data_dir: &Path, name: &str, version: &str) {
let file = data_dir.join(name).join(format!("{}.tar.gz", version));
match std::fs::read(&file) {
Ok(bytes) => {
Self::write_response(stream, 200, "application/octet-stream", &bytes);
}
Err(_) => Self::write_response(stream, 404, "text/plain", b"Not found"),
}
}
/// GET /api/v1/packages/<name> — list published versions as JSON.
fn handle_list_versions(stream: &mut TcpStream, data_dir: &Path, name: &str) {
let dir = data_dir.join(name);
let entries = match std::fs::read_dir(&dir) {
Ok(entries) => entries,
Err(_) => {
Self::write_response(stream, 404, "text/plain", b"Not found");
return;
}
};
let mut versions: Vec<String> = entries
.filter_map(|e| e.ok())
.filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok())
.filter(|f| f.ends_with(".tar.gz"))
.map(|f| f.trim_end_matches(".tar.gz").to_string())
.collect();
versions.sort();
let payload = serde_json::json!({ "name": name, "versions": versions });
let body = payload.to_string();
Self::write_response(stream, 200, "application/json", body.as_bytes());
}
fn write_response(stream: &mut TcpStream, status: u16, content_type: &str, body: &[u8]) {
let status_text = match status {
200 => "OK",
201 => "Created",
400 => "Bad Request",
401 => "Unauthorized",
404 => "Not Found",
405 => "Method Not Allowed",
409 => "Conflict",
413 => "Payload Too Large",
500 => "Internal Server Error",
_ => "Unknown",
};
let out = format!(
"HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
status,
status_text,
content_type,
body.len()
);
let _ = stream.write_all(out.as_bytes());
let _ = stream.write_all(body);
let _ = stream.flush();
}
}
impl Drop for RegistryServer {
fn drop(&mut self) {
self.stop();
}
}
/// Validate a package name/version path segment: rejects empty segments,
/// `.`/`..`, and anything outside `[A-Za-z0-9._-]`, which blocks path
/// traversal and absolute paths before they reach the filesystem.
fn valid_segment(s: &str) -> bool {
!s.is_empty()
&& s != "."
&& s != ".."
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
}
/// Check `Authorization: Bearer <token>` against the configured token.
/// When no token is configured, all requests are authorized.
fn authorized(headers: &[(String, String)], auth_token: &Option<String>) -> bool {
match auth_token {
None => true,
Some(expected) => headers.iter().any(|(k, v)| {
k.eq_ignore_ascii_case("authorization")
&& v.split_once(' ').is_some_and(|(scheme, token)| {
scheme.eq_ignore_ascii_case("Bearer") && token.trim() == expected
})
}),
}
}