phprpc 3.0.4 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,22 @@ client.encryptmode = 2
6
6
  client.keylength = 256
7
7
 
8
8
  starttime = Time.now
9
+
9
10
  10.times {
11
+ # synchronous invoke
10
12
  puts client.add(1, 2)
11
13
  puts client.hello('Ma Bingyao')
12
14
  puts client.sub(1, 2)
13
15
  puts client.use_session('Hello')
14
16
  puts client.use_session(' andot')
17
+
18
+ # asynchronous invoke
19
+ client.add(1, 2) { |result| puts result }
20
+ client.hello('Ma Bingyao') { |result| puts result }
21
+ client.sub(1, 2) { |result| puts result }
22
+ client.use_session('Hello') { |result| puts result }
23
+ client.use_session(' andot') { |result| puts result }
15
24
  }
25
+ Thread.list.each { |t| t.join if t != Thread.main }
16
26
  endtime = Time.now
17
- puts endtime - starttime
27
+ puts endtime - starttime
@@ -4,8 +4,8 @@
4
4
  # #
5
5
  # xxtea.rb #
6
6
  # #
7
- # Release 3.0.4 #
8
- # Copyright (c) 2005-2008 by Team-PHPRPC #
7
+ # Release 3.0.5 #
8
+ # Copyright by Team-PHPRPC #
9
9
  # #
10
10
  # WebSite: http://www.phprpc.org/ #
11
11
  # http://www.phprpc.net/ #
@@ -23,7 +23,7 @@
23
23
  #
24
24
  # XXTEA encryption arithmetic library.
25
25
  #
26
- # Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
26
+ # Copyright: Ma Bingyao <andot@ujn.edu.cn>
27
27
  # Version: 1.0
28
28
  # LastModified: Sep 30, 2008
29
29
  # This library is free. You can redistribute it and/or modify it.
@@ -4,8 +4,8 @@
4
4
  # #
5
5
  # formator.rb #
6
6
  # #
7
- # Release 3.0.4 #
8
- # Copyright (c) 2005-2008 by Team-PHPRPC #
7
+ # Release 3.0.5 #
8
+ # Copyright by Team-PHPRPC #
9
9
  # #
10
10
  # WebSite: http://www.phprpc.org/ #
11
11
  # http://www.phprpc.net/ #
@@ -23,37 +23,37 @@
23
23
  #
24
24
  # PHP serialize/unserialize library.
25
25
  #
26
- # Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
26
+ # Copyright: Ma Bingyao <andot@ujn.edu.cn>
27
27
  # Version: 1.3
28
- # LastModified: Jan 18, 2009
28
+ # LastModified: Feb 27, 2009
29
29
  # This library is free. You can redistribute it and/or modify it.
30
-
30
+
31
31
  require 'stringio'
32
32
 
33
33
  module PHP
34
-
34
+
35
35
  class Formator
36
-
36
+
37
37
  class << self
38
-
38
+
39
39
  public
40
-
40
+
41
41
  def serialize(obj)
42
42
  _serialize(obj, Array.new(1, nil))
43
43
  end
44
-
44
+
45
45
  def unserialize(str)
46
46
  _unserialize(StringIO.new(str), Array.new)
47
47
  end
48
-
48
+
49
49
  private
50
-
50
+
51
51
  @@classCache = Hash.new
52
-
52
+
53
53
  def serialize_int(i)
54
- (-2147483648..2147483647) === i ? "i:#{i};": "d:#{i};"
54
+ (-2147483648..2147483647) === i ? "i:#{i};": serialize_string(i.to_s)
55
55
  end
56
-
56
+
57
57
  def serialize_double(d)
58
58
  case d.infinite?
59
59
  when -1 then 'd:-INF;'
@@ -61,11 +61,11 @@ module PHP
61
61
  else d.nan? ? 'd:NAN;' : "d:#{d};"
62
62
  end
63
63
  end
64
-
64
+
65
65
  def serialize_string(s)
66
66
  "s:#{s.length}:\"#{s}\";"
67
67
  end
68
-
68
+
69
69
  def serialize_time(time, obj_container)
70
70
  obj_container.push(nil, nil, nil, nil, nil, nil, nil)
71
71
  s = 'O:11:"PHPRPC_Date":7:{'
@@ -78,7 +78,7 @@ module PHP
78
78
  s << serialize_string('millisecond') << serialize_int(time.usec / 1000)
79
79
  s << '}'
80
80
  end
81
-
81
+
82
82
  def serialize_array(a, obj_container)
83
83
  s = "a:#{a.size}:{"
84
84
  a.each_with_index { |item, index|
@@ -86,7 +86,7 @@ module PHP
86
86
  }
87
87
  s << '}'
88
88
  end
89
-
89
+
90
90
  def serialize_hash(h, obj_container)
91
91
  s = "a:#{h.size}:{"
92
92
  h.each { |key, value|
@@ -98,7 +98,7 @@ module PHP
98
98
  }
99
99
  s << '}'
100
100
  end
101
-
101
+
102
102
  def serialize_struct(obj, obj_container)
103
103
  classname = obj.class.to_s
104
104
  classname['Struct::'] = ''
@@ -108,9 +108,9 @@ module PHP
108
108
  members.each { |member|
109
109
  s << "#{serialize_string(member)}#{_serialize(obj[member], obj_container)}"
110
110
  }
111
- s << '}'
111
+ s << '}'
112
112
  end
113
-
113
+
114
114
  def serialize_object(obj, obj_container)
115
115
  classname = obj.class.to_s.split('::').join('_')
116
116
  @@classCache[classname] = obj.class
@@ -142,7 +142,7 @@ module PHP
142
142
  end
143
143
  end
144
144
  end
145
-
145
+
146
146
  def _serialize(obj, obj_container = nil)
147
147
  obj_id = obj_container.size
148
148
  obj_container.push(nil)
@@ -205,15 +205,15 @@ module PHP
205
205
  end
206
206
  end
207
207
  end
208
-
208
+
209
209
  def get_class(name)
210
210
  begin
211
- name.split('.').inject(Object) {|x,y| x.const_get(y) }
211
+ name.split('.').inject(Object) {|x,y| x.const_get(y) }
212
212
  rescue
213
213
  nil
214
- end
214
+ end
215
215
  end
216
-
216
+
217
217
  def get_class2(name, ps, i, c)
218
218
  if i < ps.size then
219
219
  p = ps[i]
@@ -227,7 +227,7 @@ module PHP
227
227
  get_class(name)
228
228
  end
229
229
  end
230
-
230
+
231
231
  def get_class_by_alias(name)
232
232
  if @@classCache.has_key?(name) then
233
233
  @@classCache[name]
@@ -249,7 +249,7 @@ module PHP
249
249
  end
250
250
  end
251
251
  end
252
-
252
+
253
253
  def read_number(string)
254
254
  num = ''
255
255
  loop do
@@ -259,7 +259,7 @@ module PHP
259
259
  end
260
260
  num
261
261
  end
262
-
262
+
263
263
  def unserialize_boolean(string)
264
264
  string.read(1)
265
265
  result = (string.read(1) == true)
@@ -282,36 +282,36 @@ module PHP
282
282
  else (d.delete('.eE') == d) ? d.to_i : d.to_f
283
283
  end
284
284
  end
285
-
285
+
286
286
  def unserialize_string(string)
287
287
  len = unserialize_int(string)
288
288
  string.read(len + 3)[1...-2]
289
289
  end
290
-
290
+
291
291
  def unserialize_escaped_string(string)
292
292
  len = unserialize_int(string)
293
293
  s = ''
294
294
  string.read(1)
295
- len.times {
295
+ len.times {
296
296
  c = string.read(1)
297
297
  s << (c == "\\" ? string.read(2).to_i(16).chr : c)
298
298
  }
299
299
  string.read(2)
300
300
  return s
301
301
  end
302
-
302
+
303
303
  def unserialize_unicode_string(string)
304
304
  len = unserialize_int(string)
305
305
  s = ''
306
306
  string.read(1)
307
- len.times {
307
+ len.times {
308
308
  c = string.read(1)
309
309
  s << (c == "\\" ? [string.read(4).to_i(16)].pack("U") : c)
310
310
  }
311
311
  string.read(2)
312
312
  return s
313
313
  end
314
-
314
+
315
315
  def unserialize_hash(string, obj_container)
316
316
  count = unserialize_int(string)
317
317
  obj = Hash.new
@@ -334,7 +334,7 @@ module PHP
334
334
  string.read(1)
335
335
  return obj
336
336
  end
337
-
337
+
338
338
  def unserialize_key(string)
339
339
  tag = string.read(1)
340
340
  if tag.nil? then
@@ -344,10 +344,10 @@ module PHP
344
344
  when 's' then unserialize_string(string)
345
345
  when 'S' then unserialize_escaped_string(string)
346
346
  when 'U' then unserialize_unicode_string(string)
347
- else raise 'Unexpected Tag: "' + tag + '".'
347
+ else raise 'Unexpected Tag: "' + tag + '".'
348
348
  end
349
349
  end
350
-
350
+
351
351
  def unserialize_date(string, obj_container)
352
352
  obj_id = obj_container.size
353
353
  obj_container.push(nil)
@@ -362,7 +362,7 @@ module PHP
362
362
  time = Time.mktime(h['year'], h['month'], h['day'], h['hour'], h['minute'], h['second'], h['millisecond'] * 1000)
363
363
  obj_container[obj_id] = time
364
364
  end
365
-
365
+
366
366
  def unserialize_object(string, obj_container)
367
367
  classname = unserialize_string(string)
368
368
  string.pos -= 1
@@ -397,7 +397,7 @@ module PHP
397
397
  obj
398
398
  end
399
399
  end
400
-
400
+
401
401
  def unserialize_custom_object(string, obj_container)
402
402
  classname = unserialize_string(string)
403
403
  string.pos -= 1
@@ -415,7 +415,7 @@ module PHP
415
415
  obj.unserialize(str)
416
416
  obj
417
417
  end
418
-
418
+
419
419
  def _unserialize(string, obj_container)
420
420
  tag = string.read(1)
421
421
  if tag.nil? then
@@ -460,9 +460,9 @@ module PHP
460
460
  end
461
461
  obj
462
462
  end
463
-
463
+
464
464
  end # class self
465
-
465
+
466
466
  end # class Formator
467
-
467
+
468
468
  end # module PHP
@@ -4,8 +4,8 @@
4
4
  # #
5
5
  # phprpc.rb #
6
6
  # #
7
- # Release 3.0.4 #
8
- # Copyright (c) 2005-2008 by Team-PHPRPC #
7
+ # Release 3.0.5 #
8
+ # Copyright by Team-PHPRPC #
9
9
  # #
10
10
  # WebSite: http://www.phprpc.org/ #
11
11
  # http://www.phprpc.net/ #
@@ -23,7 +23,7 @@
23
23
  #
24
24
  # PHPRPC library.
25
25
  #
26
- # Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
26
+ # Copyright: Ma Bingyao <andot@ujn.edu.cn>
27
27
  # Version: 3.0
28
28
  # LastModified: Sep 13, 2008
29
29
  # This library is free. You can redistribute it and/or modify it.
@@ -4,8 +4,8 @@
4
4
  # #
5
5
  # base_server.rb #
6
6
  # #
7
- # Release 3.0.4 #
8
- # Copyright (c) 2005-2008 by Team-PHPRPC #
7
+ # Release 3.0.5 #
8
+ # Copyright by Team-PHPRPC #
9
9
  # #
10
10
  # WebSite: http://www.phprpc.org/ #
11
11
  # http://www.phprpc.net/ #
@@ -23,9 +23,9 @@
23
23
  #
24
24
  # PHPRPC BaseServer library.
25
25
  #
26
- # Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
26
+ # Copyright: Ma Bingyao <andot@ujn.edu.cn>
27
27
  # Version: 3.0
28
- # LastModified: Dec 19, 2008
28
+ # LastModified: Mar 8, 2009
29
29
  # This library is free. You can redistribute it and/or modify it.
30
30
 
31
31
  require "digest/md5"
@@ -34,7 +34,8 @@ require "crypt/xxtea"
34
34
  require "powmod"
35
35
  require "cgi"
36
36
  require "cgi/session"
37
- require 'optparse'
37
+ require "optparse"
38
+ require "thread"
38
39
 
39
40
  module PHPRPC
40
41
 
@@ -46,7 +47,7 @@ module PHPRPC
46
47
 
47
48
  # Standard internet newline sequence
48
49
  EOL = CR + LF
49
-
50
+
50
51
  class BaseServer
51
52
 
52
53
  attr_accessor :charset, :debug
@@ -55,6 +56,7 @@ module PHPRPC
55
56
  @methods = {}
56
57
  @charset = 'utf-8'
57
58
  @debug = $DEBUG
59
+ @mutex = Mutex.new
58
60
  end
59
61
 
60
62
  def add(methodname, obj = nil, aliasname = nil, &block)
@@ -84,54 +86,56 @@ module PHPRPC
84
86
  body = ''
85
87
  callback = ''
86
88
  encode = true
87
- session = (env['rack.session'].is_a?(Hash) ? env['rack.session'] : CGI::Session.new(request))
88
- begin
89
- params = request.params
90
- callback = get_base64('phprpc_callback', params)
91
- encode = get_boolean('phprpc_encode', params)
92
- encrypt = get_encrypt(params)
93
- cid = "phprpc_#{(params.key?('phprpc_id') ? params['phprpc_id'][0] : '0')}"
94
- if params.key?('phprpc_func') then
95
- func = params['phprpc_func'][0].downcase
96
- if @methods.key?(func) then
97
- hash = get_session(session, cid)
98
- if hash.key?('key') then
99
- key = hash['key']
100
- elsif encrypt > 0 then
101
- encrypt = 0
102
- raise "Can't find the key for decryption."
103
- end
104
- ref = get_boolean('phprpc_ref', params)
105
- args = get_args(params, key, encrypt)
106
- result = encode_string(encrypt_string(PHP::Formator.serialize(invoke(func, args, session)), key, 2, encrypt), encode)
107
- body << 'phprpc_result="' << result << '";' << EOL
108
- if ref then
109
- args = encode_string(encrypt_string(PHP::Formator.serialize(args), key, 1, encrypt), encode)
110
- body << 'phprpc_args="' << args << '";' << EOL
89
+ @mutex.synchronize {
90
+ session = (env['rack.session'].is_a?(Hash) ? env['rack.session'] : CGI::Session.new(request))
91
+ begin
92
+ params = request.params
93
+ callback = get_base64('phprpc_callback', params)
94
+ encode = get_boolean('phprpc_encode', params)
95
+ encrypt = get_encrypt(params)
96
+ cid = "phprpc_#{(params.key?('phprpc_id') ? params['phprpc_id'][0] : '0')}"
97
+ if params.key?('phprpc_func') then
98
+ func = params['phprpc_func'][0].downcase
99
+ if @methods.key?(func) then
100
+ hash = get_session(session, cid)
101
+ if hash.key?('key') then
102
+ key = hash['key']
103
+ elsif encrypt > 0 then
104
+ encrypt = 0
105
+ raise "Can't find the key for decryption."
106
+ end
107
+ ref = get_boolean('phprpc_ref', params)
108
+ args = get_args(params, key, encrypt)
109
+ result = encode_string(encrypt_string(PHP::Formator.serialize(invoke(func, args, session)), key, 2, encrypt), encode)
110
+ body << 'phprpc_result="' << result << '";' << EOL
111
+ if ref then
112
+ args = encode_string(encrypt_string(PHP::Formator.serialize(args), key, 1, encrypt), encode)
113
+ body << 'phprpc_args="' << args << '";' << EOL
114
+ end
115
+ else
116
+ raise "Can't find this function #{func}()."
111
117
  end
118
+ write_error(body, 0, '', callback, encode)
119
+ elsif (encrypt != false) and (encrypt != 0) then
120
+ hash = get_session(session, cid)
121
+ keylen = get_keylength(params, hash)
122
+ key_exchange(body, env, request, hash, callback, encode, encrypt, keylen)
123
+ set_session(session, cid, hash)
112
124
  else
113
- raise "Can't find this function #{func}()."
125
+ write_functions(body, callback, encode)
114
126
  end
115
- write_error(body, 0, '', callback, encode)
116
- elsif (encrypt != false) and (encrypt != 0) then
117
- hash = get_session(session, cid)
118
- keylen = get_keylength(params, hash)
119
- key_exchange(body, env, request, hash, callback, encode, encrypt, keylen)
120
- set_session(session, cid, hash)
121
- else
122
- write_functions(body, callback, encode)
123
- end
124
- rescue Exception => e
125
- body = ''
126
- if @debug then
127
- write_error(body, 1, e.backtrace.unshift(e.message).join(EOL), callback, encode)
128
- else
129
- write_error(body, 1, e.message, callback, encode)
127
+ rescue Exception => e
128
+ body = ''
129
+ if @debug then
130
+ write_error(body, 1, e.backtrace.unshift(e.message).join(EOL), callback, encode)
131
+ else
132
+ write_error(body, 1, e.message, callback, encode)
133
+ end
134
+ ensure
135
+ session.close if session.respond_to?(:close)
136
+ return [header(request, body), body]
130
137
  end
131
- ensure
132
- session.close if session.respond_to?(:close)
133
- return [header(request, body), body]
134
- end
138
+ }
135
139
  end
136
140
 
137
141
  private
@@ -158,7 +162,7 @@ module PHPRPC
158
162
  end
159
163
 
160
164
  def encode_string(str, encode = true, flag = true)
161
- return str if str == ''
165
+ return str if str == ''
162
166
  encode ? [str].pack('m').delete!("\n") : add_js_slashes(str, flag)
163
167
  end
164
168
 
@@ -169,7 +173,7 @@ module PHPRPC
169
173
  def decrypt_string(str, key, level, encrypt)
170
174
  (encrypt >= level) ? Crypt::XXTEA.decrypt(str, key) : str
171
175
  end
172
-
176
+
173
177
  def header(request, body)
174
178
  h = {
175
179
  'X-Powered-By' => 'PHPRPC Server/3.0',
@@ -181,12 +185,12 @@ module PHPRPC
181
185
  }
182
186
  output_cookies = request.instance_variable_get(:@output_cookies)
183
187
  if not output_cookies.nil? then
184
- h["Set-Cookie"] = output_cookies[0].to_s
188
+ h["Set-Cookie"] = output_cookies[0].to_s
185
189
  request.instance_variable_set(:@output_cookies, nil)
186
190
  end
187
191
  return h
188
192
  end
189
-
193
+
190
194
  def write_url(body, env, request, encode)
191
195
  output_hidden = request.instance_variable_get(:@output_hidden)
192
196
  output_hidden = { env['rack.session.options'][:id] => env['rack.session.options'][:key] } if env['rack.session.options'].is_a?(Hash)
@@ -205,7 +209,7 @@ module PHPRPC
205
209
  }
206
210
  params = request.params
207
211
  if (params.size > 0) then
208
- params.each { |key, values|
212
+ params.each { |key, values|
209
213
  values.each { |value|
210
214
  url << "#{key}=#{CGI::escape(value)}&"
211
215
  } if not key.index(/^phprpc_/i)
@@ -220,14 +224,14 @@ module PHPRPC
220
224
  body << 'phprpc_functions="' << encode_string(PHP::Formator.serialize(@methods.keys), encode) << '";' << EOL
221
225
  body << callback
222
226
  end
223
-
227
+
224
228
  def write_error(body, errno, errstr, callback, encode)
225
229
  body << 'phprpc_errno="' << errno.to_s << '";' << EOL
226
230
  body << 'phprpc_errstr="' << encode_string(errstr, encode, false) << '";' << EOL
227
231
  body << 'phprpc_output="";' << EOL
228
232
  body << callback
229
233
  end
230
-
234
+
231
235
  def get_boolean(name, params)
232
236
  (params.key?(name) ? (params[name][0].downcase != "false") : true)
233
237
  end
@@ -235,7 +239,7 @@ module PHPRPC
235
239
  def get_base64(name, params)
236
240
  (params.key?(name) ? params[name][0].unpack('m')[0] : '')
237
241
  end
238
-
242
+
239
243
  def get_encrypt(params)
240
244
  if params.key?('phprpc_encrypt') then
241
245
  encrypt = params['phprpc_encrypt'][0].downcase
@@ -248,7 +252,7 @@ module PHPRPC
248
252
  0
249
253
  end
250
254
  end
251
-
255
+
252
256
  def get_args(params, key, encrypt)
253
257
  args = []
254
258
  if params.key?('phprpc_args') then
@@ -259,7 +263,7 @@ module PHPRPC
259
263
  end
260
264
  return args
261
265
  end
262
-
266
+
263
267
  def set_session(session, cid, hash)
264
268
  session[cid] = PHP::Formator.serialize(hash)
265
269
  end
@@ -272,7 +276,7 @@ module PHPRPC
272
276
  {}
273
277
  end
274
278
  end
275
-
279
+
276
280
  def get_keylength(params, hash)
277
281
  (params.key?('phprpc_keylen') ? params['phprpc_keylen'][0].to_i : ((hash.key?('keylen')) ? hash['keylen'] : 128))
278
282
  end
@@ -297,18 +301,18 @@ module PHPRPC
297
301
  end
298
302
  body << callback
299
303
  end
300
-
304
+
301
305
  end # class BaseServer
302
306
 
303
307
  class Request
304
308
 
305
309
  attr_accessor :cookies
306
310
  attr :params
307
-
311
+
308
312
  def initialize(env)
309
313
  @params = env["QUERY_STRING"] ? CGI::parse(env["QUERY_STRING"].to_s) : {}
310
314
  if (env['REQUEST_METHOD'] == 'POST' and
311
- (env['CONTENT_TYPE'].nil? or
315
+ (env['CONTENT_TYPE'].nil? or
312
316
  env['CONTENT_TYPE'].split(/\s*[;,]\s*/, 2)[0].downcase ==
313
317
  'application/x-www-form-urlencoded')) then
314
318
  # fix ebb bug, the read method of ebb can't return all bytes from the I/O stream
@@ -321,7 +325,7 @@ module PHPRPC
321
325
  end
322
326
  @params.update(CGI::parse(input_body))
323
327
  end
324
- @cookies = if env["HTTP_COOKIE"] then CGI::Cookie::parse(env["HTTP_COOKIE"].to_s) else {} end
328
+ @cookies = if env["HTTP_COOKIE"] then CGI::Cookie::parse(env["HTTP_COOKIE"].to_s) else {} end
325
329
  @output_cookies = nil
326
330
  @output_hidden = nil
327
331
  end
@@ -332,19 +336,19 @@ module PHPRPC
332
336
  value = params[0]
333
337
  if value then value else "" end
334
338
  end
335
-
339
+
336
340
  def []=(key, val)
337
341
  @params[key] = val
338
342
  end
339
-
343
+
340
344
  def keys
341
345
  @params.keys
342
346
  end
343
-
347
+
344
348
  def has_key?(key)
345
349
  @params.has_key?(key)
346
350
  end
347
-
351
+
348
352
  alias key? has_key?
349
353
 
350
354
  alias include? has_key?
@@ -353,15 +357,15 @@ module PHPRPC
353
357
 
354
358
 
355
359
  class DHParams
356
-
360
+
357
361
  class << self
358
-
362
+
359
363
  def get(length)
360
364
  length = get_nearest(length)
361
365
  dhparams = @dhparams_gen[length]
362
366
  [length, dhparams[rand(dhparams.size)]]
363
367
  end
364
-
368
+
365
369
  private
366
370
 
367
371
  def init
@@ -385,9 +389,9 @@ module PHPRPC
385
389
  }
386
390
  return @lengths[j]
387
391
  end
388
-
392
+
389
393
  end # class self
390
-
394
+
391
395
  end # class DHParams
392
396
 
393
397
  end # module PHPRPC