-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: benwis <[email protected]>
- Loading branch information
Showing
6 changed files
with
80 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::{error::ServerFnError, request::Req}; | ||
use axum::body::{Body, Bytes}; | ||
use futures::{Stream, StreamExt}; | ||
use http::{ | ||
header::{ACCEPT, CONTENT_TYPE, REFERER}, | ||
Request, | ||
}; | ||
use http_body_util::BodyExt; | ||
use std::borrow::Cow; | ||
|
||
impl<CustErr> Req<CustErr> for IncomingRequest | ||
where | ||
CustErr: 'static, | ||
{ | ||
fn as_query(&self) -> Option<&str> { | ||
self.uri().query() | ||
} | ||
|
||
fn to_content_type(&self) -> Option<Cow<'_, str>> { | ||
self.headers() | ||
.get(CONTENT_TYPE) | ||
.map(|h| String::from_utf8_lossy(h.as_bytes())) | ||
} | ||
|
||
fn accepts(&self) -> Option<Cow<'_, str>> { | ||
self.headers() | ||
.get(ACCEPT) | ||
.map(|h| String::from_utf8_lossy(h.as_bytes())) | ||
} | ||
|
||
fn referer(&self) -> Option<Cow<'_, str>> { | ||
self.headers() | ||
.get(REFERER) | ||
.map(|h| String::from_utf8_lossy(h.as_bytes())) | ||
} | ||
|
||
async fn try_into_bytes(self) -> Result<Bytes, ServerFnError<CustErr>> { | ||
let (_parts, body) = self.into_parts(); | ||
|
||
body.collect() | ||
.await | ||
.map(|c| c.to_bytes()) | ||
.map_err(|e| ServerFnError::Deserialization(e.to_string())) | ||
} | ||
|
||
async fn try_into_string(self) -> Result<String, ServerFnError<CustErr>> { | ||
let bytes = self.try_into_bytes().await?; | ||
String::from_utf8(bytes.to_vec()) | ||
.map_err(|e| ServerFnError::Deserialization(e.to_string())) | ||
} | ||
|
||
fn try_into_stream( | ||
self, | ||
) -> Result< | ||
impl Stream<Item = Result<Bytes, ServerFnError>> + Send + 'static, | ||
ServerFnError<CustErr>, | ||
> { | ||
Ok(self.into_body().into_data_stream().map(|chunk| { | ||
chunk.map_err(|e| ServerFnError::Deserialization(e.to_string())) | ||
})) | ||
} | ||
} |