diff options
Diffstat (limited to 'crates/shirabe/src/plugin')
| -rw-r--r-- | crates/shirabe/src/plugin/php_plugin_proxy.rs | 28 | ||||
| -rw-r--r-- | crates/shirabe/src/plugin/php_plugin_value.rs | 44 |
2 files changed, 66 insertions, 6 deletions
diff --git a/crates/shirabe/src/plugin/php_plugin_proxy.rs b/crates/shirabe/src/plugin/php_plugin_proxy.rs index 491a320c..c966897b 100644 --- a/crates/shirabe/src/plugin/php_plugin_proxy.rs +++ b/crates/shirabe/src/plugin/php_plugin_proxy.rs @@ -21,7 +21,7 @@ use crate::package::{DisplayMode, PackageInterfaceHandle}; use crate::plugin::capability::{Capability, CommandProvider}; use crate::plugin::capable::Capable; use crate::plugin::php_plugin_value::{ - date_time_from_wire, date_time_to_wire, link_from_wire, link_to_wire, + date_time_from_wire, date_time_to_wire, link_from_wire, link_to_wire, response_to_wire, }; use crate::plugin::plugin_interface::PluginInterface; use crate::repository::{ @@ -1177,11 +1177,27 @@ fn dispatch_http_downloader_method( downloader.borrow_mut().set_options(options); Ok(PluginValue::Null) } - // TODO(plugin): a `Composer\Util\Http\Response` has no representation on the wire, so - // the two synchronous request methods have nothing to answer with. - "get" | "copy" => Err(runtime_throw(format!( - "Shirabe does not support HttpDownloader::{method_name}() from a plugin yet" - ))), + "get" => { + let url = arg::<String>(method_name, args, 0)?; + let options = + arg_or::<IndexMap<String, PhpMixed>>(method_name, args, 1, IndexMap::new())?; + let response = downloader + .borrow() + .get(&url, options) + .map_err(|error| error_throw("get failed", &error))?; + Ok(response_to_wire(&response)) + } + "copy" => { + let url = arg::<String>(method_name, args, 0)?; + let to = arg::<String>(method_name, args, 1)?; + let options = + arg_or::<IndexMap<String, PhpMixed>>(method_name, args, 2, IndexMap::new())?; + let response = downloader + .borrow() + .copy(&url, &to, options) + .map_err(|error| error_throw("copy failed", &error))?; + Ok(response_to_wire(&response)) + } // TODO(plugin,async): the async surface resolves its promises with a Response the wire // cannot carry, and driving it needs a promise representation that crosses the boundary // unresolved. Neither exists yet. diff --git a/crates/shirabe/src/plugin/php_plugin_value.rs b/crates/shirabe/src/plugin/php_plugin_value.rs index cb9b990e..80415eef 100644 --- a/crates/shirabe/src/plugin/php_plugin_value.rs +++ b/crates/shirabe/src/plugin/php_plugin_value.rs @@ -14,6 +14,7 @@ //! constraint) has no faithful constructor call. use crate::package::Link; +use crate::util::http::Response; use chrono::{DateTime, NaiveDateTime, TimeZone, Utc}; use shirabe_php_rpc::{PhpObject, PhpThrow, PluginValue}; use shirabe_semver::constraint::{ @@ -21,6 +22,7 @@ use shirabe_semver::constraint::{ }; const LINK_CLASS: &str = "Composer\\Package\\Link"; +const RESPONSE_CLASS: &str = "Composer\\Util\\Http\\Response"; const CONSTRAINT_CLASS: &str = "Composer\\Semver\\Constraint\\Constraint"; const MULTI_CONSTRAINT_CLASS: &str = "Composer\\Semver\\Constraint\\MultiConstraint"; const MATCH_ALL_CLASS: &str = "Composer\\Semver\\Constraint\\MatchAllConstraint"; @@ -197,6 +199,48 @@ fn constraint_from_wire(value: &PluginValue) -> Result<AnyConstraint, PhpThrow> }) } +/// A `Response` is built for one request and never retained by the object graph, so the child +/// holds a real instance rather than a handle, and `collect()` frees the copy it holds. +/// +/// TODO(type-model): PHP's `$request` is the whole request array (`url`, `options`, `copyTo`) +/// and this port keeps only the url, which is all `decodeJson()` reads back out of it. +/// +/// TODO(port): Composer answers a curl request with a `Composer\Util\Http\CurlResponse` +/// carrying the transfer info; this port flattens that subclass into `Response` before the +/// value leaves `HttpDownloader`, so `getCurlInfo()` is gone and `get_class()` differs. +pub(crate) fn response_to_wire(response: &Response) -> PluginValue { + let mut object = PhpObject::new(RESPONSE_CLASS); + let mut request = indexmap::IndexMap::new(); + request.insert(b"url".to_vec(), PluginValue::string(response.request_url())); + object.set_private(RESPONSE_CLASS, "request", PluginValue::Array(request)); + object.set_private( + RESPONSE_CLASS, + "code", + PluginValue::Int(response.get_status_code()), + ); + object.set_private( + RESPONSE_CLASS, + "headers", + PluginValue::List( + response + .get_headers() + .iter() + .cloned() + .map(PluginValue::string) + .collect(), + ), + ); + object.set_private( + RESPONSE_CLASS, + "body", + match response.get_body() { + Some(body) => PluginValue::string(body), + None => PluginValue::Null, + }, + ); + PluginValue::PhpObject(object) +} + pub(crate) fn link_to_wire(link: &Link) -> PluginValue { let mut object = PhpObject::new(LINK_CLASS); object.set_protected("source", PluginValue::string(link.get_source())); |
