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

HTTP_CONTENT_LENGTH =
'HTTP_CONTENT_LENGTH'.freeze
HTTP_TRANSFER_ENCODING =
'HTTP_TRANSFER_ENCODING'.freeze
CRLF =
"\r\n".freeze
HTTP_CONNECTION =
'HTTP_CONNECTION'.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.



36
37
38
# File 'lib/async/http/protocol/http11.rb', line 36

def initialize(stream)
	super(stream, CRLF)
end

Instance Method Details

#keep_alive?(headers) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#read_requestObject



98
99
100
101
102
103
104
# File 'lib/async/http/protocol/http11.rb', line 98

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

#read_responseObject



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

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

#receive_requests(task: Task.current) ⇒ Object

Server loop.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/async/http/protocol/http11.rb', line 55

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)
		
		break unless keep_alive?(request.headers) && keep_alive?(headers)
		
		# This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
		task.yield
	end
end

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

Client request.



71
72
73
74
75
76
77
78
# File 'lib/async/http/protocol/http11.rb', line 71

def send_request(method, path, headers, body = [])
	write_request(method, path, version, headers, body)
	
	return Response.new(*read_response)

rescue EOFError
	return nil
end

#versionObject



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

def version
	VERSION
end

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



80
81
82
83
84
85
86
87
88
# File 'lib/async/http/protocol/http11.rb', line 80

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

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



106
107
108
109
110
111
112
113
114
# File 'lib/async/http/protocol/http11.rb', line 106

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