PHPRPC 2.1 的 ASP 服务器和客户端

经过 3 天的努力,终于把 ASP 的 PHPRPC 服务器和客户端写好了,为了充分利用已经写好的编码,ASP 的服务器和客户端都是用 JScript 实现的,里面调用了原来写好的 utf.js、base64.js、phpserializer.js、powmod.js 和 xxtea.js 这五个文件。ASP 版本的 PHPRPC 服务器和 PHP 版本的 PHPRPC 服务器端功能基本上一致,不过 ASP 版本的不支持输出重定向,也就是说如果远程函数中有用 Response.Write 输出的内容,在客户端是不能通过 output 参数得到的。这是由于 ASP 本身输出控制功能太弱造成的,另外,ASP 服务器端创建时,没有 debug 参数。ASP 服务器端发生的错误都是严重错误(没有警告和提示性错误)。ASP 客户端在初始化调用时,应使用绝对地址,而不能使用相对地址。

phprpc_server.js
/**
* @author      Ma Bingyao([email protected])
* @copyright   CoolCode.CN
* @package     ASP_PHPRPC_SERVER
* @version     2.1
* @last_update 2006-06-12
* @link        http://www.coolcode.cn/?p=187
*
* Example usage:
*
* server.asp
* <%@ CodePage = 65001 %>
* <script runat="server" type="text/javascript" src="phprpc_server.js"></script>
* <%
* function add(a, b)
*     add = a + b
* end function
 
* function subtract(a, b)
*     subtract = a - b
* end function
* phprpc_server.create(Array('add', 'sub'));
* %>
*/
 
function addjsslashes(str, flag) {
    var test;
    if (flag == false) {
        test = /([\0-\037\042\047\134])/g;
    }
    else {
        test = /([\0-\037\042\047\134\177-\377])/g;
    }
    return str.replace(test, function ($1) {
        var s = $1.charCodeAt(0).toString(8);
        return '\\' + ((s.length == 1) ? "00" : ((s.length == 2) ? "0" : "")) + s;
    });
}
 
function getGMTDate(date) {
    var week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    return week[date.getUTCDay()] + ", " + date.toGMTString();
}
 
function IsEmpty(o) {
    if (typeof(o) == "object" && String(o) == "undefined") return true;
    return false;
}
 
function phprpc_server(functions) {
    Response.CodePage = 65001;
    Session.CodePage = 65001;
    var func, args, result, encrypt;
    var date = getGMTDate(new Date());
    Response.Buffer = true;
    Response.ContentType = "text/plain";
    Response.Charset = "utf-8";
    Response.AddHeader("X-Powered-By", "PHPRPC Server/2.1");
    Response.AddHeader("Date", date);
    Response.AddHeader("Last0Modified", date);
    Response.AddHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    Response.AddHeader("Cache-Control", "pre-check=0, post-check=0, max-age=0");
    Response.AddHeader("Content-Encoding", "none");
    if (functions.constructor == Array) {
        this.functions = functions;
    }
    else if (functions.constructor == VBArray) {
        this.functions = functions.toArray();
    }
    else {
        this.functions = [functions];
    }
    this.encode = true;
    if (!IsEmpty(Request('phprpc_encode'))) {
        this.encode = String(Request('phprpc_encode')).toLowerCase();
        if (this.encode == "false") {
            this.encode = false;
        }
    }
    if (!IsEmpty(Request('phprpc_callback'))) {
        this.callback = utf8to16(base64decode(String(Request('phprpc_callback'))));
    }
    else {
        this.callback = "";
    }
    this.ref = true;
    if (!IsEmpty(Request('phprpc_ref'))) {
        this.ref = String(Request('phprpc_ref')).toLowerCase();
        if (this.ref == "false") {
            this.ref = false;
        }
    }
    this.errno = 0;
    this.errstr = "";
   
    try {
        this.encrypt = false;
        if (!IsEmpty(Request('phprpc_encrypt'))) {
            this.encrypt = String(Request('phprpc_encrypt'));
            if (this.encrypt === "true") this.encrypt = true;
            if (this.encrypt === "false") this.encrypt = false;
        }
        if (!IsEmpty(Request('phprpc_func'))) {
            func = String(Request('phprpc_func'));
            if (this.is_defined(func)) {
                if (!IsEmpty(Request('phprpc_args'))) {
                    args = base64decode(String(Request('phprpc_args')));
                    if (this.encrypt > 0) {
                        if (typeof(Session('PHPRPC_ENCRYPT')['k']) != "undefined") {
                            args = xxtea_decrypt(args, Session('PHPRPC_ENCRYPT')['k']);
                        }
                        else {
                            this.errno = 1;
                            this.errstr = "Can't find the key for decryption.";
                        }
                    }
                    args = unserialize(args);
                }
                else {
                    args = [];
                }
                result = serialize(this.call(func, args));
                if (this.ref) {
                    args = serialize(args);
                }
                if (this.encrypt > 0) {
                    if (typeof(Session('PHPRPC_ENCRYPT')['k']) != "undefined") {
                        if (this.encrypt > 1) {
                            result = xxtea_encrypt(result, Session('PHPRPC_ENCRYPT')['k']);
                        }
                        if (this.ref) {
                            args = xxtea_encrypt(args, Session('PHPRPC_ENCRYPT')['k']);
                        }
                    }
                    else {
                        this.errno = 1;
                        this.errstr = "Can't find the key for encryption.";
                    }
                }
                if (this.encode) {
                    result = base64encode(result);
                    if (this.ref) {
                        args = base64encode(args);
                    }
                }
                else {
                    result = addjsslashes(result);
                    if (this.ref) {
                        args = addjsslashes(args);
                    }
                }
            }
            else {
                this.errno = 1;
                this.errstr = "Can't find this function " + func + "().";
            }
            Response.Clear();
            if (this.errno != 1) {
                Response.Write('phprpc_result="' + result + '";\r\n');
                if (this.ref) {
                    Response.Write('phprpc_args="' + args + '";\r\n');
                }
            }
            Response.Write('phprpc_errno="' + this.errno + '";\r\n');
            if (this.encode) {
                Response.Write('phprpc_errstr="' + base64encode(utf16to8(this.errstr)) + '";\r\n');
                Response.Write('phprpc_output="";\r\n');
            }
            else {
                Response.Write('phprpc_errstr="' + addjsslashes(this.errstr, false) + '";\r\n');
                Response.Write('phprpc_output="";\r\n');
            }
        }
        else {
            if (this.encrypt != false) {
                if (this.encrypt === true) {
                    encrypt = phprpc_keypair[Math.floor(Math.random() * phprpc_keypair.length)];
                    Session('PHPRPC_ENCRYPT') = [];
                    Session('PHPRPC_ENCRYPT')['x'] = rand(127, 1);
                    Session('PHPRPC_ENCRYPT')['g'] = dec2num(encrypt['g']);
                    Session('PHPRPC_ENCRYPT')['p'] = dec2num(encrypt['p']);
                    encrypt['y'] = num2dec(pow_mod(Session('PHPRPC_ENCRYPT')['g'],
                                                   Session('PHPRPC_ENCRYPT')['x'],
                                                   Session('PHPRPC_ENCRYPT')['p']));
                }
                else {
                    Session('PHPRPC_ENCRYPT')['y'] = dec2num(this.encrypt);
                    var key = num2str(pow_mod(Session('PHPRPC_ENCRYPT')['y'],
                                              Session('PHPRPC_ENCRYPT')['x'],
                                              Session('PHPRPC_ENCRYPT')['p']));
                    var n = 16 - key.length;
                    var k = [];
                    for (var i = 0; i < n; i++) {
                        k[i] = '\0';
                    }
                    k[n] = key;
                    Session('PHPRPC_ENCRYPT')['k'] = k.join('');
                    encrypt = true;
                }
                if (this.encode) {
                    Response.Write('phprpc_encrypt="' + base64encode(serialize(encrypt)) + '";\r\n');
                }
                else {
                    Response.Write('phprpc_encrypt="' + addjsslashes(serialize(encrypt)) + '";\r\n');
                }
            }
            if (this.encode) {
                Response.Write('phprpc_functions="' + base64encode(serialize(this.functions)) + '";\r\n');
            }
            else {
                Response.Write('phprpc_functions="' + addjsslashes(serialize(this.functions)) + '";\r\n');
            }
        }
        Response.Write(this.callback);
    }
    catch (e) {
        this.errno = 1;
        this.errstr = e.description;
        Response.Clear();
        Response.Write('phprpc_errno=' + this.errno + ';\r\n');
        if (this.encode) {
            Response.Write('phprpc_errstr="' + base64encode(utf16to8(this.errstr)) + '";\r\n');
        }
        else {
            Response.Write('phprpc_errstr="' + addjsslashes(this.errstr, false) + '";\r\n');
        }
        Response.Write('phprpc_output="";\r\n');
        Response.Write(this.callback);
    }
    Response.End();
}
 
phprpc_server.prototype.is_defined = function (func) {
    for (var i = 0, n = this.functions.length; i < n; i++) {
        if (this.functions[i] == func) return true;
    }
    return false;
}
 
phprpc_server.prototype.call = function (func, args) {
    var a = [];
    for (var i = 0, n = args.length; i < n; i++) {
        a[i] = 'args[' + i + ']';
    }
    return eval(func + "(" + a.join(', ') + ")");
}
 
phprpc_server.create = function (functions) {
    new phprpc_server(functions);
}
下面的程序 keypair.js 是自动生成的,生成该程序的程序与生成 keypair.php 的程序差不多,这里就不单独提供了,我会在以后的 phprpc_2.1 包中发布的。

phprpc_client.js
/**
* @author      Ma Bingyao([email protected])
* @copyright   CoolCode.CN
* @package     ASP_PHPRPC_CLIENT
* @version     2.1
* @last_update 2006-06-14
* @link        http://www.coolcode.cn/?p=143
*
* Example usage:
*
* server.asp
* <%@ CodePage = 65001 %>
* <script runat="server" type="text/javascript" src="phprpc_client.js"></script>
* <%
* phprpc_client.create('rpc')
* rpc.use_service('http://test.coolcode.cn/phprpc/server.php')
* Response.Write(rpc.add(1,2))
* %>
*/
 
function phprpc_error(errno, errstr) {
    this.errno = errno;
    this.errstr = errstr;
}
 
function phprpc_client() {
    this.__url = '';
    this.__encrypt = false;
    this.encrypt = 0;
    this.args = null;
    this.warning = null;
    this.output = "";
    this.use_service = function (url, encrypt) {
        if (typeof(encrypt) == "undefined") {
            encrypt = this.__encrypt;
        }
        if (typeof(this.__name) == "undefined") {
            return false;
        }
        this.__url = url;
        var xmlhttp = this.__create_xmlhttp(); 
        if (encrypt === true) {
            xmlhttp.open("GET", [this.__url, '?phprpc_encrypt=true&phprpc_encode=false'].join(''), false);
            xmlhttp.send(null);
            if (xmlhttp.responseText) {
                eval(xmlhttp.responseText);
                if (typeof(phprpc_encrypt) == "undefined") {
                    this.__encrypt = false;
                    encrypt = false;
                }
                else {
                    this.__encrypt = unserialize(phprpc_encrypt);
                    this.__encrypt['p'] = dec2num(this.__encrypt['p']);
                    this.__encrypt['g'] = dec2num(this.__encrypt['g']);
                    this.__encrypt['y'] = dec2num(this.__encrypt['y']);
                    this.__encrypt['x'] = rand(127, 1);
                    var key = pow_mod(this.__encrypt['y'],
                                      this.__encrypt['x'],
                                      this.__encrypt['p']);
                    key = num2str(key);
                    var n = 16 - key.length;
                    var k = [];
                    for (var i = 0; i < n; i++) k[i] = '\0';
                    k[n] = key;
                    this.__encrypt['k'] = k.join('');
                    encrypt = num2dec(pow_mod(this.__encrypt['g'],
                                              this.__encrypt['x'],
                                              this.__encrypt['p']));
                }
            }
        }
 
        xmlhttp.open("GET", [this.__url, '?phprpc_encrypt=', encrypt, '&phprpc_encode=false'].join(''), false);
        xmlhttp.send(null);
        if (xmlhttp.responseText) {
            eval(xmlhttp.responseText);
            var functions = unserialize(phprpc_functions);
            var func = [];
            for (var i = 0, n = functions.length; i < n; i++) {
                func[i] = [this.__name, ".", functions[i],
                        " = function () { return this.__call('",
                        functions[i],
                        "', this.__args_to_array(arguments)); }\r\n",
                        this.__name, ".", functions[i],
                        ".ref = false;\r\n"].join('');
            }
            eval(func.join(''));
        }
        delete(xmlhttp);
    };
    this.__call = function (func, args) {
        var __args = serialize(args);
        if ((this.__encrypt !== false) && (this.encrypt > 0)) {
            __args = xxtea_encrypt(__args, this.__encrypt['k']);
        }
        __args = base64encode(__args);
        var request = ['phprpc_func=', func,
                       '&phprpc_args=', __args,
                       '&phprpc_encode=false',
                       '&phprpc_encrypt=', this.encrypt];
        var ref = eval([this.__name, ".", func, ".ref"].join(''));
        if (!ref) {
            request[request.length] = '&phprpc_ref=false';
        }
        var xmlhttp = this.__create_xmlhttp();
        var session = {'args': args, 'ref': ref, 'encrypt': this.encrypt};
        xmlhttp.open("POST", this.__url, false);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xmlhttp.send(request.join('').replace(/\+/g, '%2B'));
        if (xmlhttp.responseText) {
            this.__get_result(xmlhttp, session);
            this.output = phprpc_output;
            this.args = phprpc_args;
            this.warning = phprpc_warning;
        }
        else {
            phprpc_result = new phprpc_error(1, "No data received from server");
        }
        delete(xmlhttp);
 
        return phprpc_result;
    };
    this.__get_result = function (xmlhttp, session) {
        eval(xmlhttp.responseText);
        phprpc_warning = null;
        if ((phprpc_errno != 1) && (phprpc_errno != 16) &&
            (phprpc_errno != 64) && (phprpc_errno != 256)) {
            if ((this.__encrypt !== false) && (session.encrypt > 0)) {
                if (session.encrypt > 1) {
                    phprpc_result = xxtea_decrypt(phprpc_result, this.__encrypt['k']);
                }
                if (session.ref) {
                    phprpc_args = xxtea_decrypt(phprpc_args, this.__encrypt['k']);
                }
            }
            phprpc_result = unserialize(phprpc_result);
 
            if (session.ref) {
                phprpc_args = unserialize(phprpc_args);
            }
            else {
                phprpc_args = session.args;
            }
            phprpc_warning = new phprpc_error(phprpc_errno, phprpc_errstr);
        }
        else {
            phprpc_result = new phprpc_error(phprpc_errno, phprpc_errstr);
            phprpc_args = session.args;
        }
    }
    this.__create_xmlhttp = function() {
        var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        var n = MSXML.length;
        for(var i = 0; i < n; i++) {
            try {
                return new ActiveXObject(MSXML[i]);
            }
            catch(e) {}
        }
        throw new Error("Your server does not support xmlhttp objects");
    };
    this.__args_to_array = function (args) {
        var argArray = [];
        var n = args.length;
        for (i = 0; i < n; i++) {
            argArray[i] = args[i];
        }
        return argArray;
    }
}
 
phprpc_client.create = function (name, encrypt) {
    eval([name, ' = new phprpc_client();', name, '.__name = "', name, '";'].join(''));
    if (encrypt) {
        encrypt = true;
        eval([name, '.__encrypt = ', encrypt, ';'].join(''));
    }
}

你可能感兴趣的:(PHP,cache,prototype,asp,phprpc)