Class: XMLRPC::HTTPAuthXMLRPCServer

Inherits:
WEBrickServlet
  • Object
show all
Defined in:
lib/rbitter/xmlrpcd/xmlrpc_auth_server.rb

Instance Method Summary collapse

Instance Method Details

#extract_method(methodname, *args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rbitter/xmlrpcd/xmlrpc_auth_server.rb', line 10

def extract_method(methodname, *args)
  for name, obj in @handler
    if obj.kind_of? Proc
      next unless methodname == name
    else
      next unless methodname =~ /^#{name}(.+)$/
      next unless obj.respond_to? $1
      return obj.method($1)
    end
  end
  nil
end

#service(request, response) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::LengthRequired)


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
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
# File 'lib/rbitter/xmlrpcd/xmlrpc_auth_server.rb', line 23

def service(request, response)
  # Taken from xmlrpc/server.rb
  if @valid_ip
    raise WEBrick::HTTPStatus::Forbidden unless @valid_ip.any? { |ip| request.peeraddr[3] =~ ip }
  end

  if request.request_method != "POST"
    raise WEBrick::HTTPStatus::MethodNotAllowed,
          "unsupported method `#{request.request_method}'."
  end

  if parse_content_type(request['Content-type']).first != "text/xml"
    raise WEBrick::HTTPStatus::BadRequest
  end

  length = (request['Content-length'] || 0).to_i

  raise WEBrick::HTTPStatus::LengthRequired unless length > 0

  data = request.body

  if data.nil? or data.bytesize != length
    raise WEBrick::HTTPStatus::BadRequest
  end

  # Originally, process(data) was here.
  # We need to check whether a method requires authorization.
  rpc_method_name, rpc_params = parser().parseMethodCall(data)
  rpc_method = extract_method(rpc_method_name)

  if RPCHandles.auth.nil?
    resp = handle(rpc_method_name, *rpc_params)
  else
    if rpc_method.owner.ancestors.include?(RPCHandles::BaseHandle::Auth)
      # Check cookie and check it's valid
      if request.cookies.size == 1 \
        and request.cookies[0].name == "auth_key" \
        and RPCHandles.auth.include?(request.cookies[0].value)
        resp = handle(rpc_method_name, *rpc_params)
      else
        # Permission required
        raise WEBrick::HTTPStatus::Forbidden
      end
    elsif rpc_method.owner.ancestors.include?(RPCHandles::BaseHandle::NoAuth)
      resp = handle(rpc_method_name, *rpc_params)
    else
      raise WEBrick::HTTPStatus::Forbidden
    end
  end

  if resp.nil? or resp.bytesize <= 0
    raise WEBrick::HTTPStatus::InternalServerError
  end

  response.status = 200
  response['Content-Length'] = resp.bytesize
  response['Content-Type']   = "text/xml; charset=utf-8"
  response.body = resp
end