Class: Hatetepe::Server::KeepAlive

Inherits:
Object
  • Object
show all
Defined in:
lib/hatetepe/server/keep_alive.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ KeepAlive

Returns a new instance of KeepAlive.



5
6
7
# File 'lib/hatetepe/server/keep_alive.rb', line 5

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/hatetepe/server/keep_alive.rb', line 3

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/hatetepe/server/keep_alive.rb', line 9

def call(env)
  m = case env["HTTP_CONNECTION"].to_s.downcase
    when "close" then :close
    when "keep-alive" then :keep_alive
    else env["HTTP_VERSION"] =~ /^HTTP\/(0\.9|1\.0)$/ ? :close : :keep_alive
  end
  
  send :"call_and_#{m}", env
end

#call_and_close(env, response = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hatetepe/server/keep_alive.rb', line 19

def call_and_close(env, response = nil)
  req, conn = extract_request(env), extract_connection(env)
  
  conn.processing_enabled = false
  req.callback &conn.method(:close_connection_after_writing)
  req.errback &conn.method(:close_connection_after_writing)
  
  if response
    response[1]["Connection"] = "close"
  else
    stream_start = env["stream.start"]
    env["stream.start"] = proc do |res|
      res[1]["Connection"] = "close"
      stream_start.call res
    end
  end
  
  response || app.call(env)
end

#call_and_keep_alive(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hatetepe/server/keep_alive.rb', line 39

def call_and_keep_alive(env)
  stream_start = env["stream.start"]
  env["stream.start"] = proc do |res|
    if res[1]["Connection"] == "close"
      call_and_close env, res
    else
      res[1]["Connection"] = "keep-alive"
    end
    stream_start.call res
  end
  
  app.call env
end

#extract_connection(env) ⇒ Object



57
58
59
# File 'lib/hatetepe/server/keep_alive.rb', line 57

def extract_connection(env)
  env["hatetepe.connection"] || raise("env[hatetepe.connection] not set")
end

#extract_request(env) ⇒ Object



53
54
55
# File 'lib/hatetepe/server/keep_alive.rb', line 53

def extract_request(env)
  env["hatetepe.request"] || raise("env[hatetepe.request] not set")
end