Class: Async::HTTP::Protocol::HTTP1::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/async/http/protocol/http1/request.rb

Constant Summary collapse

URI_PATTERN =
%r{\A(?<scheme>[^:/]+)://(?<authority>[^/]+)(?<path>.*)\z}
UPGRADE =
"upgrade"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Request

#inspect, #peer, #remote_address

Constructor Details

#initialize(connection, scheme, authority, method, path, version, headers, body) ⇒ Request



45
46
47
48
49
50
51
52
# File 'lib/async/http/protocol/http1/request.rb', line 45

def initialize(connection, scheme, authority, method, path, version, headers, body)
  @connection = connection
  
  # HTTP/1 requests with an upgrade header (which can contain zero or more values) are extracted into the protocol field of the request, and we expect a response to select one of those protocols with a status code of 101 Switching Protocols.
  protocol = headers.delete("upgrade")
  
  super(scheme, authority, method, path, version, headers, body, protocol, self.public_method(:write_interim_response))
end

Class Method Details

.read(connection) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/async/http/protocol/http1/request.rb', line 25

def self.read(connection)
  connection.read_request do |authority, method, target, version, headers, body|
    if method == ::Protocol::HTTP::Methods::CONNECT
      # We put the target into the authority field for CONNECT requests, as per HTTP/2 semantics.
      self.new(connection, nil, target, method, nil, version, headers, body)
    elsif valid_path?(target)
      # This is a valid request.
      self.new(connection, nil, authority, method, target, version, headers, body)
    elsif match = target.match(URI_PATTERN)
      # We map the incoming absolute URI target to the scheme, authority, and path fields of the request.
      self.new(connection, match[:scheme], match[:authority], method, match[:path], version, headers, body)
    else
      # This is an invalid request.
      raise ::Protocol::HTTP1::BadRequest.new("Invalid request target: #{target}")
    end
  end
end

.valid_path?(target) ⇒ Boolean



13
14
15
16
17
18
19
20
21
# File 'lib/async/http/protocol/http1/request.rb', line 13

def self.valid_path?(target)
  if target.start_with?("/")
    return true
  elsif target == "*"
    return true
  else
    return false
  end
end

Instance Method Details

#connectionObject



54
55
56
# File 'lib/async/http/protocol/http1/request.rb', line 54

def connection
  @connection
end

#hijack!Object



62
63
64
# File 'lib/async/http/protocol/http1/request.rb', line 62

def hijack!
  @connection.hijack!
end

#hijack?Boolean



58
59
60
# File 'lib/async/http/protocol/http1/request.rb', line 58

def hijack?
  true
end

#write_interim_response(status, headers = nil) ⇒ Object



66
67
68
# File 'lib/async/http/protocol/http1/request.rb', line 66

def write_interim_response(status, headers = nil)
  @connection.write_interim_response(@version, status, headers)
end