| @@ -1,5 +1,5 @@ |
| 1 | use crate::{client::SetServiceResponse, Protocol, Service}; | 1 | use crate::{client::SetServiceResponse, Protocol, Service}; |
| 2 | use bstr::{BString, ByteVec}; | 2 | use bstr::BString; |
| 3 | use std::{io, net::TcpStream}; | 3 | use std::{io, net::TcpStream}; |
| 4 | | 4 | |
| 5 | pub struct Connection<R, W> { | 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( | 20 | pub mod message { |
| 21 | service: Service, | 21 | use crate::{Protocol, Service}; |
| 22 | version: Protocol, | 22 | use bstr::{BString, ByteVec}; |
| 23 | path: &[u8], | 23 | |
| 24 | virtual_host: Option<&(String, Option<u16>)>, | 24 | pub fn connect( |
| 25 | ) -> BString { | 25 | service: Service, |
| 26 | let mut out = bstr::BString::from(service.as_str()); | 26 | version: Protocol, |
| 27 | out.push(b' '); | 27 | path: &[u8], |
| 28 | out.extend_from_slice(&path); | 28 | virtual_host: Option<&(String, Option<u16>)>, |
| 29 | out.push(0); | 29 | ) -> BString { |
| 30 | if let Some((host, port)) = virtual_host { | 30 | let mut out = bstr::BString::from(service.as_str()); |
| 31 | out.push_str("host="); | 31 | out.push(b' '); |
| 32 | out.extend_from_slice(host.as_bytes()); | 32 | out.extend_from_slice(&path); |
| 33 | if let Some(port) = port { | | |
| 34 | out.push_byte(b':'); | | |
| 35 | out.push_str(&format!("{}", port)); | | |
| 36 | } | | |
| 37 | out.push(0); | 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 { | 50 | } |
| 40 | out.push(0); | 51 | |
| 41 | out.push_str(format!("version={}", version as usize)); | 52 | pub(crate) mod recv { |
| 42 | out.push(0); | 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 | impl<R, W> crate::client::TransportSketch for Connection<R, W> | 74 | impl<R, W> crate::client::TransportSketch for Connection<R, W> |
| @@ -50,7 +77,7 @@ where · 2 unmodified lines |
| 50 | W: io::Write, | 77 | W: io::Write, |
| 51 | { | 78 | { |
| 52 | fn set_service(&mut self, service: Service) -> Result<SetServiceResponse, crate::client::Error> { | 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 | service, | 81 | service, |
| 55 | self.protocol, | 82 | self.protocol, |
| 56 | &self.path, | 83 | &self.path, |
| @@ -58,23 +85,11 @@ where · 1 unmodified line |
| 58 | ))?; | 85 | ))?; |
| 59 | self.write.flush()?; | 86 | self.write.flush()?; |
| 60 | | 87 | |
| 61 | self.line_reader.fail_on_err_lines(true); | 88 | let (capabilities, refs) = recv::capabilties_and_possibly_refs(&mut self.line_reader)?; |
| 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'); | | |
| 74 | Ok(SetServiceResponse { | 89 | Ok(SetServiceResponse { |
| 75 | actual_protocol: Protocol::V1, // TODO - read actual only if we are in version two or above | 90 | actual_protocol: Protocol::V1, // TODO - read actual only if we are in version two or above |
| 76 | capabilities, | 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 | use quick_error::quick_error; | 119 | use quick_error::quick_error; |
| 106 | quick_error! { | 120 | quick_error! { |
| 107 | #[derive(Debug)] | 121 | #[derive(Debug)] |