Expect stream to implement ScmSocket trait

The stream used by the Connection structure is expected to implement
both Read and Write traits. Since we want the server to be able to
receive file descriptors through control messages mechanism, this patch
extends the expectations regarding the stream by adding ScmSocket to the
list of traits.

Since the stream is a UnixStream structure, and since vmm-sys-util
already provides an ScmSocket implementation for UnixStream, extending
the list of traits is very straightforward.

Relying on the newly added trait, the server now reads incoming bytes
through recv_with_fd() function, which replaces the former call to
read(). This change has no intent of modifying the former behavior from
the read(), which is why the returned Option<File> is ignored for now.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2021-07-12 17:28:52 +02:00
committed by georgepisaltu
parent 81a3c71efb
commit 3c4cc3aa91
3 changed files with 24 additions and 16 deletions
+6 -5
View File
@@ -10,6 +10,7 @@ pub use crate::common::{ConnectionError, HttpHeaderError, RequestError};
use crate::headers::Headers;
use crate::request::{find, Request, RequestLine};
use crate::response::{Response, StatusCode};
use vmm_sys_util::sock_ctrl_msg::ScmSocket;
const BUFFER_SIZE: usize = 1024;
@@ -51,7 +52,7 @@ pub struct HttpConnection<T> {
response_buffer: Option<Vec<u8>>,
}
impl<T: Read + Write> HttpConnection<T> {
impl<T: Read + Write + ScmSocket> HttpConnection<T> {
/// Creates an empty connection.
pub fn new(stream: T) -> Self {
Self {
@@ -126,10 +127,10 @@ impl<T: Read + Write> HttpConnection<T> {
}
// Append new bytes to what we already have in the buffer.
// The slice access is safe, the index is checked above.
let bytes_read = self
let (bytes_read, _) = self
.stream
.read(&mut self.buffer[self.read_cursor..])
.map_err(ConnectionError::StreamError)?;
.recv_with_fd(&mut self.buffer[self.read_cursor..])
.map_err(ConnectionError::StreamReadError)?;
// If the read returned 0 then the client has closed the connection.
if bytes_read == 0 {
@@ -392,7 +393,7 @@ impl<T: Read + Write> HttpConnection<T> {
let mut response_buffer_vec: Vec<u8> = Vec::new();
response
.write_all(&mut response_buffer_vec)
.map_err(ConnectionError::StreamError)?;
.map_err(ConnectionError::StreamWriteError)?;
self.response_buffer = Some(response_buffer_vec);
} else {
return Err(ConnectionError::InvalidWrite);