Class: Async::HTTP::Protocol::HTTP11
- Inherits:
-
IO::Protocol::Line
- Object
- IO::Protocol::Line
- Async::HTTP::Protocol::HTTP11
- Defined in:
- lib/async/http/protocol/http11.rb
Overview
Implements basic HTTP/1.1 request/response.
Direct Known Subclasses
Defined Under Namespace
Classes: ChunkedBody
Constant Summary collapse
- CRLF =
"\r\n".freeze
- KEEP_ALIVE =
'keep-alive'.freeze
- CLOSE =
'close'.freeze
- VERSION =
"HTTP/1.1".freeze
Instance Method Summary collapse
-
#initialize(stream) ⇒ HTTP11
constructor
A new instance of HTTP11.
- #keep_alive?(headers) ⇒ Boolean
-
#multiplex ⇒ Object
Only one simultaneous connection at a time.
- #read_request ⇒ Object
- #read_response ⇒ Object
-
#receive_requests(task: Task.current) ⇒ Object
Server loop.
- #reusable? ⇒ Boolean
-
#send_request(authority, method, path, headers = {}, body = []) ⇒ Object
Client request.
- #version ⇒ Object
- #write_request(authority, method, path, version, headers, body) ⇒ Object
- #write_response(version, status, headers, body) ⇒ Object
Constructor Details
Instance Method Details
#keep_alive?(headers) ⇒ Boolean
62 63 64 |
# File 'lib/async/http/protocol/http11.rb', line 62 def keep_alive?(headers) headers['connection'] != CLOSE end |
#multiplex ⇒ Object
Only one simultaneous connection at a time.
40 41 42 |
# File 'lib/async/http/protocol/http11.rb', line 40 def multiplex 1 end |
#read_request ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/async/http/protocol/http11.rb', line 121 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_response ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/async/http/protocol/http11.rb', line 111 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/async/http/protocol/http11.rb', line 67 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) request.close 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
44 45 46 |
# File 'lib/async/http/protocol/http11.rb', line 44 def reusable? @keep_alive end |
#send_request(authority, method, path, headers = {}, body = []) ⇒ Object
Client request.
89 90 91 92 93 94 95 96 97 |
# File 'lib/async/http/protocol/http11.rb', line 89 def send_request(, method, path, headers = {}, body = []) Async.logger.debug(self) {"#{method} #{path} #{headers.inspect}"} write_request(, method, path, version, headers, body) return Response.new(*read_response) rescue EOFError return nil end |
#version ⇒ Object
58 59 60 |
# File 'lib/async/http/protocol/http11.rb', line 58 def version VERSION end |
#write_request(authority, method, path, version, headers, body) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/async/http/protocol/http11.rb', line 99 def write_request(, method, path, version, headers, body) @stream.write("#{method} #{path} #{version}\r\n") @stream.write("Host: #{}\r\n") write_headers(headers) write_body(body) @stream.flush return true end |
#write_response(version, status, headers, body) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/async/http/protocol/http11.rb', line 129 def write_response(version, status, headers, body) @stream.write("#{version} #{status}\r\n") write_headers(headers) write_body(body) @stream.flush return true end |