hubcio commented on code in PR #2923: URL: https://github.com/apache/iggy/pull/2923#discussion_r3001226093
########## core/common/src/utils/net.rs: ########## @@ -0,0 +1,291 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +use crate::IggyError; +use std::net::{Ipv4Addr, Ipv6Addr}; + +/// Validates that `addr` is syntactically a valid `host:port` string. +/// Does NOT perform DNS resolution. +/// +/// Accepted formats: +/// - `hostname:port` (e.g. `iggy-server:8090`, `localhost:8090`) +/// - `ipv4:port` (e.g. `127.0.0.1:8090`) +/// - `[ipv6]:port` (e.g. `[::1]:8090`) +/// +/// Rejected formats: +/// - Bare IPv6 without brackets (e.g. `::1:8080`) — ambiguous due to colons +/// - Missing port (e.g. `localhost`) +/// - Invalid port (e.g. `localhost:abc`, `localhost:65536`) +pub fn parse_server_address(addr: &str) -> Result<(), IggyError> { + if let Some(rest) = addr.strip_prefix('[') { + // Bracketed IPv6: "[::1]:port" + let close = rest.find(']').ok_or(IggyError::InvalidIpAddress( + addr.to_string(), + "".to_string(), + ))?; + let ipv6_str = &rest[0..close]; Review Comment: `&rest[0..close]` - the `0..` is redundant, `&rest[..close]` is idiomatic. ########## core/common/src/types/configuration/tcp_config/tcp_client_config_builder.rs: ########## @@ -105,11 +104,7 @@ impl TcpClientConfigBuilder { /// Builds the TCP client configuration. pub fn build(self) -> Result<TcpClientConfig, IggyError> { let addr = self.config.server_address.trim(); Review Comment: `build()` validates the trimmed `addr` but returns `self.config` with the original untrimmed `server_address`. if someone passes `" localhost:8090 "`, validation passes but `TcpStream::connect` later gets the whitespace-padded string. same issue in `websocket_client_config_builder.rs`. fix: `self.config.server_address = self.config.server_address.trim().to_string();` before validating. ########## core/common/src/utils/mod.rs: ########## @@ -23,6 +23,7 @@ pub(crate) mod crypto; pub(crate) mod duration; pub(crate) mod expiry; pub(crate) mod hash; +pub mod net; Review Comment: `pub mod net` but the sibling modules are `pub(crate)`. since only `parse_server_address` is re-exported in `lib.rs`, this could be `pub(crate) mod net` to match the pattern. ########## core/sdk/src/prelude.rs: ########## @@ -63,6 +63,7 @@ pub use iggy_common::{ TcpClientReconnectionConfig, Topic, TopicDetails, TopicPermissions, TransportEndpoints, TransportProtocol, UserId, UserStatus, Validatable, WebSocketClientConfig, WebSocketClientConfigBuilder, WebSocketClientReconnectionConfig, defaults, locking, + parse_server_address, Review Comment: `parse_server_address` is a low-level validation utility. the builders already call it internally, so SDK consumers rarely need it directly. consider keeping it importable from `iggy_common` without putting it in the prelude - prelude is for iggy users. ########## core/common/src/utils/net.rs: ########## @@ -0,0 +1,291 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +use crate::IggyError; +use std::net::{Ipv4Addr, Ipv6Addr}; + +/// Validates that `addr` is syntactically a valid `host:port` string. +/// Does NOT perform DNS resolution. +/// +/// Accepted formats: +/// - `hostname:port` (e.g. `iggy-server:8090`, `localhost:8090`) +/// - `ipv4:port` (e.g. `127.0.0.1:8090`) +/// - `[ipv6]:port` (e.g. `[::1]:8090`) +/// +/// Rejected formats: +/// - Bare IPv6 without brackets (e.g. `::1:8080`) — ambiguous due to colons +/// - Missing port (e.g. `localhost`) +/// - Invalid port (e.g. `localhost:abc`, `localhost:65536`) +pub fn parse_server_address(addr: &str) -> Result<(), IggyError> { Review Comment: the function name says "parse" but it returns `Result<(), IggyError>` - it validates, it doesn't parse anything. `validate_server_address` would be more accurate for a public API, or return a structured type like `(host, port)` to make the parse useful. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
