mirror of
https://github.com/gnh1201/caterpillar.git
synced 2024-11-26 07:21:46 +00:00
Add the dynamic loading feature in relay_invoke_method
method
This commit is contained in:
parent
3977d0c719
commit
604a4d7886
|
@ -28,6 +28,10 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], "php-httpproxy/") !== 0 && strpos($_SERV
|
||||||
exit('<!DOCTYPE html><html><head><title>It works!</title><meta charset="utf-8"></head><body><h1>It works!</h1><p><a href="https://github.com/gnh1201/caterpillar">Download the client</a></p><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
|
exit('<!DOCTYPE html><html><head><title>It works!</title><meta charset="utf-8"></head><body><h1>It works!</h1><p><a href="https://github.com/gnh1201/caterpillar">Download the client</a></p><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function jsonrpc2_cast_to_array($data) {
|
||||||
|
return is_array($data) ? $data : array($data);
|
||||||
|
}
|
||||||
|
|
||||||
function jsonrpc2_encode($method, $params, $id = '') {
|
function jsonrpc2_encode($method, $params, $id = '') {
|
||||||
$data = array(
|
$data = array(
|
||||||
"jsonrpc" => "2.0",
|
"jsonrpc" => "2.0",
|
||||||
|
@ -431,7 +435,7 @@ function relay_fetch_url($params) {
|
||||||
|
|
||||||
// check it is POST request
|
// check it is POST request
|
||||||
if ($data == "POST") {
|
if ($data == "POST") {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, array($data));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, jsonrpc2_cast_to_array($data));
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,7 +496,26 @@ function relay_get_geolocation() {
|
||||||
|
|
||||||
function relay_invoke_method($params) {
|
function relay_invoke_method($params) {
|
||||||
$callback = $params['callback'];
|
$callback = $params['callback'];
|
||||||
$args = (is_array($params['args']) ? $params['args'] : array());
|
$requires = jsonrpc2_cast_to_array($params['requires']);
|
||||||
|
$args = jsonrpc2_cast_to_array($params['args']);
|
||||||
|
|
||||||
|
foreach($requires as $required_url) {
|
||||||
|
try {
|
||||||
|
$result = relay_fetch_url($required_url);
|
||||||
|
if ($result['success']) {
|
||||||
|
$fh = tmpfile();
|
||||||
|
if ($fh !== false) {
|
||||||
|
fwrite($fh, $result['data']);
|
||||||
|
$path = stream_get_meta_data($fh)['uri'];
|
||||||
|
@require($path);
|
||||||
|
fclose($fh);
|
||||||
|
@unlink($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// ignore an exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$data = call_user_func_array($callback, $args);
|
$data = call_user_func_array($callback, $args);
|
||||||
|
|
16
console.html
16
console.html
|
@ -251,21 +251,36 @@
|
||||||
var k = (args.length > 0 ? args[0] : '');
|
var k = (args.length > 0 ? args[0] : '');
|
||||||
var v = (args.length > 1 ? args.slice(1) : []).join(' ');
|
var v = (args.length > 1 ? args.slice(1) : []).join(' ');
|
||||||
|
|
||||||
|
// "env" is the reserved word
|
||||||
if (k == "env") {
|
if (k == "env") {
|
||||||
this.echo("env is the reserved word");
|
this.echo("env is the reserved word");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check a variable is it Array
|
||||||
|
if (k in env && env[k] instanceof Array) {
|
||||||
|
env[k].push(v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// method(relay_web_search)
|
// method(relay_web_search)
|
||||||
if (env.method == "relay_web_search" && k == "page") {
|
if (env.method == "relay_web_search" && k == "page") {
|
||||||
env[k] = parseInt(v);
|
env[k] = parseInt(v);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
env[k] = v || null;
|
env[k] = v || null;
|
||||||
|
|
||||||
if (k == "method") {
|
if (k == "method") {
|
||||||
this.set_prompt('method([[b;red;black]' + env.method + '])> ');
|
this.set_prompt('method([[b;red;black]' + env.method + '])> ');
|
||||||
|
|
||||||
|
// method(relay_invoke_method)
|
||||||
|
if (env.method == "relay_invoke_method") {
|
||||||
|
set_default_env({
|
||||||
|
"requires": []
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// method(relay_sendmail)
|
// method(relay_sendmail)
|
||||||
if (env.method == "relay_sendmail") {
|
if (env.method == "relay_sendmail") {
|
||||||
|
@ -323,6 +338,7 @@
|
||||||
|
|
||||||
jsonrpc2_request(this, env.method, {
|
jsonrpc2_request(this, env.method, {
|
||||||
"callback": args[0],
|
"callback": args[0],
|
||||||
|
"requires": env.requires,
|
||||||
"args": args.slice(1)
|
"args": args.slice(1)
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user