From 0db5b82a870938bd2e4dff61e5eb90d270e182b1 Mon Sep 17 00:00:00 2001 From: Miro Date: Fri, 15 Sep 2023 12:54:29 +0800 Subject: [PATCH 1/2] refactor(services/http): Rewrite http methods signature using OpRead/OpStat --- core/src/services/http/backend.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/core/src/services/http/backend.rs b/core/src/services/http/backend.rs index 37048748f921..1bbbea014023 100644 --- a/core/src/services/http/backend.rs +++ b/core/src/services/http/backend.rs @@ -233,7 +233,7 @@ impl Accessor for HttpBackend { async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { let resp = self - .http_get(path, args.range(), args.if_match(), args.if_none_match()) + .http_get(path, &args) .await?; let status = resp.status(); @@ -254,7 +254,7 @@ impl Accessor for HttpBackend { } let resp = self - .http_head(path, args.if_match(), args.if_none_match()) + .http_head(path, &args) .await?; let status = resp.status(); @@ -275,9 +275,7 @@ impl HttpBackend { async fn http_get( &self, path: &str, - range: BytesRange, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpRead, ) -> Result> { let p = build_rooted_abs_path(&self.root, path); @@ -285,11 +283,11 @@ impl HttpBackend { let mut req = Request::get(&url); - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } @@ -297,8 +295,8 @@ impl HttpBackend { req = req.header(header::AUTHORIZATION, auth.clone()) } - if !range.is_full() { - req = req.header(header::RANGE, range.to_header()); + if !args.range().is_full() { + req = req.header(header::RANGE, args.range().to_header()); } let req = req @@ -311,8 +309,7 @@ impl HttpBackend { async fn http_head( &self, path: &str, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpStat, ) -> Result> { let p = build_rooted_abs_path(&self.root, path); @@ -320,11 +317,11 @@ impl HttpBackend { let mut req = Request::head(&url); - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } From 237913beba8f96f9e08f527f92cb73da06b06f7b Mon Sep 17 00:00:00 2001 From: Miro Date: Fri, 15 Sep 2023 13:15:42 +0800 Subject: [PATCH 2/2] chore(services/http): format backend.rs --- core/src/services/http/backend.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/core/src/services/http/backend.rs b/core/src/services/http/backend.rs index 1bbbea014023..af50631cca47 100644 --- a/core/src/services/http/backend.rs +++ b/core/src/services/http/backend.rs @@ -232,9 +232,7 @@ impl Accessor for HttpBackend { } async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { - let resp = self - .http_get(path, &args) - .await?; + let resp = self.http_get(path, &args).await?; let status = resp.status(); @@ -253,9 +251,7 @@ impl Accessor for HttpBackend { return Ok(RpStat::new(Metadata::new(EntryMode::DIR))); } - let resp = self - .http_head(path, &args) - .await?; + let resp = self.http_head(path, &args).await?; let status = resp.status(); @@ -272,11 +268,7 @@ impl Accessor for HttpBackend { } impl HttpBackend { - async fn http_get( - &self, - path: &str, - args: &OpRead, - ) -> Result> { + async fn http_get(&self, path: &str, args: &OpRead) -> Result> { let p = build_rooted_abs_path(&self.root, path); let url = format!("{}{}", self.endpoint, percent_encode_path(&p)); @@ -306,11 +298,7 @@ impl HttpBackend { self.client.send(req).await } - async fn http_head( - &self, - path: &str, - args: &OpStat, - ) -> Result> { + async fn http_head(&self, path: &str, args: &OpStat) -> Result> { let p = build_rooted_abs_path(&self.root, path); let url = format!("{}{}", self.endpoint, percent_encode_path(&p));