History

[git-protocol] move everything into `blocking_io` for later translation…

…into async-io, as usual.

Sebastian Thiel committed 5 years ago

Commit fa033741 parent 0d39b5d12 files · +295 −254Browse files

Showing the changes to git-protocol/src/fetch/blocking_io/response.rs (1 of 12 files). Show all

Unified · Split

git-protocol/src/fetch/blocking_io/response.rs+280−0
@@ -0,0 +1,280 @@
1+use crate::fetch::blocking_io::command::Feature;
2+use git_transport::{client, Protocol};
3+use quick_error::quick_error;
4+use std::io;
5+
6+quick_error! {
7+ /// The error used in the [response module][crate::fetch::response].
8+ #[derive(Debug)]
9+ #[allow(missing_docs)]
10+ pub enum Error {
11+ Io(err: io::Error) {
12+ display("Failed to read from line reader")
13+ from()
14+ source(err)
15+ }
16+ Transport(err: client::Error) {
17+ display("An error occurred when decoding a line")
18+ from()
19+ source(err)
20+ }
21+ MissingServerCapability(feature: &'static str) {
22+ display("Currently we require feature '{}', which is not supported by the server", feature)
23+ }
24+ UnknownLineType(line: String) {
25+ display("Encountered an unknown line prefix in '{}'", line)
26+ }
27+ UnknownSectionHeader(header: String) {
28+ display("Unknown or unsupported header: '{}'", header)
29+ }
30+ }
31+}
32+
33+/// An 'ACK' line received from the server.
34+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
35+#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
36+pub enum Acknowledgement {
37+ /// The contained `id` is in common.
38+ Common(git_hash::ObjectId),
39+ /// The server is ready to receive more lines.
40+ Ready,
41+ /// The server isn't ready yet.
42+ Nak,
43+}
44+
45+/// A shallow line received from the server.
46+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
47+#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
48+pub enum ShallowUpdate {
49+ /// Shallow the given `id`.
50+ Shallow(git_hash::ObjectId),
51+ /// Don't shallow the given `id` anymore.
52+ Unshallow(git_hash::ObjectId),
53+}
54+
55+impl ShallowUpdate {
56+ /// Parse a `ShallowUpdate` from a `line` as received to the server.
57+ fn from_line(line: &str) -> Result<ShallowUpdate, Error> {
58+ let mut tokens = line.trim_end().splitn(2, ' ');
59+ match (tokens.next(), tokens.next()) {
60+ (Some(prefix), Some(id)) => {
61+ let id =
62+ git_hash::ObjectId::from_hex(id.as_bytes()).map_err(|_| Error::UnknownLineType(line.to_owned()))?;
63+ Ok(match prefix {
64+ "shallow" => ShallowUpdate::Shallow(id),
65+ "unshallow" => ShallowUpdate::Unshallow(id),
66+ _ => return Err(Error::UnknownLineType(line.to_owned())),
67+ })
68+ }
69+ _ => unreachable!("cannot have an entirely empty line"),
70+ }
71+ }
72+}
73+
74+impl Acknowledgement {
75+ /// Parse an `Acknowledgement` from a `line` as received to the server.
76+ fn from_line(line: &str) -> Result<Acknowledgement, Error> {
77+ let mut tokens = line.trim_end().splitn(3, ' ');
78+ Ok(match (tokens.next(), tokens.next(), tokens.next()) {
79+ (Some(first), id, description) => match first {
80+ "ready" => Acknowledgement::Ready, // V2
81+ "NAK" => Acknowledgement::Nak, // V1
82+ "ACK" => {
83+ let id = match id {
84+ Some(id) => git_hash::ObjectId::from_hex(id.as_bytes())
85+ .map_err(|_| Error::UnknownLineType(line.to_owned()))?,
86+ None => return Err(Error::UnknownLineType(line.to_owned())),
87+ };
88+ if let Some(description) = description {
89+ match description {
90+ "common" => {}
91+ "ready" => return Ok(Acknowledgement::Ready),
92+ _ => return Err(Error::UnknownLineType(line.to_owned())),
93+ }
94+ }
95+ Acknowledgement::Common(id)
96+ }
97+ _ => return Err(Error::UnknownLineType(line.to_owned())),
98+ },
99+ (None, _, _) => unreachable!("cannot have an entirely empty line"),
100+ })
101+ }
102+ /// Returns the hash of the acknowledged object if this instance acknowledges a common one.
103+ pub fn id(&self) -> Option<&git_hash::ObjectId> {
104+ match self {
105+ Acknowledgement::Common(id) => Some(id),
106+ _ => None,
107+ }
108+ }
109+}
110+
111+/// A representation of a complete fetch response
112+pub struct Response {
113+ acks: Vec<Acknowledgement>,
114+ shallows: Vec<ShallowUpdate>,
115+ has_pack: bool,
116+}
117+
118+impl Response {
119+ /// Return true if the response has a pack which can be read next.
120+ pub fn has_pack(&self) -> bool {
121+ self.has_pack
122+ }
123+
124+ /// Return an error if the given `features` don't contain the required ones for the given `version` of the protocol.
125+ ///
126+ /// Even though technically any set of features supported by the server could work, we only implement the ones that
127+ /// make it easy to maintain all versions with a single code base that aims to be and remain maintainable.
128+ pub fn check_required_features(version: Protocol, features: &[Feature]) -> Result<(), Error> {
129+ match version {
130+ Protocol::V1 => {
131+ let has = |name: &str| features.iter().any(|f| f.0 == name);
132+ // Let's focus on V2 standards, and simply not support old servers to keep our code simpler
133+ if !has("multi_ack_detailed") {
134+ return Err(Error::MissingServerCapability("multi_ack_detailed"));
135+ }
136+ // It's easy to NOT do sideband for us, but then again, everyone supports it.
137+ // CORRECTION: If side-band is off, it would send the packfile without packet line encoding,
138+ // which is nothing we ever want to deal with (despite it being more efficient). In V2, this
139+ // is not even an option anymore, sidebands are always present.
140+ if !has("side-band") && !has("side-band-64k") {
141+ return Err(Error::MissingServerCapability("side-band OR side-band-64k"));
142+ }
143+ }
144+ Protocol::V2 => {}
145+ }
146+ Ok(())
147+ }
148+ /// Parse a response of the given `version` of the protocol from `reader`.
149+ pub fn from_line_reader(version: Protocol, reader: &mut impl client::ExtendedBufRead) -> Result<Response, Error> {
150+ match version {
151+ Protocol::V1 => {
152+ let mut line = String::new();
153+ let mut acks = Vec::<Acknowledgement>::new();
154+ let mut shallows = Vec::<ShallowUpdate>::new();
155+ let has_pack = 'lines: loop {
156+ line.clear();
157+ let peeked_line = match reader.peek_data_line() {
158+ Some(Ok(Ok(line))) => String::from_utf8_lossy(line),
159+ // This special case (hang/block forver) deals with a single NAK being a legitimate EOF sometimes
160+ // Note that this might block forever in stateful connections as there it's not really clear
161+ // if something will be following or not by just looking at the response. Instead you have to know
162+ // the arguments sent to the server and count response lines based on intricate knowledge on how the
163+ // server works.
164+ // For now this is acceptable, as V2 can be used as a workaround, which also is the default.
165+ Some(Err(err)) if err.kind() == io::ErrorKind::UnexpectedEof => break 'lines false,
166+ Some(Err(err)) => return Err(err.into()),
167+ Some(Ok(Err(err))) => return Err(err.into()),
168+ None => {
169+ // maybe we saw a shallow flush packet, let's reset and retry
170+ reader.read_line(&mut line)?;
171+ reader.reset(Protocol::V1);
172+ match reader.peek_data_line() {
173+ Some(Ok(Ok(line))) => String::from_utf8_lossy(line),
174+ Some(Err(err)) => return Err(err.into()),
175+ Some(Ok(Err(err))) => return Err(err.into()),
176+ None => break 'lines false, // EOF
177+ }
178+ }
179+ };
180+
181+ // with a friendly server, we just assume that a non-ack line is a pack line
182+ // which is our hint to stop here.
183+ match Acknowledgement::from_line(&peeked_line) {
184+ Ok(ack) => match ack.id() {
185+ Some(id) => {
186+ if !acks.iter().any(|a| a.id() == Some(id)) {
187+ acks.push(ack);
188+ }
189+ }
190+ None => acks.push(ack),
191+ },
192+ Err(_) => match ShallowUpdate::from_line(&peeked_line) {
193+ Ok(shallow) => {
194+ shallows.push(shallow);
195+ }
196+ Err(_) => break 'lines true,
197+ },
198+ };
199+ assert_ne!(reader.read_line(&mut line)?, 0, "consuming a peeked line works");
200+ };
201+ Ok(Response {
202+ acks,
203+ shallows,
204+ has_pack,
205+ })
206+ }
207+ Protocol::V2 => {
208+ // NOTE: We only read acknowledgements and scrub to the pack file, until we have use for the other features
209+ let mut line = String::new();
210+ reader.reset(Protocol::V2);
211+ let mut acks = Vec::<Acknowledgement>::new();
212+ let mut shallows = Vec::<ShallowUpdate>::new();
213+ let has_pack = 'section: loop {
214+ line.clear();
215+ if reader.read_line(&mut line)? == 0 {
216+ return Err(Error::Io(io::Error::new(
217+ io::ErrorKind::UnexpectedEof,
218+ "Could not read message headline",
219+ )));
220+ };
221+
222+ match line.trim_end() {
223+ "acknowledgments" => {
224+ if parse_section(&mut line, reader, &mut acks, Acknowledgement::from_line)? {
225+ break 'section false;
226+ }
227+ }
228+ "shallow-info" => {
229+ if parse_section(&mut line, reader, &mut shallows, ShallowUpdate::from_line)? {
230+ break 'section false;
231+ }
232+ }
233+ "packfile" => {
234+ // what follows is the packfile itself, which can be read with a sideband enabled reader
235+ break 'section true;
236+ }
237+ _ => return Err(Error::UnknownSectionHeader(line)),
238+ }
239+ };
240+ Ok(Response {
241+ acks,
242+ shallows,
243+ has_pack,
244+ })
245+ }
246+ }
247+ }
248+
249+ /// Return all acknowledgements [parsed previously][Response::from_line_reader()].
250+ pub fn acknowledgements(&self) -> &[Acknowledgement] {
251+ &self.acks
252+ }
253+
254+ /// Return all shallow update lines [parsed previously][Response::from_line_reader()].
255+ pub fn shallow_updates(&self) -> &[ShallowUpdate] {
256+ &self.shallows
257+ }
258+}
259+
260+fn parse_section<T>(
261+ line: &mut String,
262+ reader: &mut impl client::ExtendedBufRead,
263+ res: &mut Vec<T>,
264+ parse: impl Fn(&str) -> Result<T, Error>,
265+) -> Result<bool, Error> {
266+ line.clear();
267+ while reader.read_line(line)? != 0 {
268+ res.push(parse(line)?);
269+ line.clear();
270+ }
271+ // End of message, or end of section?
272+ Ok(if reader.stopped_at() == Some(client::MessageKind::Delimiter) {
273+ // try reading more sections
274+ reader.reset(Protocol::V2);
275+ false
276+ } else {
277+ // we are done, there is no pack
278+ true
279+ })
280+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t