aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/http
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-06 19:47:36 +0900
committernsfisis <nsfisis@gmail.com>2026-06-06 19:47:36 +0900
commitaf4b9fcba1206e3fcc97fc243ffc674f85547942 (patch)
tree7066e219d80024150919316af900c666957a64fb /crates/shirabe/src/util/http
parentd9090c4c52fa29ee1569aedf8be858bf78001d7b (diff)
downloadphp-shirabe-af4b9fcba1206e3fcc97fc243ffc674f85547942.tar.gz
php-shirabe-af4b9fcba1206e3fcc97fc243ffc674f85547942.tar.zst
php-shirabe-af4b9fcba1206e3fcc97fc243ffc674f85547942.zip
refactor(http-response): take url directly instead of request map
Response only ever reads the url out of the request array, so accept it as a String directly. With url always present the 'url key missing' LogicException can no longer fire, so Response::new and CurlResponse::new return Self instead of a double Result. Also drops the unused from_php_mixed/to_php_mixed stubs and the request_to_map helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/util/http')
-rw-r--r--crates/shirabe/src/util/http/curl_downloader.rs76
-rw-r--r--crates/shirabe/src/util/http/curl_response.rs10
-rw-r--r--crates/shirabe/src/util/http/response.rs51
3 files changed, 38 insertions, 99 deletions
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!()
- }
}