Class: PHPRPC::Client

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

Defined Under Namespace

Classes: Proxy

Constant Summary collapse

VERSION =
'3.00'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Client

Returns a new instance of Client.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/phprpc/client.rb', line 49

def initialize(url = nil)
  Net::HTTP.version_1_2
  @httpclient = nil
  @http = Net::HTTP
  @uri = nil
  if url then
    @uri = URI.parse(url)
  end
  @timeout = 30
  @output = ''
  @warning = nil
  @key = nil
  @keylength = 128
  @encryptmode = 0
  @cookie = ''
  @cookies = {}
  @charset = 'utf-8'
  @server_version = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ Object (private)



249
250
251
# File 'lib/phprpc/client.rb', line 249

def method_missing(methodname, *args)
  self.invoke(methodname, args)
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



115
116
117
# File 'lib/phprpc/client.rb', line 115

def charset
  @charset
end

#encryptmodeObject

Returns the value of attribute encryptmode.



109
110
111
# File 'lib/phprpc/client.rb', line 109

def encryptmode
  @encryptmode
end

#keylengthObject

Returns the value of attribute keylength.



103
104
105
# File 'lib/phprpc/client.rb', line 103

def keylength
  @keylength
end

#outputObject (readonly)

Returns the value of attribute output.



117
118
119
# File 'lib/phprpc/client.rb', line 117

def output
  @output
end

#timeoutObject

Returns the value of attribute timeout.



115
116
117
# File 'lib/phprpc/client.rb', line 115

def timeout
  @timeout
end

#warningObject (readonly)

Returns the value of attribute warning.



117
118
119
# File 'lib/phprpc/client.rb', line 117

def warning
  @warning
end

Instance Method Details

#closeObject



160
161
162
163
# File 'lib/phprpc/client.rb', line 160

def close
  @httpclient.finish if (not @httpclient.nil?) and @httpclient.started?
  @httpclient = nil
end

#invoke(methodname, args, byref = false) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/phprpc/client.rb', line 119

def invoke(methodname, args, byref = false)
  begin
    _key_exchange()
    request = "phprpc_func=#{methodname}"
    request << '&phprpc_args=' << [_encrypt(PHP::Formator.serialize(args), 1)].pack('m').delete!("\n").gsub('+', '%2B') if args.size > 0
    request << "&phprpc_encrypt=#{@encryptmode}"
    request << '&phprpc_ref=false' unless byref
    result = _post(request)
    if result.key?('phprpc_errstr') and result.key?('phprpc_errno') then
      @warning = PHPRPC::Error.new(result['phprpc_errstr'].unpack('m')[0])
      @warning.number = result['phprpc_errno'].to_i
    elsif result.key?('phprpc_functions') then
      @warning = PHPRPC::Error.new("PHPRPC server haven't received then POST data!")
      @warning.number = 1
    else
      @warning = PHPRPC::Error.new("PHPRPC server occured unknown error!")
      @warning.number = 1
    end
    if result.key?('phprpc_output') then
      @output = result['phprpc_output'].unpack('m')[0]
      @output = _decrypt(@output, 3) if @server_version >= 3
    else
      @output = ''
    end
    if result.key?('phprpc_result') then
      PHP::Formator.unserialize(_decrypt(result['phprpc_arg'].unpack('m')[0], 1)).each { |key, value|
        args[key] = value
      } if result.key?('phprpc_args')
      PHP::Formator.unserialize(_decrypt(result['phprpc_result'].unpack('m')[0], 2))
    else
      @warning
    end
  rescue PHPRPC::Error => e
    return e
  rescue Exception => ex
    e = PHPRPC::Error.new(ex.message)
    e.number = 1
    return e
  end
end

#proxy=(proxy) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/phprpc/client.rb', line 90

def proxy=(proxy)
  close
  @http = case proxy
  when Net::HTTP then
    proxy
  when String then
    uri = URI.parse(proxy)
    Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
  else
    proxy.superclass == Net::HTTP ? proxy : Net::HTTP
  end
end

#set_proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil) ⇒ Object



85
86
87
88
# File 'lib/phprpc/client.rb', line 85

def set_proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
  close
  @http = Net::HTTP::Proxy(p_addr, p_port, p_user, p_pass)
end

#use_service(url = nil, username = nil, password = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/phprpc/client.rb', line 69

def use_service(url = nil, username = nil, password = nil)
  if url then
    close
    @uri = URI.parse(url)
    @key = nil
    @keylength = 128
    @encryptmode = 0
    @cookie = ''
    @cookies = {}
    @charset = 'utf-8'
    @uri.user = username unless username.nil?
    @uri.password = password unless password.nil?
  end
  Proxy.new(self)
end