Skip to content

Commit 933f379

Browse files
zh-jq-blrstewart
andauthored
s2n-tls rust binding: expose selected application protocol (#4599)
Co-authored-by: Lindsay Stewart <[email protected]>
1 parent 5ea31b1 commit 933f379

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

bindings/rust/s2n-tls/src/connection.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,14 @@ impl Connection {
947947
})
948948
}
949949

950+
pub fn application_protocol(&self) -> Option<&[u8]> {
951+
let protocol = unsafe { s2n_get_application_protocol(self.connection.as_ptr()) };
952+
if protocol.is_null() {
953+
return None;
954+
}
955+
Some(unsafe { CStr::from_ptr(protocol).to_bytes() })
956+
}
957+
950958
/// Provides access to the TLS-Exporter functionality.
951959
///
952960
/// See https://datatracker.ietf.org/doc/html/rfc5705 and https://www.rfc-editor.org/rfc/rfc8446.

bindings/rust/s2n-tls/src/testing/s2n_tls.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,31 @@ mod tests {
998998

999999
Ok(())
10001000
}
1001+
1002+
#[test]
1003+
fn no_application_protocol() -> Result<(), Error> {
1004+
let config = config_builder(&security::DEFAULT)?.build()?;
1005+
let mut pair = tls_pair(config);
1006+
assert!(poll_tls_pair_result(&mut pair).is_ok());
1007+
assert!(pair.server.0.connection.application_protocol().is_none());
1008+
Ok(())
1009+
}
1010+
1011+
#[test]
1012+
fn application_protocol() -> Result<(), Error> {
1013+
let config = config_builder(&security::DEFAULT)?.build()?;
1014+
let mut pair = tls_pair(config);
1015+
pair.server
1016+
.0
1017+
.connection
1018+
.set_application_protocol_preference(["http/1.1", "h2"])?;
1019+
pair.client
1020+
.0
1021+
.connection
1022+
.append_application_protocol_preference(b"h2")?;
1023+
assert!(poll_tls_pair_result(&mut pair).is_ok());
1024+
let protocol = pair.server.0.connection.application_protocol().unwrap();
1025+
assert_eq!(protocol, b"h2");
1026+
Ok(())
1027+
}
10011028
}

0 commit comments

Comments
 (0)