| @@ -1,5 +1,5 @@ |
| 1 | 1 | use crate::{client::SetServiceResponse, Protocol, Service}; |
| 2 | | −use bstr::{BString, ByteVec}; |
| 2 | +use bstr::BString; |
| 3 | 3 | use std::{io, net::TcpStream}; |
| 4 | 4 | |
| 5 | 5 | pub struct Connection<R, W> { |
| @@ -17,31 +17,58 @@ where · 11 unmodified lines |
| 17 | 17 | { |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | | −pub fn connect_message( |
| 21 | | − service: Service, |
| 22 | | − version: Protocol, |
| 23 | | − path: &[u8], |
| 24 | | − virtual_host: Option<&(String, Option<u16>)>, |
| 25 | | −) -> BString { |
| 26 | | − let mut out = bstr::BString::from(service.as_str()); |
| 27 | | − out.push(b' '); |
| 28 | | − out.extend_from_slice(&path); |
| 29 | | − out.push(0); |
| 30 | | − if let Some((host, port)) = virtual_host { |
| 31 | | − out.push_str("host="); |
| 32 | | − out.extend_from_slice(host.as_bytes()); |
| 33 | | − if let Some(port) = port { |
| 34 | | − out.push_byte(b':'); |
| 35 | | − out.push_str(&format!("{}", port)); |
| 36 | | − } |
| 20 | +pub mod message { |
| 21 | + use crate::{Protocol, Service}; |
| 22 | + use bstr::{BString, ByteVec}; |
| 23 | + |
| 24 | + pub fn connect( |
| 25 | + service: Service, |
| 26 | + version: Protocol, |
| 27 | + path: &[u8], |
| 28 | + virtual_host: Option<&(String, Option<u16>)>, |
| 29 | + ) -> BString { |
| 30 | + let mut out = bstr::BString::from(service.as_str()); |
| 31 | + out.push(b' '); |
| 32 | + out.extend_from_slice(&path); |
| 37 | 33 | out.push(0); |
| 34 | + if let Some((host, port)) = virtual_host { |
| 35 | + out.push_str("host="); |
| 36 | + out.extend_from_slice(host.as_bytes()); |
| 37 | + if let Some(port) = port { |
| 38 | + out.push_byte(b':'); |
| 39 | + out.push_str(&format!("{}", port)); |
| 40 | + } |
| 41 | + out.push(0); |
| 42 | + } |
| 43 | + if version != Protocol::V1 { |
| 44 | + out.push(0); |
| 45 | + out.push_str(format!("version={}", version as usize)); |
| 46 | + out.push(0); |
| 47 | + } |
| 48 | + out |
| 38 | 49 | } |
| 39 | | − if version != Protocol::V1 { |
| 40 | | − out.push(0); |
| 41 | | − out.push_str(format!("version={}", version as usize)); |
| 42 | | − out.push(0); |
| 50 | +} |
| 51 | + |
| 52 | +pub(crate) mod recv { |
| 53 | + use crate::client::Capabilities; |
| 54 | + use std::io; |
| 55 | + |
| 56 | + pub fn capabilties_and_possibly_refs<'a, T: io::Read>( |
| 57 | + rd: &'a mut git_packetline::Reader<T>, |
| 58 | + ) -> Result<(Capabilities, Option<Box<dyn io::BufRead + 'a>>), crate::client::Error> { |
| 59 | + rd.fail_on_err_lines(true); |
| 60 | + let capabilities = rd |
| 61 | + .peek_line() |
| 62 | + .ok_or(crate::client::Error::ExpectedLine("capabilities or version"))???; |
| 63 | + let (capabilities, delimiter_position) = Capabilities::from_bytes( |
| 64 | + capabilities |
| 65 | + .to_text() |
| 66 | + .ok_or(crate::client::Error::ExpectedDataLine)? |
| 67 | + .as_slice(), |
| 68 | + )?; |
| 69 | + rd.peek_buffer_replace_and_truncate(delimiter_position, b'\n'); |
| 70 | + Ok((capabilities, Some(Box::new(rd.as_read())))) |
| 43 | 71 | } |
| 44 | | − out |
| 45 | 72 | } |
| 46 | 73 | |
| 47 | 74 | impl<R, W> crate::client::TransportSketch for Connection<R, W> |
| @@ -50,7 +77,7 @@ where · 2 unmodified lines |
| 50 | 77 | W: io::Write, |
| 51 | 78 | { |
| 52 | 79 | fn set_service(&mut self, service: Service) -> Result<SetServiceResponse, crate::client::Error> { |
| 53 | | − self.write.write_all(&connect_message( |
| 80 | + self.write.write_all(&message::connect( |
| 54 | 81 | service, |
| 55 | 82 | self.protocol, |
| 56 | 83 | &self.path, |
| @@ -58,23 +85,11 @@ where · 1 unmodified line |
| 58 | 85 | ))?; |
| 59 | 86 | self.write.flush()?; |
| 60 | 87 | |
| 61 | | − self.line_reader.fail_on_err_lines(true); |
| 62 | | − let capabilities = self |
| 63 | | − .line_reader |
| 64 | | − .peek_line() |
| 65 | | − .ok_or(crate::client::Error::ExpectedLine("capabilities or version"))???; |
| 66 | | − let (capabilities, delimiter_position) = Capabilities::from_bytes( |
| 67 | | − capabilities |
| 68 | | − .to_text() |
| 69 | | − .ok_or(crate::client::Error::ExpectedDataLine)? |
| 70 | | − .as_slice(), |
| 71 | | − )?; |
| 72 | | − self.line_reader |
| 73 | | − .peek_buffer_replace_and_truncate(delimiter_position, b'\n'); |
| 88 | + let (capabilities, refs) = recv::capabilties_and_possibly_refs(&mut self.line_reader)?; |
| 74 | 89 | Ok(SetServiceResponse { |
| 75 | 90 | actual_protocol: Protocol::V1, // TODO - read actual only if we are in version two or above |
| 76 | 91 | capabilities, |
| 77 | | − refs: Some(Box::new(self.line_reader.as_read())), |
| 92 | + refs, |
| 78 | 93 | }) |
| 79 | 94 | } |
| 80 | 95 | } |
| @@ -101,7 +116,6 @@ where · 20 unmodified lines |
| 101 | 116 | } |
| 102 | 117 | } |
| 103 | 118 | |
| 104 | | −use crate::client::Capabilities; |
| 105 | 119 | use quick_error::quick_error; |
| 106 | 120 | quick_error! { |
| 107 | 121 | #[derive(Debug)] |