diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-21 15:35:19 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-21 15:35:19 +0000 |
| commit | ee7e115b2d0e6a6eee42eb875199c965696017d5 (patch) | |
| tree | 634ab7f960d56dc9073ebc85baf9fa6c193a32c8 /src/http | |
| parent | 97e21b62eab89bab1456db7df27df8f1c85399f0 (diff) | |
fixed http clone
but do we really nedd to create a blank commit?
I dont think ngit-relay does that.
Do we need to se the default branch or is this automatic?
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/mod.rs | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index 28ccd7b..c676bda 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs | |||
| @@ -14,7 +14,7 @@ use hyper::server::conn::http1; | |||
| 14 | use hyper::service::Service; | 14 | use hyper::service::Service; |
| 15 | use hyper::{Method, Request, Response}; | 15 | use hyper::{Method, Request, Response}; |
| 16 | use hyper_util::rt::TokioIo; | 16 | use hyper_util::rt::TokioIo; |
| 17 | use http_body_util::BodyExt; | 17 | use http_body_util::{BodyExt, Full}; |
| 18 | use nostr_sdk::hashes::sha1::Hash as Sha1Hash; | 18 | use nostr_sdk::hashes::sha1::Hash as Sha1Hash; |
| 19 | use nostr_sdk::hashes::{Hash, HashEngine}; | 19 | use nostr_sdk::hashes::{Hash, HashEngine}; |
| 20 | use nostr_relay_builder::LocalRelay; | 20 | use nostr_relay_builder::LocalRelay; |
| @@ -42,7 +42,7 @@ impl HttpService { | |||
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | impl Service<Request<Incoming>> for HttpService { | 44 | impl Service<Request<Incoming>> for HttpService { |
| 45 | type Response = Response<String>; | 45 | type Response = Response<Full<Bytes>>; |
| 46 | type Error = String; | 46 | type Error = String; |
| 47 | type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; | 47 | type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; |
| 48 | 48 | ||
| @@ -65,6 +65,11 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 65 | let repo_path = git::resolve_repo_path(&git_data_path, &npub, &identifier); | 65 | let repo_path = git::resolve_repo_path(&git_data_path, &npub, &identifier); |
| 66 | 66 | ||
| 67 | return Box::pin(async move { | 67 | return Box::pin(async move { |
| 68 | // Collect request body once before the match statement | ||
| 69 | let body_bytes = req.collect().await | ||
| 70 | .map(|collected| collected.to_bytes()) | ||
| 71 | .unwrap_or_else(|_| Bytes::new()); | ||
| 72 | |||
| 68 | let result = match (method.as_ref(), subpath.as_str()) { | 73 | let result = match (method.as_ref(), subpath.as_str()) { |
| 69 | // GET /info/refs?service=git-upload-pack or git-receive-pack | 74 | // GET /info/refs?service=git-upload-pack or git-receive-pack |
| 70 | (m, sp) if m == Method::GET && sp.starts_with("info/refs") => { | 75 | (m, sp) if m == Method::GET && sp.starts_with("info/refs") => { |
| @@ -85,22 +90,12 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 85 | 90 | ||
| 86 | // POST /git-upload-pack (clone/fetch) | 91 | // POST /git-upload-pack (clone/fetch) |
| 87 | (m, "git-upload-pack") if m == Method::POST => { | 92 | (m, "git-upload-pack") if m == Method::POST => { |
| 88 | // Read request body | ||
| 89 | let body_bytes = req.collect().await | ||
| 90 | .map(|collected| collected.to_bytes()) | ||
| 91 | .unwrap_or_else(|_| Bytes::new()); | ||
| 92 | |||
| 93 | git::handlers::handle_upload_pack(repo_path, body_bytes).await | 93 | git::handlers::handle_upload_pack(repo_path, body_bytes).await |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | // POST /git-receive-pack (push) | 96 | // POST /git-receive-pack (push) |
| 97 | (m, "git-receive-pack") if m == Method::POST => { | 97 | (m, "git-receive-pack") if m == Method::POST => { |
| 98 | // Read request body | 98 | git::handlers::handle_receive_pack(repo_path, body_bytes.clone()).await |
| 99 | let body_bytes = req.collect().await | ||
| 100 | .map(|collected| collected.to_bytes()) | ||
| 101 | .unwrap_or_else(|_| Bytes::new()); | ||
| 102 | |||
| 103 | git::handlers::handle_receive_pack(repo_path, body_bytes).await | ||
| 104 | } | 99 | } |
| 105 | 100 | ||
| 106 | _ => { | 101 | _ => { |
| @@ -112,9 +107,10 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 112 | Ok(response) => Ok(response), | 107 | Ok(response) => Ok(response), |
| 113 | Err(e) => { | 108 | Err(e) => { |
| 114 | tracing::error!("Git handler error: {}", e); | 109 | tracing::error!("Git handler error: {}", e); |
| 110 | let error_msg = format!("Git error: {}", e); | ||
| 115 | Ok(Response::builder() | 111 | Ok(Response::builder() |
| 116 | .status(e.status_code()) | 112 | .status(e.status_code()) |
| 117 | .body(format!("Git error: {}", e)) | 113 | .body(Full::new(Bytes::from(error_msg))) |
| 118 | .unwrap()) | 114 | .unwrap()) |
| 119 | } | 115 | } |
| 120 | } | 116 | } |
| @@ -141,7 +137,7 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 141 | .status(200) | 137 | .status(200) |
| 142 | .header("content-type", "application/nostr+json") | 138 | .header("content-type", "application/nostr+json") |
| 143 | .header("access-control-allow-origin", "*") | 139 | .header("access-control-allow-origin", "*") |
| 144 | .body(json) | 140 | .body(Full::new(Bytes::from(json))) |
| 145 | .unwrap()) | 141 | .unwrap()) |
| 146 | }); | 142 | }); |
| 147 | } | 143 | } |
| @@ -185,7 +181,7 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 185 | .header(CONNECTION, "upgrade") | 181 | .header(CONNECTION, "upgrade") |
| 186 | .header(UPGRADE, "websocket") | 182 | .header(UPGRADE, "websocket") |
| 187 | .header(SEC_WEBSOCKET_ACCEPT, derived.unwrap()) | 183 | .header(SEC_WEBSOCKET_ACCEPT, derived.unwrap()) |
| 188 | .body("".to_string()) | 184 | .body(Full::new(Bytes::new())) |
| 189 | .unwrap()) | 185 | .unwrap()) |
| 190 | }); | 186 | }); |
| 191 | } | 187 | } |
| @@ -197,7 +193,7 @@ impl Service<Request<Incoming>> for HttpService { | |||
| 197 | Ok(base | 193 | Ok(base |
| 198 | .status(200) | 194 | .status(200) |
| 199 | .header("content-type", "text/html; charset=utf-8") | 195 | .header("content-type", "text/html; charset=utf-8") |
| 200 | .body(html) | 196 | .body(Full::new(Bytes::from(html))) |
| 201 | .unwrap()) | 197 | .unwrap()) |
| 202 | }) | 198 | }) |
| 203 | } | 199 | } |