| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use std::{ |
| 10 | io, |
| 11 | pin::Pin, |
| 12 | task::{Context, Poll}, |
| 13 | }; |
| 14 | |
| 15 | use bytes::Bytes; |
| 16 | use futures_util::TryStreamExt; |
| 17 | use http_body::Frame; |
| 18 | use serde::Deserialize; |
| 19 | use tokio::io::{AsyncRead, ReadBuf}; |
| 20 | use tokio_util::io::StreamReader; |
| 21 | use topcoat::{ |
| 22 | Result, |
| 23 | context::Cx, |
| 24 | router::{ |
| 25 | Body, Response, |
| 26 | error::{RouterErrorExt, bad_request, forbidden, not_found}, |
| 27 | parse_query_params, path_param, route, |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | use crate::{ |
| 32 | application::{Error, GitClientHeaders, GitEndpoint, GitService, port::ByteStream, serve_git}, |
| 33 | domain::{DomainError, RepoName}, |
| 34 | }; |
| 35 | |
| 36 | use super::{ |
| 37 | context::{current_actor, memberships, orgs, protocol, repos, server_error}, |
| 38 | profile::handle_param, |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | const CHUNK: usize = 16 * 1024; |
| 46 | |
| 47 | |
| 48 | #[path_param] |
| 49 | struct Repo(str); |
| 50 | |
| 51 | #[derive(Debug, Deserialize)] |
| 52 | struct ServiceQuery { |
| 53 | service: Option<String>, |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | fn repo_param(cx: &Cx) -> Result<RepoName> { |
| 62 | let raw = path_param::<Repo>(cx); |
| 63 | let name = raw.strip_suffix(".git").ok_or_else(not_found)?; |
| 64 | |
| 65 | Ok(RepoName::new(name).map_err(|_| not_found())?) |
| 66 | } |
| 67 | |
| 68 | |
| 69 | fn client_headers(cx: &Cx) -> GitClientHeaders { |
| 70 | let headers = topcoat::router::headers(cx); |
| 71 | let value = |name: &str| { |
| 72 | headers |
| 73 | .get(name) |
| 74 | .and_then(|value| value.to_str().ok()) |
| 75 | .map(str::to_owned) |
| 76 | }; |
| 77 | |
| 78 | GitClientHeaders { |
| 79 | content_type: value("content-type"), |
| 80 | content_encoding: value("content-encoding"), |
| 81 | content_length: value("content-length"), |
| 82 | git_protocol: value("git-protocol"), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | async fn serve(cx: &Cx, endpoint: GitEndpoint, body: ByteStream) -> Result<Response<GitBody>> { |
| 91 | let handle = handle_param(cx)?; |
| 92 | let name = repo_param(cx)?; |
| 93 | let actor = current_actor(cx).await?; |
| 94 | |
| 95 | let served = serve_git( |
| 96 | &handle, |
| 97 | &name, |
| 98 | endpoint, |
| 99 | client_headers(cx), |
| 100 | body, |
| 101 | &actor, |
| 102 | &orgs(cx), |
| 103 | &memberships(cx), |
| 104 | &repos(cx), |
| 105 | &protocol(cx), |
| 106 | ) |
| 107 | .await |
| 108 | .map_err(|error| match error { |
| 109 | |
| 110 | Error::Domain(DomainError::Forbidden) => forbidden().into(), |
| 111 | other => server_error(other), |
| 112 | })? |
| 113 | .ok_or_not_found()?; |
| 114 | |
| 115 | let mut response = Response::builder().status(served.status); |
| 116 | for (name, value) in served.headers { |
| 117 | response = response.header(name, value); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | response |
| 124 | .body(GitBody::new(served.body)) |
| 125 | .map_err(server_error) |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | #[route(GET "/{handle}/repos/{repo}/info/refs")] |
| 133 | async fn info_refs(cx: &Cx) -> Result<Response<GitBody>> { |
| 134 | let query = |
| 135 | parse_query_params::<ServiceQuery>(cx).map_err(|error| bad_request(error.to_string()))?; |
| 136 | |
| 137 | |
| 138 | |
| 139 | let service = query.service.ok_or_else(not_found)?; |
| 140 | let service: GitService = service.parse().map_err(|_| not_found())?; |
| 141 | |
| 142 | serve( |
| 143 | cx, |
| 144 | GitEndpoint::Advertisement(service), |
| 145 | Box::pin(tokio::io::empty()), |
| 146 | ) |
| 147 | .await |
| 148 | } |
| 149 | |
| 150 | #[route(POST "/{handle}/repos/{repo}/git-upload-pack")] |
| 151 | async fn upload_pack(cx: &Cx, body: Body) -> Result<Response<GitBody>> { |
| 152 | serve( |
| 153 | cx, |
| 154 | GitEndpoint::Rpc(GitService::UploadPack), |
| 155 | into_reader(body), |
| 156 | ) |
| 157 | .await |
| 158 | } |
| 159 | |
| 160 | #[route(POST "/{handle}/repos/{repo}/git-receive-pack")] |
| 161 | async fn receive_pack(cx: &Cx, body: Body) -> Result<Response<GitBody>> { |
| 162 | serve( |
| 163 | cx, |
| 164 | GitEndpoint::Rpc(GitService::ReceivePack), |
| 165 | into_reader(body), |
| 166 | ) |
| 167 | .await |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | fn into_reader(body: Body) -> ByteStream { |
| 175 | Box::pin(StreamReader::new( |
| 176 | body.into_data_stream().map_err(io::Error::other), |
| 177 | )) |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | pub struct GitBody { |
| 185 | reader: ByteStream, |
| 186 | } |
| 187 | |
| 188 | impl GitBody { |
| 189 | fn new(reader: ByteStream) -> Self { |
| 190 | Self { reader } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl http_body::Body for GitBody { |
| 195 | type Data = Bytes; |
| 196 | type Error = io::Error; |
| 197 | |
| 198 | fn poll_frame( |
| 199 | mut self: Pin<&mut Self>, |
| 200 | cx: &mut Context<'_>, |
| 201 | ) -> Poll<Option<std::result::Result<Frame<Bytes>, io::Error>>> { |
| 202 | let mut buffer = [0u8; CHUNK]; |
| 203 | let mut read = ReadBuf::new(&mut buffer); |
| 204 | |
| 205 | match Pin::new(&mut self.reader).poll_read(cx, &mut read) { |
| 206 | Poll::Pending => Poll::Pending, |
| 207 | Poll::Ready(Err(error)) => Poll::Ready(Some(Err(error))), |
| 208 | Poll::Ready(Ok(())) => { |
| 209 | let filled = read.filled(); |
| 210 | |
| 211 | |
| 212 | if filled.is_empty() { |
| 213 | Poll::Ready(None) |
| 214 | } else { |
| 215 | Poll::Ready(Some(Ok(Frame::data(Bytes::copy_from_slice(filled))))) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |