///|
pub fn encode_v1(header : ProxyHeader) -> Result[Bytes, ProxyError] {
if header.version != V1 ||
header.command != Proxy ||
header.tlvs.length() != 0 {
return Err(
proxy_error(
PolicyViolation,
0,
"v1 accepts only PROXY headers without TLVs",
),
)
}
let line = match header.address {
NoAddress => "PROXY UNKNOWN\r\n"
Ipv4(endpoints) =>
"PROXY TCP4 \{format_ipv4(endpoints.source_address)} \{format_ipv4(endpoints.destination_address)} \{endpoints.source_port} \{endpoints.destination_port}\r\n"
Ipv6(endpoints) =>
"PROXY TCP6 \{format_ipv6(endpoints.source_address)} \{format_ipv6(endpoints.destination_address)} \{endpoints.source_port} \{endpoints.destination_port}\r\n"
_ =>
return Err(
proxy_error(PolicyViolation, 0, "v1 cannot encode this address block"),
)
}
let output = @utf8.encode(line, bom=false)
if output.length() > 108 {
Err(
proxy_error(
V1LineTooLong,
output.length(),
"encoded v1 line exceeds 108 bytes",
),
)
} else {
Ok(output)
}
}