Class: Async::HTTP::Protocol::HTTP11

Inherits:
IO::Protocol::Line
  • Object
show all
Defined in:
lib/async/http/protocol/http11.rb

Overview

Implements basic HTTP/1.1 request/response.

Direct Known Subclasses

HTTP10

Constant Summary collapse

CRLF =
"\r\n".freeze
CONNECTION =
'connection'.freeze
HOST =
'host'.freeze
CLOSE =
'close'.freeze
VERSION =
"HTTP/1.1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ HTTP11

Returns a new instance of HTTP11.



43
44
45
46
47
48
# File 'lib/async/http/protocol/http11.rb', line 43

def initialize(stream)
	super(stream, CRLF)
	
	@persistent = true
	@count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

Instance Method Details

#call(request) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/async/http/protocol/http11.rb', line 105

def call(request)
	request.version ||= self.version
	
	Async.logger.debug(self) {"#{request.method} #{request.path} #{request.headers.inspect}"}
	
	# We carefully interpret https://tools.ietf.org/html/rfc7230#section-6.3.1 to implement this correctly.
	begin
		write_request(request.authority, request.method, request.path, request.version, request.headers)
	rescue
		# If we fail to fully write the request and body, we can retry this request.
		raise RequestFailed.new
	end
	
	# Once we start writing the body, we can't recover if the request fails. That's because the body might be generated dynamically, streaming, etc.
	write_body(request.body)
	
	return Response.new(*read_response)
rescue
	# This will ensure that #reusable? returns false.
	@stream.close
	
	raise
end

#good?Boolean

Can we use this connection to make requests?

Returns:

  • (Boolean)


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

def good?
	@stream.connected?
end

#multiplexObject

Only one simultaneous connection at a time.



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

def multiplex
	1
end

#persistent?(headers) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/async/http/protocol/http11.rb', line 75

def persistent?(headers)
	if connection = headers[CONNECTION]
		return !connection.include?(CLOSE)
	else
		return true
	end
end

#read_requestObject



149
150
151
152
153
154
155
156
157
# File 'lib/async/http/protocol/http11.rb', line 149

def read_request
	method, path, version = read_line.split(/\s+/, 3)
	headers = read_headers
	body = read_body(headers)
	
	@count += 1
	
	return headers.delete(HOST), method, path, version, headers, body
end

#read_responseObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/async/http/protocol/http11.rb', line 137

def read_response
	version, status, reason = read_line.split(/\s+/, 3)
	headers = read_headers
	body = read_body(headers)
	
	@count += 1
	
	@persistent = persistent?(headers)
	
	return version, Integer(status), reason, headers, body
end

#receive_requests(task: Task.current) ⇒ Object

Server loop.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/async/http/protocol/http11.rb', line 84

def receive_requests(task: Task.current)
	while @persistent
		request = Request.new(*read_request)
		
		unless persistent?(request.headers)
			@persistent = false
		end
		
		response = yield request
		
		response.version ||= request.version
		
		write_response(response.version, response.status, response.headers, response.body)
		
		request.finish
		
		# This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
		task.yield
	end
end

#reusable?Boolean

Returns:

  • (Boolean)


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

def reusable?
	@persistent && !@stream.closed?
end

#versionObject



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

def version
	VERSION
end

#write_request(authority, method, path, version, headers) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/async/http/protocol/http11.rb', line 129

def write_request(authority, method, path, version, headers)
	@stream.write("#{method} #{path} #{version}\r\n")
	@stream.write("Host: #{authority}\r\n")
	write_headers(headers)
	
	@stream.flush
end

#write_response(version, status, headers, body) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/async/http/protocol/http11.rb', line 159

def write_response(version, status, headers, body)
	@stream.write("#{version} #{status}\r\n")
	write_headers(headers)
	write_body(body)
	
	@stream.flush
end