[Hiki-cvs 1444] [991] add Hiki::Request

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 8月 24日 (月) 00:01:52 JST


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

Log Message:
-----------
add Hiki::Request

Modified Paths:
--------------
    hiki/branches/rack/hiki/command.rb
    hiki/branches/rack/hiki.cgi

Added Paths:
-----------
    hiki/branches/rack/hiki/request.rb

Modified: hiki/branches/rack/hiki/command.rb
===================================================================
--- hiki/branches/rack/hiki/command.rb	2009-08-23 13:56:22 UTC (rev 990)
+++ hiki/branches/rack/hiki/command.rb	2009-08-23 15:01:52 UTC (rev 991)
@@ -21,6 +21,7 @@
   end
 
   class Command
+    # TODO cgi -> request
     def initialize(cgi, db, conf)
       @db     = db
       @params = cgi.params
@@ -30,22 +31,22 @@
 
       # for TrackBack
       if %r|/tb/(.+)$| =~ ENV['REQUEST_URI']
-        @cgi.params['p'] = [CGI.unescape($1)]
-        @cgi.params['c'] = ['plugin']
-        @cgi.params['plugin'] = ['trackback_post']
+        @params['p'] = CGI.unescape($1)
+        @params['c'] = 'plugin'
+        @params['plugin'] = 'trackback_post'
       end
 
-      @cmd    = @params['c'][0]
+      @cmd    = @params['c']
       @p = case****@param*****
            when 0
              'FrontPage'
            when 1
-             @cmd ? nil : @params.keys[0]
+             @cmd ? nil : @params.keys.first
            else
              if @cmd == "create"
-               @params['key'][0] ? @params['key'][0] : nil
+               @params['key'] ? @params['key'] : nil
              else
-               @params['p'][0] ? @params['p'][0] : nil
+               @params['p'] ? @params['p'] : nil
              end
            end
 
@@ -98,9 +99,9 @@
             cmd_edit( @p )
           elsif @cmd == 'save'
             raise PermissionError, 'Permission denied' unles****@plugi*****?
-            if @params['save'][0]
-              cmd_save(@p, @params['contents'][0], @params['md5hex'][0], @params['update_timestamp'][0])
-            elsif @params['edit_form_button'][0]
+            if @params['save']
+              cmd_save(@p, @params['contents'], @params['md5hex'], @params['update_timestamp'])
+            elsif @params['edit_form_button']
               @cmd = 'edit'
               cmd_plugin(false)
               cmd_edit( @p, @plugin.text )
@@ -169,7 +170,7 @@
     def cmd_preview
       raise SessionError if****@plugi*****_id && @plugin.session_id != @cgi['session_id']
       @cmd = 'preview'
-      cmd_edit( @p, @params['contents'][0], @conf.msg_preview, @params['page_title'][0] )
+      cmd_edit( @p, @params['contents'], @conf.msg_preview, @params['page_title'] )
     end
 
     def cmd_view
@@ -189,7 +190,7 @@
       formatter =****@conf*****( tokens, @db, @plugin, @conf )
       contents, toc = formatter.to_s, formatter.toc
       if****@conf*****_keys
-        word = @params['key'][0]
+        word = @params['key']
         if word && word.size > 0
           contents = hilighten(contents, word.unescape.split)
         end
@@ -308,9 +309,9 @@
         formatter =****@conf*****( p, @db, @plugin, @conf )
         preview_text, toc = formatter.to_s, formatter.toc
         save_button = ''
-        data[:keyword] = h(@params['keyword'][0] || '')
-        data[:update_timestamp] = @params['update_timestamp'][0] ? ' checked' : ''
-        data[:freeze] = @params['freeze'][0] ? ' checked' : ''
+        data[:keyword] = h(@params['keyword'] || '')
+        data[:update_timestamp] = @params['update_timestamp'] ? ' checked' : ''
+        data[:freeze] = @params['freeze'] ? ' checked' : ''
       elsif @cmd == 'conflict'
         old = text.gsub(/\r/, '')
         new =****@db*****( page ) || ''
@@ -320,13 +321,13 @@
 
       @cmd = 'edit'
 
-      if rev = @params['r'][0]
+      if rev = @params['r']
         text =****@conf*****_revision(page, rev.to_i)
         raise 'No such revision.' if text.empty?
       else
         text = ( @db.load( page ) || '' ) unless text
       end
-      md5hex = @params['md5hex'][0] || @db.md5hex( page )
+      md5hex = @params['md5hex'] || @db.md5hex( page )
 
       @plugin.text = text
 
@@ -381,7 +382,7 @@
         data[:link]      = h(page)
         generate_page(data)
       else
-        title = @params['page_title'][0] ? @params['page_title'][0].strip : page
+        title = @params['page_title'] ? @params['page_title'].strip : page
         title = title.size > 0 ? title : page
 
         if exist?(title)
@@ -397,7 +398,7 @@
         end
 
         if****@plugi*****( page, text, md5hex, update_timestamp, false )
-          keyword = @params['keyword'][0].split("\n").collect {|k|
+          keyword = @params['keyword'].split("\n").collect {|k|
             k.chomp.strip}.delete_if{|k| k.size == 0}
           attr = [[:keyword, keyword.uniq], [:title, title]]
           attr << [:editor, @plugin.user]
@@ -408,13 +409,13 @@
           return
         end
 
-        @db.freeze_page( page, @params['freeze'][0] ? true : false) if****@plugi*****?
+        @db.freeze_page( page, @params['freeze'] ? true : false) if****@plugi*****?
         redirect(@cgi, @conf.base_url +****@plugi*****_url(page))
       end
     end
 
     def cmd_search
-      word = @params['key'][0]
+      word = @params['key']
       if word && word.size > 0
         total, l =****@db*****(word)
         if****@conf*****_keys
@@ -450,11 +451,11 @@
     end
 
     def cmd_create( msg = nil )
-      p = @params['key'][0]
+      p = @params['key']
       if p
         @p =****@alias*****_name(p).to_euc
         if /^\./ =~ @p || @p.size > @conf.max_name_size || @p.size == 0
-          @params['key'][0] = nil
+          @params['key'] = nil
           cmd_create( @conf.msg_invalid_filename( @conf.max_name_size) )
           return
         end
@@ -466,7 +467,7 @@
           s =****@db*****( @p )
           cmd_edit( orig_page || @p, s, @conf.msg_already_exist )
         else
-          cmd_edit( @p, @params['text'][0] )
+          cmd_edit( @p, @params['text'] )
         end
       else
         data           = get_common_data( @db, @plugin, @conf )
@@ -483,9 +484,9 @@
     end
 
     def cmd_login
-      name = @params['name'][0]
-      password = @params['password'][0]
-      page = @params['p'][0]
+      name = @params['name']
+      password = @params['password']
+      page = @params['p']
       msg_login_result = nil
       status = 'OK'
       if name && password
@@ -519,11 +520,11 @@
       raise PermissionError, 'Permission denied' unles****@plugi*****?
 
       data = get_common_data( @db, @plugin, @conf )
-      data[:key]            = h(@cgi.params['conf'][0] || 'default')
+      data[:key]            = h(@params['conf'] || 'default')
 
       data[:title]          = title( @conf.msg_admin )
       data[:session_id]     =****@plugi*****_id
-      if****@cgi*****['saveconf'][0]
+      if @params['saveconf']
         raise SessionError if****@plugi*****_id && @plugin.session_id != @cgi['session_id']
         data[:save_config]    = true
       end
@@ -555,7 +556,7 @@
 
     def cmd_plugin(redirect_mode = true)
       return unles****@conf*****_plugin
-      plugin = @params['plugin'][0]
+      plugin = @params['plugin']
 
       result = true
       if****@plugi*****_to?( plugin ) && !Object.method_defined?( plugin )

Added: hiki/branches/rack/hiki/request.rb
===================================================================
--- hiki/branches/rack/hiki/request.rb	                        (rev 0)
+++ hiki/branches/rack/hiki/request.rb	2009-08-23 15:01:52 UTC (rev 991)
@@ -0,0 +1,137 @@
+# -*- coding: utf-8 -*-
+
+module Hiki
+  if Module.const_defined?(:CGI)
+    # CGI を Rack::Request っぽいインターフェイスに変換する
+    class Request
+      attr_reader :env
+      def initialize(env)
+        @cgi = CGI.new
+        @env = env
+      end
+
+      def params
+        return @params if @params
+        @params = { }
+        @cgi.params.map{|k,v| @params[k] = v[0] }
+        @params
+      end
+
+      def [](key)
+        params[key]
+      end
+
+      def []=(key, val)
+        params[key] = val
+      end
+
+      def accept_encoding
+      end
+
+      def body
+      end
+
+      def content_charset
+        @env['CONTENT_CHARSET']
+      end
+
+      def content_length
+        @env['CONTENT_LENGTH']
+      end
+
+      def content_type
+        @env['CONTENT_TYPE']
+      end
+
+      def cookies
+      end
+
+      def delete?
+      end
+
+      def form_data?
+      end
+
+      def fullpath
+      end
+
+      def get?
+      end
+
+      def head?
+      end
+
+      def host
+      end
+
+      def ip
+      end
+
+      def media_type
+      end
+
+      def media_type_params
+      end
+
+      def openid_request
+        raise 'not implemented'
+      end
+
+      def openid_response
+        raise 'not implemented'
+      end
+
+      def parseable_data?
+      end
+
+      def path
+      end
+
+      def path_info
+      end
+
+      def path_info=(s)
+      end
+
+      def port
+      end
+
+      def post?
+      end
+
+      def put?
+      end
+
+      def query_string
+      end
+
+      def referer
+      end
+      alias referrer referer
+
+      def request_method
+        @env['REQUEST_METHOD']
+      end
+
+      def schema
+      end
+
+      def script_name
+      end
+
+      def session_options
+      end
+
+      def url
+      end
+
+      def values_at(*keys)
+      end
+
+      def xhr?
+      end
+    end
+  else
+    Request = Rack::Request
+  end
+end

Modified: hiki/branches/rack/hiki.cgi
===================================================================
--- hiki/branches/rack/hiki.cgi	2009-08-23 13:56:22 UTC (rev 990)
+++ hiki/branches/rack/hiki.cgi	2009-08-23 15:01:52 UTC (rev 991)
@@ -21,7 +21,9 @@
   $:.delete(".") if File.writable?(".")
 
   require 'hiki/config'
+  require 'hiki/request'
   conf = Hiki::Config.new
+  request = Hiki::Request.new(ENV)
 
   if ENV['CONTENT_TYPE'] =~ %r!\Atext/xml!i and ENV['REQUEST_METHOD'] =~ /\APOST\z/i
     require 'hiki/xmlrpc'
@@ -30,11 +32,11 @@
   else
     # FIXME encoding can be different (eg. iso-8859-1 in
     # hikiconf.rb.sample.en).
-    cgi = CGI.new(:accept_charset=>"euc-jp")
+    #cgi = CGI.new(:accept_charset=>"euc-jp")
 
     db = conf.database
     db.open_db {
-      cmd = Hiki::Command.new( cgi, db, conf )
+      cmd = Hiki::Command.new(request, db, conf)
       cmd.dispatch
     }
   end




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