1use super::log::DaemonLog;
6use super::platform::{DaemonPlatform, StdPlatform};
7use std::fmt;
8use std::path::PathBuf;
9use std::sync::Arc;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum DaemonService {
14 UploadPack,
15 UploadArchive,
16 ReceivePack,
17}
18
19impl DaemonService {
20 pub const ALL: [Self; 3] = [Self::UploadPack, Self::UploadArchive, Self::ReceivePack];
21
22 pub fn name(self) -> &'static str {
24 match self {
25 Self::UploadPack => "upload-pack",
26 Self::UploadArchive => "upload-archive",
27 Self::ReceivePack => "receive-pack",
28 }
29 }
30
31 pub fn config_key(self) -> &'static str {
33 match self {
34 Self::UploadPack => "daemon.uploadpack",
35 Self::UploadArchive => "daemon.uploadarch",
36 Self::ReceivePack => "daemon.receivepack",
37 }
38 }
39
40 pub fn from_name(name: &str) -> Option<Self> {
41 Self::ALL.into_iter().find(|s| s.name() == name)
42 }
43
44 pub fn from_request(command: &str) -> Option<Self> {
46 command.strip_prefix("git-").and_then(Self::from_name)
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub struct ServicePolicy {
53 pub enabled: bool,
55 pub overridable: bool,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub struct ServiceTable {
63 pub upload_pack: ServicePolicy,
64 pub upload_archive: ServicePolicy,
65 pub receive_pack: ServicePolicy,
66}
67
68impl Default for ServiceTable {
69 fn default() -> Self {
70 let on = ServicePolicy {
71 enabled: true,
72 overridable: true,
73 };
74 let off = ServicePolicy {
75 enabled: false,
76 overridable: true,
77 };
78 Self {
79 upload_pack: on,
80 upload_archive: off,
81 receive_pack: off,
82 }
83 }
84}
85
86impl ServiceTable {
87 pub fn get(&self, service: DaemonService) -> ServicePolicy {
88 match service {
89 DaemonService::UploadPack => self.upload_pack,
90 DaemonService::UploadArchive => self.upload_archive,
91 DaemonService::ReceivePack => self.receive_pack,
92 }
93 }
94
95 pub fn get_mut(&mut self, service: DaemonService) -> &mut ServicePolicy {
96 match service {
97 DaemonService::UploadPack => &mut self.upload_pack,
98 DaemonService::UploadArchive => &mut self.upload_archive,
99 DaemonService::ReceivePack => &mut self.receive_pack,
100 }
101 }
102}
103
104#[derive(Clone)]
107pub struct DaemonOptions {
108 pub base_path: Option<PathBuf>,
110 pub base_path_relaxed: bool,
112 pub whitelist: Vec<PathBuf>,
114 pub strict_paths: bool,
116 pub user_path: Option<String>,
118 pub interpolated_path: Option<String>,
120 pub export_all: bool,
122 pub services: ServiceTable,
124 pub informative_errors: bool,
126 pub access_hook: Option<PathBuf>,
128 pub timeout_secs: u64,
130 pub init_timeout_secs: u64,
132 pub max_connections: usize,
134 pub log: DaemonLog,
136 pub platform: Arc<dyn DaemonPlatform>,
138}
139
140impl Default for DaemonOptions {
141 fn default() -> Self {
142 Self {
143 base_path: None,
144 base_path_relaxed: false,
145 whitelist: Vec::new(),
146 strict_paths: false,
147 user_path: None,
148 interpolated_path: None,
149 export_all: false,
150 services: ServiceTable::default(),
151 informative_errors: false,
152 access_hook: None,
153 timeout_secs: 0,
154 init_timeout_secs: 0,
155 max_connections: 32,
156 log: DaemonLog::default(),
157 platform: Arc::new(StdPlatform),
158 }
159 }
160}
161
162impl fmt::Debug for DaemonOptions {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 f.debug_struct("DaemonOptions")
165 .field("base_path", &self.base_path)
166 .field("base_path_relaxed", &self.base_path_relaxed)
167 .field("whitelist", &self.whitelist)
168 .field("strict_paths", &self.strict_paths)
169 .field("user_path", &self.user_path)
170 .field("interpolated_path", &self.interpolated_path)
171 .field("export_all", &self.export_all)
172 .field("services", &self.services)
173 .field("informative_errors", &self.informative_errors)
174 .field("access_hook", &self.access_hook)
175 .field("timeout_secs", &self.timeout_secs)
176 .field("init_timeout_secs", &self.init_timeout_secs)
177 .field("max_connections", &self.max_connections)
178 .field("log", &self.log)
179 .finish_non_exhaustive()
180 }
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186
187 #[test]
188 fn service_names_and_config_keys_follow_git_daemon() {
189 assert_eq!(
190 DaemonService::from_request("git-upload-pack"),
191 Some(DaemonService::UploadPack)
192 );
193 assert_eq!(
194 DaemonService::from_request("git-upload-archive"),
195 Some(DaemonService::UploadArchive)
196 );
197 assert_eq!(DaemonService::from_request("git-foo"), None);
198 assert_eq!(DaemonService::from_request("upload-pack"), None);
199 assert_eq!(
200 DaemonService::UploadArchive.config_key(),
201 "daemon.uploadarch"
202 );
203 assert_eq!(
204 DaemonService::ReceivePack.config_key(),
205 "daemon.receivepack"
206 );
207 assert_eq!(DaemonService::from_name("bogus"), None);
208 }
209
210 #[test]
211 fn defaults_match_git_daemon() {
212 let t = ServiceTable::default();
213 assert!(t.upload_pack.enabled);
214 assert!(!t.upload_archive.enabled);
215 assert!(!t.receive_pack.enabled);
216 assert!(DaemonService::ALL.iter().all(|s| t.get(*s).overridable));
217 let o = DaemonOptions::default();
218 assert_eq!(o.max_connections, 32);
219 assert_eq!(o.timeout_secs, 0);
220 assert_eq!(o.init_timeout_secs, 0);
221 assert!(!o.informative_errors);
222 }
223}