Class: Mongrel2::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, conn_id, path, headers, body) ⇒ Request

Returns a new instance of Request.



27
28
29
30
# File 'lib/mongrel2/request.rb', line 27

def initialize(uuid, conn_id, path, headers, body)
  @uuid, @conn_id, @path, @headers, @body = uuid, conn_id, path, headers, body
  @data = headers['METHOD'] == 'JSON' ? Mongrel2::JSON.parse(body) : {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/mongrel2/request.rb', line 5

def body
  @body
end

#conn_idObject (readonly)

Returns the value of attribute conn_id.



5
6
7
# File 'lib/mongrel2/request.rb', line 5

def conn_id
  @conn_id
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/mongrel2/request.rb', line 5

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/mongrel2/request.rb', line 5

def path
  @path
end

#uuidObject (readonly)

Returns the value of attribute uuid.



5
6
7
# File 'lib/mongrel2/request.rb', line 5

def uuid
  @uuid
end

Class Method Details

.parse(msg) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/mongrel2/request.rb', line 8

def parse(msg)
  # UUID CONN_ID PATH SIZE:HEADERS,SIZE:BODY,
  uuid, conn_id, path, rest = msg.split(' ', 4)
  headers, rest = parse_netstring(rest)
  body, _ = parse_netstring(rest)
  headers = Mongrel2::JSON.parse(headers)
  new(uuid, conn_id, path, headers, body)
end

.parse_netstring(ns) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/mongrel2/request.rb', line 17

def parse_netstring(ns)
  # SIZE:HEADERS,

  len, rest = ns.split(':', 2)
  len = len.to_i
  raise "Netstring did not end in ','" unless rest[len].chr == ','
  [rest[0, len], rest[(len + 1)..-1]]
end

Instance Method Details

#close?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/mongrel2/request.rb', line 36

def close?
  headers['connection'] == 'close' || headers['VERSION'] == 'HTTP/1.0'
end

#disconnect?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/mongrel2/request.rb', line 32

def disconnect?
  headers['METHOD'] == 'JSON' && @data['type'] == 'disconnect'
end