Skip to content

Commit a7d417c

Browse files
committed
Fix socket.getattrinfo
1 parent d1ff2cd commit a7d417c

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

crates/stdlib/src/socket.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,13 +1951,31 @@ mod _socket {
19511951
flags: opts.flags,
19521952
};
19531953

1954-
let host = opts.host.as_ref().map(|s| s.as_str());
1955-
let port = opts.port.as_ref().map(|p| -> std::borrow::Cow<'_, str> {
1956-
match p {
1957-
Either::A(s) => s.as_str().into(),
1958-
Either::B(i) => i.to_string().into(),
1954+
// Encode host using IDNA encoding
1955+
let host_encoded: Option<String> = match opts.host.as_ref() {
1956+
Some(s) => {
1957+
let encoded =
1958+
vm.state
1959+
.codec_registry
1960+
.encode_text(s.to_owned().into(), "idna", None, vm)?;
1961+
let host_str = std::str::from_utf8(encoded.as_bytes())
1962+
.map_err(|_| vm.new_runtime_error("idna output is not utf8".to_owned()))?;
1963+
Some(host_str.to_owned())
19591964
}
1960-
});
1965+
None => None,
1966+
};
1967+
let host = host_encoded.as_deref();
1968+
1969+
// Encode port using UTF-8
1970+
let port: Option<std::borrow::Cow<'_, str>> = match opts.port.as_ref() {
1971+
Some(Either::A(s)) => {
1972+
Some(std::borrow::Cow::Borrowed(s.to_str().ok_or_else(|| {
1973+
vm.new_unicode_encode_error("surrogates not allowed".to_owned())
1974+
})?))
1975+
}
1976+
Some(Either::B(i)) => Some(std::borrow::Cow::Owned(i.to_string())),
1977+
None => None,
1978+
};
19611979
let port = port.as_ref().map(|p| p.as_ref());
19621980

19631981
let addrs = dns_lookup::getaddrinfo(host, port, Some(hints))

0 commit comments

Comments
 (0)