[Hiki-cvs 1448] [995] use Hiki::Request, Hiki::Response instead of raw CGI

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 8月 24日 (月) 00:02:03 JST


Revision: 995
          http://sourceforge.jp/projects/hiki/svn/view?view=rev&revision=995
Author:   okkez
Date:     2009-08-24 00:02:03 +0900 (Mon, 24 Aug 2009)

Log Message:
-----------
use Hiki::Request, Hiki::Response instead of raw CGI

Modified Paths:
--------------
    hiki/branches/rack/hiki/command.rb
    hiki/branches/rack/hiki/config.rb
    hiki/branches/rack/hiki/filter.rb
    hiki/branches/rack/hiki/page.rb
    hiki/branches/rack/hiki/plugin.rb
    hiki/branches/rack/hiki/request.rb
    hiki/branches/rack/hiki/util.rb
    hiki/branches/rack/hiki.cgi
    hiki/branches/rack/plugin/00default.rb
    hiki/branches/rack/plugin/01sp.rb

Modified: hiki/branches/rack/hiki/command.rb
===================================================================
--- hiki/branches/rack/hiki/command.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/command.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -30,7 +30,7 @@
       code_conv
 
       # for TrackBack
-      if %r|/tb/(.+)$| =~ ENV['REQUEST_URI']
+      if %r|/tb/(.+)$| =~ @cgi.env['REQUEST_URI']
         @params['p'] = CGI.unescape($1)
         @params['c'] = 'plugin'
         @params['plugin'] = 'trackback_post'
@@ -67,7 +67,7 @@
       options['params'] = @params
 
       @plugin = Plugin.new( options, @conf )
-      session_id =****@cgi*****['session_id'][0]
+      session_id =****@cgi*****['session_id']
       if session_id
         session = Hiki::Session.new( @conf, session_id )
         if session.check
@@ -89,7 +89,7 @@
     def dispatch
       begin
         Timeout.timeout(@conf.timeout) {
-          if 'POST' ==****@cgi*****_method
+          if****@cgi*****?
             raise PermissionError, 'Permission denied' unles****@plugi*****?
           end
           @cmd = 'view' unless @cmd
@@ -197,7 +197,7 @@
       end
 
       old_ref =****@db*****_attribute( @p, :references )
-      new_ref = formatter.references 
+      new_ref = formatter.references
       @db.set_references( @p, new_ref ) if new_ref != old_ref
       ref =****@db*****_references( @p )
 
@@ -387,14 +387,12 @@
 
         if exist?(title)
           @cmd = 'edit'
-          cmd_edit( page, text, @conf.msg_duplicate_page_title )
-          return
+          return cmd_edit( page, text, @conf.msg_duplicate_page_title )
         end
 
         if Filter.new_page_is_spam?(page, text, title)
           @cmd = 'is_spam'
-          cmd_edit( page, text, @conf.msg_input_is_spam )
-          return
+          return cmd_edit( page, text, @conf.msg_input_is_spam )
         end
 
         if****@plugi*****( page, text, md5hex, update_timestamp, false )
@@ -405,12 +403,11 @@
           @db.set_attribute(page, attr)
         else
           @cmd = 'conflict'
-          cmd_edit( page, text, @conf.msg_save_conflict )
-          return
+          return cmd_edit( page, text, @conf.msg_save_conflict )
         end
 
         @db.freeze_page( page, @params['freeze'] ? true : false) if****@plugi*****?
-        redirect(@cgi, @conf.base_url +****@plugi*****_url(page))
+        return redirect(@cgi, @conf.base_url +****@plugi*****_url(page))
       end
     end
 
@@ -497,11 +494,10 @@
           session.user =****@plugi*****
           session.save
           if page && !page.empty?
-            redirect(@cgi, @conf.base_url +****@plugi*****_url( page ), session_cookie( session.session_id ))
+            return redirect(@cgi, @conf.base_url +****@plugi*****_url( page ), session_cookie( session.session_id ))
           else
-            redirect(@cgi, @conf.index_url, session_cookie( session.session_id ))
+            return redirect(@cgi, @conf.index_url, session_cookie( session.session_id ))
           end
-          return
         else
           msg_login_result =****@conf*****_login_failure
           status = '403 Forbidden'

Modified: hiki/branches/rack/hiki/config.rb
===================================================================
--- hiki/branches/rack/hiki/config.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/config.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -13,6 +13,8 @@
 HIKI_RELEASE_DATE = Hiki::RELEASE_DATE
 
 require 'cgi'
+require 'hiki/request'
+require 'hiki/response'
 require 'hiki/command'
 
 module Hiki

Modified: hiki/branches/rack/hiki/filter.rb
===================================================================
--- hiki/branches/rack/hiki/filter.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/filter.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -46,7 +46,11 @@
 
       title =****@db*****_attribute(page, :title) || "" unless title
       title = page if title.empty?
-      new_page = PageData.new(page, text.gsub(/\r\n/, "\n"), title, (@cgi.params["keyword"][0]||"").gsub(/\r\n/, "\n").split(/\n/), @cgi.remote_addr)
+      new_page = PageData.new(page,
+                              text.gsub(/\r\n/, "\n"),
+                              title,
+                              (@cgi.params["keyword"] || "").gsub(/\r\n/, "\n").split(/\n/),
+                              @cgi.remote_addr)
 
       old_title =****@db*****_attribute(page, :title) || ""
       old_title = page if old_title.empty?

Modified: hiki/branches/rack/hiki/page.rb
===================================================================
--- hiki/branches/rack/hiki/page.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/page.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -52,10 +52,7 @@
 
     def out( headers = nil )
       @headers.update( headers ) if headers
-      print****@cgi*****( @headers )
-      if****@cgi*****_method != 'HEAD'
-        print @body
-      end
+      return Hiki::Response.new(@body, 200, @headers)
     end
   end
 end

Modified: hiki/branches/rack/hiki/plugin.rb
===================================================================
--- hiki/branches/rack/hiki/plugin.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/plugin.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -50,8 +50,8 @@
       @plugin_menu      = []
       @text             = ''
 
-      @mode = 'conf' if options['params']['c'][0] == 'admin'
-      @mode = 'saveconf' if options['params']['saveconf'][0]
+      @mode = 'conf' if options['params']['c'] == 'admin'
+      @mode = 'saveconf' if options['params']['saveconf']
 
       # loading plugins
       @plugin_files = []

Modified: hiki/branches/rack/hiki/request.rb
===================================================================
--- hiki/branches/rack/hiki/request.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/request.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -1,10 +1,10 @@
 # -*- coding: utf-8 -*-
 
 module Hiki
-  if Module.const_defined?(:CGI)
+  if Object.const_defined?(:CGI)
     # CGI を Rack::Request っぽいインターフェイスに変換する
     class Request
-      attr_reader :env
+      attr_reader :env, :cgi
       def initialize(env)
         @cgi = CGI.new
         @env = env
@@ -13,18 +13,58 @@
       def params
         return @params if @params
         @params = { }
-        @cgi.params.map{|k,v| @params[k] = v[0] }
+        @cgi.params.each{|k,v|
+          case v.size
+          when 0
+            @params[k] = nil
+          when 1
+            @params[k] = v[0]
+          else
+            @params[k] = v
+          end
+        }
         @params
       end
 
       def [](key)
-        params[key]
+        params[key.to_s]
       end
 
       def []=(key, val)
-        params[key] = val
+        params[key.to_s] = val
       end
 
+      def request_method
+        @env['REQUEST_METHOD']
+      end
+
+      def header(header)
+        @cgi.header(header)
+      end
+
+      def get?
+        request_method == 'GET'
+      end
+
+      def head?
+        request_method = 'HEAD'
+      end
+
+      def post?
+        request_method == 'POST'
+      end
+
+      def put?
+        request_method == 'PUT'
+      end
+
+      def delete?
+        request_method == 'DELETE'
+      end
+
+      def xhr?
+      end
+
       def accept_encoding
       end
 
@@ -43,10 +83,24 @@
         @env['CONTENT_TYPE']
       end
 
-      def cookies
+      def remote_addr
+        @env['REMOTE_ADDR']
       end
 
-      def delete?
+      def cookies
+        return @cookies if @cookies
+        @cookies = { }
+        @cgi.cookies.each{|k, v|
+          case v.size
+          when 0
+            @cookies[k] = nil
+          when 1
+            @cookies[k] = v[0]
+          else
+            @cookies[k] = v
+          end
+        }
+        @cookies
       end
 
       def form_data?
@@ -55,12 +109,6 @@
       def fullpath
       end
 
-      def get?
-      end
-
-      def head?
-      end
-
       def host
       end
 
@@ -96,12 +144,6 @@
       def port
       end
 
-      def post?
-      end
-
-      def put?
-      end
-
       def query_string
       end
 
@@ -109,10 +151,6 @@
       end
       alias referrer referer
 
-      def request_method
-        @env['REQUEST_METHOD']
-      end
-
       def schema
       end
 
@@ -127,11 +165,8 @@
 
       def values_at(*keys)
       end
-
-      def xhr?
-      end
     end
   else
-    Request = Rack::Request
+    Request = ::Rack::Request
   end
 end

Modified: hiki/branches/rack/hiki/util.rb
===================================================================
--- hiki/branches/rack/hiki/util.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki/util.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -118,9 +118,9 @@
         :end_after_change    => '</ins>',
       }
       if digest
-        return View.new( diff, src.encoding, src.eol ).to_html_digest(overriding_tags, false).join.gsub( %r|<br />|, '' ).gsub( %r|\n</ins>|, "</ins>\n" )
+        return View.new( diff, src.encoding, src.eol ).to_html_digest(overriding_tags, false).join.gsub( %r|<br />|, '' ).gsub( %r|\n</ins>|, "</ins>\n" ) #"
       else
-        return View.new( diff, src.encoding, src.eol ).to_html(overriding_tags, false).join.gsub( %r|<br />|, '' ).gsub( %r|\n</ins>|, "</ins>\n" )
+        return View.new( diff, src.encoding, src.eol ).to_html(overriding_tags, false).join.gsub( %r|<br />|, '' ).gsub( %r|\n</ins>|, "</ins>\n" ) #"
       end
     end
 
@@ -144,8 +144,7 @@
       header = {}
       header['cookie'] = cookies if cookies
       header['type'] = 'text/html'
-      print cgi.header(header)
-      print %Q[
+      body = %Q[
                <html>
                <head>
                <meta http-equiv="refresh" content="0;url=#{url}">
@@ -153,6 +152,7 @@
                </head>
                <body>Wait or <a href="#{url}">Click here!</a></body>
                </html>]
+      Hiki::Response.new(body, 200, header)
     end
 
     def sendmail(subject, body)

Modified: hiki/branches/rack/hiki.cgi
===================================================================
--- hiki/branches/rack/hiki.cgi	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/hiki.cgi	2009-08-23 15:02:03 UTC (rev 995)
@@ -21,7 +21,6 @@
   $:.delete(".") if File.writable?(".")
 
   require 'hiki/config'
-  require 'hiki/request'
   conf = Hiki::Config.new
   request = Hiki::Request.new(ENV)
 
@@ -34,15 +33,18 @@
     # hikiconf.rb.sample.en).
     #cgi = CGI.new(:accept_charset=>"euc-jp")
 
+    response = nil
     db = conf.database
     db.open_db {
       cmd = Hiki::Command.new(request, db, conf)
-      cmd.dispatch
+      response = cmd.dispatch
     }
+    print response.header
+    print response.body
   end
 rescue Exception => err
-  if cgi
-    print cgi.header( 'status' => '500 Internal Server Error', 'type' => 'text/html' )
+  if request
+    print request.cgi.header( 'status' => '500 Internal Server Error', 'type' => 'text/html' )
   else
     print "Status: 500 Internal Server Error\n"
     print "Content-Type: text/html\n\n"

Modified: hiki/branches/rack/plugin/00default.rb
===================================================================
--- hiki/branches/rack/plugin/00default.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/plugin/00default.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -265,20 +265,20 @@
   # dummy
 end
 
-if****@cgi*****['conf'][0] == 'theme' && @mode == 'saveconf'
-  @conf.theme          =****@cgi*****['theme'][0] || ''
-  @conf.use_sidebar    =****@cgi*****['sidebar'][0] == "true"
-  @conf.main_class     =****@cgi*****['main_class'][0]
+if****@cgi*****['conf'] == 'theme' && @mode == 'saveconf'
+  @conf.theme          =****@cgi*****['theme'] || ''
+  @conf.use_sidebar    =****@cgi*****['sidebar'] == "true"
+  @conf.main_class     =****@cgi*****['main_class']
   @conf.main_class     = 'main' if****@conf*****_class == ''
-  @conf.sidebar_class  =****@cgi*****['sidebar_class'][0]
+  @conf.sidebar_class  =****@cgi*****['sidebar_class']
   @conf.sidebar_class  = 'sidebar' if****@conf*****_class == ''
-  @conf.auto_link      =****@cgi*****['auto_link'][0] == "true"
-  @conf.use_wikiname   =****@cgi*****['use_wikiname'][0] == "true"
-  @conf.theme_url      =****@cgi*****['theme_url'][0]
-  @conf.theme_path     =****@cgi*****['theme_path'][0]
+  @conf.auto_link      =****@cgi*****['auto_link'] == "true"
+  @conf.use_wikiname   =****@cgi*****['use_wikiname'] == "true"
+  @conf.theme_url      =****@cgi*****['theme_url']
+  @conf.theme_path     =****@cgi*****['theme_path']
 end
 
-if****@cgi*****['conf'][0] == 'theme'
+if****@cgi*****['conf'] == 'theme'
   @conf_theme_list = []
   Dir.glob( "#{@conf.theme_path}/*".untaint ).sort.each do |dir|
     theme = File.basename( dir )
@@ -291,7 +291,7 @@
 # conf: XML-RPC
 def saveconf_xmlrpc
   if @mode == 'saveconf'
-    @conf.xmlrpc_enabled =****@cgi*****['xmlrpc_enabled'][0] == 'true'
+    @conf.xmlrpc_enabled =****@cgi*****['xmlrpc_enabled'] == 'true'
   end
 end
 

Modified: hiki/branches/rack/plugin/01sp.rb
===================================================================
--- hiki/branches/rack/plugin/01sp.rb	2009-08-23 15:02:00 UTC (rev 994)
+++ hiki/branches/rack/plugin/01sp.rb	2009-08-23 15:02:03 UTC (rev 995)
@@ -131,7 +131,7 @@
 end
 
 # things needed to configure this plugin
-if SP_PREFIX ==****@cgi*****['conf'][0]
+if SP_PREFIX ==****@cgi*****['conf']
   # list of plugins
   @sp_opt = sp_hash_from_dirs( @sp_path )
 
@@ -141,7 +141,7 @@
     @conf["#{SP_PREFIX}.selected"] = ''
     @conf["#{SP_PREFIX}.notselected"] = ''
     @sp_opt.each_key do |file|
-      if 't' ==****@cgi*****["#{SP_PREFIX}.#{file}"][0]
+      if 't' ==****@cgi*****["#{SP_PREFIX}.#{file}"]
         @conf["#{SP_PREFIX}.selected"] << "#{file}\n"
       else
         @conf["#{SP_PREFIX}.notselected"] << "#{file}\n"




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