diff options
| -rw-r--r-- | crates/shirabe/src/repository/composer_repository.rs | 5 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/vcs/forgejo_driver.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/vcs/github_driver.rs | 16 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/vcs/gitlab_driver.rs | 24 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http/curl_downloader.rs | 76 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http/curl_response.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http/response.rs | 51 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http_downloader.rs | 48 |
9 files changed, 63 insertions, 184 deletions
diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs index 74f1426..0d2fa2f 100644 --- a/crates/shirabe/src/repository/composer_repository.rs +++ b/crates/shirabe/src/repository/composer_repository.rs @@ -3253,14 +3253,15 @@ impl ComposerRepository { // if the file is in the cache, we fake a 304 Not Modified to allow the process to continue if last_modified_time.is_some() { - let resp = Response::new_fake(&self.url, 304, IndexMap::new(), String::new()); + let resp = Response::new(self.url.clone(), Some(304), Vec::new(), Some(String::new())); return self.async_fetch_file_accept(resp, filename, cache_key); } // special error code returned when network is being artificially disabled if let Some(te) = e.downcast_ref::<TransportException>() { if te.get_status_code() == Some(499) { - let resp = Response::new_fake(&self.url, 404, IndexMap::new(), String::new()); + let resp = + Response::new(self.url.clone(), Some(404), Vec::new(), Some(String::new())); return self.async_fetch_file_accept(resp, filename, cache_key); } } diff --git a/crates/shirabe/src/repository/vcs/forgejo_driver.rs b/crates/shirabe/src/repository/vcs/forgejo_driver.rs index ca7e9dd..30a0932 100644 --- a/crates/shirabe/src/repository/vcs/forgejo_driver.rs +++ b/crates/shirabe/src/repository/vcs/forgejo_driver.rs @@ -625,17 +625,11 @@ impl ForgejoDriver { })?; return Ok(Response::new( - { - let mut m = IndexMap::new(); - m.insert("url".to_string(), PhpMixed::String("dummy".to_string())); - m - }, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } if !self.inner.io.has_authentication(&self.inner.origin_url) { diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs index 85288bd..644ac90 100644 --- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs @@ -728,15 +728,12 @@ impl GitBitbucketDriver { if !self.inner.io.is_interactive() && fetching_repo_data { self.attempt_clone_fallback()?; - let mut headers: IndexMap<String, PhpMixed> = IndexMap::new(); - headers - .insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - headers, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - )??); + )); } } } diff --git a/crates/shirabe/src/repository/vcs/github_driver.rs b/crates/shirabe/src/repository/vcs/github_driver.rs index 076f4a0..093cd03 100644 --- a/crates/shirabe/src/repository/vcs/github_driver.rs +++ b/crates/shirabe/src/repository/vcs/github_driver.rs @@ -1046,16 +1046,12 @@ impl GitHubDriver { self.attempt_clone_fallback(Some(&e)) .map_err(|err| TransportException::new(err.to_string(), 0))?; - let mut req = IndexMap::new(); - req.insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - req, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } let mut scopes_issued: Vec<String> = vec![]; @@ -1112,16 +1108,12 @@ impl GitHubDriver { self.attempt_clone_fallback(Some(&e)) .map_err(|err| TransportException::new(err.to_string(), 0))?; - let mut req = IndexMap::new(); - req.insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - req, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } let rate_limited = git_hub_util diff --git a/crates/shirabe/src/repository/vcs/gitlab_driver.rs b/crates/shirabe/src/repository/vcs/gitlab_driver.rs index 6f2c4e9..3e2cd38 100644 --- a/crates/shirabe/src/repository/vcs/gitlab_driver.rs +++ b/crates/shirabe/src/repository/vcs/gitlab_driver.rs @@ -841,16 +841,12 @@ impl GitLabDriver { self.attempt_clone_fallback() .map_err(|e| TransportException::new(e.to_string(), 0))?; - let mut req = IndexMap::new(); - req.insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - req, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } } @@ -919,16 +915,12 @@ impl GitLabDriver { self.attempt_clone_fallback() .map_err(|err| TransportException::new(err.to_string(), 0))?; - let mut req = IndexMap::new(); - req.insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - req, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } self.inner.io.write_error3( &format!( @@ -960,16 +952,12 @@ impl GitLabDriver { self.attempt_clone_fallback() .map_err(|err| TransportException::new(err.to_string(), 0))?; - let mut req = IndexMap::new(); - req.insert("url".to_string(), PhpMixed::String("dummy".to_string())); return Ok(Response::new( - req, + "dummy".to_string(), Some(200), vec![], Some("null".to_string()), - ) - .unwrap() - .unwrap()); + )); } Err(e) diff --git a/crates/shirabe/src/util/http/curl_downloader.rs b/crates/shirabe/src/util/http/curl_downloader.rs index b86564f..9d572fa 100644 --- a/crates/shirabe/src/util/http/curl_downloader.rs +++ b/crates/shirabe/src/util/http/curl_downloader.rs @@ -1060,31 +1060,19 @@ impl CurlDownloader { ); } contents = c; - response = Some( - CurlResponse::new( - { - let mut m: IndexMap<String, PhpMixed> = IndexMap::new(); - m.insert( - "url".to_string(), - PhpMixed::String( - job.get("url") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(), - ), - ); - m - }, - status_code, - headers.clone().unwrap_or_default(), - contents.as_string().map(|s| s.to_string()), - progress - .iter() - .map(|(k, v)| (k.clone(), (**v).clone())) - .collect(), - )? - .map_err(|e| anyhow::anyhow!(e.message))?, - ); + response = Some(CurlResponse::new( + job.get("url") + .and_then(|v| v.as_string()) + .unwrap_or("") + .to_string(), + status_code, + headers.clone().unwrap_or_default(), + contents.as_string().map(|s| s.to_string()), + progress + .iter() + .map(|(k, v)| (k.clone(), (**v).clone())) + .collect(), + )); self.io.write_error3( &format!( "[{}] {}", @@ -1139,31 +1127,19 @@ impl CurlDownloader { ); } - response = Some( - CurlResponse::new( - { - let mut m: IndexMap<String, PhpMixed> = IndexMap::new(); - m.insert( - "url".to_string(), - PhpMixed::String( - job.get("url") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(), - ), - ); - m - }, - status_code, - headers.clone().unwrap_or_default(), - contents.as_string().map(|s| s.to_string()), - progress - .iter() - .map(|(k, v)| (k.clone(), (**v).clone())) - .collect(), - )? - .map_err(|e| anyhow::anyhow!(e.message))?, - ); + response = Some(CurlResponse::new( + job.get("url") + .and_then(|v| v.as_string()) + .unwrap_or("") + .to_string(), + status_code, + headers.clone().unwrap_or_default(), + contents.as_string().map(|s| s.to_string()), + progress + .iter() + .map(|(k, v)| (k.clone(), (**v).clone())) + .collect(), + )); self.io.write_error3( &format!( "[{}] {}", diff --git a/crates/shirabe/src/util/http/curl_response.rs b/crates/shirabe/src/util/http/curl_response.rs index 9f330b6..cc3d21b 100644 --- a/crates/shirabe/src/util/http/curl_response.rs +++ b/crates/shirabe/src/util/http/curl_response.rs @@ -13,16 +13,14 @@ pub struct CurlResponse { impl CurlResponse { pub fn new( - request: IndexMap<String, PhpMixed>, + url: String, code: Option<i64>, headers: Vec<String>, body: Option<String>, curl_info: IndexMap<String, PhpMixed>, - ) -> anyhow::Result<Result<Self, shirabe_php_shim::LogicException>> { - match Response::new(request, code, headers, body)? { - Ok(inner) => Ok(Ok(Self { inner, curl_info })), - Err(e) => Ok(Err(e)), - } + ) -> Self { + let inner = Response::new(url, code, headers, body); + Self { inner, curl_info } } pub fn get_curl_info(&self) -> &IndexMap<String, PhpMixed> { diff --git a/crates/shirabe/src/util/http/response.rs b/crates/shirabe/src/util/http/response.rs index a2bc0f1..4810397 100644 --- a/crates/shirabe/src/util/http/response.rs +++ b/crates/shirabe/src/util/http/response.rs @@ -1,37 +1,25 @@ //! ref: composer/src/Composer/Util/Http/Response.php use crate::json::JsonFile; -use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; -use shirabe_php_shim::{LogicException, PhpMixed, preg_quote}; +use shirabe_php_shim::{PhpMixed, preg_quote}; #[derive(Debug)] pub struct Response { - request: IndexMap<String, PhpMixed>, + url: String, code: i64, headers: Vec<String>, body: Option<String>, } impl Response { - pub fn new( - request: IndexMap<String, PhpMixed>, - code: Option<i64>, - headers: Vec<String>, - body: Option<String>, - ) -> anyhow::Result<Result<Self, LogicException>> { - if !request.contains_key("url") { - return Ok(Err(LogicException { - message: "url key missing from request array".to_string(), - code: 0, - })); - } - Ok(Ok(Self { - request, + pub fn new(url: String, code: Option<i64>, headers: Vec<String>, body: Option<String>) -> Self { + Self { + url, code: code.unwrap_or(0), headers, body, - })) + } } pub fn get_status_code(&self) -> i64 { @@ -63,16 +51,11 @@ impl Response { } pub fn decode_json(&self) -> anyhow::Result<PhpMixed> { - let url = self - .request - .get("url") - .and_then(|u| u.as_string()) - .unwrap_or(""); - JsonFile::parse_json(self.body.as_deref(), Some(url)) + JsonFile::parse_json(self.body.as_deref(), Some(self.url.as_str())) } pub fn collect(&mut self) { - self.request = IndexMap::new(); + self.url = String::new(); self.code = 0; self.headers = vec![]; self.body = None; @@ -96,22 +79,4 @@ impl Response { } value } - - // TODO(phase-b): historical helpers used in composer_repository — provide stubs. - pub fn from_php_mixed(_data: PhpMixed) -> Self { - todo!() - } - - pub fn to_php_mixed(&self) -> PhpMixed { - todo!() - } - - pub fn new_fake( - _url: &str, - _code: i64, - _headers: IndexMap<String, PhpMixed>, - _body: String, - ) -> Self { - todo!() - } } diff --git a/crates/shirabe/src/util/http_downloader.rs b/crates/shirabe/src/util/http_downloader.rs index b33ed58..5d0eae6 100644 --- a/crates/shirabe/src/util/http_downloader.rs +++ b/crates/shirabe/src/util/http_downloader.rs @@ -388,10 +388,7 @@ impl HttpDownloader { let headers = rfs.get_last_headers().to_vec(); let code = RemoteFilesystem::find_status_code(&headers); let body = Some(format!("{}~", copy_to)); - match Response::new(Self::request_to_map(&request), code, headers, body)? { - Ok(r) => Ok(r), - Err(e) => Err(e.into()), - } + Ok(Response::new(request.url.clone(), code, headers, body)) } else { let body = match rfs.get_contents(&origin, &url, false, options.clone())? { GetResult::Content(s) => Some(s), @@ -399,10 +396,7 @@ impl HttpDownloader { }; let headers = rfs.get_last_headers().to_vec(); let code = RemoteFilesystem::find_status_code(&headers); - match Response::new(Self::request_to_map(&request), code, headers, body)? { - Ok(r) => Ok(r), - Err(e) => Err(e.into()), - } + Ok(Response::new(request.url.clone(), code, headers, body)) } })() }; @@ -410,30 +404,6 @@ impl HttpDownloader { self.settle_job(id, result); } - /// PHP `new Response($job['request'], ...)` is fed the whole request array; reproduce it. - fn request_to_map(request: &Request) -> IndexMap<String, PhpMixed> { - let mut m: IndexMap<String, PhpMixed> = IndexMap::new(); - m.insert("url".to_string(), PhpMixed::String(request.url.clone())); - m.insert( - "options".to_string(), - PhpMixed::Array( - request - .options - .iter() - .map(|(k, v)| (k.clone(), Box::new(v.clone()))) - .collect(), - ), - ); - m.insert( - "copyTo".to_string(), - match &request.copy_to { - Some(s) => PhpMixed::String(s.clone()), - None => PhpMixed::Null, - }, - ); - m - } - /// Applies the effect of PHP's promise `.then` handlers: records the response/exception, /// transitions the job status and decrements the running-job counter. fn settle_job(&mut self, id: i64, result: anyhow::Result<Response>) { @@ -508,14 +478,12 @@ impl HttpDownloader { } }; if has_if_modified_since { - let mut req_map: IndexMap<String, PhpMixed> = IndexMap::new(); - req_map.insert("url".to_string(), PhpMixed::String(url.clone())); - let response = - match Response::new(req_map, Some(304), Vec::new(), Some(String::new())) { - Ok(Ok(r)) => Ok(r), - Ok(Err(e)) => Err(e.into()), - Err(e) => Err(e), - }; + let response = Ok(Response::new( + url.clone(), + Some(304), + Vec::new(), + Some(String::new()), + )); self.settle_job(id, response); } else { let mut e = TransportException::new( |
