[Hiki-cvs 1461] [1008] add XML-RPC support for Rack

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 8月 27日 (木) 23:59:20 JST


Revision: 1008
          http://sourceforge.jp/projects/hiki/svn/view?view=rev&revision=1008
Author:   okkez
Date:     2009-08-27 23:59:20 +0900 (Thu, 27 Aug 2009)

Log Message:
-----------
add XML-RPC support for Rack

Modified Paths:
--------------
    hiki/branches/rack/hiki/app.rb
    hiki/branches/rack/hiki/xmlrpc.rb
    hiki/branches/rack/hiki.cgi

Added Paths:
-----------
    hiki/branches/rack/hiki/xmlrpc/
    hiki/branches/rack/hiki/xmlrpc/rackserver.rb

Modified: hiki/branches/rack/hiki/app.rb
===================================================================
--- hiki/branches/rack/hiki/app.rb	2009-08-24 15:33:07 UTC (rev 1007)
+++ hiki/branches/rack/hiki/app.rb	2009-08-27 14:59:20 UTC (rev 1008)
@@ -3,6 +3,7 @@
 require 'rack'
 
 require 'hiki/config'
+require 'hiki/xmlrpc'
 
 $LOAD_PATH.unshift 'hiki'
 
@@ -14,11 +15,16 @@
       # HACK replace ENV values to web application environment
       env.each{|k,v| ENV[k] = v unless /\Arack\./ =~ k }
       conf = Hiki::Config.new
-      db = conf.database
       response = nil
-      db.open_db do
-        command = Hiki::Command.new(request, db, conf)
-        response = command.dispatch
+      if %r|text/xml| =~ request.content_type and request.post?
+        server = Hiki::XMLRPCServer.new(conf, request)
+        response = server.serve
+      else
+        db = conf.database
+        db.open_db do
+          command = Hiki::Command.new(request, db, conf)
+          response = command.dispatch
+        end
       end
       # [body, status, headers]
       # Rack::Response.new(*response){|r|

Added: hiki/branches/rack/hiki/xmlrpc/rackserver.rb
===================================================================
--- hiki/branches/rack/hiki/xmlrpc/rackserver.rb	                        (rev 0)
+++ hiki/branches/rack/hiki/xmlrpc/rackserver.rb	2009-08-27 14:59:20 UTC (rev 1008)
@@ -0,0 +1,38 @@
+
+module XMLRPC
+  class RackServer < BasicServer
+    def initialize(request, class_delim = '.')
+      @request = request
+      super(class_delim)
+    end
+
+    def serve
+      return error_response(405, "Method Not Allowed") unles****@reque*****?
+      return error_response(400, "Bad Request") unless %r|text/xml| =~ @request.content_type
+      return error_response(411, "Length Required") unles****@reque*****_length.to_i > 0
+      data = ""
+      @request.body.read(nil, data)
+      return error_response(400, "Bad Request") if data.nil? or data.size !=****@reque*****_length.to_i
+      Hiki::Response.new(process(data), 200, "Content-Type" => "text/xml; charset=utf-8")
+    end
+
+    private
+
+    def error_response(status, message = "")
+      error = "#{status} #{message}"
+      body=<<-BODY
+        <html>
+          <head>
+            <title>#{error}</title>
+          </head>
+          <body>
+            <h1>#{error}</h1>
+            <p>Unexpected error occured while processing XML-RPC request!</p>
+          </body>
+        </html>
+      BODY
+      Hiki::Response.new(body, status, "Content-Type" => "text/html")
+    end
+
+  end
+end

Modified: hiki/branches/rack/hiki/xmlrpc.rb
===================================================================
--- hiki/branches/rack/hiki/xmlrpc.rb	2009-08-24 15:33:07 UTC (rev 1007)
+++ hiki/branches/rack/hiki/xmlrpc.rb	2009-08-27 14:59:20 UTC (rev 1008)
@@ -7,10 +7,9 @@
   module XMLRPCHandler
     module_function
 
-    def init_handler(server, cgi_class=CGI)
+    def init_handler(server, conf, request)
       server.add_handler('wiki.getPage') do |page|
         page = utf8_to_euc( page )
-        conf = Hiki::Config.new
         db = conf.database
         ret = db.load( page )
         unless ret
@@ -21,7 +20,6 @@
 
       server.add_handler('wiki.getPageInfo') do |page|
         page = utf8_to_euc( page )
-        conf = Hiki::Config.new
         db = conf.database
         title = db.get_attribute( page, :title )
         title = page if title.nil? || title.empty?
@@ -46,19 +44,17 @@
             v.map!{ |s| s.replace( utf8_to_euc( s ) ) }
           end
         }
-        conf = Hiki::Config.new
-        cgi = cgi_class.new
-        cgi.params['c'] = ['save']
-        cgi.params['p'] = [page]
+        request.params['c'] = 'save'
+        request.params['p'] = page
         db = conf.database
-        options = conf.options || Hash.new( '' )
+        options = conf.options || Hash.new('')
         options['page'] = page
-        options['cgi']  = cgi
+        options['cgi']  = request
         options['db']  = db
-        options['params'] = Hash.new( [] )
+        options['params'] = Hash.new('')
         plugin = Hiki::Plugin.new( options, conf )
         plugin.login( attributes['name'], attributes['password'] )
-        Hiki::Filter.init(conf, cgi, plugin, db)
+        Hiki::Filter.init(conf, request, plugin, db)
 
         unless plugin.editable?( page )
           raise XMLRPC::FaultException.new(10, "can't edit this page.")
@@ -81,7 +77,6 @@
       end
 
       server.add_handler('wiki.getAllPages') do
-        conf = Hiki::Config.new
         db = conf.database
         db.pages.collect{|p| XMLRPC::Base64.new( euc_to_utf8( p ) )}
       end
@@ -94,16 +89,22 @@
   class XMLRPCServer
     include XMLRPCHandler
 
-    def initialize(xmlrpc_enabled)
-      return unless xmlrpc_enabled
+    def initialize(conf, request)
+      return unless conf.xmlrpc_enabled
 
-      if defined?(MOD_RUBY)
+      case
+      when Object.const_defined?(:Rack)
+        require 'hiki/xmlrpc/rackserver.rb'
+        @server = XMLRPC::RackServer.new(request)
+      when Object.const_defined?(:MOD_RUBY)
         @server = XMLRPC::ModRubyServer.new
+      when Object.const_defined?(:CGI)
+        @server = XMLRPC::CGIServer.new
       else
-        @server = XMLRPC::CGIServer.new
+        raise 'must not happen!'
       end
 
-      init_handler(@server)
+      init_handler(@server, conf, request)
     end
 
     def serve

Modified: hiki/branches/rack/hiki.cgi
===================================================================
--- hiki/branches/rack/hiki.cgi	2009-08-24 15:33:07 UTC (rev 1007)
+++ hiki/branches/rack/hiki.cgi	2009-08-27 14:59:20 UTC (rev 1008)
@@ -26,7 +26,7 @@
 
   if ENV['CONTENT_TYPE'] =~ %r!\Atext/xml!i and ENV['REQUEST_METHOD'] =~ /\APOST\z/i
     require 'hiki/xmlrpc'
-    server = Hiki::XMLRPCServer.new( conf.xmlrpc_enabled )
+    server = Hiki::XMLRPCServer.new(conf, request)
     server.serve
   else
     # FIXME encoding can be different (eg. iso-8859-1 in




Hiki-cvs メーリングリストの案内
Back to archive index