Skip to content
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 36 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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 Oct 24, 2024
c4ceb7e
feat(http): add cookie feature for client
StellarisW Oct 24, 2024
80d948b
feat(http): add multipart for server
StellarisW Oct 24, 2024
6ccaa68
feat(http): add multipart for server
StellarisW Oct 24, 2024
d75e64d
feat(http): add multipart for server
StellarisW Oct 24, 2024
709de2f
feat(http): add cookie for client
StellarisW Oct 29, 2024
66ff8d1
feat(http): add cookie for client
StellarisW Oct 29, 2024
d75b8a3
Merge branch 'main' into feat(http)/cookie_jar
StellarisW Oct 29, 2024
a05fdc3
feat(http): add cookie for client
StellarisW Oct 29, 2024
ab0cf6c
feat(http): add cookie for client
StellarisW Oct 29, 2024
c39b065
feat(http): add cookie for client
StellarisW Oct 29, 2024
8c7bc94
feat(http): add cookie for client
StellarisW Oct 29, 2024
8063073
feat(http): add cookie for client
StellarisW Oct 29, 2024
5c6f746
feat(http): add cookie for client
StellarisW Oct 29, 2024
3522e3e
feat(http): add cookie for client
StellarisW Oct 30, 2024
93a97a4
feat(http): add cookie for client
StellarisW Oct 30, 2024
5c71f99
feat(http): add cookie for client
StellarisW Oct 30, 2024
46e52af
feat(http): add cookie for client
StellarisW Oct 31, 2024
6c9b8ad
feat(http): add cookie for client
StellarisW Oct 31, 2024
1f2e378
feat(http): add cookie for client
StellarisW Oct 31, 2024
4c4c5c9
feat(http): add cookie for client
StellarisW Oct 31, 2024
fc10524
feat(http): add cookie for client
StellarisW Oct 31, 2024
58dece8
feat(http): add cookie for client
StellarisW Oct 31, 2024
c7d9017
feat(http): add cookie for client
StellarisW Oct 31, 2024
4e19070
feat(http): add cookie for client
StellarisW Oct 31, 2024
c73efcc
feat(http): add cookie for client
StellarisW Oct 31, 2024
9ba3062
feat(http): add cookie for client
StellarisW Oct 31, 2024
0b3d1e8
feat(http): add cookie for client
StellarisW Oct 31, 2024
e49ab79
feat(http): add cookie for client
StellarisW Nov 1, 2024
2c8d222
feat(http): add cookie for client
StellarisW Nov 1, 2024
91e936c
feat(http): add cookie for client
StellarisW Nov 1, 2024
2ac63d8
feat(http): add cookie for client
StellarisW Nov 1, 2024
3adaace
feat(http): add cookie for client
StellarisW Nov 1, 2024
5e173f0
Merge branch 'main' into feat(http)/cookie_jar
StellarisW Nov 4, 2024
4563027
feat(http): add cookie for client
StellarisW Nov 4, 2024
ecc906c
feat(http): add cookie for client
StellarisW Nov 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ chrono = { version = "0.4", default-features = false, features = [
clap = "4"
colored = "2"
cookie = "0.18"
cookie_store = "0.21"
dashmap = "6"
dirs = "5"
faststr = { version = "0.2.21", features = ["serde"] }
Expand Down
4 changes: 3 additions & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ tokio-native-tls = { workspace = true, optional = true }

# cookie support
cookie = { workspace = true, optional = true, features = ["percent-encode"] }
cookie_store = { workspace = true, optional = true }
url = { workspace = true, optional = true }

# serde and form, query, json
serde = { workspace = true, optional = true }
Expand Down Expand Up @@ -109,7 +111,7 @@ rustls = ["__tls", "dep:tokio-rustls", "volo/rustls"]
native-tls = ["__tls", "dep:tokio-native-tls", "volo/native-tls"]
native-tls-vendored = ["native-tls", "volo/native-tls-vendored"]

cookie = ["dep:cookie"]
cookie = ["dep:cookie", "dep:cookie_store", "dep:url"]

__serde = ["dep:serde"] # a private feature for enabling `serde` by `serde_xxx`
query = ["__serde", "dep:serde_urlencoded"]
Expand Down
88 changes: 88 additions & 0 deletions volo-http/src/client/cookie.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::sync::RwLock;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用异步锁


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)
}
}
Loading
Loading