allow multiple headers with the same name
This commit is contained in:
parent
91da62d719
commit
ce52997bed
@ -136,11 +136,11 @@ impl Body {
|
|||||||
pub async fn recv(stream: &mut (impl AsyncReadExt + Unpin), headers: &Headers) -> Result<Body, HttpError> {
|
pub async fn recv(stream: &mut (impl AsyncReadExt + Unpin), headers: &Headers) -> Result<Body, HttpError> {
|
||||||
let mut reqdata: Vec<u8> = Vec::new();
|
let mut reqdata: Vec<u8> = Vec::new();
|
||||||
|
|
||||||
if let Some(content_size) = headers.clone().get("content-length".to_string()) {
|
if let Some(content_size) = headers.clone().get("content-length".to_string()).get(0) {
|
||||||
let content_size: usize = content_size.parse().map_err(|_| HttpError::InvalidContentSize)?;
|
let content_size: usize = content_size.parse().map_err(|_| HttpError::InvalidContentSize)?;
|
||||||
reqdata.resize(content_size, 0);
|
reqdata.resize(content_size, 0);
|
||||||
stream.read_exact(&mut reqdata).await.map_err(|_| HttpError::InvalidContent)?;
|
stream.read_exact(&mut reqdata).await.map_err(|_| HttpError::InvalidContent)?;
|
||||||
} else if let Some(transfer_encoding) = headers.clone().get("transfer_encoding".to_string()) {
|
} else if let Some(transfer_encoding) = headers.clone().get("transfer_encoding".to_string()).get(0) {
|
||||||
if transfer_encoding.split(",").map(|o| o.trim()).find(|o| o == &"chunked").is_some() {
|
if transfer_encoding.split(",").map(|o| o.trim()).find(|o| o == &"chunked").is_some() {
|
||||||
loop {
|
loop {
|
||||||
let length = usize::from_str_radix(&read_line_crlf(stream).await?, 16).map_err(|_| HttpError::InvalidContent)?;
|
let length = usize::from_str_radix(&read_line_crlf(stream).await?, 16).map_err(|_| HttpError::InvalidContent)?;
|
||||||
|
@ -51,39 +51,32 @@ impl Headers {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, key: impl ToString) -> Option<String> {
|
pub fn get(&self, key: impl ToString) -> Vec<String> {
|
||||||
for (k, v) in &self.entries {
|
return self.entries.clone().into_iter()
|
||||||
if k.to_lowercase() == key.to_string().to_lowercase() {
|
.filter(|(k, _)| k.to_lowercase() == key.to_string().to_lowercase())
|
||||||
return Some(v.clone());
|
.map(|(_, v)| v)
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return None;
|
pub fn add(&mut self, key: impl ToString, value: String) {
|
||||||
|
self.entries.push((key.to_string(), value));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put(&mut self, key: impl ToString, value: String) {
|
pub fn put(&mut self, key: impl ToString, value: String) {
|
||||||
for t in self.entries.iter_mut() {
|
self.remove(key.to_string());
|
||||||
if t.0.to_lowercase() == key.to_string().to_lowercase() {
|
|
||||||
t.1 = value;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.entries.push((key.to_string(), value));
|
self.entries.push((key.to_string(), value));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put_default(&mut self, key: impl ToString, value: String) {
|
pub fn put_default(&mut self, key: impl ToString, value: String) {
|
||||||
for t in self.entries.iter_mut() {
|
if self.get(key.to_string()).is_empty() {
|
||||||
if t.0.to_lowercase() == key.to_string().to_lowercase() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.entries.push((key.to_string(), value));
|
self.entries.push((key.to_string(), value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, key: impl ToString) {
|
pub fn remove(&mut self, key: impl ToString) {
|
||||||
for (i, t) in self.entries.iter_mut().enumerate() {
|
for (i, t) in self.entries.clone().iter().enumerate() {
|
||||||
if t.0.to_lowercase() == key.to_string().to_lowercase() {
|
if t.0.to_lowercase() == key.to_string().to_lowercase() {
|
||||||
self.entries.remove(i);
|
self.entries.remove(i);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +109,7 @@ impl Headers {
|
|||||||
if text.len() == 0 { break }
|
if text.len() == 0 { break }
|
||||||
|
|
||||||
let (key, value) = text.split_once(": ").ok_or(HttpError::InvalidHeaders)?;
|
let (key, value) = text.split_once(": ").ok_or(HttpError::InvalidHeaders)?;
|
||||||
headers.put(key.to_lowercase(), value.to_string());
|
headers.add(key, value.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(headers)
|
Ok(headers)
|
||||||
|
@ -186,7 +186,7 @@ impl HttpRequest {
|
|||||||
|
|
||||||
/// Get multipart parts (requires Content-Type header)
|
/// Get multipart parts (requires Content-Type header)
|
||||||
pub fn get_multipart(&self) -> Option<Vec<Part>> {
|
pub fn get_multipart(&self) -> Option<Vec<Part>> {
|
||||||
let boundary = self.headers.get("content-type")?
|
let boundary = self.headers.get("content-type").get(0)?
|
||||||
.split(";")
|
.split(";")
|
||||||
.map(|o| o.trim())
|
.map(|o| o.trim())
|
||||||
.find(|o| o.starts_with("boundary="))
|
.find(|o| o.starts_with("boundary="))
|
||||||
|
@ -49,7 +49,7 @@ impl HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_multipart(&self) -> Option<Vec<Part>> {
|
pub fn get_multipart(&self) -> Option<Vec<Part>> {
|
||||||
let boundary = self.headers.get("content-type")?
|
let boundary = self.headers.get("content-type").get(0)?
|
||||||
.split(";")
|
.split(";")
|
||||||
.map(|o| o.trim())
|
.map(|o| o.trim())
|
||||||
.find(|o| o.starts_with("boundary="))
|
.find(|o| o.starts_with("boundary="))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user