Class: EmmyHttp::Server::Request

Inherits:
Object
  • Object
show all
Includes:
ModelPack::Document
Defined in:
lib/emmy_http/server/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



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
# File 'lib/emmy_http/server/request.rb', line 24

def initialize
  @parser = Server::Parser.new
  @body   = StringIO.new(Server::INITIAL_BODY.dup)
  request = self

  on :data do |chunk|
    @body << chunk
  end

  parser.on :head do |headers|
    request.http_version = parser.http_version
    request.http_method  = parser.http_method
    request.url     = parser.request_url
    request.headers = parser.headers
  end

  parser.on :body do |chunk|
    request.data!(request.decoder ? (request.decoder.decompress(chunk) || '') : chunk)
  end

  parser.on :complete do
    conn = request.connection
    dettach
    complete!(request, conn)
  end
end

Instance Attribute Details

#connectionObject (readonly)

attribute :upgrade_data



17
18
19
# File 'lib/emmy_http/server/request.rb', line 17

def connection
  @connection
end

#parserObject (readonly)

Returns the value of attribute parser.



18
19
20
# File 'lib/emmy_http/server/request.rb', line 18

def parser
  @parser
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



20
21
22
# File 'lib/emmy_http/server/request.rb', line 20

def remote_address
  @remote_address
end

#requestObject (readonly)

Returns the value of attribute request.



19
20
21
# File 'lib/emmy_http/server/request.rb', line 19

def request
  @request
end

Instance Method Details

#attach(conn) ⇒ Object



128
129
130
131
132
133
# File 'lib/emmy_http/server/request.rb', line 128

def attach(conn)
  @connection = conn
  @remote_address = socket_address
  listen conn, :data,  :post_data
  listen conn, :close, :close
end

#close(reason = nil) ⇒ Object



123
124
125
126
# File 'lib/emmy_http/server/request.rb', line 123

def close(reason=nil)
  terminate!(reason, self, connection) if reason
  dettach
end

#dettachObject



135
136
137
138
139
140
141
# File 'lib/emmy_http/server/request.rb', line 135

def dettach
  if connection
    stop_listen connection, :data
    stop_listen connection, :close
    @connection = nil
  end
end

#keep_alive?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/emmy_http/server/request.rb', line 61

def keep_alive?
  #return false if terminate_connection?
  case http_version
  when [1,0] then
    (headers['Connection'].downcase == 'keep-alive') rescue false
  when [1,1] then
    (headers['Connection'].downcase != 'close') rescue true
  when [2,0] then
    false
  else
    false
  end
end

#post_data(chunk) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/emmy_http/server/request.rb', line 51

def post_data(chunk)
  #p chunk
  parser << chunk

  #if parser.upgrade?
  #end
rescue EmmyHttp::ParserError => e
  close(e.message)
end

#prepare_envObject



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
# File 'lib/emmy_http/server/request.rb', line 75

def prepare_env
  @env = {}

  # Server info

  @env['rack.version']      = Server::RACK_VERSION_NUM
  @env['rack.errors']       = STDERR
  @env['rack.multithread']  = false
  @env['rack.multiprocess'] = false
  @env['rack.run_once']     = false
  #env['rack.logger']       = logger

  # HTTP headers

  headers.each do |n, v|
    @env['HTTP_' + n.gsub('-','_').upcase] = v
  end

  %w(CONTENT_TYPE CONTENT_LENGTH).each do |n|
    @env[n] = @env.delete("HTTP_#{n}") if @env["HTTP_#{n}"]
  end

  @env['SERVER_NAME']       = 'localhost'
  @env['SERVER_SOFTWARE']   = Server::SERVER_NAME

  if @env['HTTP_HOST'] # FIXME
    name, port = @env['HTTP_HOST'].split(':')
    @env['SERVER_NAME'] = name if name
    @env['SERVER_PORT'] = port if port
  end

  uri = URI(parser.request_url)

  @env['HTTP_VERSION']    = "HTTP/#{http_version.join('.')}"
  @env['REMOTE_ADDR']     = remote_address
  @env['REQUEST_METHOD']  = http_method
  @env['REQUEST_URI']     = url.to_s
  @env['QUERY_STRING']    = url.query || '' # string required
  @env['SCRIPT_NAME']     = ''
  @env['REQUEST_PATH']    = url.path
  @env['PATH_INFO']       = url.path
  @env['FRAGMENT']        = url.fragment

  # HTTP body

  @env['rack.input'] = body
end