PHPRPC 2.1 of the ASP server and client

After three days of efforts, finally put the ASP server and client PHPRPC written, in order to make full use of already written code, ASP's server and client are using JScript's implementation, which calls the original written utf. js, base64.js, phpserializer.js, powmod.js and xxtea.js these five documents. ASP version of PHPRPC server and PHP version of PHPRPC server-side functionality is basically in line, but the version of ASP does not support output redirection, that is useful if the remote function Response.Write output content, the client can not be output parameters are received. This is because ASP itself too weak caused by the output control function, and the other, ASP server-side to create, no debug parameters. ASP server-side errors are serious errors (not warnings and indicative error). ASP client calls the initialization should be used when the absolute address, and should not use the relative address.

phprpc_server.js
/ **
* @ Author Ma Bingyao (andot@ujn.edu.cn)
* @ 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 the following procedures are generated automatically generate the program's procedures and the procedures to generate keypair.php almost here is not provided separately, so I will be in subsequent releases phprpc_2.1 package.

phprpc_client.js
/ **
* @ Author Ma Bingyao (andot@ujn.edu.cn)
* @ 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 (''));
)
)

分类:AJAX 时间:2009-03-04 人气:1641
分享到:
blog comments powered by Disqus

相关文章

  • Javascript copy the contents of the Web site after the paste is to take the copyrighted content code 2010-12-15

    <script type="text/javascript"> document.body.oncopy = function () { setTimeout( function () { var text = clipboardData.getData("text"); if (text) { text = text + "\r\n This article comes from : Copyright © Wuhan star plasti

  • javascript学习笔记(四)function函数部分 2014-03-19

    本文主要介绍了函数的调用方式.返回函数的函数.创建匿名函数.javascript创建动态函数.回调函数.方法和函数的区别.js全局函数.函数的几个作用.prototype属性.高阶函数,非常实用,有需要的参考下 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. Jscript 支持两种函数:一类是语言内部的函数(如eval() ),另一类是自己创建的. 在 JavaScript 函数内部声明的变量(使用 var)是局部变量,所以只能在函数内部访问它.(该变量的作用域是局部的). 您可

  • 浅谈javascript对象模型和function对象 2014-08-10

    这篇文章主要简单介绍了javascript对象模型和function对象,以及相关的示例代码,非常实用,需要的小伙伴参考下 javascript中,函数就是对象 <html> <head> <script type="text/javascript"> function add(number){ alert(number+20); } var add=function(number){ alert(number+20); } function add

  • JavaScript学习笔记之Function对象 2015-04-05

    这篇文章主要介绍了JavaScript学习笔记之Function对象的相关资料,需要的朋友可以参考下 在JavaScript中,函数function就是对象. JS中没有方法重载 在JavaScript中,没有方法(函数)重载的概念. 例子: <html> <head> <script type="text/javascript"> function add(number) { alert(number + 20); } function add(n

  • ERROR org.apache.hadoop.hdfs.server.namenode.NameNode: java.lang.NumberFormatExc 2011-04-19

    namenode baffling not start to see log: 2011-04-19 12:06:59,967 INFO org.apache.hadoop.hdfs.server.common.Storage: Number of files = 11471 2011-04-19 12:07:00,592 INFO org.apache.hadoop.hdfs.server.common.Storage: Number of files under construction =

  • RHCE course-RH033Linux base notes of frequently used text processing commands 5. Input and output redirection. Pipeline 2010-07-16

    RHCE Course - Part 5 primary, frequently used text processing commands, input and output redirection, pipe Today, an order based mainly, though not common technical content of the order, but there are technical content based on the order ordinary Cat

  • Dos: change the IP address of the bat script file, in a different LAN Switch IP address 2011-06-06

    Writing. Bat file contents: netsh interface ip set address local static 192.168.1.1 255.255.255.0 192.168.1.254 1 connection nesh interface ip set dns "Local Area Connection" static 192.168.1.200 Double-click the. Bat file, the address of a loca

  • javascript cross-domain access (front-end solutions) 2011-10-03

    What is cross-domain JavaScript for security reasons, do not allow cross-domain calls to other page objects. However, security restrictions also apply to inject iframe or ajax on a lot of trouble. Here's some of the issues related to cross-domain sim

  • javascript同页面多次调用弹出层具体实例代码 2013-12-17

    一个在同一个页面可多次调用的javascript弹出层效果,有需要的同学可以参考一下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <

  • Javascript prompt and window function to achieve mode 2011-01-08

    Prompt window of the interface: img{ border:0px; } #overlay{ position: absolute; top: 0; left: 0; z-index: 99; width: 100%; height: 500px; background-color: #000; filter:alpha(opacity=70); -moz-opacity: 0.6; opacity: 0.6; } .floatDiv{ padding:0 0 0 0

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.051 (s). 11 q(s)