lib/hayabusa_cgi_session.rb in hayabusa-0.0.1 vs lib/hayabusa_cgi_session.rb in hayabusa-0.0.2

- old
+ new

@@ -8,11 +8,27 @@ @config = @hb.config @handlers_cache = @config[:handlers_cache] cgi_conf = @config[:cgi] @get, @post, @meta, @headers = cgi_conf[:get], cgi_conf[:post], cgi_conf[:meta], cgi_conf[:headers] + @browser = Knj::Web.browser(@meta) + #Parse cookies and other headers. + @cookie = {} + @headers.each do |key, val| + #$stderr.puts "Header-Key: '#{key}'." + + case key + when "COOKIE" + Knj::Web.parse_cookies(val).each do |key, val| + @cookie[key] = val + end + end + end + + + #Set up the 'out', 'written_size' and 'size_send' variables which is used to write output. if cgi_conf[:cgi] @out = cgi_conf[:cgi] else @out = $stdout end @@ -34,30 +50,66 @@ http_version = "1.#{match[1]}" Dir.chdir(@config[:doc_root]) @page_path = @meta["PATH_TRANSLATED"] + @page_path = "index.rhtml" if @page_path == "/" + @ext = File.extname(@page_path).downcase[1..-1].to_s @resp = Hayabusa::Http_session::Response.new(:socket => self) @resp.reset(:http_version => http_version, :mode => :cgi) - @resp.header("Content-Type", "text/html") @cgroup = Hayabusa::Http_session::Contentgroup.new(:socket => self, :hb => @hb, :resp => @resp, :httpsession => self) @cgroup.reset @resp.cgroup = @cgroup + @resp.header("Content-Type", "text/html") + + #Set up session-variables. + if @cookie["HayabusaSession"].to_s.length > 0 + @session_id = @cookie["HayabusaSession"] + elsif @browser["browser"] == "bot" + @session_id = "bot" + else + @session_id = @hb.session_generate_id(@meta) + send_cookie = true + end + begin + @session, @session_hash = @hb.session_fromid(@ip, @session_id, @meta) + rescue ArgumentError => e + #User should not have the session he asked for because of invalid user-agent or invalid IP. + @session_id = @hb.session_generate_id(@meta) + @session, @session_hash = @hb.session_fromid(@ip, @session_id, @meta) + send_cookie = true + end + + if send_cookie + @resp.cookie( + "name" => "HayabusaSession", + "value" => @session_id, + "path" => "/", + "expires" => Time.now + 32140800 #add around 12 months + ) + end + + raise "'session'-variable could not be spawned." if !@session + raise "'session_hash'-variable could not be spawned." if !@session_hash + Thread.current[:hayabusa][:session] = @session + + + begin @hb.events.call(:request_begin, :httpsession => self) if @hb.events Timeout.timeout(@hb.config[:timeout]) do if @handlers_cache.key?(@ext) STDOUT.print "Calling handler.\n" if @debug @handlers_cache[@ext].call(self) else - raise "CGI-mode shouldnt serve static files." + raise "CGI-mode shouldnt serve static files: '#{@page_path}'." end end @cgroup.mark_done @cgroup.write_output @@ -116,10 +168,9 @@ def init_thread Thread.current[:hayabusa] = { :hb => @hb, :httpsession => self, - :session => @session, :get => @get, :post => @post, :meta => @meta, :cookie => @cookie } \ No newline at end of file