Class: Hayabusa::Http_session
- Inherits:
-
Client_session
- Object
- Client_session
- Hayabusa::Http_session
- Defined in:
- lib/hayabusa_http_session.rb
Overview
This class handels the HTTP-sessions.
Defined Under Namespace
Classes: Contentgroup, Page_environment, Post_multipart, Request, Response
Instance Attribute Summary
Attributes inherited from Client_session
#active, #alert_sent, #browser, #cgroup, #cookie, #data, #debug, #eruby, #get, #handler, #hb, #headers, #httpsession_var, #ip, #meta, #out, #page_path, #post, #request_hash, #resp, #session, #session_hash, #session_id, #working
Class Method Summary collapse
-
.const_missing(name) ⇒ Object
Autoloader for subclasses.
- .finalize(id) ⇒ Object
Instance Method Summary collapse
- #destruct ⇒ Object
-
#initialize(httpserver, socket) ⇒ Http_session
constructor
A new instance of Http_session.
- #serve ⇒ Object
- #thread_request_run ⇒ Object
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(httpserver, socket) ⇒ Http_session
Returns a new instance of Http_session.
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 |
# File 'lib/hayabusa_http_session.rb', line 10 def initialize(httpserver, socket) @data = {} @socket = socket @httpserver = httpserver @hb = httpserver.hb @types = @hb.types @config = @hb.config @active = true @debug = @hb.debug @handlers_cache = @config[:handlers_cache] @httpsession_var = {} @eruby = Knj::Eruby.new( :cache_hash => @hb.eruby_cache, :binding_callback => self.method(:create_binding) ) #Set socket stuff. if RUBY_PLATFORM == "java" or RUBY_ENGINE == "rbx" if @hb.config[:peeraddr_static] addr_peer = [0, 0, @hb.config[:peeraddr_static]] else addr_peer = @socket.peeraddr end addr = @socket.addr else addr = @socket.addr(false) addr_peer = @socket.peeraddr(false) end @socket_meta = { "REMOTE_ADDR" => addr[2], "REMOTE_PORT" => addr[1], "SERVER_ADDR" => addr_peer[2], "SERVER_PORT" => addr_peer[1] } @resp = Hayabusa::Http_session::Response.new(:hb => @hb, :socket => @socket) @handler = Hayabusa::Http_session::Request.new(:hb => @hb, :httpsession => self) @cgroup = Hayabusa::Http_session::Contentgroup.new(:socket => @socket, :hb => @hb, :resp => @resp, :httpsession => self) @resp.cgroup = @cgroup Dir.chdir(@config[:doc_root]) ObjectSpace.define_finalizer(self, self.class.method(:finalize).to_proc) if @debug @hb.log_puts "New httpsession #{self.__id__} (total: #{@httpserver.http_sessions.count})." if @debug @thread_request = Thread.new(&self.method(:thread_request_run)) end |
Class Method Details
.const_missing(name) ⇒ Object
Autoloader for subclasses.
4 5 6 7 8 |
# File 'lib/hayabusa_http_session.rb', line 4 def self.const_missing(name) require "#{File.dirname(__FILE__)}/hayabusa_http_session_#{name.to_s.downcase}.rb" raise "Still not defined: '#{name}'." if !Hayabusa::Http_session.const_defined?(name) return Hayabusa::Http_session.const_get(name.to_s.to_sym) end |
.finalize(id) ⇒ Object
138 139 140 |
# File 'lib/hayabusa_http_session.rb', line 138 def self.finalize(id) @hb.log_puts "Hayabusa: Http_session finalize #{id}." if @debug end |
Instance Method Details
#destruct ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/hayabusa_http_session.rb', line 142 def destruct @hb.log_puts "Hayabusa: Http_session destruct (#{@httpserver.http_sessions.length})" if @debug and @httpserver and @httpserver.http_sessions begin @socket.close if !@socket.closed? rescue => e @hb.log_puts(e.inspect) @hb.log_puts(e.backtrace) #ignore if it fails... end @httpserver.http_sessions.delete(self) if @httpserver and @httpserver.http_sessions @eruby.destroy if @eruby @hb.events.call(:http_session_destruct, :httpsession => self) if @hb.events @thread_request.kill if @thread_request.alive? end |
#serve ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/hayabusa_http_session.rb', line 159 def serve @hb.log_puts "Hayabusa: Generating meta, cookie, get, post and headers." if @debug @meta = @handler..merge(@socket_meta) @cookie = @handler. @get = @handler.get @post = @handler.post @headers = @handler.headers close = true if @meta["HTTP_CONNECTION"] == "close" @resp.reset( :http_version => @handler.http_version, :close => close, :cookie => @cookie ) if @handler.http_version == "1.1" @cgroup.chunked = true @resp.chunked = true else @cgroup.chunked = false @resp.chunked = false end @page_path = @handler.page_path @hb.log_puts "Hayabusa: Page path: #{@page_path}" if @debug @ext = File.extname(@page_path).downcase[1..-1].to_s @ctype = @types[@ext.to_sym] if @ext.length > 0 and @types.key?(@ext.to_sym) @ctype = @config[:default_filetype] if !@ctype and @config.key?(:default_filetype) @resp.header("Content-Type", @ctype) @browser = Knj::Web.browser(@meta) @ip = @hb.ip(:meta => @meta) @hb.log_puts "Hayabusa: Figuring out session-ID, session-object and more." if @debug 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) = 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) = true end if @resp.( "name" => "HayabusaSession", "value" => @session_id, "path" => "/", "expires" => Time.now + 32140800 #add around 12 months from the current time ) end if @config.key?(:logging) and @config[:logging][:access_db] @hb.log_puts "Hayabusa: Doing access-logging." if @debug @ips = [@meta["REMOTE_ADDR"]] @ips << @meta["HTTP_X_FORWARDED_FOR"].split(",")[0].strip if @meta["HTTP_X_FORWARDED_FOR"] @hb.logs_access_pending << { :session_id => @session.id, :date_request => Time.now, :ips => @ips, :get => @get, :post => @post, :meta => @meta, :cookie => @cookie } end @hb.log_puts "Initializing thread and content-group." if @debug self.init_thread Thread.current[:hayabusa][:contentgroup] = @cgroup self.execute_page self.execute_done end |
#thread_request_run ⇒ Object
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/hayabusa_http_session.rb', line 60 def thread_request_run Thread.current[:hayabusa] = {} if !Thread.current[:hayabusa] Thread.current[:type] = :httpsession if @config.key?(:max_requests_working) max_requests_working = @config[:max_requests_working].to_i else max_requests_working = false end begin while @active begin @cgroup.reset @written_size = 0 @size_send = @config[:size_send] @alert_sent = false @working = false break if @hb.should_restart @hb.log_puts "#{__id__} - Waiting to parse from socket." if @debug Timeout.timeout(1800) do @handler.socket_parse(@socket) end begin @hb.log_puts "#{__id__} - Done parsing from socket." if @debug while @hb.paused? #Check if we should be waiting with executing the pending request. @hb.log_puts "#{__id__} - Paused! (#{@hb.paused}) - sleeping." if @debug sleep 0.1 end break if @hb.should_restart if max_requests_working and @httpserver while @httpserver.working_count.to_i >= max_requests_working @hb.log_puts "#{__id__} - Maximum amounts of requests are working (#{@httpserver.working_count}, #{max_requests_working}) - sleeping." if @debug sleep 0.1 end end #Reserve database connections. @hb.db_handler.get_and_register_thread if @hb.db_handler.opts[:threadsafe] @hb.ob.db.get_and_register_thread if @hb.ob.db.opts[:threadsafe] @working = true @hb.log_puts "#{__id__} - Serving." if @debug @httpserver.count_block do self.serve end ensure @handler.delete_tempfiles end ensure @hb.log_puts "#{__id__} - Closing request." if @debug @working = false #Free reserved database-connections. @hb.db_handler.free_thread if @hb and @hb.db_handler.opts[:threadsafe] @hb.ob.db.free_thread if @hb and @hb.ob.db.opts[:threadsafe] end end rescue Timeout::Error @hb.log_puts "#{__id__} - Closing httpsession because of timeout." if @debug rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE => e @hb.log_puts "#{__id__} - Connection error (#{e.inspect})..." if @debug @hb.log_puts e.backtrace if @debug rescue Interrupt => e raise e rescue Exception => e @hb.log_puts Knj::Errors.error_str(e) ensure self.destruct end end |