Skip to content

Commit

Permalink
crates/examples/sel4cp/http-server: Add support for keep-alive
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Aug 31, 2023
1 parent 03a325d commit bda21f0
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions crates/examples/sel4cp/http-server/pds/server/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ impl<T: BytesIO> Server<T> {
&self,
conn: &mut U,
) -> Result<(), ClosedError<U::Error>> {
let mut buf = vec![0; 1024 * 16];
let mut i = 0;
loop {
let n = conn.recv(&mut buf[i..]).await?;
assert_ne!(n, 0);
i += n;
if is_request_complete(&buf[..i]).unwrap_or(false) {
break;
let mut buf = vec![0; 1024 * 16];
let mut i = 0;
loop {
let n = conn.recv(&mut buf[i..]).await?;
assert_ne!(n, 0);
i += n;
if is_request_complete(&buf[..i]).unwrap_or(false) {
break;
}
}
}
let mut headers = [httparse::EMPTY_HEADER; 32];
let mut req = httparse::Request::new(&mut headers);
match req.parse(&buf) {
Ok(status) => {
assert!(status.is_complete());
self.handle_request(conn, req.path.unwrap()).await?;
let mut headers = [httparse::EMPTY_HEADER; 32];
let mut req = httparse::Request::new(&mut headers);
let mut keep_alive = false;
match req.parse(&buf) {
Ok(status) => {
assert!(status.is_complete());
self.handle_request(conn, req.path.unwrap()).await?;
if should_keep_alive(&req) {
keep_alive = true;
}
}
Err(err) => {
log::warn!("error parsing request: {err:?}");
}
}
Err(err) => {
log::warn!("error parsing request: {err:?}");
if !keep_alive {
break;
}
}
Ok(())
Expand Down Expand Up @@ -228,6 +237,27 @@ fn is_request_complete(buf: &[u8]) -> Result<bool, httparse::Error> {
req.parse(buf).map(|status| status.is_complete())
}

fn should_keep_alive(req: &httparse::Request) -> bool {
let version = req.version.unwrap();
let default = match version {
0 => false,
1 => true,
_ => panic!(),
};
for header in req.headers.iter() {
if header.name.to_lowercase() == "Connection" {
if header.value == b"close" {
return false;
}
if header.value == b"keep-alive" {
return true;
}
panic!();
}
}
default
}

fn content_type_from_name(name: &str) -> &'static str {
for (ext, ty) in MIME_ASSOCS {
if ext.is_suffix_of(name) {
Expand Down

0 comments on commit bda21f0

Please sign in to comment.