Exemplo n.º 1
0
function start_standalone_http_server($port = NULL, $addr = NULL)
{
    if (!$addr) {
        $addr = '127.0.0.1';
    } else {
        if (!is_ipaddress($addr)) {
            return "invalid address\n";
        }
    }
    if (!$port) {
        $port = 8000;
    } else {
        if (!preg_match('/\\d+/', $port)) {
            return "invalid port\n";
        }
    }
    $socket = stream_socket_server("tcp://{$addr}:{$port}", $errno, $errstr);
    if (!$socket) {
        return $errstr . ' (' . $errno . ')' . PHP_EOL;
    } else {
        $defaults = array('Content-Type' => 'text/html', 'Server' => 'PHP ' . phpversion());
        while ($conn = stream_socket_accept($socket, -1)) {
            $request = '';
            $ct = 0;
            // The ronin-rpc engine may not be the most compliant web client
            // while (substr($request, -4) !== "\r\n\r\n") {
            while (!preg_match('/\\r\\n\\r\\n/m', $request)) {
                $request .= fread($conn, 1024);
            }
            // We expect only one query parameter and we do not care what its called
            if (!preg_match('|[A-Z]+ /[^\\?]*(?:([^=]+)=([^\\s]+))? HTTP/\\d.\\d|', $request, $matches)) {
                print "recieved an invalid query string ({$request})\n";
                continue;
            }
            $request = urldecode($matches[2]);
            $response = rpc_serialize(rpc_call(rpc_deserialize($request)));
            $body = "<!-- <rpc:response>{$response}</rpc:response> -->";
            $header = "Content-Type: text/html\r\n";
            $header .= "Server: PHP " . phpversion() . "\r\n";
            $header .= "Content-Length: " . strlen($body) . "\r\n";
            fwrite($conn, implode("\r\n", array('HTTP/1.1 200', $header, $body)));
            fclose($conn);
        }
        fclose($socket);
    }
}
Exemplo n.º 2
0
/**
 * Takes an object and the parameter list from a box call, executes
 * the specified method (a 'method' entry in the parameter list)
 * with the rest of the values from the parameter list, and
 * returns a serialized JavaScript variable, suitable for passing
 * to the JavaScript eval() function.
 *
 * This function operates independently of the others (ie. you
 * do not need the calls to rpc_init() or rpc_response().  This
 * function is used in conjunction with the new XMLHttpRequest-based
 * RPC facilities in js/rpc.js.
 *
 * Note: The following are reserved parameter names:
 *
 * - error
 * - files
 * - method
 * - mode
 * - page
 * - param
 *
 * Usage:
 *
 * class Test {
 *   function hello ($name) {
 *     return 'hello ' . $name;
 *   }
 * }
 *
 * echo rpc_handle (new Test (), $parameters);
 * exit;
 *
 * @param object reference
 * @param array hash
 * @param boolean serialize data ? 
 * @return string
 * @package Misc
 */
function rpc_handle(&$obj, $parameters, $serialize = true)
{
    // determine the method to call
    $method = $parameters['method'];
    if (!$method) {
        return rpc_serialize(false);
    }
    // remove unwanted parameters
    unset($parameters['mode']);
    unset($parameters['page']);
    unset($parameters['error']);
    unset($parameters['files']);
    unset($parameters['param']);
    unset($parameters['method']);
    unset($parameters['_rewrite_sticky']);
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        $res = call_user_func_array(array(&$obj, $method), $parameters);
    } else {
        $res = call_user_func_array(array(&$obj, $method), $_POST);
    }
    if ($serialize) {
        $res = rpc_serialize($res);
    }
    if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && extension_loaded('zlib')) {
        header('Content-Encoding: gzip');
        $res = gzencode($res, 9);
    }
    return $res;
}