Class: Protocol::HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Body::Reader
Defined in:
lib/protocol/http/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Body::Reader

#body?, #close, #each, #finish, #read, #save

Constructor Details

#initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil) ⇒ Request



31
32
33
34
35
36
37
38
39
40
# File 'lib/protocol/http/request.rb', line 31

def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil)
  @scheme = scheme
  @authority = authority
  @method = method
  @path = path
  @version = version
  @headers = headers
  @body = body
  @protocol = protocol
end

Instance Attribute Details

#authorityObject

The request authority, usually a hostname and port number.



46
47
48
# File 'lib/protocol/http/request.rb', line 46

def authority
  @authority
end

#bodyObject

The request body, an instance of Protocol::HTTP::Body::Readable or similar.



61
62
63
# File 'lib/protocol/http/request.rb', line 61

def body
  @body
end

#headersObject

The request headers, contains metadata associated with the request such as the user agent, accept (content type), accept-language, etc.



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

def headers
  @headers
end

#methodObject

The request method, usually one of “GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “CONNECT” or “OPTIONS”.



49
50
51
# File 'lib/protocol/http/request.rb', line 49

def method
  @method
end

#pathObject

The request path, usually a path and query string.



52
53
54
# File 'lib/protocol/http/request.rb', line 52

def path
  @path
end

#protocolObject

The request protocol, usually empty, but occasionally “websocket” or “webtransport”, can be either single value ‘String` or multi-value `Array` of `String` instances. In HTTP/1, it is used to request a connection upgrade, and in HTTP/2 it is used to indicate a specfic protocol for the stream.



64
65
66
# File 'lib/protocol/http/request.rb', line 64

def protocol
  @protocol
end

#schemeObject

The request scheme, usually one of “http” or “https”.



43
44
45
# File 'lib/protocol/http/request.rb', line 43

def scheme
  @scheme
end

#versionObject

The request version, usually “http/1.0”, “http/1.1”, “h2”, or “h3”.



55
56
57
# File 'lib/protocol/http/request.rb', line 55

def version
  @version
end

Class Method Details

.[](method, path, headers, body) ⇒ Object



79
80
81
82
83
84
# File 'lib/protocol/http/request.rb', line 79

def self.[](method, path, headers, body)
  body = Body::Buffered.wrap(body)
  headers = ::Protocol::HTTP::Headers[headers]
  
  self.new(nil, nil, method, path, nil, headers, body)
end

Instance Method Details

#call(connection) ⇒ Object

Send the request to the given connection.



67
68
69
# File 'lib/protocol/http/request.rb', line 67

def call(connection)
  connection.call(self)
end

#connect?Boolean



75
76
77
# File 'lib/protocol/http/request.rb', line 75

def connect?
  @method == Methods::CONNECT
end

#head?Boolean



71
72
73
# File 'lib/protocol/http/request.rb', line 71

def head?
  @method == Methods::HEAD
end

#idempotent?Boolean



86
87
88
# File 'lib/protocol/http/request.rb', line 86

def idempotent?
  @method != Methods::POST && (@body.nil? || @body.empty?)
end

#to_sObject



90
91
92
# File 'lib/protocol/http/request.rb', line 90

def to_s
  "#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}"
end