-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat(http): add cookie feature for client #512
Merged
Merged
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
4bc8068
feat(http): add cookie feature for client
StellarisW c4ceb7e
feat(http): add cookie feature for client
StellarisW 80d948b
feat(http): add multipart for server
StellarisW 6ccaa68
feat(http): add multipart for server
StellarisW d75e64d
feat(http): add multipart for server
StellarisW 709de2f
feat(http): add cookie for client
StellarisW 66ff8d1
feat(http): add cookie for client
StellarisW d75b8a3
Merge branch 'main' into feat(http)/cookie_jar
StellarisW a05fdc3
feat(http): add cookie for client
StellarisW ab0cf6c
feat(http): add cookie for client
StellarisW c39b065
feat(http): add cookie for client
StellarisW 8c7bc94
feat(http): add cookie for client
StellarisW 8063073
feat(http): add cookie for client
StellarisW 5c6f746
feat(http): add cookie for client
StellarisW 3522e3e
feat(http): add cookie for client
StellarisW 93a97a4
feat(http): add cookie for client
StellarisW 5c71f99
feat(http): add cookie for client
StellarisW 46e52af
feat(http): add cookie for client
StellarisW 6c9b8ad
feat(http): add cookie for client
StellarisW 1f2e378
feat(http): add cookie for client
StellarisW 4c4c5c9
feat(http): add cookie for client
StellarisW fc10524
feat(http): add cookie for client
StellarisW 58dece8
feat(http): add cookie for client
StellarisW c7d9017
feat(http): add cookie for client
StellarisW 4e19070
feat(http): add cookie for client
StellarisW c73efcc
feat(http): add cookie for client
StellarisW 9ba3062
feat(http): add cookie for client
StellarisW 0b3d1e8
feat(http): add cookie for client
StellarisW e49ab79
feat(http): add cookie for client
StellarisW 2c8d222
feat(http): add cookie for client
StellarisW 91e936c
feat(http): add cookie for client
StellarisW 2ac63d8
feat(http): add cookie for client
StellarisW 3adaace
feat(http): add cookie for client
StellarisW 5e173f0
Merge branch 'main' into feat(http)/cookie_jar
StellarisW 4563027
feat(http): add cookie for client
StellarisW ecc906c
feat(http): add cookie for client
StellarisW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,88 @@ | ||
use std::sync::RwLock; | ||
|
||
use motore::{layer::Layer, Service}; | ||
|
||
use crate::{ | ||
context::ClientContext, | ||
error::ClientError, | ||
request::{ClientRequest, RequestPartsExt}, | ||
response::ClientResponse, | ||
utils::cookie::CookieStore, | ||
}; | ||
|
||
pub struct CookieService<S> { | ||
inner: S, | ||
cookie_store: RwLock<CookieStore>, | ||
} | ||
|
||
impl<S> CookieService<S> { | ||
fn new(inner: S, cookie_store: RwLock<CookieStore>) -> Self { | ||
Self { | ||
inner, | ||
cookie_store, | ||
} | ||
} | ||
} | ||
|
||
impl<S, B> Service<ClientContext, ClientRequest<B>> for CookieService<S> | ||
where | ||
S: Service<ClientContext, ClientRequest<B>, Response = ClientResponse, Error = ClientError> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
B: Send + 'static, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
|
||
async fn call( | ||
&self, | ||
cx: &mut ClientContext, | ||
mut req: ClientRequest<B>, | ||
) -> Result<Self::Response, Self::Error> { | ||
let url = req.url(); | ||
|
||
let (mut parts, body) = req.into_parts(); | ||
if let Some(url) = &url { | ||
if parts.headers.get(http::header::COOKIE).is_none() { | ||
self.cookie_store | ||
.read() | ||
.unwrap() | ||
.add_cookie_header(&mut parts.headers, url); | ||
} | ||
} | ||
|
||
req = ClientRequest::from_parts(parts, body); | ||
|
||
let resp = self.inner.call(cx, req).await?; | ||
|
||
if let Some(url) = &url { | ||
self.cookie_store | ||
.write() | ||
.unwrap() | ||
.with_response_headers(resp.headers(), url); | ||
} | ||
|
||
Ok(resp) | ||
} | ||
} | ||
|
||
pub struct CookieLayer { | ||
cookie_store: RwLock<CookieStore>, | ||
} | ||
|
||
impl CookieLayer { | ||
pub fn new(cookie_store: CookieStore) -> Self { | ||
Self { | ||
cookie_store: RwLock::new(cookie_store), | ||
} | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for CookieLayer { | ||
type Service = CookieService<S>; | ||
|
||
fn layer(self, inner: S) -> Self::Service { | ||
CookieService::new(inner, self.cookie_store) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用异步锁