Skip to content

Commit

Permalink
chore: fix parse request
Browse files Browse the repository at this point in the history
  • Loading branch information
tejmagar committed Jun 11, 2024
1 parent 80fd8c8 commit cb64918
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/core/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::core::cookie::{parse_cookies_from_header, Cookies};
use crate::core::session::{Session, SessionManager};
use crate::core::shortcuts::SingleText;

use super::forms::FormFieldError;

pub type QueryParams = HashMap<String, Vec<String>>;

pub struct Request {
Expand Down Expand Up @@ -106,10 +108,16 @@ impl Request {
}

pub async fn parse(&self) -> (FormData, Files) {
self.parse_body(self.form_constraints.clone()).await
return match self.parse_body(self.form_constraints.clone()).await {
Ok((form_data, files)) => (form_data, files),
Err(_) => (FormData::new(), Files::new()),
};
}

async fn parse_body(&self, form_constraints: Arc<FormConstraints>) -> (FormData, Files) {
pub async fn parse_body(
&self,
form_constraints: Arc<FormConstraints>,
) -> Result<(FormData, Files), FormFieldError> {
let form_data = FormData::new();
let files = Files::new();

Expand All @@ -118,7 +126,7 @@ impl Request {
content_type = value;
} else {
racoon_debug!("Content type is missing.");
return (form_data, files);
return Ok((form_data, files));
}

let body_read = self.body_read.clone();
Expand All @@ -139,18 +147,19 @@ impl Request {
{
Ok((form_data, files)) => {
self.body_read.store(true, Ordering::Relaxed);
(form_data, files)
Ok((form_data, files))
}
Err(error) => {
racoon_error!("Error while parsing multipart body: {:?}", error);
(form_data, files)
Ok((form_data, files))
}
};
} else if content_type
.to_lowercase()
.starts_with("application/x-www-form-urlencoded")
{
racoon_debug!("Parsing with UrlEncoded parser.");

return match UrlEncodedParser::parse(
self.stream.clone(),
&self.headers,
Expand All @@ -160,17 +169,17 @@ impl Request {
{
Ok(form_data) => {
self.body_read.store(true, Ordering::Relaxed);
(form_data, files)
Ok((form_data, files))
}
Err(error) => {
racoon_error!("Error while parsing x-www-urlencoded form. {:?}", error);
(form_data, files)
Err(error)
}
};
}

racoon_debug!("Unhandled enctype: {}", content_type);
(form_data, files)
Ok((form_data, files))
}
}

Expand Down

0 comments on commit cb64918

Please sign in to comment.