Class: Hayabusa::Client_session

Inherits:
Object
  • Object
show all
Defined in:
lib/hayabusa_client_session.rb

Overview

Various client-sessions should extend this class.

Direct Known Subclasses

Cgi_session, Http_session

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def active
  @active
end

#alert_sentObject

Returns the value of attribute alert_sent.



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

def alert_sent
  @alert_sent
end

#browserObject (readonly)

Returns the value of attribute browser.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def browser
  @browser
end

#cgroupObject (readonly)

Returns the value of attribute cgroup.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def cgroup
  @cgroup
end

Returns the value of attribute cookie.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def cookie
  @cookie
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#debugObject (readonly)

Returns the value of attribute debug.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def debug
  @debug
end

#erubyObject (readonly)

Returns the value of attribute eruby.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def eruby
  @eruby
end

#getObject (readonly)

Returns the value of attribute get.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def get
  @get
end

#handlerObject (readonly)

Returns the value of attribute handler.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def handler
  @handler
end

#hbObject (readonly)

Returns the value of attribute hb.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def hb
  @hb
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def headers
  @headers
end

#httpsession_varObject (readonly)

Returns the value of attribute httpsession_var.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def httpsession_var
  @httpsession_var
end

#ipObject (readonly)

Returns the value of attribute ip.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def ip
  @ip
end

#metaObject (readonly)

Returns the value of attribute meta.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def meta
  @meta
end

#outObject (readonly)

Returns the value of attribute out.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def out
  @out
end

#page_pathObject

Returns the value of attribute page_path.



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

def page_path
  @page_path
end

#postObject (readonly)

Returns the value of attribute post.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def post
  @post
end

#request_hashObject (readonly)

Returns the value of attribute request_hash.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def request_hash
  @request_hash
end

#respObject (readonly)

Returns the value of attribute resp.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def resp
  @resp
end

#sessionObject (readonly)

Returns the value of attribute session.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def session
  @session
end

#session_hashObject (readonly)

Returns the value of attribute session_hash.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def session_hash
  @session_hash
end

#session_idObject (readonly)

Returns the value of attribute session_id.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def session_id
  @session_id
end

#workingObject (readonly)

Returns the value of attribute working.



4
5
6
# File 'lib/hayabusa_client_session.rb', line 4

def working
  @working
end

Instance Method Details

#add_size(size) ⇒ Object

Is called when content is added and begings to write the output if it goes above the limit.



39
40
41
42
# File 'lib/hayabusa_client_session.rb', line 39

def add_size(size)
  @written_size += size
  @cgroup.write_output if @written_size >= @size_send
end

#create_bindingObject

Creates a new Hayabusa::Binding-object and returns the binding for that object.



34
35
36
# File 'lib/hayabusa_client_session.rb', line 34

def create_binding
  return Hayabusa::Http_session::Page_environment.new(:httpsession => self, :hb => @hb).get_binding
end

#execute_doneObject



152
153
154
155
156
157
158
159
160
# File 'lib/hayabusa_client_session.rb', line 152

def execute_done
  @cgroup.mark_done
  @cgroup.write_output
  @hb.log_puts "#{__id__} - Served '#{@meta["REQUEST_URI"]}' in #{Time.now.to_f - @time_start} secs (#{@resp.status})." if @debug
  @time_start = nil
  @cgroup.join
  @hb.events.call(:request_done, :httpsession => self) if @hb.events
  @httpsession_var = {}
end

#execute_pageObject



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hayabusa_client_session.rb', line 84

def execute_page
  @request_hash = {}
  
  begin
    @time_start = Time.now.to_f if @debug
    @hb.events.call(:request_begin, :httpsession => self) if @hb.events
    
    Timeout.timeout(@hb.config[:timeout]) do
      if @handlers_cache.key?(@ext)
        @hb.log_puts("Calling handler.") if @debug
        @handlers_cache[@ext].call(self)
        @hb.log_puts("Called handler.") if @debug
      else
        #check if we should use a handler for this request.
        @config[:handlers].each do |handler_info|
          if handler_info.key?(:file_ext) and handler_info[:file_ext] == @ext
            handler_info[:callback].call(self)
            break
          elsif handler_info.key?(:path) and handler_info[:mount] and @meta["SCRIPT_NAME"].slice(0, handler_info[:path].length) == handler_info[:path]
            @page_path = "#{handler_info[:mount]}#{@meta["SCRIPT_NAME"].slice(handler_info[:path].length, @meta["SCRIPT_NAME"].length)}"
            break
          elsif handler_info.key?(:regex) and @meta["REQUEST_URI"].to_s.match(handler_info[:regex])
            handler_info[:callback].call(:httpsession => self)
            break
          end
        end
        
        if @page_path
          if !File.exists?(@page_path)
            @resp.status = 404
            @resp.header("Content-Type", "text/html")
            @cgroup.write("File you are looking for was not found: '#{@meta["REQUEST_URI"]}'.")
          else
            if @headers["cache-control"] and @headers["cache-control"][0]
              cache_control = {}
              @headers["cache-control"][0].scan(/(.+)=(.+)/) do |match|
                cache_control[match[1]] = match[2]
              end
            end
            
            cache_dont = true if cache_control and cache_control.key?("max-age") and cache_control["max-age"].to_i <= 0
            lastmod = File.mtime(@page_path)
            
            @resp.header("Last-Modified", lastmod.httpdate)
            @resp.header("Expires", (Time.now + 86400).httpdate) #next day.
            
            if !cache_dont and @headers["if-modified-since"] and @headers["if-modified-since"][0]
              request_mod = Datet.in(@headers["if-modified-since"].first).time
              
              if request_mod == lastmod
                @resp.status = 304
                return nil
              end
            end
            
            self.force_fileread(@page_path)
          end
        end
      end
    end
  rescue SystemExit
    #do nothing - ignore.
  rescue Timeout::Error
    @resp.status = 408
    @hb.log_puts "The request timed out."
  end
end

#force_content(newcont) ⇒ Object

Forces the content to be the input - nothing else can be added after calling this.



21
22
23
# File 'lib/hayabusa_client_session.rb', line 21

def force_content(newcont)
  @cgroup.force_content(newcont)
end

#force_fileread(fpath) ⇒ Object

Forces the output to be read from a file.



26
27
28
29
30
31
# File 'lib/hayabusa_client_session.rb', line 26

def force_fileread(fpath)
  raise "Invalid filepath given: '#{fpath}'." if !fpath || !File.exists?(fpath)
  @resp.chunked = false
  @resp.header("Content-Length", File.size(fpath))
  @cgroup.new_io(:type => :file, :path => fpath)
end

#init_threadObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/hayabusa_client_session.rb', line 73

def init_thread
  Thread.current[:hayabusa] = {} if !Thread.current[:hayabusa]
  Thread.current[:hayabusa][:hb] = @hb
  Thread.current[:hayabusa][:httpsession] = self
  Thread.current[:hayabusa][:session] = @session
  Thread.current[:hayabusa][:get] = @get
  Thread.current[:hayabusa][:post] = @post
  Thread.current[:hayabusa][:meta] = @meta
  Thread.current[:hayabusa][:cookie] = @cookie
end

#modified_sinceObject

Parses the if-modified-since header and returns it as a Time-object. Returns false is no if-modified-since-header is given or raises an RuntimeError if it cant be parsed.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hayabusa_client_session.rb', line 7

def modified_since
  return @modified_since if @modified_since
  return false if !@meta["HTTP_IF_MODIFIED_SINCE"]
  
  mod_match = @meta["HTTP_IF_MODIFIED_SINCE"].match(/^([A-z]+),\s+(\d+)\s+([A-z]+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(.+)$/)
  raise "Could not parse 'HTTP_IF_MODIFIED_SINCE'." if !mod_match
  
  month_no = Datet.month_str_to_no(mod_match[3])
  @modified_since = Time.utc(mod_match[4].to_i, month_no, mod_match[2].to_i, mod_match[5].to_i, mod_match[6].to_i, mod_match[7].to_i)
  
  return @modified_since
end

#threadded_content(block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hayabusa_client_session.rb', line 49

def threadded_content(block)
  raise "No block was given." if !block
  cgroup = Thread.current[:hayabusa][:contentgroup].new_thread
  
  Thread.new do
    begin
      self.init_thread
      cgroup.register_thread
      
      @hb.db_handler.get_and_register_thread if @hb and @hb.db_handler.opts[:threadsafe]
      @hb.ob.db.get_and_register_thread if @hb and @hb.ob.db.opts[:threadsafe]
      
      block.call
    rescue Exception => e
      Thread.current[:hayabusa][:contentgroup].write Knj::Errors.error_str(e, {:html => true})
      _hb.handle_error(e)
    ensure
      Thread.current[:hayabusa][:contentgroup].mark_done
      @hb.ob.db.free_thread if @hb and @hb.ob.db.opts[:threadsafe]
      @hb.db_handler.free_thread if @hb and @hb.db_handler.opts[:threadsafe]
    end
  end
end

#write(str) ⇒ Object

Called from content-group.



45
46
47
# File 'lib/hayabusa_client_session.rb', line 45

def write(str)
  @out.print(str)
end