MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / read_proxy_v2_header

Method read_proxy_v2_header

src/server-core/src/lib.rs:139–177  ·  view source on GitHub ↗

Fallback path for [`Self::take_proxy_header_address`] when the initial peek returned an incomplete PROXY v2 header. Reads the fixed 16-byte v2 prefix to learn the total header size, then reads the rest.

(&mut self, buf: &mut [u8; 1024])

Source from the content-addressed store, hash-verified

137 /// peek returned an incomplete PROXY v2 header. Reads the fixed 16-byte
138 /// v2 prefix to learn the total header size, then reads the rest.
139 async fn read_proxy_v2_header(&mut self, buf: &mut [u8; 1024]) -> Option<ProxiedAddress> {
140 // PROXY v2 fixed prefix: 12-byte signature + ver/cmd + fam/proto + 2-byte length.
141 const V2_PREFIX_LEN: usize = 16;
142 if self.read_exact(&mut buf[..V2_PREFIX_LEN]).await.is_err() {
143 debug!("Failed to read PROXY v2 fixed header");
144 return None;
145 }
146 let addr_len = usize::from(u16::from_be_bytes([buf[14], buf[15]]));
147 let total = V2_PREFIX_LEN + addr_len;
148 if total > buf.len() {
149 debug!("PROXY v2 header too large: {total} bytes");
150 return None;
151 }
152 if self
153 .read_exact(&mut buf[V2_PREFIX_LEN..total])
154 .await
155 .is_err()
156 {
157 debug!("Failed to read PROXY v2 address data");
158 return None;
159 }
160 match ProxyHeader::parse(
161 &buf[..total],
162 ParseConfig {
163 include_tlvs: false,
164 allow_v1: false,
165 allow_v2: true,
166 },
167 ) {
168 Ok((header, _)) => {
169 debug!("Proxied connection with header {:?}", header);
170 header.proxied_address().map(|a| a.to_owned())
171 }
172 Err(e) => {
173 debug!("Proxy header parse error '{:?}', ignoring header.", e);
174 None
175 }
176 }
177 }
178
179 /// Peer address of the inner tcp_stream.
180 pub fn peer_addr(&self) -> Result<std::net::SocketAddr, io::Error> {

Callers 1

Calls 4

parseFunction · 0.50
lenMethod · 0.45
mapMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected