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 |
# 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 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
82 83 84 |
# File 'lib/tp2/http1_adapter.rb', line 82 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.
152 153 154 155 156 157 158 159 |
# File 'lib/tp2/http1_adapter.rb', line 152 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
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tp2/http1_adapter.rb', line 48 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tp2/http1_adapter.rb', line 60 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tp2/http1_adapter.rb', line 95 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tp2/http1_adapter.rb', line 21 def run while true @done = nil @response_headers = nil persist = serve_request break if !persist end rescue UM::Terminate # terminated rescue Exception => e puts '!' * 40 p e puts e.backtrace.join("\n") exit! ensure @machine.close(@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.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tp2/http1_adapter.rb', line 132 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.
117 118 119 120 121 122 123 |
# File 'lib/tp2/http1_adapter.rb', line 117 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
39 40 41 42 43 44 45 46 |
# File 'lib/tp2/http1_adapter.rb', line 39 def serve_request headers = parse_headers return false if !headers request = Qeweney::Request.new(headers, self) @app.call(request) persist_connection?(headers) end |