Skip to content

Commit

Permalink
Addressed cargo clippy comments.
Browse files Browse the repository at this point in the history
Refactored network code to improve readability.
  • Loading branch information
zlogic committed Jul 27, 2024
1 parent a8f502d commit 6e3770c
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 210 deletions.
36 changes: 15 additions & 21 deletions src/fortivpn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,15 @@ pub async fn get_oauth_cookie(config: &Config) -> Result<String, FortiError> {
};
let mut socket = BufStream::new(socket);
let headers = http::read_headers(&mut socket).await?;
let token_id = headers
.lines()
.next()
.map(|line| {
if !line.starts_with("GET /?id=") {
return None;
}
let start_index = line.find("=")?;
let line = &line[start_index + 1..];
let end_index = line.find(" ")?;
Some((&line[..end_index]).to_string())
})
.flatten();
let token_id = headers.lines().next().and_then(|line| {
if !line.starts_with("GET /?id=") {
return None;
}
let start_index = line.find("=")?;
let line = &line[start_index + 1..];
let end_index = line.find(" ")?;
Some(line[..end_index].to_string())
});

let token_id = if let Some(token_id) = token_id {
token_id
Expand Down Expand Up @@ -108,7 +104,7 @@ pub async fn get_oauth_cookie(config: &Config) -> Result<String, FortiError> {
if let Some(start_index) = line.find(":") {
let line = &line[start_index + 2..];
if let Some(end_index) = line.find("; ") {
cookie = Some((&line[..end_index]).to_string());
cookie = Some(line[..end_index].to_string());
}
}
}
Expand Down Expand Up @@ -168,7 +164,7 @@ impl FortiVPNTunnel {
}

pub fn mtu(&self) -> usize {
self.mtu as usize
self.mtu
}

async fn connect(hostport: &str, domain: &str) -> Result<BufTlsStream, FortiError> {
Expand Down Expand Up @@ -283,7 +279,7 @@ impl FortiVPNTunnel {
.iter_options()
.collect::<Result<Vec<_>, ppp::FormatError>>();
let options_match = match received_opts {
Ok(received_opts) => &opts == received_opts.as_slice(),
Ok(received_opts) => opts == received_opts.as_slice(),
Err(err) => {
debug!("Failed to decode LCP Ack options: {}", err);
return Err("Failed to decode LCP Ack options".into());
Expand Down Expand Up @@ -504,7 +500,7 @@ impl FortiVPNTunnel {
packet_header[6..].copy_from_slice(&protocol.value().to_be_bytes());

socket.write_all(&packet_header).await?;
Ok(socket.write_all(&ppp_data).await?)
Ok(socket.write_all(ppp_data).await?)
}

async fn read_ppp_packet(
Expand Down Expand Up @@ -781,9 +777,7 @@ impl PPPState {
}
}
}
if let Err(err) = self.validate_link(socket).await {
return Err(err);
}
self.validate_link(socket).await?;

let mut ppp_size = [0u8; 2];
ppp_size.copy_from_slice(&self.ppp_header[..2]);
Expand All @@ -799,7 +793,7 @@ impl PPPState {
);
return Err("Header has conflicting length data".into());
}
if magic != &[0x50, 0x50] {
if magic != [0x50, 0x50] {
debug!(
"Found {:x}{:x} instead of magic",
self.ppp_header[2], self.ppp_header[3]
Expand Down
12 changes: 6 additions & 6 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn read_content<S>(socket: &mut S, headers: &str) -> Result<String, Ht
where
S: AsyncRead + AsyncBufRead + AsyncWrite + Unpin,
{
if headers.find("Transfer-Encoding: chunked").is_some() {
if headers.contains("Transfer-Encoding: chunked") {
// This is super janky, but should work if chunks are small enough.
// openfortivpn works the same way.
return read_until(socket, CHUNKS_END_MARKER).await;
Expand All @@ -60,9 +60,9 @@ where
pub fn read_content_length(headers: &str) -> Option<usize> {
const CONTENT_LENGTH_HEADER: &str = "Content-Length: ";
for line in headers.lines() {
if line.starts_with(CONTENT_LENGTH_HEADER) {
match &line[CONTENT_LENGTH_HEADER.len()..].parse::<usize>() {
Ok(content_length) => return Some(*content_length),
if let Some(content_length) = line.strip_prefix(CONTENT_LENGTH_HEADER) {
match content_length.parse::<usize>() {
Ok(content_length) => return Some(content_length),
Err(err) => {
warn!("Failed to parse content-length: {}", err);
continue;
Expand All @@ -76,8 +76,8 @@ pub fn read_content_length(headers: &str) -> Option<usize> {
pub fn read_host(headers: &str) -> Option<&str> {
const HOST_HEADER: &str = "Host: ";
for line in headers.lines() {
if line.starts_with(HOST_HEADER) {
return Some(&line[HOST_HEADER.len()..]);
if let Some(host) = line.strip_prefix(HOST_HEADER) {
return Some(host);
}
}
None
Expand Down
Loading

0 comments on commit 6e3770c

Please sign in to comment.