Class: Hayabusa::Cgi_session

Inherits:
Client_session show all
Defined in:
lib/hayabusa_cgi_session.rb

Instance Attribute Summary collapse

Attributes inherited from Client_session

#request_hash

Instance Method Summary collapse

Methods inherited from Client_session

#add_size, #create_binding, #execute_done, #execute_page, #force_content, #force_fileread, #init_thread, #modified_since, #threadded_content, #write

Constructor Details

#initialize(args) ⇒ Cgi_session

Returns a new instance of Cgi_session.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hayabusa_cgi_session.rb', line 5

def initialize(args)
  @args = args
  @hb = @args[:hb]
  
  @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
  
  @written_size = 0
  @size_send = @config[:size_send]
  
  @eruby = Knj::Eruby.new(
    :cache_hash => @hb.eruby_cache,
    :binding_callback => self.method(:create_binding)
  )
  
  self.init_thread
  
  
  #Parse URI (page_path and get).
  match = @meta["SERVER_PROTOCOL"].match(/^HTTP\/1\.(\d+)\s*/)
  raise "Could match HTTP-protocol from: '#{@meta["SERVER_PROTOCOL"]}'." if !match
  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, :cookie => @cookie)
  
  @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.empty?
    @session_id = @cookie["HayabusaSession"]
  elsif @browser["browser"] == "bot"
    @session_id = "bot"
  else
    @session_id = @hb.session_generate_id(@meta)
    send_cookie = true
  end
  
  #Set the 'ip'-variable which is required for sessions.
  @ip = @hb.ip(:meta => @meta)
  raise "No 'ip'-variable was set: '#{@meta}'." if !@ip
  raise "'session_id' was not valid." if @session_id.to_s.strip.empty?
  
  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
    self.execute_page
    self.execute_done
  rescue SystemExit
    #do nothing - ignore.
  rescue Timeout::Error
    @resp.status = 408
  end
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def active
  @active
end

#alert_sentObject

Returns the value of attribute alert_sent.



2
3
4
# File 'lib/hayabusa_cgi_session.rb', line 2

def alert_sent
  @alert_sent
end

#browserObject (readonly)

Returns the value of attribute browser.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def browser
  @browser
end

#cgroupObject (readonly)

Returns the value of attribute cgroup.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def cgroup
  @cgroup
end

Returns the value of attribute cookie.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def cookie
  @cookie
end

#dataObject

Returns the value of attribute data.



2
3
4
# File 'lib/hayabusa_cgi_session.rb', line 2

def data
  @data
end

#debugObject (readonly)

Returns the value of attribute debug.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def debug
  @debug
end

#erubyObject (readonly)

Returns the value of attribute eruby.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def eruby
  @eruby
end

#getObject (readonly)

Returns the value of attribute get.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def get
  @get
end

#hbObject (readonly)

Returns the value of attribute hb.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def hb
  @hb
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def headers
  @headers
end

#httpsession_varObject (readonly)

Returns the value of attribute httpsession_var.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def httpsession_var
  @httpsession_var
end

#ipObject (readonly)

Returns the value of attribute ip.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def ip
  @ip
end

#metaObject (readonly)

Returns the value of attribute meta.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def meta
  @meta
end

#outObject (readonly)

Returns the value of attribute out.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def out
  @out
end

#page_pathObject

Returns the value of attribute page_path.



2
3
4
# File 'lib/hayabusa_cgi_session.rb', line 2

def page_path
  @page_path
end

#postObject (readonly)

Returns the value of attribute post.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def post
  @post
end

#respObject (readonly)

Returns the value of attribute resp.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def resp
  @resp
end

#sessionObject (readonly)

Returns the value of attribute session.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def session
  @session
end

#session_hashObject (readonly)

Returns the value of attribute session_hash.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def session_hash
  @session_hash
end

#session_idObject (readonly)

Returns the value of attribute session_id.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def session_id
  @session_id
end

#workingObject (readonly)

Returns the value of attribute working.



3
4
5
# File 'lib/hayabusa_cgi_session.rb', line 3

def working
  @working
end

Instance Method Details

#handlerObject



117
118
119
# File 'lib/hayabusa_cgi_session.rb', line 117

def handler
  return self
end