Class: H2::Server::Stream::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/h2/server/stream/request.rb

Constant Summary collapse

HEADER_HASH =

a case-insensitive hash that also handles symbol translation i.e. s/_/-/

Hash.new do |hash, key|
  k = key.to_s.upcase
  k.gsub! '_', '-' if Symbol === key
  _, value = hash.find {|header_key,v| header_key.upcase == k}
  hash[key] = value if value
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
# File 'lib/h2/server/stream/request.rb', line 17

def initialize stream
  @stream  = stream
  @headers = HEADER_HASH.dup
  @body    = ''
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



15
16
17
# File 'lib/h2/server/stream/request.rb', line 15

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/h2/server/stream/request.rb', line 15

def headers
  @headers
end

#streamObject (readonly)

Returns the value of attribute stream.



15
16
17
# File 'lib/h2/server/stream/request.rb', line 15

def stream
  @stream
end

Instance Method Details

#addrObject

retreive the IP address of the connection



25
26
27
# File 'lib/h2/server/stream/request.rb', line 25

def addr
  @addr ||= @stream.connection.socket.peeraddr[3] rescue nil
end

#authorityObject

retreive the authority from the stream request headers



31
32
33
# File 'lib/h2/server/stream/request.rb', line 31

def authority
  @authority ||= headers[AUTHORITY_KEY]
end

#methodObject

retreive the HTTP method as a lowercase Symbol



37
38
39
40
41
42
# File 'lib/h2/server/stream/request.rb', line 37

def method
  return @method if defined? @method
  @method = headers[METHOD_KEY]
  @method = @method.downcase.to_sym if @method
  @method
end

#pathObject

retreive the path from the stream request headers



46
47
48
49
50
# File 'lib/h2/server/stream/request.rb', line 46

def path
  return @path if defined?(@path)
  @path = headers[PATH_KEY]
  @path = @path.split('?').first if @path
end

#query_stringObject

retreive the query string from the stream request headers



54
55
56
57
58
59
# File 'lib/h2/server/stream/request.rb', line 54

def query_string
  return @query_string if defined?(@query_string)
  @query_string = headers[PATH_KEY].index '?'
  return if @query_string.nil?
  @query_string = headers[PATH_KEY][(@query_string + 1)..-1]
end

#respond(status:, headers: {}, body: '') ⇒ Object

respond to this request on its stream



69
70
71
# File 'lib/h2/server/stream/request.rb', line 69

def respond status:, headers: {}, body: ''
  @stream.respond status: status, headers: headers, body: body
end

#schemeObject

retreive the scheme from the stream request headers



63
64
65
# File 'lib/h2/server/stream/request.rb', line 63

def scheme
  @scheme ||= headers[SCHEME_KEY]
end