Class: PHPRPC::BaseServer

Inherits:
Object
  • Object
show all
Defined in:
lib/phprpc/base_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseServer

Returns a new instance of BaseServer.



54
55
56
57
58
# File 'lib/phprpc/base_server.rb', line 54

def initialize()
  @methods = {}
  @charset = 'utf-8'
  @debug = $DEBUG
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



52
53
54
# File 'lib/phprpc/base_server.rb', line 52

def charset
  @charset
end

#debugObject

Returns the value of attribute debug.



52
53
54
# File 'lib/phprpc/base_server.rb', line 52

def debug
  @debug
end

Instance Method Details

#add(methodname, obj = nil, aliasname = nil, &block) ⇒ Object

Raises:

  • (TypeError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/phprpc/base_server.rb', line 60

def add(methodname, obj = nil, aliasname = nil, &block)
  raise TypeError, "methodname must be a string or a string list." if methodname.nil?
  aliasname = methodname if aliasname.nil?
  raise TypeError, "aliasname's type must match with methodname's type" if aliasname.class != methodname.class
  if methodname.kind_of?(String) then
    methodname = [methodname]
    aliasname = [aliasname]
  end
  raise RangeError, "aliasname's size must equal methodname's size" if methodname.size != aliasname.size
  obj = Object if obj.nil?
  methodname.each_with_index { |name, index|
    if block_given? then
      @methods[aliasname[index].downcase] = block
    elsif obj.respond_to?(name, true) then
      @methods[aliasname[index].downcase] = [name, obj]
    end
  }
end

#call(env) ⇒ Object



79
80
81
# File 'lib/phprpc/base_server.rb', line 79

def call(env)
  [200, *call!(env, PHPRPC::Request.new(env))]
end

#call!(env, request) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/phprpc/base_server.rb', line 83

def call!(env, request)
  body = ''
  callback = ''
  encode = true
  session = (env['rack.session'].is_a?(Hash) ? env['rack.session'] : CGI::Session.new(request))
  begin
    params = request.params
    callback = get_base64('phprpc_callback', params)
    encode = get_boolean('phprpc_encode', params)
    encrypt = get_encrypt(params)
    cid = "phprpc_#{(params.key?('phprpc_id') ? params['phprpc_id'][0] : '0')}"
    if params.key?('phprpc_func') then
      func = params['phprpc_func'][0].downcase
      if @methods.key?(func) then
        hash = get_session(session, cid)
        if hash.key?('key') then
          key = hash['key']
        elsif encrypt > 0 then
          encrypt = 0
          raise "Can't find the key for decryption."
        end
        ref = get_boolean('phprpc_ref', params)
        args = get_args(params, key, encrypt)
        result = encode_string(encrypt_string(PHP::Formator.serialize(invoke(func, args)), key, 2, encrypt), encode)
        body << 'phprpc_result="' << result << '";' << EOL
        if ref then
          args = encode_string(encrypt_string(PHP::Formator.serialize(args), key, 1, encrypt), encode)
          body << 'phprpc_args="' << args << '";' << EOL
        end
      else
        raise "Can't find this function #{func}()."
      end
      write_error(body, 0, '', callback, encode)
    elsif (encrypt != false) and (encrypt != 0) then
      hash = get_session(session, cid)
      keylen = get_keylength(params, hash)
      key_exchange(body, env, request, hash, callback, encode, encrypt, keylen)
      set_session(session, cid, hash)
    else
      write_functions(body, callback, encode)          
    end
  rescue Exception => e
    body = ''
    if @debug then
      write_error(body, 1, e.backtrace.unshift(e.message).join(EOL), callback, encode)
    else
      write_error(body, 1, e.message, callback, encode)
    end
  ensure
    session.close if session.respond_to?(:close)        
    return [header(request, body), body]
  end
end