aquatic_http: prococol module: parse Request from path

This commit is contained in:
Joakim Frostegård 2020-07-02 22:53:03 +02:00
parent d5b82bcf70
commit fb6caf7343
2 changed files with 13 additions and 15 deletions

View file

@ -65,19 +65,21 @@ impl EstablishedConnection {
} }
let mut headers = [httparse::EMPTY_HEADER; 16]; let mut headers = [httparse::EMPTY_HEADER; 16];
let mut request = httparse::Request::new(&mut headers); let mut http_request = httparse::Request::new(&mut headers);
let request = match request.parse(&self.buf[..self.bytes_read]){ match http_request.parse(&self.buf[..self.bytes_read]){
Ok(httparse::Status::Complete(_)) => { Ok(httparse::Status::Complete(_)) => {
let result = if let Some(request) = Request::from_http(request){ let opt_request = http_request.path.and_then(
Ok(request) Request::from_http_get_path
} else { );
Err(RequestReadError::Invalid)
};
self.bytes_read = 0; self.bytes_read = 0;
result if let Some(request) = opt_request {
Ok(request)
} else {
Err(RequestReadError::Invalid)
}
}, },
Ok(httparse::Status::Partial) => { Ok(httparse::Status::Partial) => {
Err(RequestReadError::NeedMoreData) Err(RequestReadError::NeedMoreData)
@ -87,9 +89,7 @@ impl EstablishedConnection {
Err(RequestReadError::Parse(err)) Err(RequestReadError::Parse(err))
} }
}; }
request
} }
pub fn send_response(&mut self, body: &[u8]) -> ::std::io::Result<()> { pub fn send_response(&mut self, body: &[u8]) -> ::std::io::Result<()> {

View file

@ -142,10 +142,8 @@ pub enum Request {
impl Request { impl Request {
pub fn from_http(http: httparse::Request) -> Option<Self> { pub fn from_http_get_path(path: &str) -> Option<Self> {
log::debug!("path: {:?}", http.path); log::debug!("path: {:?}", path);
let path = http.path?;
let mut split_parts= path.splitn(2, '?'); let mut split_parts= path.splitn(2, '?');