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

CONTENT_LENGTH =
Headers['Content-Length']
TRANSFER_ENCODING =
Headers['Transfer-Encoding']
CRLF =
"\r\n".freeze
KEEP_ALIVE =
'keep-alive'.freeze
CLOSE =
'close'.freeze
VERSION =
"HTTP/1.1".freeze

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ HTTP11

Returns a new instance of HTTP11.



37
38
39
40
41
# File 'lib/async/http/protocol/http11.rb', line 37

def initialize(stream)
	super(stream, CRLF)
	
	@keep_alive = true
end

Instance Method Details

#keep_alive?(headers) ⇒ Boolean

Returns:

  • (Boolean)


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

def keep_alive?(headers)
	headers[:connection] != CLOSE
end

#multiplexObject

Only one simultaneous connection at a time.



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

def multiplex
	1
end

#read_requestObject



123
124
125
126
127
128
129
# File 'lib/async/http/protocol/http11.rb', line 123

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

#read_responseObject



113
114
115
116
117
118
119
120
121
# File 'lib/async/http/protocol/http11.rb', line 113

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

#receive_requests(task: Task.current) ⇒ Object

Server loop.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/async/http/protocol/http11.rb', line 71

def receive_requests(task: Task.current)
	while true
		request = Request.new(*read_request)
		
		status, headers, body = yield request
		
		write_response(request.version, status, headers, body)
		
		unless keep_alive?(request.headers) and keep_alive?(headers)
			@keep_alive = false
			
			break
		end
		
		# 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)


48
49
50
# File 'lib/async/http/protocol/http11.rb', line 48

def reusable?
	@keep_alive
end

#send_request(authority, method, path, headers = {}, body = []) ⇒ Object

Client request.



91
92
93
94
95
96
97
98
99
# File 'lib/async/http/protocol/http11.rb', line 91

def send_request(authority, method, path, headers = {}, body = [])
	Async.logger.debug(self) {"#{method} #{path} #{headers.inspect}"}
	
	write_request(authority, method, path, version, headers, body)
	
	return Response.new(*read_response)
rescue EOFError
	return nil
end

#versionObject



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

def version
	VERSION
end

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/async/http/protocol/http11.rb', line 101

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

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



131
132
133
134
135
136
137
138
139
# File 'lib/async/http/protocol/http11.rb', line 131

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