| 1 |
use std::{ |
| 2 |
collections::HashMap, |
| 3 |
io::Write, |
| 4 |
process::{Command, Stdio}, |
| 5 |
}; |
| 6 |
|
| 7 |
use actix_web::web::Bytes; |
| 8 |
|
| 9 |
use crate::prelude::*; |
| 10 |
|
| 11 |
#[get("/{repo}/info/refs")] |
| 12 |
pub async fn git_info_refs( |
| 13 |
path: web::Path<String>, |
| 14 |
req_query: web::Query<HashMap<String, String>>, |
| 15 |
data: web::Data<App>, |
| 16 |
) -> ActixResult<HttpResponse> { |
| 17 |
let service = match req_query.get("service") { |
| 18 |
Some(s) => s, |
| 19 |
None => return Ok(HttpResponse::BadRequest().body("Missing service parameter")), |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if service != "git-upload-pack" { |
| 25 |
return Ok(HttpResponse::Forbidden().body("Push over HTTP is disabled; use SSH")); |
| 26 |
} |
| 27 |
|
| 28 |
let repo = data.get_repo(&path)?; |
| 29 |
|
| 30 |
let first_line = "# service=git-upload-pack\n"; |
| 31 |
let pkt_line = format!("{:04x}{first_line}", first_line.len() + 4); |
| 32 |
let zero_line = "0000"; |
| 33 |
|
| 34 |
let output = Command::new("git") |
| 35 |
.arg("upload-pack") |
| 36 |
.arg("--stateless-rpc") |
| 37 |
.arg("--advertise-refs") |
| 38 |
.arg(&repo.path) |
| 39 |
.output(); |
| 40 |
|
| 41 |
match output { |
| 42 |
Ok(out) => { |
| 43 |
let mut body = Vec::new(); |
| 44 |
body.extend_from_slice(pkt_line.as_bytes()); |
| 45 |
body.extend_from_slice(zero_line.as_bytes()); |
| 46 |
body.extend_from_slice(&out.stdout); |
| 47 |
|
| 48 |
Ok(HttpResponse::Ok() |
| 49 |
.content_type("application/x-git-upload-pack-advertisement") |
| 50 |
.body(body)) |
| 51 |
} |
| 52 |
Err(_) => Ok(HttpResponse::InternalServerError().body("Git command failed")), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[post("/{repo}/git-upload-pack")] |
| 57 |
pub async fn git_upload_pack( |
| 58 |
path: web::Path<String>, |
| 59 |
data: web::Data<App>, |
| 60 |
body: Bytes, |
| 61 |
) -> Result<HttpResponse, Error> { |
| 62 |
let repo = data.get_repo(&path)?; |
| 63 |
|
| 64 |
let mut child = Command::new("git") |
| 65 |
.arg("upload-pack") |
| 66 |
.arg("--stateless-rpc") |
| 67 |
.arg(&repo.path) |
| 68 |
.stdin(Stdio::piped()) |
| 69 |
.stdout(Stdio::piped()) |
| 70 |
.spawn() |
| 71 |
.map_err(|e| anyhow::anyhow!(e))?; |
| 72 |
|
| 73 |
if let Some(mut stdin) = child.stdin.take() { |
| 74 |
stdin.write_all(&body).map_err(|e| anyhow::anyhow!(e))?; |
| 75 |
} |
| 76 |
|
| 77 |
let output = child.wait_with_output().map_err(|e| anyhow::anyhow!(e))?; |
| 78 |
|
| 79 |
Ok(HttpResponse::Ok() |
| 80 |
.content_type("application/x-git-upload-pack-result") |
| 81 |
.body(output.stdout)) |
| 82 |
} |