Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP protocol #469

Merged
merged 17 commits into from
Oct 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand klog format to also include response status
swlynch99 committed Oct 3, 2022
commit a7239057fc1f0ea9dd12af117491d4d5db458628
21 changes: 15 additions & 6 deletions src/protocol/http/src/request.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
use std::fmt;
use std::mem::MaybeUninit;

use crate::{Error, ParseResult};
use crate::{response::status_line, Error, ParseResult};
use httparse::{Header, ParserConfig, Status};
use logger::{error, klog};
use protocol_common::{Parse, ParseOk};
@@ -146,16 +146,25 @@ impl Parse<ParseData> for RequestParser {
impl logger::Klog for Request {
type Response = crate::Response;

fn klog(&self, _: &Self::Response) {
fn klog(&self, response: &Self::Response) {
use bstr::BStr;

let status = response.status();
let line = status_line(status).unwrap_or("");

match self.data() {
RequestData::Get(key) => klog!("GET {}", BStr::new(key)),
RequestData::Delete(key) => klog!("DELETE {}", BStr::new(key)),
RequestData::Get(key) => klog!("GET '{}' => {} {}", BStr::new(key), status, line),
RequestData::Delete(key) => klog!("DELETE '{}' => {} {}", BStr::new(key), status, line),
RequestData::Put(key, val) => {
klog!("PUT {} length={}", BStr::new(key), val.len())
klog!(
"PUT '{}' {} => {} {}",
BStr::new(key),
val.len(),
status,
line
)
}
}
};
}
}

13 changes: 12 additions & 1 deletion src/protocol/http/src/response.rs
Original file line number Diff line number Diff line change
@@ -16,10 +16,15 @@ impl Response {
pub fn builder(status: u16) -> ResponseBuilder {
ResponseBuilder::new(status)
}

pub fn status(&self) -> u16 {
self.builder.status
}
}

pub struct ResponseBuilder {
headers: Vec<u8>,
status: u16,
close: bool,
}

@@ -30,13 +35,14 @@ impl ResponseBuilder {
&mut data,
"HTTP/1.1 {} {}\r\n",
status,
STATUSES.get(&status).copied().unwrap_or("")
status_line(status).unwrap_or("")
)
.unwrap();

Self {
headers: data,
close: false,
status,
}
}

@@ -82,6 +88,7 @@ impl ResponseBuilder {
Self {
headers: std::mem::take(&mut self.headers),
close: self.close,
status: self.status,
}
}
}
@@ -122,6 +129,10 @@ impl Compose for Response {
}
}

pub(crate) fn status_line(status: u16) -> Option<&'static str> {
STATUSES.get(&status).copied()
}

const STATUSES: Map<u16, &'static str> = phf_map! {
// Informational Responses
100u16 => "Continue",