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

Returns a new instance of Request.



17
18
19
20
21
22
23
24
25
26
# File 'lib/protocol/http/request.rb', line 17

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.



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

def authority
  @authority
end

#bodyObject

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



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

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.



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

def headers
  @headers
end

#methodObject

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



35
36
37
# File 'lib/protocol/http/request.rb', line 35

def method
  @method
end

#pathObject

The request path, usually a path and query string.



38
39
40
# File 'lib/protocol/http/request.rb', line 38

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.



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

def protocol
  @protocol
end

#schemeObject

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



29
30
31
# File 'lib/protocol/http/request.rb', line 29

def scheme
  @scheme
end

#versionObject

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



41
42
43
# File 'lib/protocol/http/request.rb', line 41

def version
  @version
end

Class Method Details

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



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

def self.[](method, path, headers = nil, body = nil)
	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.



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

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

#connect?Boolean

Returns:

  • (Boolean)


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

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

#head?Boolean

Returns:

  • (Boolean)


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

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

#idempotent?Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

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