Class: RPCQooxdooHandler
Constant Summary collapse
- @@paths =
{}
- @@file_paths =
{}
- @@server =
[]
Class Method Summary collapse
- .add_file_path(web, dir) ⇒ Object
- .add_path(path, cl) ⇒ Object
-
.answer(result, id, error = nil) ⇒ Object
self.answer and self.error return a hash, which will be converted later.
- .error(origin, code, message, id) ⇒ Object
- .get_ip(req) ⇒ Object
-
.parse(data, web_req = nil) ⇒ Object
Parsing of an incoming RPC-request - returns a string to be sent to the client.
-
.parse_query(q) ⇒ Object
A more easy handler for a query-hash, e.g.
- .parse_query_xdomain(q) ⇒ Object
-
.request(id, service, method, params, web_req = nil) ⇒ Object
Replies to a request.
-
.webrick(port, dir = ".", duration = nil) ⇒ Object
And a no-worry with Webrick.
Class Method Details
.add_file_path(web, dir) ⇒ Object
369 370 371 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 369 def self.add_file_path(web, dir) @@file_paths[web.to_sym] = dir end |
.add_path(path, cl) ⇒ Object
365 366 367 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 365 def self.add_path(path, cl) @@paths[path.to_sym] = cl end |
.answer(result, id, error = nil) ⇒ Object
self.answer and self.error return a hash, which will be converted later
150 151 152 153 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 150 def self.answer(result, id, error = nil) not result and result = [] {'result' => result, 'error' => error, 'id' => id} end |
.error(origin, code, message, id) ⇒ Object
155 156 157 158 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 155 def self.error(origin, code, , id) self.answer(nil, id, {'origin' => origin, 'code' => code, 'message' => }.to_json) end |
.get_ip(req) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 160 def self.get_ip(req) dputs(3) { "header is #{req.header.inspect} - peeraddr is #{req.peeraddr.inspect}" } if (ret = req.header['x-forwarded-for']) && (ret != []) dputs(3) { "x-forward of #{ret.inspect}" } ret.first else dputs(3) { "peeraddr - #{req.peeraddr[3]}" } req.peeraddr[3] end end |
.parse(data, web_req = nil) ⇒ Object
Parsing of an incoming RPC-request - returns a string to be sent to the client
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 234 def self.parse(data, web_req = nil) # Prepare all variables if not data answer = self.error(2, 0, "Didn't receive request", -1) else dputs(3) { "Request-data is: #{data.inspect}" } answer = self.request(data['id'], data['service'], data['method'], data['params'], web_req) end # And put it in a nice qx-compatible reply dputs(3) { "Answer is: #{answer}" } return answer end |
.parse_query(q) ⇒ Object
A more easy handler for a query-hash, e.g. camping or webrick
249 250 251 252 253 254 255 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 249 def self.parse_query(q) request = JSON.parse(q.body) dputs(4) { "JSON of body is #{request.inspect}" } reply = self.parse(request, q).to_json # dp "Reply is: #{reply.encoding} - #{reply.size} - #{reply.bytesize} - #{reply}" return reply.force_encoding(Encoding::ASCII_8BIT) end |
.parse_query_xdomain(q) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 257 def self.parse_query_xdomain(q) stid = q.query['_ScriptTransport_id'] answer = self.parse(JSON.parse(q.query['_ScriptTransport_data']), q) dp "Answer is: #{answer} - #{answer.to_json}" return "qx.io.remote.transport.Script._requestFinished('#{stid}', " + "#{answer.to_json} );" end |
.request(id, service, method, params, web_req = nil) ⇒ Object
Replies to a request
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 172 def self.request(id, service, method, params, web_req = nil) # dputs_func # dp params[0] #dp Sessions.search_all_ #dp web_req show_request_reply = 3 time = Timing.new(3) session = Sessions.match_by_sid(params[0].shift) || Sessions.create dputs(3) { "session is #{session.inspect}" } if service =~ /^View/ and session dputs(3) { "Going to test if we can view #{service}" } if not session.can_view(service) return self.error(2, 3, 'Not allowed to view that!', id) end session.web_req = web_req if web_req session.client_ip = self.get_ip(web_req) end end dputs(show_request_reply) { "Going to call #{service}, #{method}. Args = #{params.inspect}" } # Get an answer with some error-checking if RPCQooxdooService::services.has_key?(service) s = RPCQooxdooService::services[service] method = "rpc_#{method}" if s.respond_to?(method) dputs(3) { "Calling #{method} with #{params.inspect}" } System.rescue_all("while handling #{method} with #{params.inspect}") do parsed = s.parse_request(method, session, params[0]) time.probe("Parsing #{service}.#{method}") dputs(4) { "Parsed request is #{parsed.inspect}" } answer = s.parse_reply(method, session, parsed) time.probe("Replying #{service}.#{method}") dputs(3) { "First answer is #{answer.inspect}" } if answer.class == Array answer.delete_if { |a| a.class != Hash or (a.keys.join != 'cmddata' and a.keys.join != 'datacmd') } else dputs(3) { 'Creating empty reply' } answer = [{:cmd => 'none', :data => []}] end dputs(show_request_reply) { "Final answer is #{answer.inspect}" } return self.answer(answer, id) end return self.error(2, 2, 'Error in handling method', id) else return self.error(2, 2, "No such method #{method} for #{s.class.name}", id) end else return self.error(2, 1, 'No such service', id) end end |
.webrick(port, dir = ".", duration = nil) ⇒ Object
And a no-worry with Webrick
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/qooxview/rpcqooxdoo.rb', line 268 def self.webrick(port, dir = ".", duration = nil) dputs(3) { "Starting webrick for port #{port}, dir #{dir}, duration #{duration.inspect}" } access_log_stream = File.open('webrick.access.log', 'w') logger = [[access_log_stream, AccessLog::COMBINED_LOG_FORMAT]] #logger.push [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT] #logger.push [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT] if @@server[port] dputs(2) { 'Server already running - halting' } @@server[port].shutdown end begin @@server[port] = HTTPServer.new(:Port => port, :Logger => WEBrick::Log.new('webrick.log'), :AccessLog => logger, :DoNotReverseLookup => true) rescue Errno::EADDRINUSE => e dputs(0) { "Couldn't bind to address #{port} - already in use" } raise Errno::EADDRINUSE end # server = HTTPServer.new(:Port => port ) #server.mount "/rpc", GetPost # This is the remote-procedure-handling from the Frontend @@server[port].mount_proc('/rpc') { |req, res| $webrick_request = req dputs(5) { "Request is #{req.inspect}" } dputs(5) { "Body is is #{req.body.inspect}" } dputs(4) { "Request-path is #{req.path}" } if req.body res.body = self.parse_query(req) else res.body = self.parse_query_xdomain(req) end #res['content-type'] = "text/html" res['content-type'] = 'application/json' res['content-length'] = res.body.length dputs(3) { "RPC-Reply is #{res.body}" } raise HTTPStatus::OK } # And any other handling required by modules @@paths.each { |path, cl| dputs(2) { "Mounting path /#{path} to class #{cl.name}" } @@server[port].mount_proc("/#{path.to_s}") { |req, res| $webrick_request = req dputs(5) { "Webrick_request is #{$webrick_request.inspect}" } dputs(4) { "#{path}-Request is #{req.path} and " + "method is #{req.request_method}" } status = HTTPStatus::OK res['content-type'] = 'text/html' System.rescue_all("while handling #{cl.name} with #{req.inspect}") { if cl.respond_to? :parse_req_res res.body = cl.parse_req_res(req, res).to_s elsif cl.respond_to? :parse_req res.body = cl.parse_req(req).to_s else res.body = cl.parse(req.request_method, req.path, req.query).to_s end } or res.body = 'Error in handling method' res.body.force_encoding(Encoding::ASCII_8BIT) res['content-length'] = res.body.length res['status'] = status if res['content-type'] == 'text/html' dputs(3) { "#{path}-reply is #{res.body.inspect}" } end dputs(3) { "Status is #{status.inspect}" } } } @@file_paths.each { |web, dir| dputs(2) { "Mounting web-path /#{web} to file-path #{dir}" } @@server[port].mount("/#{web}", HTTPServlet::FileHandler, dir) } @@server[port].mount('/tmp', HTTPServlet::FileHandler, '/tmp') @@server[port].mount('/', HTTPServlet::FileHandler, dir) if not duration dputs(2) { 'Starting forever' } %w(INT TERM).each { |signal| trap(signal) { dputs(3) { 'Shutting down http-server' } @@server[port].shutdown } } @@server[port].start else dputs(2) { "Starting for #{duration} seconds" } server_loop = Thread.new { @@server[port].start dputs(2) { 'Webrick stopped' } } sleep duration @@server[port].shutdown server_loop.join end end |