Class: XMLRPC::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlrpc/rack/version.rb,
lib/xmlrpc/rack.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(xmlrpc_server) ⇒ Rack

Returns a new instance of Rack.



5
6
7
8
9
10
# File 'lib/xmlrpc/rack.rb', line 5

def initialize xmlrpc_server
  @xmlrpc_server = xmlrpc_server
  unless @xmlrpc_server.respond_to? :process
    raise ArgumentError, "xml rpc server must respond to `process'"
  end
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/xmlrpc/rack.rb', line 12

def call env

  request = Rack::Request.new(env)

  if request.request_method != "POST"
    return [400,{},['request must be a post']]
  end

  if request.content_type !~ %r{^text/xml}
    return [400,{},['Content-type must be text/xml']]
  end

  length = (request.content_length || 0).to_i

  if length <= 0
    return [400,{},['post body content requied']]
  end

  data = request.body.read

  if data.nil?
    return [400,{},['post body cannot be empty']]
  end

  if data.bytesize != length
    return [400,{},['post body length does not match Content-Length']]
  end

  response = @xmlrpc_server.process(data)
  if response.nil? or response.bytesize <= 0
    return [500, {}, ['']]
  end

  headers = {
    'Content-Type' => "text/xml; charset=utf-8",
    'Content-Length' => response.bytesize.to_s
  }

  [200, headers, [response]]

end