== dehehe.rb
require 'socket' #require 'cgi' not yet require 'timeout' ServerVersion = "Dehehe"; class AyashiiServer # サーバーの初期化。 def initialize(port) @serverSocket = TCPserver.open(port) @handle_tab = []; end # ハンドラの登録 def add_handler(level,re_method,re_path,re_host,handle) @handle_tab << [ level, re_method, re_path, re_host, handle ] 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(socket) begin head, method, path , host , content = readRequest(socket) @handle_tab.each {|t| print t[4].to_s,"\n"; if (t[1] =~ method && t[2] =~ path && t[3] =~ host) @response = t[4]::handler(head, method, path, content) break; end } sendResponse(socket, @response) ensure socket.close end end # リクエストを読みハンドラ起動 def readRequest(socket) action = '' method = '' head = {} while(true) str = nil begin timeout(1) { str = socket.gets } rescue TimeoutError raise end break unless str str.chop! if str =~ /^(get|post) \/([\S^]*)/i then method = $1; uri = $2; 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 key = $1.downcase head[key] = $2; elsif str =~ /^\s*$/i then break end end host = head['host']; content = '' if head['content-length'] && head['content-length'].to_i > 0 then content = socket.read(head['content-length'].to_i) end return [head, method, path, host, content] end # レスポンスを送信する def sendResponse(socket,result) if result.type == Array then head = result[0] content = result[1] else head = {} content = result end head['Content-length'] = content.length.to_s head['Content-Type'] = 'text/html' head['Cache-Control'] = 'no-cache' head['Date'] = Time.new.to_s head['Server'] = ServerVersion header = '' head.each{|k,v| header += k + ": " + v + "\r\n" } socket.write("HTTP/1.1 200 OK\r\n") socket.write(header + "\r\n" + content) end end $server = AyashiiServer.new(8887); #load "./viewtext.rb" # ViewText クラス class ViewText def self.view_file(path,uri) ret = ''; io = File::open(path, 'r') ret.concat "<HTML>\n<BODY><CENTER><SCRIPT language='javascript'><!-- document.write('<A HREF=http://www.xrea.com/ TARGET=_blank><IMG SRC=http://img0.mysuite.net/~cgi/xrea/banner_top_1.gif WIDTH=468 HEIGHT=10 BORDER=0></A><BR>'); //--></SCRIPT> <IFRAME HEIGHT=60 WIDTH=468 FRAMEBORDER=0 MARGINHEIGHT=0 MARGINWIDTH=0 SCROLLING=NO ALLOWTRANSPARENCY=TRUE SRC=http://ad.xrea.com/ad_iframe.fcg> <A HREF=http://ad.xrea.com/ad_click.fcg TARGET=_BLANK><IMG SRC=http://ad.xrea.com/ad_img.fcg BORDER=0></A> </IFRAME></CENTER> \n<PRE>\n"; while (io.gets) ret.concat($_); end ret.concat "</PRE>\n</BODY>\n</HTML>\n"; return ret; end def self.view_dir(path,uri) ret = ''; ret.concat "<HTML>\n<BODY>\n"; ret.concat "----- " + path + "\n"; dir = Dir::entries(path); dir.each {|file| ret.concat "<A HREF=\""+uri+"/" + file + "\">" + file + "</A>\n"; } ret.concat "----\n"; ret.concat "\n</BODY>\n</HTML>\n"; return ret; end def self.handler(head, method, path, content) uri = head['REQUEST_URI']; path = "./" + path; ret = ''; print "path = ",path,"\n"; s = File::stat(path); return ret if (s == nil || !s.readable?); if (s.file?) print "view_file\n"; return self::view_file(path,uri); elsif (s.directory?) print "view_dir\n"; return self::view_dir(path,uri); end end end #load "./dehehe.rb" # Dhhehe クラス class Dehehe def Dehehe.handler(head, method, path, content) ret = ''; ret.concat "<HTML>\n<BODY>\n<PRE>\n"; ret.concat "----AAA\n"; head.each {|key,val| ret.concat(sprintf("%s=%s\n",key,val)); } ret.concat "----AAA\n"; ret.concat "</PRE>\n</BODY>\n</HTML>\n"; return ret; end end #server.add_handler( 1, /.*/ , /^$/ , /.*/ , Tiki ); $server.add_handler( 5, /GET/ , /^(lib|text)/, /.*/ , ViewText ); $server.add_handler(99, /.*/ , /.*/ , /.*/ , Dehehe ); $server.start;