1use crate::error::ServerError;
8use crate::push_policy::Pusher;
9use crate::receive::{receive_pack, receive_pack_advertisement, receive_pack_as};
10use crate::upload::{upload_pack_advertisement, upload_pack_result_with_nak};
11use arks_protocol::{Packet, decode_packet, decode_text_line, encode_text_line};
12use arks_repo::Repository;
13use std::io::{Read, Write};
14
15pub fn upload_pack_session<R: Read, W: Write>(
17 repo: &Repository,
18 mut stdin: R,
19 mut stdout: W,
20) -> Result<(), ServerError> {
21 let ad = upload_pack_advertisement(repo)?;
22 stdout.write_all(&ad)?;
23 stdout.flush()?;
24 let (body, sent_nak) = read_upload_split(&mut stdin, &mut stdout)?;
25 let out = upload_pack_result_with_nak(repo, &body, !sent_nak)?;
26 stdout.write_all(&out)?;
27 stdout.flush()?;
28 Ok(())
29}
30
31pub fn receive_pack_session<R: Read, W: Write>(
32 repo: &mut Repository,
33 mut stdin: R,
34 mut stdout: W,
35) -> Result<(), ServerError> {
36 let ad = receive_pack_advertisement(repo)?;
37 stdout.write_all(&ad)?;
38 stdout.flush()?;
39 let mut body = Vec::new();
40 stdin.read_to_end(&mut body)?;
41 let out = receive_pack(repo, &body)?;
42 stdout.write_all(&out)?;
43 stdout.flush()?;
44 Ok(())
45}
46
47pub fn upload_pack_stream<S: Read + Write>(
49 repo: &Repository,
50 stream: &mut S,
51) -> Result<(), ServerError> {
52 let ad = upload_pack_advertisement(repo)?;
53 stream.write_all(&ad)?;
54 stream.flush()?;
55 let (body, sent_nak) = read_upload_one(stream)?;
56 let out = upload_pack_result_with_nak(repo, &body, !sent_nak)?;
57 stream.write_all(&out)?;
58 stream.flush()?;
59 Ok(())
60}
61
62pub fn receive_pack_stream<S: Read + Write>(
63 repo: &mut Repository,
64 stream: &mut S,
65) -> Result<(), ServerError> {
66 receive_pack_stream_as(repo, stream, &Pusher::local())
67}
68
69pub fn receive_pack_stream_as<S: Read + Write>(
71 repo: &mut Repository,
72 stream: &mut S,
73 pusher: &Pusher,
74) -> Result<(), ServerError> {
75 let ad = receive_pack_advertisement(repo)?;
76 stream.write_all(&ad)?;
77 stream.flush()?;
78 let mut body = Vec::new();
79 stream.read_to_end(&mut body)?;
80 let out = receive_pack_as(repo, &body, pusher)?;
81 stream.write_all(&out)?;
82 stream.flush()?;
83 Ok(())
84}
85
86fn read_upload_split<R: Read, W: Write>(
87 stdin: &mut R,
88 stdout: &mut W,
89) -> Result<(Vec<u8>, bool), ServerError> {
90 let mut body = Vec::new();
91 let mut buf = [0u8; 8192];
92 let mut sent_nak = false;
93 let mut nak_for_flush_at = None::<usize>;
94 loop {
95 while body.len() < 4 || incomplete_last_packet(&body) {
96 let n = stdin.read(&mut buf)?;
97 if n == 0 {
98 return Ok((body, sent_nak));
99 }
100 body.extend_from_slice(&buf[..n]);
101 }
102 if request_has_done(&body) {
103 return Ok((body, sent_nak));
104 }
105 if let Some(flush_end) = trailing_flush_end(&body) {
106 if nak_for_flush_at != Some(flush_end) {
107 let nak =
108 encode_text_line("NAK").map_err(|e| ServerError::Protocol(e.to_string()))?;
109 stdout.write_all(&nak)?;
110 stdout.flush()?;
111 sent_nak = true;
112 nak_for_flush_at = Some(flush_end);
113 }
114 }
115 let n = stdin.read(&mut buf)?;
116 if n == 0 {
117 return Ok((body, sent_nak));
118 }
119 body.extend_from_slice(&buf[..n]);
120 }
121}
122
123fn read_upload_one<S: Read + Write>(stream: &mut S) -> Result<(Vec<u8>, bool), ServerError> {
124 let mut body = Vec::new();
125 let mut buf = [0u8; 8192];
126 let mut sent_nak = false;
127 let mut nak_for_flush_at = None::<usize>;
128 loop {
129 while body.len() < 4 || incomplete_last_packet(&body) {
130 let n = stream.read(&mut buf)?;
131 if n == 0 {
132 return Ok((body, sent_nak));
133 }
134 body.extend_from_slice(&buf[..n]);
135 }
136 if request_has_done(&body) {
137 return Ok((body, sent_nak));
138 }
139 if let Some(flush_end) = trailing_flush_end(&body) {
140 if nak_for_flush_at != Some(flush_end) {
141 let nak =
142 encode_text_line("NAK").map_err(|e| ServerError::Protocol(e.to_string()))?;
143 stream.write_all(&nak)?;
144 stream.flush()?;
145 sent_nak = true;
146 nak_for_flush_at = Some(flush_end);
147 }
148 }
149 let n = stream.read(&mut buf)?;
150 if n == 0 {
151 return Ok((body, sent_nak));
152 }
153 body.extend_from_slice(&buf[..n]);
154 }
155}
156
157fn request_has_done(body: &[u8]) -> bool {
158 let mut offset = 0;
159 while offset + 4 <= body.len() {
160 let Ok((packet, n)) = decode_packet(&body[offset..]) else {
161 return false;
162 };
163 offset += n;
164 if let Packet::Data(payload) = packet {
165 if let Ok(text) = decode_text_line(&payload) {
166 if text == "done" || text.starts_with("done ") {
167 return true;
168 }
169 }
170 }
171 }
172 false
173}
174
175fn trailing_flush_end(body: &[u8]) -> Option<usize> {
176 let mut offset = 0;
177 let mut last_flush_end = None;
178 while offset + 4 <= body.len() {
179 match decode_packet(&body[offset..]) {
180 Ok((packet, n)) => {
181 offset += n;
182 last_flush_end = match packet {
183 Packet::Flush => Some(offset),
184 _ => None,
185 };
186 }
187 Err(_) => return None,
188 }
189 }
190 if offset != body.len() {
191 return None;
192 }
193 last_flush_end
194}
195
196fn incomplete_last_packet(body: &[u8]) -> bool {
197 let mut offset = 0;
198 while offset + 4 <= body.len() {
199 match decode_packet(&body[offset..]) {
200 Ok((_, n)) => offset += n,
201 Err(_) => return true,
202 }
203 }
204 offset != body.len()
205}