MCPcopy Index your code
hub / github.com/RustPython/RustPython / parse_length_prefixed_alpn

Function parse_length_prefixed_alpn

crates/stdlib/src/ssl.rs:561–599  ·  view source on GitHub ↗

Parse length-prefixed ALPN protocol list Format: [len1, proto1..., len2, proto2..., ...] This is the wire format used by Python's ssl.py when calling _set_alpn_protocols(). Each protocol is prefixed with a single byte indicating its length. # Arguments `bytes` - The length-prefixed protocol data `vm` - VirtualMachine for error creation # Returns `Ok(Vec >)` - List of protocol names as b

(bytes: &[u8], vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

559 /// * `Ok(Vec<Vec<u8>>)` - List of protocol names as byte vectors
560 /// * `Err(PyBaseExceptionRef)` - ValueError with detailed error message
561 fn parse_length_prefixed_alpn(bytes: &[u8], vm: &VirtualMachine) -> PyResult<Vec<Vec<u8>>> {
562 let mut alpn_list = Vec::new();
563 let mut offset = 0;
564
565 while offset < bytes.len() {
566 // Check if we can read the length byte
567 if offset + 1 > bytes.len() {
568 return Err(vm.new_value_error(format!(
569 "Invalid ALPN protocol data: unexpected end at offset {offset}",
570 )));
571 }
572
573 let proto_len = bytes[offset] as usize;
574 offset += 1;
575
576 // Validate protocol length
577 if proto_len == 0 {
578 return Err(vm.new_value_error(format!(
579 "Invalid ALPN protocol data: protocol length cannot be 0 at offset {}",
580 offset - 1
581 )));
582 }
583
584 // Check if we have enough bytes for the protocol data
585 if offset + proto_len > bytes.len() {
586 return Err(vm.new_value_error(format!(
587 "Invalid ALPN protocol data: expected {} bytes at offset {}, but only {} bytes remain",
588 proto_len, offset, bytes.len() - offset
589 )));
590 }
591
592 // Extract protocol bytes
593 let proto = bytes[offset..offset + proto_len].to_vec();
594 alpn_list.push(proto);
595 offset += proto_len;
596 }
597
598 Ok(alpn_list)
599 }
600
601 /// Parse OpenSSL cipher string to rustls SupportedCipherSuite list
602 ///

Callers 1

_set_alpn_protocolsMethod · 0.85

Calls 5

newFunction · 0.85
to_vecMethod · 0.80
ErrClass · 0.50
lenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected