Class: Hyperion::ResponseWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion/response_writer.rb

Overview

Serializes a Rack [status, headers, body] tuple to an HTTP/1.1 wire stream. Phase 5 replaces this with an io_buffer-batched writer; Phase 7 adds a sibling Http2ResponseWriter. Public surface (#write) stays stable.

Constant Summary collapse

REASONS =
{
  200 => 'OK',
  201 => 'Created',
  204 => 'No Content',
  301 => 'Moved Permanently',
  302 => 'Found',
  304 => 'Not Modified',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  413 => 'Payload Too Large',
  414 => 'URI Too Long',
  422 => 'Unprocessable Entity',
  429 => 'Too Many Requests',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout'
}.freeze
CRLF_HEADER_VALUE =
/[\r\n]/

Instance Method Summary collapse

Instance Method Details

#write(io, status, headers, body, keep_alive: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hyperion/response_writer.rb', line 38

def write(io, status, headers, body, keep_alive: false)
  # Zero-copy fast path: bodies that point at an on-disk file (Rack::Files,
  # asset servers, signed-download responders) get streamed via
  # IO.copy_stream which delegates to sendfile(2) on Linux for plain TCP
  # sockets — bytes go from the file's page cache straight to the socket
  # buffer with no userspace allocation. For TLS sockets we still avoid the
  # multi-MB String build, but encryption forces a userspace round-trip so
  # we count that path separately.
  return write_sendfile(io, status, headers, body, keep_alive: keep_alive) if body.respond_to?(:to_path)

  write_buffered(io, status, headers, body, keep_alive: keep_alive)
end