ruby-box-007 に 添付 (というか これが ruby-box の main)
tiki の修正
HOST OS の ruby で試すには ...
たぶんこれで動くはず。
dehehe.rb あらため init.rb
=begin ./etc/tiki.conf << EOF $TIKIDIR="./tiki/"; # tiki installed directory path $USETIKICONF=true; # if you use tiki.conf, to true $ROOTDIR="./tiki"; # absolute path to place data files $LOGO='/icons/suz.gif'; # put logo file to world readable directory $CGIURL=""; # CGI PATH in URL $LANG = "jp"; # "C":English, "jp":Japanese, "tw":Big5 $usegmtime = false; # false if you want to use local time zone $USE_EUCFILENAME=true; # use euc kanji file name EOF ./tiki/text :copy from tiki ./tiki/data :copy from tiki ./tiki/plugin :copy from tiki mkdir ./tiki/backup tiki-patch : @ check CGI class (if !defined(CGI) then load cgi) @ change $TIKICONFIGFILE @ add $USE_EUCFILENAME @ change using ENV -> cgi.env_table if (defined RUBY_BOX) @ add parameter Tiki.process tiki-1.4.5 patch << EOF --- tiki-1.4.5/cgi-bin/tiki.cgi Tue Feb 19 15:31:15 2002 +++ ruby-box-006c/bin/tiki.rb Sun Mar 10 20:57:32 2002 @@ -3,7 +3,10 @@ # REWRITE ruby path like # check REWRITE mark for your host # ruby 1.4.5 or later required + +if !defined?(CGI) require 'cgi'; +end $nkf = false; # remove this block if you don't need nkf. @@ -705,7 +708,10 @@ @ATTRDIR = File.join(dir, 'attr'); @BACKUP = $USERCS ? RCSBackup.new(dir) : SeparatorBackup.new(dir); end - def textname(name) (File.join (@TEXTDIR, Tiki::escape(name))).untaint end + def textname(name) + (File.join (@TEXTDIR, Tiki::escape(name))).untaint if (! $USE_EUCFILENAME); + (File.join (@TEXTDIR, name.gsub(/\//,'%2F'))).untaint; + end def mtime(name) return (!exists(name)) ? nil: File.mtime(textname(name)); end def exists(name) test(?e, textname(name)); end def isreadable(name) test(?r, textname(name)); end @@ -1304,6 +1310,7 @@ $REFERER_SHOW_UNREGISTERED=false; $REFERER_IGNORED_LIST=[]; $DISPLAYWIKILINKS=false; +$USE_EUCFILENAME=false; $USERCS = false; # if you prefer absolute path to rcs commands, set this, it should be end with '/' $RCSBIN = ''; @@ -1345,7 +1352,7 @@ $htmlunescape = { '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"'}; $TIKIVERSION = '1.4.5'; -$TIKICONFIGFILE = 'tiki.conf'; +$TIKICONFIGFILE = 'etc/tiki.conf'; $delB = '<S>'; $delE = '</S>'; $addB = '<B>'; $addE = '</B>'; @@ -1374,7 +1381,7 @@ attr_reader :cgi, :out, :repository, :cmd, :page, :timestamp, :plugins, :session, :referer, :now, :remote_addr, :sid; attr_accessor :is_contents_context, :lastmodified_is_now, :result, :footer, :filetimestamp, :httpstatus, :httpheader, :wikilinks, :plugins; -def process +def process(cgi=nil) # print "Content-Type: text/html\r\n\r\n"; begin @now = Time.now; @@ -1382,7 +1389,8 @@ @httpstatus = "HTTP/1.0 200 OK\r\n"; @httpheader = []; @result = []; @footer = []; - @cgi = CGI.new; + @cgi = cgi; + @cgi = CGI.new if (cgi == nil); @cmd = @cgi['c'][0]; @page = @cgi['ep'][0]; # ep is escaped name (see edit.html) in case of POST and type hidden; @page = (@page == nil || @page == '') ? @cgi['p'][0]: Tiki::unescape(@page); @@ -1399,8 +1407,13 @@ @plugins.beginsession(@session = @plugins.newsession(self)); @is_contents_context = false; @lastmodified_is_now = false; - @referer = ENV['HTTP_REFERER']; - @remote_addr = ENV['REMOTE_ADDR']; + if defined?(RUBY_BOX) + @referer = cgi.env['HTTP_REFERER']; + @remote_addr = cgi.env['REMOTE_ADDR']; + else + @referer = ENV['HTTP_REFERER']; + @remote_addr = ENV['REMOTE_ADDR']; + end @wikilinks = []; case @cmd EOF =end require 'socket' require 'cgi' require 'timeout' #load "bin/magic.rb" require 'magic' $magic = Magic.new(); class LocalCGI < CGI attr_reader :out, :in, :env def initialize(_in, _out, _env) @in = _in; @out = _out; @env = _env; extend QueryExtension @params = _env['CGI_PARAMS']; @cookies = _env['CGI_COOKIES']; initialize_query() # set @params, @cookies _env['CGI_PARAMS'] = @params.nil? ? nil : @params.dup _env['CGI_COOKIES'] = @cookies.nil? ? nil : @cookies.dup @output_cookies = nil @output_hidden = nil print stdoutput,"\n"; end def env_table @env end def stdinput @in end def stdoutout @out end def write (str) @out.write(str) end def gets @in.gets end end class AyashiiServer # サーバーの初期化。 def initialize(port) @serverSocket = TCPserver.open(port) @svr_port = port; @svr_name = Socket::gethostname; @handle_tab = []; end # ハンドラの登録 def add_handler(level,re_method,re_path, re_prot, re_host,handle) @handle_tab << [ level, re_method, re_path, re_prot, re_host, handle ] end def sort_handler @handle_tab.sort! end # サーバーのスタート def start while true begin socket = @serverSocket.accept print "STARTT session\n"; process(socket) print "END session\n"; # rescue # print "an error occered.\n" end end end # 一つのセッションを処理する。 def process(s) reader = nil; begin env = {}; method, path , prot, host_port = readRequest(s,env) #print "env dump\n" #env.each {|key,val| #print sprintf(" key=%s val=%s\n",key,val); #} #print "end env dump\n" cgi = LocalCGI.new(s,s,env) @handle_tab.each {|t| print t[5].to_s,"\n"; if (t[1] =~ method && t[2] =~ path && t[3] =~ prot && t[4] =~ host_port) t[5]::handler(cgi, method, path, prot, host_port) break; end } ensure s.close reader.close if (reader != nil) end end # リクエストを読みハンドラ起動 def readRequest(socket,head) action = '' method = '' svr = socket.addr; cli = socket.peeraddr; head['REMOTE_ADDR'] = cli[3]; head['REMOTE_NAME'] = cli[2]; head['REMOTE_PORT'] = cli[1]; head['SERVER_ADDR'] = svr[3]; head['SERVER_NAME'] = svr[2]; head['SERVER_PORT'] = svr[1]; head['SERVER_SOFTWARE'] = RUBY_BOX; head['SERVER_PROTOCOL'] = 'HTTP/1.1'; while(true) str = nil begin timeout(1) { str = socket.gets } rescue TimeoutError raise end break unless str str.chop! if str =~ /^(get|post) (([a-z]+):\/\/([^\s\/]+)|)\/([\S^]*)/i then method = $1.upcase; uri = '/'; uri << $5 if ($5 != nil); prot = "http"; prot = $3.downcase if ($3 != nil); host_port = ''; host_port = $4 if ($4 != nil); head['REQUEST_URI'] = uri; path = uri; query = ''; if (uri =~ /^\/([\S\?]*)\?([\S]*)/) then path = '/' + $1; query = $2; end head['REQUEST_METHOD'] = method; head['SCRIPT_NAME'] = path; head['QUERY_STRING'] = query; elsif str =~ /^(\S+):\s*(.*)$/ then val = $2; key = $1.upcase key.gsub!(/-/n, '_') head[key] = val; elsif str =~ /^\s*$/i then break end end # content = '' # if head['CONTENT_LENGTH'] && head['CONTENT_LENGTH'].to_i > 0 then # content = socket.read(head['CONTENT_LENGTH'].to_i) # end return [method, path, prot , host_port] end end $0 = "not file"; load "./bin/tiki.rb" RUBY_BOX = "ruby-box (version 0.7)"; $server = AyashiiServer.new(8888); #load "./dehehe.rb" # Dhhehe クラス class Dehehe def Dehehe.handler(cgi, method, path, prot, host_port) stat = {}; stat['status'] = "200 OK"; stat['type'] = "text/plain"; header = cgi.header(stat); cgi.write header; cgi.write header; cgi.write "method :" + method + "\n"; cgi.write "path :" + path + "\n"; cgi.write "host+p :" + host_port + "\n"; cgi.write "----AAA\n"; cgi.env_table.each {|key,val| cgi.write (sprintf("%s=%s\n",key,val)); } cgi.write "----AAA\n"; # while (str = cgi.gets) # print str; # end cgi.write "----AAA\n"; end end #load "./bin/http.rb" class Http def self.view_file(cgi,path,uri) stat = {}; stat['status'] = "200 OK"; stat['type'] = $magic.file(path); header = cgi.header(stat); io = File::open(path, 'r') if (io == nil) stat['status'] = "FORBIDDEN"; cgi.write header; return nil; end cgi.write header; while (str = io.gets) cgi.write(str); end io.close(); return nil; end def self.view_dir(cgi,path,uri) stat = {}; stat['status'] = "200 OK"; stat['type'] = "text/html"; dir = Dir::entries(path); if (dir == nil) stat['status'] = "FORBIDDEN"; cgi.write header; return nil; end header = cgi.header(stat); cgi.write header; cgi.write "<HTML>\n<BODY>\n"; cgi.write "----- " + path + "\n"; cgi.write "<HR><TABLE>"; dir.each {|file| fname = (path + "/" + file); page_name = CGI::unescape(file); begin s = File::stat(fname); rescue next; end if (s.directory?) type = "DIR"; elsif (s.file?) type = $magic.file(fname); else next; end len = (s.size + 1023) / 1024; tm = s.mtime.strftime("%y%m%d/%H:%M"); cgi.write "<TR><TD>" cgi.write type + "</TD><TD>"; cgi.write len.to_s + "</TD><TD>"; cgi.write tm + "</TD><TD>"; cgi.write "<A HREF=\""+uri+"/" + CGI::escape(file) + "\">" + page_name + "</A>" cgi.write "</TD></TR>\n"; } cgi.write "</TABLE><HR>"; cgi.write "\n</BODY>\n</HTML>\n"; end def self.handler(cgi, method, path, prot, host_port) stat = {}; stat['type'] = "text/html"; path = "." + path = CGI::unescape(path); if (path =~ /^\/tiki\/(text|backup)\//) dir = $&; text = $'; path = dir + Tiki::textname(text); end; path = "." + path; print "page = ",path; uri = cgi.env_table['REQUEST_URI']; begin s = File::stat(path); rescue stat['status'] = "NOT_FOUND"; header = cgi.header(stat); cgi.write header; return nil; end if (s.file?) self::view_file(cgi,path,uri); elsif (s.directory?) self::view_dir(cgi,path,uri); else stat['status'] = "NOT_ACCEPTABLE"; header = cgi.header(stat); cgi.write header; end end end def Tiki.handler(cgi, method, path, prot, host_port) tiki = Tiki.new; # invoke as cgi tiki.process(cgi); if (tiki.result == []) then tiki.internalerror('no content'); end cgi.write tiki.httpheader ; cgi.write tiki.result ; cgi.write tiki.footer ; return nil; end # pri method path prot host_port Class $server.add_handler( 1, /.*/ , /^\/$/ , /^http$/ , /^$/ , Tiki ); $server.add_handler( 5, /GET/ , /.*/, /^http$/ , /^$/ , Http ); $server.add_handler(99, /.*/ , /.*/ , /^http$/ , /^$/ , Dehehe ); $server.sort_handler $server.start;