Class: Riddl::Server
- Inherits:
-
Object
- Object
- Riddl::Server
- Includes:
- Daemonism
- Defined in:
- lib/ruby/riddl/server.rb
Defined Under Namespace
Classes: Execution
Instance Attribute Summary collapse
-
#riddl_log ⇒ Object
readonly
Returns the value of attribute riddl_log.
-
#riddl_method ⇒ Object
readonly
Returns the value of attribute riddl_method.
-
#riddl_pinfo ⇒ Object
readonly
Returns the value of attribute riddl_pinfo.
-
#riddl_status ⇒ Object
readonly
Returns the value of attribute riddl_status.
Instance Method Summary collapse
-
#__call ⇒ Object
}}}.
-
#__http_call(env) ⇒ Object
}}}.
-
#accessible_description(ad) ⇒ Object
}}}.
-
#call(env) ⇒ Object
{{{.
- #cleanup(&blk) ⇒ Object
-
#cross_site_xhr(csxhr) ⇒ Object
}}}.
-
#declaration_path ⇒ Object
}}}.
-
#declaration_resource ⇒ Object
}}}.
- #delete(min = '*') ⇒ Object
- #get(min = '*') ⇒ Object
-
#initialize(riddl, opts = {}, &blk) ⇒ Server
constructor
{{{.
-
#interface(name, &block) ⇒ Object
}}}.
-
#logger(lgr) ⇒ Object
}}}.
-
#loop! ⇒ Object
}}}.
-
#matching_path ⇒ Object
{{{.
-
#method(what) ⇒ Object
}}}.
-
#on(resource, &block) ⇒ Object
}}}.
-
#parallel(&blk) ⇒ Object
}}}.
- #patch(min = '*') ⇒ Object
-
#post(min = '*') ⇒ Object
}}}.
-
#process_out(pout) ⇒ Object
}}}.
- #put(min = '*') ⇒ Object
- #resource(rname = nil) ⇒ Object
-
#run(what, *args) ⇒ Object
}}}.
- #sse ⇒ Object
- #startup(&blk) ⇒ Object
-
#use(blk, *args) ⇒ Object
}}}.
- #websocket ⇒ Object
Constructor Details
#initialize(riddl, opts = {}, &blk) ⇒ Server
{{{
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby/riddl/server.rb', line 48 def initialize(riddl,opts={},&blk)# {{{ @riddl_opts = DAEMONISM_DEFAULT_OPTS.merge({ :bind => '0.0.0.0', :host => 'localhost', :port => 9292, :secure => false, :verbose => false, :http_only => false, :runtime_opts => [ ["--port [PORT]", "-p [PORT]", "Specify http port.", ->(p){ @riddl_opts[:port] = p.to_i @riddl_opts[:pidfile] = @riddl_opts[:pidfile].gsub(/\.pid/,'') + '-' + @riddl_opts[:port].to_s + '.pid' }], ["--http-only", "-s", "Only http, no other protocols.", ->(){ @riddl_opts[:http_only] = true }], ["--override-option [OPTION]", "-o [OPTION]", "Override option, i.e., name=value", ->(val){ k,v = val.split('=',2); @riddl_opts[k.to_sym] = v }] ], :runtime_cmds => [], :runtime_proc => Proc.new { |opts| @riddl_opts[:cmdl_info] = (@riddl_opts[:secure] ? 'https://' : 'http://') + @riddl_opts[:host] + ':' + @riddl_opts[:port].to_s @riddl_opts[:url] ||= @riddl_opts[:cmdl_info] } }).merge(opts) @riddl_logger = nil @riddl_process_out = true @riddl_cross_site_xhr = false @accessible_description = false @riddl_description_string = '' @riddl_paths = [] @riddl_at_exit = nil @riddl_interfaces = {} daemonism @riddl_opts, &blk @riddl = Riddl::Wrapper.new(riddl,@accessible_description) if @riddl.description? raise SpecificationError, 'RIDDL description does not conform to specification' unless @riddl.validate! @riddl_description_string = @riddl.description.xml elsif @riddl.declaration? raise SpecificationError, 'RIDDL declaration does not conform to specification' unless @riddl.validate! @riddl_description_string = @riddl.declaration.description_xml else raise SpecificationError, 'Not a RIDDL file' end @riddl.load_necessary_handlers! @riddl_paths = @riddl.paths end |
Instance Attribute Details
#riddl_log ⇒ Object (readonly)
Returns the value of attribute riddl_log.
46 47 48 |
# File 'lib/ruby/riddl/server.rb', line 46 def riddl_log @riddl_log end |
#riddl_method ⇒ Object (readonly)
Returns the value of attribute riddl_method.
46 47 48 |
# File 'lib/ruby/riddl/server.rb', line 46 def riddl_method @riddl_method end |
#riddl_pinfo ⇒ Object (readonly)
Returns the value of attribute riddl_pinfo.
46 47 48 |
# File 'lib/ruby/riddl/server.rb', line 46 def riddl_pinfo @riddl_pinfo end |
#riddl_status ⇒ Object (readonly)
Returns the value of attribute riddl_status.
46 47 48 |
# File 'lib/ruby/riddl/server.rb', line 46 def riddl_status @riddl_status end |
Instance Method Details
#__call ⇒ Object
}}}
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 231 232 |
# File 'lib/ruby/riddl/server.rb', line 175 def __call #{{{ @riddl_message = @riddl.(@riddl_matching_path[0],@riddl_method,@riddl_parameters,@riddl_headers) if @riddl_message.nil? if @riddl_info[:env].has_key?('HTTP_ORIGIN') && @riddl_cross_site_xhr && @riddl_method == 'options' @riddl_res['Access-Control-Allow-Origin'] = '*' @riddl_res['Access-Control-Allow-Methods'] = 'GET, POST, PUT, PATCH, DELETE, OPTIONS' @riddl_res['Access-Control-Allow-Headers'] = @riddl_info[:env]['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] if @riddl_info[:env]['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] @riddl_res['Access-Control-Max-Age'] = '0' @riddl_res['Content-Length'] = '0' @riddl_status = 200 else @riddl_log.write "501: a #{@riddl_method} with the these parameters is not part in the description (xml).\n" @riddl_status = 501 # not implemented?! end else if !@riddl_message.nil? && @accessible_description && @riddl_message.in.name == 'riddl-description-request' && @riddl_method == 'get' && '/' + @riddl_info[:s].join('/') == '/' run Riddl::Utils::Description::RDR, @riddl_description_string elsif !@riddl_message.nil? && @accessible_description && @riddl_message.in.name == 'riddl-resource-description-request' && @riddl_method == 'get' @riddl_path = File.dirname('/' + @riddl_info[:s].join('/')).gsub(/\/+/,'/') on resource File.basename('/' + @riddl_info[:s].join('/')).gsub(/\/+/,'/') do run Riddl::Utils::Description::RDR, @riddl.resource_description(@riddl_matching_path[0]) end else if @riddl.description? instance_exec(@riddl_info, &@riddl_interfaces[nil]) elsif @riddl.declaration? mess = @riddl_message @riddl_message.route_to_a.each do |m| @riddl_message = m @riddl_path = '/' if m.interface.base.nil? if @riddl_interfaces.key? m.interface.name @riddl_info[:r] = m.interface.real_path(@riddl_pinfo).sub(/^\//,'').split('/') @riddl_info[:h]['RIDDL_DECLARATION_PATH'] = @riddl_pinfo @riddl_info[:h]['RIDDL_DECLARATION_RESOURCE'] = m.interface.top @riddl_info[:s] = m.interface.sub.sub(/\//,'').split('/') @riddl_info.merge!(:match => matching_path) instance_exec(@riddl_info, &@riddl_interfaces[m.interface.name]) else @riddl_log.write "501: not implemented (for remote: add @location in declaration; for local: add to Riddl::Server).\n" @riddl_status = 501 # not implemented?! break end else run Riddl::Utils::Description::Call, @riddl_exe, @riddl_pinfo, m.interface.top, m.interface.base, m.interface.real_path(@riddl_pinfo) end break if @riddl_status < 200 || @riddl_status >= 300 @riddl_info.merge!(:h => @riddl_exe.headers, :p => @riddl_exe.response) end @riddl_message = mess end end if @riddl_info[:env].has_key?('HTTP_ORIGIN') && @riddl_cross_site_xhr @riddl_res['Access-Control-Allow-Origin'] = '*' @riddl_res['Access-Control-Max-Age'] = '0' end end end |
#__http_call(env) ⇒ Object
}}}
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 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 |
# File 'lib/ruby/riddl/server.rb', line 234 def __http_call(env) #{{{ @riddl_env = env @riddl_env['rack.logger'] = @riddl_logger if @riddl_logger @riddl_log = @riddl_logger || @riddl_env['rack.errors'] @riddl_res = Rack::Response.new @riddl_status = 404 @riddl_pinfo = Riddl::Protocols::Utils::unescape(@riddl_env["PATH_INFO"].gsub(/\/+/,'/')) @riddl_matching_path = @riddl_paths.find{ |e| @riddl_pinfo.match(e[1]).to_s.length == @riddl_pinfo.length } if @riddl_matching_path @riddl_query_string = @riddl_env['QUERY_STRING'] @riddl_raw = @riddl_env['rack.input'] @riddl_headers = {} @riddl_env.each do |h,v| @riddl_headers[$1] = v if h =~ /^HTTP_(.*)$/ end @riddl_parameters = Protocols::HTTP::Parser.new( @riddl_query_string, @riddl_raw, @riddl_env['CONTENT_TYPE'], @riddl_env['CONTENT_LENGTH'], @riddl_env['HTTP_CONTENT_DISPOSITION'], @riddl_env['HTTP_CONTENT_ID'], @riddl_env['HTTP_RIDDL_TYPE'] ).params if @riddl_opts[:http_debug] pp @riddl_parameters end @riddl_method = @riddl_env['REQUEST_METHOD'].downcase @riddl_path = '/' @riddl_info = { :h => @riddl_headers, :p => @riddl_parameters, :r => @riddl_pinfo.sub(/^\//,'').split('/').map{|e|Protocols::Utils::unescape(e)}, :s => @riddl_matching_path[0].sub(/\//,'').split('/'), :m => @riddl_method, :env => @riddl_env.reject{|k,v| k =~ /^rack\./}.merge({'riddl.transport' => 'http', 'custom_protocol' => @riddl_opts[:custom_protocol]}), :match => [] } if @riddl_info[:env]['HTTP_CONNECTION'] =~ /Upgrade/ && @riddl_info[:env]['HTTP_UPGRADE'] =~ /\AWebSocket\z/i # TODO raise error when declaration and route or (not route and non-local interface) # raise SpecificationError, 'RIDDL description does not conform to specification' unless @riddl.validate! @riddl_info[:m] = @riddl_method = 'websocket' @riddl_message = @riddl.(@riddl_matching_path[0],'websocket',@riddl_parameters,@riddl_headers) if @riddl.description? instance_exec(@riddl_info, &@riddl_interfaces[nil]) elsif @riddl.declaration? # one ws connection, no overlay unless @riddl_message.nil? if @riddl_interfaces.key? @riddl_message.interface.name @riddl_info[:r] = @riddl_message.interface.real_path(@riddl_pinfo).sub(/^\//,'').split('/') @riddl_info[:h]['RIDDL_DECLARATION_PATH'] = @riddl_pinfo @riddl_info[:h]['RIDDL_DECLARATION_RESOURCE'] = @riddl_message.interface.top @riddl_info[:s] = @riddl_message.interface.sub.sub(/\//,'').split('/') @riddl_info.merge!(:match => matching_path) instance_exec(@riddl_info, &@riddl_interfaces[@riddl_message.interface.name]) end end end throw :async elsif @riddl_info[:env]['HTTP_ACCEPT'] == 'text/event-stream' @riddl_info[:m] = @riddl_method = 'sse' @riddl_message = @riddl.(@riddl_matching_path[0],'sse',@riddl_parameters,@riddl_headers) if @riddl.description? instance_exec(@riddl_info, &@riddl_interfaces[nil]) elsif @riddl.declaration? # one ws connection, no overlay unless @riddl_message.nil? if @riddl_interfaces.key? @riddl_message.interface.name @riddl_info[:r] = @riddl_message.interface.real_path(@riddl_pinfo).sub(/^\//,'').split('/') @riddl_info[:h]['RIDDL_DECLARATION_PATH'] = @riddl_pinfo @riddl_info[:h]['RIDDL_DECLARATION_RESOURCE'] = @riddl_message.interface.top @riddl_info[:s] = @riddl_message.interface.sub.sub(/\//,'').split('/') @riddl_info.merge!(:match => matching_path) instance_exec(@riddl_info, &@riddl_interfaces[@riddl_message.interface.name]) end end end throw :async else __call end else @riddl_log.write "404: this resource for sure does not exist.\n" @riddl_status = 404 # client requests wrong path end if @riddl_exe @riddl_res.write Protocols::HTTP::Generator.new(@riddl_exe.response,@riddl_res).generate.read @riddl_exe.headers.each do |n,h| @riddl_res[n] = h end end @riddl_res.status = @riddl_status @riddl_res.finish end |
#accessible_description(ad) ⇒ Object
}}}
343 344 345 |
# File 'lib/ruby/riddl/server.rb', line 343 def accessible_description(ad)# {{{ @accessible_description = ad end |
#call(env) ⇒ Object
{{{
171 172 173 |
# File 'lib/ruby/riddl/server.rb', line 171 def call(env)# {{{ dup.__http_call(env) end |
#cleanup(&blk) ⇒ Object
167 168 169 |
# File 'lib/ruby/riddl/server.rb', line 167 def cleanup(&blk) @riddl_opts[:cleanup] = blk end |
#cross_site_xhr(csxhr) ⇒ Object
}}}
337 338 339 |
# File 'lib/ruby/riddl/server.rb', line 337 def cross_site_xhr(csxhr)# {{{ @riddl_cross_site_xhr = csxhr end |
#declaration_path ⇒ Object
}}}
434 435 436 |
# File 'lib/ruby/riddl/server.rb', line 434 def declaration_path #{{{ @riddl_info[:h]['RIDDL_DECLARATION_PATH'] end |
#declaration_resource ⇒ Object
}}}
437 438 439 |
# File 'lib/ruby/riddl/server.rb', line 437 def declaration_resource #{{{ @riddl_info[:h]['RIDDL_DECLARATION_RESOURCE'] end |
#delete(min = '*') ⇒ Object
423 |
# File 'lib/ruby/riddl/server.rb', line 423 def delete(min='*'); return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_message.in && min == @riddl_message.in.name && @riddl_method == 'delete' end |
#get(min = '*') ⇒ Object
422 |
# File 'lib/ruby/riddl/server.rb', line 422 def get(min='*'); return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_message.in && min == @riddl_message.in.name && @riddl_method == 'get' end |
#interface(name, &block) ⇒ Object
}}}
346 347 348 |
# File 'lib/ruby/riddl/server.rb', line 346 def interface(name,&block) #{{{ @riddl_interfaces[name] = block end |
#logger(lgr) ⇒ Object
}}}
340 341 342 |
# File 'lib/ruby/riddl/server.rb', line 340 def logger(lgr)# {{{ @riddl_logger = lgr end |
#loop! ⇒ Object
}}}
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ruby/riddl/server.rb', line 99 def loop! #{{{ app = Rack::Builder.new self unless @riddl_logger.nil? app.use Rack::CommonLogger, @riddl_logger end server = Rack::Server.new( :app => app, :Host => @riddl_opts[:bind], :Port => @riddl_opts[:port], :environment => @riddl_opts[:verbose] ? 'deployment' : 'none', :server => 'thin', :signals => false ) @riddl_opts[:startup].call if @riddl_opts[:startup] if @riddl_opts[:custom_protocol] && !@riddl_opts[:http_only] @riddl_opts[:custom_protocol] = @riddl_opts[:custom_protocol].new(@riddl_opts) puts @riddl_opts[:custom_protocol].support if @riddl_opts[:custom_protocol].support end begin EM.run do if @riddl_opts[:secure] server.start do |srv| srv.ssl = true srv. = @riddl_opts[:secure_options] end else server.start end if @riddl_opts[:custom_protocol] && !@riddl_opts[:http_only] @riddl_opts[:custom_protocol].start end [:INT, :TERM].each do |signal| Signal.trap(signal) do if @riddl_opts[:cleanup] @riddl_opts[:cleanup].call end EM.stop end end [:HUP].each do |signal| Signal.trap(signal) do EM.stop end end if @riddl_opts[:parallel] EM.defer do @riddl_opts[:parallel].call end end end rescue => e if @riddl_opts[:custom_protocol] && !@riddl_opts[:http_only] @riddl_opts[:custom_protocol].error_handling(e) end puts "Server (#{@riddl_opts[:cmdl_info]}) stopped due to connection error (PID:#{Process.pid})" end end |
#matching_path ⇒ Object
{{{
430 431 432 |
# File 'lib/ruby/riddl/server.rb', line 430 def matching_path #{{{ @riddl_path.sub(/\//,'').split('/') end |
#method(what) ⇒ Object
}}}
413 414 415 416 417 418 419 420 |
# File 'lib/ruby/riddl/server.rb', line 413 def method(what)# {{{ if !@riddl_message.nil? && what.class == Hash && what.length == 1 met, min = what.first @riddl_path == @riddl_matching_path[0] && min == @riddl_message.in.name && @riddl_method == met.to_s.downcase else false end end |
#on(resource, &block) ⇒ Object
}}}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/ruby/riddl/server.rb', line 350 def on(resource, &block)# {{{ if @riddl_paths.empty? # default interface, when a description and "on" syntax in server @riddl_interfaces[nil] = block return end @riddl_path << (@riddl_path == '/' ? resource : '/' + resource) ### only descend when there is a possibility that it holds the right path rp = @riddl_path.sub(/\//,'').split('/') block.call(@riddl_info.merge!(:match => matching_path)) if @riddl_info[:s][rp.length-1] == rp.last @riddl_path = File.dirname(@riddl_path).gsub(/\/+/,'/') end |
#parallel(&blk) ⇒ Object
}}}
161 162 163 |
# File 'lib/ruby/riddl/server.rb', line 161 def parallel(&blk) @riddl_opts[:parallel] = blk end |
#patch(min = '*') ⇒ Object
425 |
# File 'lib/ruby/riddl/server.rb', line 425 def patch(min='*'); return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_message.in && min == @riddl_message.in.name && @riddl_method == 'patch' end |
#post(min = '*') ⇒ Object
}}}
421 |
# File 'lib/ruby/riddl/server.rb', line 421 def post(min='*'); return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_message.in && min == @riddl_message.in.name && @riddl_method == 'post' end |
#process_out(pout) ⇒ Object
}}}
334 335 336 |
# File 'lib/ruby/riddl/server.rb', line 334 def process_out(pout)# {{{ @riddl_process_out = pout end |
#put(min = '*') ⇒ Object
424 |
# File 'lib/ruby/riddl/server.rb', line 424 def put(min='*'); return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_message.in && min == @riddl_message.in.name && @riddl_method == 'put' end |
#resource(rname = nil) ⇒ Object
428 |
# File 'lib/ruby/riddl/server.rb', line 428 def resource(rname=nil); return rname.nil? ? '{}' : rname end |
#run(what, *args) ⇒ Object
}}}
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/ruby/riddl/server.rb', line 369 def run(what,*args)# {{{ return if @riddl_path == '' if what.class == Class && what.superclass == Riddl::SSEImplementation data = Riddl::Protocols::SSE::ParserData.new data.request_path = @riddl_pinfo data.request_url = @riddl_pinfo + '?' + @riddl_query_string data.query_string = @riddl_query_string data.http_method = @riddl_env['REQUEST_METHOD'] data.body = @riddl_env['rack.input'].read data.headers = Hash[ @riddl_headers.map { |key, value| [key.downcase.gsub('_','-'), value] } ] w = what.new(@riddl_info.merge!(:a => args, :match => matching_path)) w.io = Riddl::Protocols::SSE.new(w, @riddl_env) w.io.dispatch(data, @riddl_cross_site_xhr) end if what.class == Class && what.superclass == Riddl::WebSocketImplementation data = Riddl::Protocols::WebSocket::ParserData.new data.request_path = @riddl_pinfo data.request_url = @riddl_pinfo + '?' + @riddl_query_string data.query_string = @riddl_query_string data.http_method = @riddl_env['REQUEST_METHOD'] data.body = @riddl_env['rack.input'].read data.headers = Hash[ @riddl_headers.map { |key, value| [key.downcase.gsub('_','-'), value] } ] w = what.new(@riddl_info.merge!(:a => args, :version => @riddl_env['HTTP_SEC_WEBSOCKET_VERSION'], :match => matching_path)) w.io = Riddl::Protocols::WebSocket.new(w, @riddl_env['thin.connection']) w.io.dispatch(data) end if what.class == Class && what.superclass == Riddl::Implementation w = what.new(@riddl_info.merge!(:a => args, :match => matching_path)) @riddl_exe = Riddl::Server::Execution.new(w.response,w.headers) @riddl_status = w.status if @riddl_process_out && @riddl_status >= 200 && @riddl_status < 300 unless @riddl.(@riddl_exe.response,@riddl_exe.headers,@riddl_message.out) @riddl_log.write "500: the return for the #{@riddl_method} is not matching anything in the description.\n" @riddl_status = 500 return end end end end |
#sse ⇒ Object
427 |
# File 'lib/ruby/riddl/server.rb', line 427 def sse; return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_method == 'sse' end |
#startup(&blk) ⇒ Object
164 165 166 |
# File 'lib/ruby/riddl/server.rb', line 164 def startup(&blk) @riddl_opts[:startup] = blk end |
#use(blk, *args) ⇒ Object
}}}
365 366 367 |
# File 'lib/ruby/riddl/server.rb', line 365 def use(blk,*args)# {{{ instance_eval(&blk) end |
#websocket ⇒ Object
426 |
# File 'lib/ruby/riddl/server.rb', line 426 def websocket; return false if @riddl_message.nil?; @riddl_path == '/' + @riddl_info[:s].join('/') && @riddl_method == 'websocket' end |