whois_rust/who_is_lookup_options.rs
1use std::time::Duration;
2
3use validators::prelude::*;
4
5use crate::{Target, WhoIsError, WhoIsServerValue};
6
7const DEFAULT_FOLLOW: u16 = 2;
8const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
9const DEFAULT_MAX_RESPONSE_SIZE: usize = 4 * 1024 * 1024;
10
11/// The options about how to lookup.
12#[derive(Debug, Clone)]
13#[non_exhaustive]
14pub struct WhoIsLookupOptions {
15 /// The target that you want to lookup.
16 pub target: Target,
17 /// The WHOIS server that you want to use. If it is **None**, an appropriate WHOIS server will be chosen from the list of WHOIS servers that the `WhoIs` instance have. The default value is **None**.
18 pub server: Option<WhoIsServerValue>,
19 /// Number of times to follow redirects. The default value is 2.
20 pub follow: u16,
21 /// The timeout of each connection to a WHOIS server. **None** means no timeout at all. The default value is 60 seconds.
22 ///
23 /// The timeout is a budget for one connection: it covers resolving the address of the server, connecting to it (even when the server has several addresses to try) and reading the whole response. A lookup which follows referrals spends one budget per connection, so it can take up to `follow + 1` times this value.
24 pub timeout: Option<Duration>,
25 /// The maximum number of bytes to accept from a WHOIS server. A bigger response makes the lookup fail instead of being truncated. **None** means no limit at all, which lets a broken or malicious server exhaust the memory. The default value is 4 MiB.
26 pub max_response_size: Option<usize>,
27}
28
29impl WhoIsLookupOptions {
30 /// Create options with the default settings for a target.
31 #[inline]
32 pub fn from_target(target: Target) -> WhoIsLookupOptions {
33 WhoIsLookupOptions {
34 target,
35 server: None,
36 follow: DEFAULT_FOLLOW,
37 timeout: Some(DEFAULT_TIMEOUT),
38 max_response_size: Some(DEFAULT_MAX_RESPONSE_SIZE),
39 }
40 }
41
42 /// Parse a domain or an IP into a target and create options with the default settings.
43 #[allow(clippy::should_implement_trait)]
44 #[inline]
45 pub fn from_str<S: AsRef<str>>(s: S) -> Result<WhoIsLookupOptions, WhoIsError> {
46 Ok(Self::from_target(Target::parse_str(s)?))
47 }
48
49 /// Parse a domain or an IP into a target and create options with the default settings. The string may be reused as the target, avoiding an allocation.
50 #[inline]
51 pub fn from_string<S: Into<String>>(s: S) -> Result<WhoIsLookupOptions, WhoIsError> {
52 Ok(Self::from_target(Target::parse_string(s)?))
53 }
54}