2003-07-19 06:05:54 -04:00
|
|
|
#
|
|
|
|
# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
|
|
|
|
require "date"
|
|
|
|
require "xmlrpc/base64"
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
module XMLRPC # :nodoc:
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
module XMLWriter
|
|
|
|
|
|
|
|
class Abstract
|
|
|
|
def ele(name, *children)
|
2008-10-01 09:46:53 -04:00
|
|
|
element(name, nil, *children)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag(name, txt)
|
2008-10-01 09:46:53 -04:00
|
|
|
element(name, nil, text(txt))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Simple < Abstract
|
|
|
|
|
|
|
|
def document_to_str(doc)
|
2008-10-01 09:46:53 -04:00
|
|
|
doc
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def document(*params)
|
2008-10-01 09:46:53 -04:00
|
|
|
params.join("")
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pi(name, *params)
|
2008-10-01 09:46:53 -04:00
|
|
|
"<?#{name} " + params.join(" ") + " ?>"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def element(name, attrs, *children)
|
2008-10-01 09:46:53 -04:00
|
|
|
raise "attributes not yet implemented" unless attrs.nil?
|
2003-07-19 06:05:54 -04:00
|
|
|
if children.empty?
|
2009-03-05 22:56:38 -05:00
|
|
|
"<#{name}/>"
|
2003-07-19 06:05:54 -04:00
|
|
|
else
|
|
|
|
"<#{name}>" + children.join("") + "</#{name}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def text(txt)
|
|
|
|
cleaned = txt.dup
|
|
|
|
cleaned.gsub!(/&/, '&')
|
|
|
|
cleaned.gsub!(/</, '<')
|
|
|
|
cleaned.gsub!(/>/, '>')
|
|
|
|
cleaned
|
|
|
|
end
|
|
|
|
|
|
|
|
end # class Simple
|
|
|
|
|
|
|
|
|
|
|
|
class XMLParser < Abstract
|
|
|
|
|
|
|
|
def initialize
|
2008-10-01 09:46:53 -04:00
|
|
|
require "xmltreebuilder"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def document_to_str(doc)
|
2008-10-01 09:46:53 -04:00
|
|
|
doc.to_s
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def document(*params)
|
2009-03-05 22:56:38 -05:00
|
|
|
XML::SimpleTree::Document.new(*params)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pi(name, *params)
|
2008-10-01 09:46:53 -04:00
|
|
|
XML::SimpleTree::ProcessingInstruction.new(name, *params)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def element(name, attrs, *children)
|
2008-10-01 09:46:53 -04:00
|
|
|
XML::SimpleTree::Element.new(name, attrs, *children)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def text(txt)
|
2008-10-01 09:46:53 -04:00
|
|
|
XML::SimpleTree::Text.new(txt)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # class XMLParser
|
|
|
|
|
2004-11-15 18:26:20 -05:00
|
|
|
Classes = [Simple, XMLParser]
|
|
|
|
|
|
|
|
# yields an instance of each installed XML writer
|
|
|
|
def self.each_installed_writer
|
|
|
|
XMLRPC::XMLWriter::Classes.each do |klass|
|
|
|
|
begin
|
|
|
|
yield klass.new
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
end # module XMLWriter
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Creates XML-RPC call/response documents
|
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
class Create
|
|
|
|
|
|
|
|
def initialize(xml_writer = nil)
|
|
|
|
@writer = xml_writer || Config::DEFAULT_WRITER.new
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def methodCall(name, *params)
|
|
|
|
name = name.to_s
|
|
|
|
|
|
|
|
if name !~ /[a-zA-Z0-9_.:\/]+/
|
2008-10-01 09:46:53 -04:00
|
|
|
raise ArgumentError, "Wrong XML-RPC method-name"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
parameter = params.collect do |param|
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.ele("param", conv2value(param))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
tree = @writer.document(
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.pi("xml", 'version="1.0"'),
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("methodCall",
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("methodName", name),
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("params", *parameter)
|
2008-10-01 09:46:53 -04:00
|
|
|
)
|
|
|
|
)
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
@writer.document_to_str(tree) + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
2012-09-12 22:22:10 -04:00
|
|
|
# Generates a XML-RPC methodResponse document
|
2003-07-19 06:05:54 -04:00
|
|
|
#
|
2012-09-12 22:22:10 -04:00
|
|
|
# When +is_ret+ is +false+ then the +params+ array must
|
2003-07-19 06:05:54 -04:00
|
|
|
# contain only one element, which is a structure
|
|
|
|
# of a fault return-value.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2012-09-12 22:22:10 -04:00
|
|
|
# When +is_ret+ is +true+ then a normal
|
|
|
|
# return-value of all the given +params+ is created.
|
2003-07-19 06:05:54 -04:00
|
|
|
#
|
|
|
|
def methodResponse(is_ret, *params)
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
if is_ret
|
2008-10-01 09:46:53 -04:00
|
|
|
resp = params.collect do |param|
|
|
|
|
@writer.ele("param", conv2value(param))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
resp = [@writer.ele("params", *resp)]
|
2003-07-19 06:05:54 -04:00
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
if params.size != 1 or params[0] === XMLRPC::FaultException
|
2008-10-01 09:46:53 -04:00
|
|
|
raise ArgumentError, "no valid fault-structure given"
|
|
|
|
end
|
|
|
|
resp = @writer.ele("fault", conv2value(params[0].to_h))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
tree = @writer.document(
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.pi("xml", 'version="1.0"'),
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("methodResponse", resp)
|
2008-10-01 09:46:53 -04:00
|
|
|
)
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
@writer.document_to_str(tree) + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
#
|
2012-09-12 22:22:10 -04:00
|
|
|
# Converts a Ruby object into a XML-RPC <code><value></code> tag
|
2003-07-19 06:05:54 -04:00
|
|
|
#
|
2012-09-12 22:22:10 -04:00
|
|
|
def conv2value(param) # :doc:
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
val = case param
|
2011-04-21 22:50:45 -04:00
|
|
|
when Fixnum, Bignum
|
|
|
|
# XML-RPC's int is 32bit int, and Fixnum also may be beyond 32bit
|
2003-07-19 06:05:54 -04:00
|
|
|
if Config::ENABLE_BIGINT
|
|
|
|
@writer.tag("i4", param.to_s)
|
|
|
|
else
|
|
|
|
if param >= -(2**31) and param <= (2**31-1)
|
|
|
|
@writer.tag("i4", param.to_s)
|
|
|
|
else
|
|
|
|
raise "Bignum is too big! Must be signed 32-bit integer!"
|
|
|
|
end
|
|
|
|
end
|
2008-10-01 09:46:53 -04:00
|
|
|
when TrueClass, FalseClass
|
|
|
|
@writer.tag("boolean", param ? "1" : "0")
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
when Symbol
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("string", param.to_s)
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
when String
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("string", param)
|
2006-11-01 19:21:28 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
when NilClass
|
|
|
|
if Config::ENABLE_NIL_CREATE
|
|
|
|
@writer.ele("nil")
|
|
|
|
else
|
|
|
|
raise "Wrong type NilClass. Not allowed!"
|
|
|
|
end
|
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
when Float
|
2011-04-22 09:35:43 -04:00
|
|
|
raise "Wrong value #{param}. Not allowed!" unless param.finite?
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("double", param.to_s)
|
|
|
|
|
|
|
|
when Struct
|
2009-03-05 22:56:38 -05:00
|
|
|
h = param.members.collect do |key|
|
2008-10-01 09:46:53 -04:00
|
|
|
value = param[key]
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("member",
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("name", key.to_s),
|
2009-03-05 22:56:38 -05:00
|
|
|
conv2value(value)
|
2008-10-01 09:46:53 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("struct", *h)
|
2008-10-01 09:46:53 -04:00
|
|
|
|
|
|
|
when Hash
|
|
|
|
# TODO: can a Hash be empty?
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
h = param.collect do |key, value|
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("member",
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.tag("name", key.to_s),
|
2009-03-05 22:56:38 -05:00
|
|
|
conv2value(value)
|
2008-10-01 09:46:53 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.ele("struct", *h)
|
2008-10-01 09:46:53 -04:00
|
|
|
|
|
|
|
when Array
|
|
|
|
# TODO: can an Array be empty?
|
|
|
|
a = param.collect {|v| conv2value(v) }
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
@writer.ele("array",
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.ele("data", *a)
|
|
|
|
)
|
|
|
|
|
|
|
|
when Time, Date, ::DateTime
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.tag("dateTime.iso8601", param.strftime("%Y%m%dT%H:%M:%S"))
|
2008-10-01 09:46:53 -04:00
|
|
|
|
|
|
|
when XMLRPC::DateTime
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.tag("dateTime.iso8601",
|
2008-10-01 09:46:53 -04:00
|
|
|
format("%.4d%02d%02dT%02d:%02d:%02d", *param.to_a))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
when XMLRPC::Base64
|
2009-03-05 22:56:38 -05:00
|
|
|
@writer.tag("base64", param.encoded)
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2003-07-19 06:05:54 -04:00
|
|
|
if Config::ENABLE_MARSHALLING and param.class.included_modules.include? XMLRPC::Marshallable
|
|
|
|
# convert Ruby object into Hash
|
|
|
|
ret = {"___class___" => param.class.name}
|
2009-03-05 22:56:38 -05:00
|
|
|
param.instance_variables.each {|v|
|
2004-11-15 18:26:20 -05:00
|
|
|
name = v[1..-1]
|
|
|
|
val = param.instance_variable_get(v)
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
if val.nil?
|
|
|
|
ret[name] = val if Config::ENABLE_NIL_CREATE
|
|
|
|
else
|
|
|
|
ret[name] = val
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return conv2value(ret)
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2003-07-19 06:05:54 -04:00
|
|
|
ok, pa = wrong_type(param)
|
|
|
|
if ok
|
|
|
|
return conv2value(pa)
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2003-07-19 06:05:54 -04:00
|
|
|
raise "Wrong type!"
|
|
|
|
end
|
|
|
|
end
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
@writer.ele("value", val)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def wrong_type(value)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
end # class Create
|
|
|
|
|
|
|
|
end # module XMLRPC
|
|
|
|
|