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
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
-
#initialize(stream) ⇒ HTTP11
constructor
A new instance of HTTP11.
- #keep_alive?(headers) ⇒ Boolean
- #read_request ⇒ Object
- #read_response ⇒ Object
-
#receive_requests ⇒ Object
Server loop.
-
#send_request(method, path, headers, body = []) ⇒ Object
Client request.
- #version ⇒ Object
- #write_request(method, path, version, headers, body) ⇒ Object
- #write_response(version, status, headers, body) ⇒ Object
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
50 51 52 |
# File 'lib/async/http/protocol/http11.rb', line 50 def keep_alive?(headers) headers[HTTP_CONNECTION] != CLOSE end |
#read_request ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/async/http/protocol/http11.rb', line 93 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_response ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/async/http/protocol/http11.rb', line 85 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 ⇒ Object
Server loop.
55 56 57 58 59 60 61 62 63 |
# File 'lib/async/http/protocol/http11.rb', line 55 def receive_requests while 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) end end |
#send_request(method, path, headers, body = []) ⇒ Object
Client request.
66 67 68 69 70 71 72 73 |
# File 'lib/async/http/protocol/http11.rb', line 66 def send_request(method, path, headers, body = []) write_request(method, path, version, headers, body) return Response.new(*read_response) rescue EOFError return nil end |
#version ⇒ Object
46 47 48 |
# File 'lib/async/http/protocol/http11.rb', line 46 def version VERSION end |
#write_request(method, path, version, headers, body) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/async/http/protocol/http11.rb', line 75 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
101 102 103 104 105 106 107 108 109 |
# File 'lib/async/http/protocol/http11.rb', line 101 def write_response(version, status, headers, body) @stream.write("#{version} #{status}\r\n") write_headers(headers) write_body(body) @stream.flush return true end |