Class: Knjappserver::Httpsession

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

Overview

This class handels the HTTP-sessions.

Defined Under Namespace

Classes: Contentgroup, Http_request, Http_response, Page_environment, Post_multipart

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(httpserver, socket) ⇒ Httpsession

Returns a new instance of Httpsession.



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/include/class_httpsession.rb', line 12

def initialize(httpserver, socket)
  @data = {}
  @socket = socket
  @httpserver = httpserver
  @kas = httpserver.kas
  @types = @kas.types
  @config = @kas.config
  @active = true
  @debug = @kas.debug
  @handlers_cache = @config[:handlers_cache]
  @httpsession_var = {}
  
  @eruby = Knj::Eruby.new(
    :cache_hash => @kas.eruby_cache,
    :binding_callback => self.method("create_binding")
  )
  
  #Set socket stuff.
  if RUBY_PLATFORM == "java" or RUBY_ENGINE == "rbx"
    if @kas.config[:peeraddr_static]
      addr_peer = [0, 0, @kas.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 = Knjappserver::Httpsession::Http_response.new(:socket => @socket)
  @handler = Knjappserver::Httpsession::Http_request.new(:kas => @kas, :httpsession => self)
  
  @cgroup = Knjappserver::Httpsession::Contentgroup.new(
    :socket => @socket,
    :kas => @kas,
    :resp => @resp,
    :httpsession => self
  )
  @resp.cgroup = @cgroup
  
  Dir.chdir(@config[:doc_root])
  ObjectSpace.define_finalizer(self, self.class.method(:finalize).to_proc) if @debug
  STDOUT.print "New httpsession #{self.__id__} (total: #{@httpserver.http_sessions.count}).\n" if @debug
  
  @thread_request = Thread.new do
    Thread.current[:knjappserver] = {} if !Thread.current[:knjappserver]
    
    begin
      while @active
        begin
          @cgroup.reset
          @written_size = 0
          @size_send = @config[:size_send]
          @alert_sent = false
          @working = false
          break if @kas.should_restart
          
          STDOUT.print "#{__id__} - Waiting to parse from socket.\n" if @debug
          
          #Timeout on sockets is currently broken in JRuby.
          if RUBY_ENGINE == "jruby"
            @handler.socket_parse(@socket)
          else
            Timeout.timeout(1800) do
              @handler.socket_parse(@socket)
            end
          end
          
          STDOUT.print "#{__id__} - Done parsing from socket.\n" if @debug
          
          while @kas.paused? #Check if we should be waiting with executing the pending request.
            STDOUT.print "#{__id__} - Paused! (#{@kas.paused}) - sleeping.\n" if @debug
            sleep 0.1
          end
          
          break if @kas.should_restart
          
          if @config.key?(:max_requests_working)
            while @httpserver and @config and @httpserver.working_count.to_i >= @config[:max_requests_working].to_i
              STDOUT.print "#{__id__} - Maximum amounts of requests are working (#{@httpserver.working_count}, #{@config[:max_requests_working]}) - sleeping.\n" if @debug
              sleep 0.1
            end
          end
          
          #Reserve database connections.
          @kas.db_handler.get_and_register_thread if @kas.db_handler.opts[:threadsafe]
          @kas.ob.db.get_and_register_thread if @kas.ob.db.opts[:threadsafe]
          
          @httpserver.count_add
          @working = true
          STDOUT.print "#{__id__} - Serving.\n" if @debug
          
          begin
            self.serve
          ensure
            @httpserver.count_remove
            @kas.served += 1 if @kas
            @working = false
          end
        ensure
          STDOUT.print "#{__id__} - Closing request.\n" if @debug
          #Free reserved database-connections.
          @kas.db_handler.free_thread if @kas and @kas.db_handler.opts[:threadsafe]
          @kas.ob.db.free_thread if @kas and @kas.ob.db.opts[:threadsafe]
        end
      end
    rescue Timeout::Error
      STDOUT.print "#{__id__} - Closing httpsession because of timeout.\n" if @debug
    rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE => e
      STDOUT.print "#{__id__} - Connection error (#{e.inspect})...\n" if @debug
    rescue Interrupt => e
      raise e
    rescue Exception => e
      STDOUT.puts Knj::Errors.error_str(e)
    ensure
      self.destruct
    end
  end
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



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

def active
  @active
end

#alert_sentObject

Returns the value of attribute alert_sent.



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

def alert_sent
  @alert_sent
end

#browserObject (readonly)

Returns the value of attribute browser.



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

def browser
  @browser
end

#cgroupObject (readonly)

Returns the value of attribute cgroup.



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

def cgroup
  @cgroup
end

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#debugObject (readonly)

Returns the value of attribute debug.



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

def debug
  @debug
end

#erubyObject (readonly)

Returns the value of attribute eruby.



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

def eruby
  @eruby
end

#getObject (readonly)

Returns the value of attribute get.



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

def get
  @get
end

#handlerObject (readonly)

Returns the value of attribute handler.



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

def handler
  @handler
end

#httpsession_varObject (readonly)

Returns the value of attribute httpsession_var.



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

def httpsession_var
  @httpsession_var
end

#kasObject (readonly)

Returns the value of attribute kas.



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

def kas
  @kas
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#outObject (readonly)

Returns the value of attribute out.



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

def out
  @out
end

#page_pathObject (readonly)

Returns the value of attribute page_path.



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

def page_path
  @page_path
end

#postObject (readonly)

Returns the value of attribute post.



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

def post
  @post
end

#respObject (readonly)

Returns the value of attribute resp.



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

def resp
  @resp
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

#session_hashObject (readonly)

Returns the value of attribute session_hash.



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

def session_hash
  @session_hash
end

#session_idObject (readonly)

Returns the value of attribute session_id.



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

def session_id
  @session_id
end

#workingObject (readonly)

Returns the value of attribute working.



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

def working
  @working
end

Class Method Details

.const_missing(name) ⇒ Object

Autoloader for subclasses.



7
8
9
10
# File 'lib/include/class_httpsession.rb', line 7

def self.const_missing(name)
  require "#{File.dirname(__FILE__)}/class_httpsession_#{name.to_s.downcase}.rb"
  return Knjappserver::Httpsession.const_get(name.to_s.to_sym)
end

.finalize(id) ⇒ Object



188
189
190
# File 'lib/include/class_httpsession.rb', line 188

def self.finalize(id)
  STDOUT.print "Httpsession finalize #{id}.\n" if @debug
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.



148
149
150
151
# File 'lib/include/class_httpsession.rb', line 148

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

#create_bindingObject

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



142
143
144
145
# File 'lib/include/class_httpsession.rb', line 142

def create_binding
  binding_obj = Knjappserver::Httpsession::Page_environment.new(:httpsession => self, :kas => @kas)
  return binding_obj.get_binding
end

#destructObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/include/class_httpsession.rb', line 192

def destruct
  STDOUT.print "Httpsession destruct (#{@httpserver.http_sessions.length})\n" if @debug and @httpserver and @httpserver.http_sessions
  
  begin
    @socket.close if !@socket.closed?
  rescue => e
    STDOUT.puts e.inspect
    STDOUT.puts e.backtrace
    #ignore if it fails...
  end
  
  @httpserver.http_sessions.delete(self) if @httpserver and @httpserver.http_sessions
  
  @eruby.destroy if @eruby
  @thread_request.kill if @thread_request.alive?
end

#force_content(newcont) ⇒ Object

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



210
211
212
# File 'lib/include/class_httpsession.rb', line 210

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

#init_threadObject



177
178
179
180
181
182
183
184
185
186
# File 'lib/include/class_httpsession.rb', line 177

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

#serveObject



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/include/class_httpsession.rb', line 214

def serve
  STDOUT.print "Generating meta, cookie, get, post and headers.\n" if @debug
  @meta = @handler.meta.merge(@socket_meta)
  @cookie = @handler.cookie
  @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 
  )
  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
  @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)
  
  if @meta["HTTP_X_FORWARDED_FOR"]
    @ip = @meta["HTTP_X_FORWARDED_FOR"].split(",")[0].strip
  elsif @meta["REMOTE_ADDR"]
    @ip = @meta["REMOTE_ADDR"]
  else
    raise "Could not figure out the IP of the session."
  end
  
  STDOUT.print "Figuring out session-ID, session-object and more.\n" if @debug
  if @cookie["KnjappserverSession"].to_s.length > 0
    @session_id = @cookie["KnjappserverSession"]
  elsif @browser["browser"] == "bot"
    @session_id = "bot"
  else
    @session_id = @kas.session_generate_id(@meta)
    send_cookie = true
  end
  
  begin
    @session, @session_hash = @kas.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 = @kas.session_generate_id(@meta)
    @session, @session_hash = @kas.session_fromid(@ip, @session_id, @meta)
    send_cookie = true
  end
  
  if send_cookie
    @resp.cookie(
      "name" => "KnjappserverSession",
      "value" => @session_id,
      "path" => "/",
      "expires" => Time.now + 32140800 #add around 12 months
    )
  end
  
  if @config.key?(:logging) and @config[:logging][:access_db]
    STDOUT.print "Doing access-logging.\n" if @debug
    @ips = [@meta["REMOTE_ADDR"]]
    @ips << @meta["HTTP_X_FORWARDED_FOR"].split(",")[0].strip if @meta["HTTP_X_FORWARDED_FOR"]
    @kas.logs_access_pending << {
      :session_id => @session.id,
      :date_request => Time.now,
      :ips => @ips,
      :get => @get,
      :post => @post,
      :meta => @meta,
      :cookie => @cookie
    }
  end
  
  STDOUT.print "Initializing thread and content-group.\n" if @debug
  self.init_thread
  Thread.current[:knjappserver][:contentgroup] = @cgroup
  time_start = Time.now.to_f if @debug
  
  begin
    @kas.events.call(:request_begin, :httpsession => self) if @kas.events
    
    Timeout.timeout(@kas.config[:timeout]) do
      if @handlers_cache.key?(@ext)
        STDOUT.print "Calling handler.\n" if @debug
        @handlers_cache[@ext].call(self)
      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
            return handler_info[:callback].call(self)
          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
          end
        end
        
        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
          
          @cgroup.new_io(:type => :file, :path => @page_path)
        end
      end
    end
  rescue SystemExit
    #do nothing - ignore.
  rescue Timeout::Error
    @resp.status = 500
    print "The request timed out."
  end
  
  @cgroup.mark_done
  @cgroup.write_output
  STDOUT.print "#{__id__} - Served '#{@meta["REQUEST_URI"]}' in #{Time.now.to_f - time_start} secs (#{@resp.status}).\n" if @debug
  @cgroup.join
  
  @kas.events.call(:request_done, {
    :httpsession => self
  }) if @kas.events
  @httpsession_var = {}
end

#threadded_content(block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/include/class_httpsession.rb', line 153

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