Class: TP2::HTTP1Adapter
- Inherits:
-
Object
- Object
- TP2::HTTP1Adapter
- Defined in:
- lib/tp2/http1_adapter.rb
Defined Under Namespace
Classes: ProtocolError
Constant Summary collapse
- SEND_FLAGS =
response API
UM::MSG_NOSIGNAL | UM::MSG_WAITALL
- EMPTY_CHUNK =
"0\r\n\r\n"
- EMPTY_CHUNK_LEN =
EMPTY_CHUNK.bytesize
Instance Attribute Summary collapse
-
#fd ⇒ Object
readonly
Returns the value of attribute fd.
Instance Method Summary collapse
- #complete?(req) ⇒ Boolean
-
#finish(request) ⇒ void
Finishes the response to the current request.
- #get_body(req) ⇒ Object
- #get_body_chunk(req, buffered_only = false) ⇒ Object
-
#initialize(machine, fd, opts, &app) ⇒ HTTP1Adapter
constructor
A new instance of HTTP1Adapter.
-
#respond(request, body, headers) ⇒ Object
Sends response including headers and body.
- #run ⇒ Object
-
#send_chunk(request, chunk, done: false) ⇒ void
Sends a response body chunk.
-
#send_headers(request, headers, empty_response: false, chunked: true) ⇒ void
Sends response headers.
- #serve_request ⇒ Object
Constructor Details
#initialize(machine, fd, opts, &app) ⇒ HTTP1Adapter
Returns a new instance of HTTP1Adapter.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tp2/http1_adapter.rb', line 10 def initialize(machine, fd, opts, &app) @machine = machine @fd = fd @opts = opts @stream = UM::Stream.new(machine, fd) @app = app @done = nil @response_headers = nil @status = nil end |
Instance Attribute Details
#fd ⇒ Object (readonly)
Returns the value of attribute fd.
8 9 10 |
# File 'lib/tp2/http1_adapter.rb', line 8 def fd @fd end |
Instance Method Details
#complete?(req) ⇒ Boolean
88 89 90 |
# File 'lib/tp2/http1_adapter.rb', line 88 def complete?(req) req.headers[':body-done-reading'] end |
#finish(request) ⇒ void
This method returns an undefined value.
Finishes the response to the current request. If no headers were sent, default headers are sent using #send_headers.
158 159 160 161 162 163 164 165 |
# File 'lib/tp2/http1_adapter.rb', line 158 def finish(request) request.tx_incr(EMPTY_CHUNK_LEN) @machine.send(@fd, EMPTY_CHUNK, EMPTY_CHUNK_LEN, SEND_FLAGS) if !@done log(request, @response_headers) if @opts[:log] @done = true end end |
#get_body(req) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tp2/http1_adapter.rb', line 54 def get_body(req) headers = req.headers content_length = headers['content-length'] return read(content_length.to_i) if content_length chunked_encoding = headers['transfer-encoding']&.downcase == 'chunked' return get_body_chunked_encoding(headers) if chunked_encoding # if content-length is not specified, we read to EOF, up to max 1MB size read(1 << 20, nil, false) end |
#get_body_chunk(req, buffered_only = false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tp2/http1_adapter.rb', line 66 def get_body_chunk(req, buffered_only = false) headers = req.headers content_length = headers['content-length'] if content_length return nil if headers[':body-done-reading'] chunk = read(content_length.to_i) headers[':body-done-reading'] = true return chunk end chunked_encoding = headers['transfer-encoding']&.downcase == 'chunked' return read_chunk(headers, nil) if chunked_encoding return nil if headers[':body-done-reading'] # if content-length is not specified, we read to EOF, up to max 1MB size chunk = read(1 << 20, nil, false) headers[':body-done-reading'] = true chunk end |
#respond(request, body, headers) ⇒ Object
Sends response including headers and body. Waits for the request to complete if not yet completed. The body is sent using chunked transfer encoding.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tp2/http1_adapter.rb', line 101 def respond(request, body, headers) formatted_headers = format_headers(headers, body, false) request.tx_incr(formatted_headers.bytesize + (body ? body.bytesize : 0)) if body buf = formatted_headers + body @machine.send(@fd, buf, buf.bytesize, SEND_FLAGS) # handle_write(formatted_headers + body) else @machine.send(@fd, formatted_headers, formatted_headers.bytesize, SEND_FLAGS) end log(request, headers) if @opts[:log] @done = true @response_headers = headers end |
#run ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tp2/http1_adapter.rb', line 22 def run while true @done = nil @response_headers = nil persist = serve_request break if !persist end rescue Exception => e case e when UM::Terminate # we're done when SystemCallError @opts[:log]&.log("Encountered #{e.class}, closing connection") when StandardError @opts[:log]&.log("Unhandled exception #{e.class}: #{e.}, closing connection") @opts[:log]&.log(e.backtrace.join("\n")) else raise end ensure @machine.close_async(@fd) end |
#send_chunk(request, chunk, done: false) ⇒ void
This method returns an undefined value.
Sends a response body chunk. If no headers were sent, default headers are sent using #send_headers. if the done option is true(thy), an empty chunk will be sent to signal response completion to the client.
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tp2/http1_adapter.rb', line 138 def send_chunk(request, chunk, done: false) data = +'' data << "#{chunk.bytesize.to_s(16)}\r\n#{chunk}\r\n" if chunk data << "0\r\n\r\n" if done return if data.empty? request.tx_incr(data.bytesize) @machine.send(@fd, data, data.bytesize, SEND_FLAGS) if done && !@done log(request, @response_headers) if @opts[:log] @done = true end end |
#send_headers(request, headers, empty_response: false, chunked: true) ⇒ void
This method returns an undefined value.
Sends response headers. If empty_response is truthy, the response status code will default to 204, otherwise to 200.
123 124 125 126 127 128 129 |
# File 'lib/tp2/http1_adapter.rb', line 123 def send_headers(request, headers, empty_response: false, chunked: true) formatted_headers = format_headers(headers, !empty_response, http1_1?(request) && chunked) request.tx_incr(formatted_headers.bytesize) # @machine.send(@fd, formatted_headers, formatted_headers.bytesize, SEND_FLAGS) @response_headers = headers end |
#serve_request ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/tp2/http1_adapter.rb', line 45 def serve_request headers = parse_headers return false if !headers request = Qeweney::Request.new(headers, self) @app.call(request) persist_connection?(headers) end |