class HTTP::Server

An HTTP/1.1 server: a task per connection, requests on one connection served in order. Shareable, so that a Signal.trap block or another task can shut it down.

handler = HTTP.handler { |req| HTTP::Response.text("hello") }
server = HTTP::Server.new(handler, host: "127.0.0.1", port: 8080,
                          trusted_proxies: ["127.0.0.1", "::1"])
Signal.trap(:TERM) { server.shutdown }
server.serve

<std:http>:1176

Class methods

def self.new(handler: HTTP::Handler, host: String = …, port: Int = …, path: String? = …, read_header_timeout: Duration = …, idle_timeout: Duration = …, read_timeout: Duration = …, write_timeout: Duration = …, max_header_bytes: Int = …, max_body_bytes: Int = …, max_connections: Int = …, trusted_proxies: Array[String] = …, on_error: shareable (Exception, HTTP::Request?) -> Void? = …) -> HTTP::Server

A server on host:port, or on a UNIX socket at path (whose peers count as trusted proxies). It listens at once, so that local_address gives the port of port: 0.

<std:http>:1198

def self.unix(handler: HTTP::Handler, path: String, read_header_timeout: Duration = …, idle_timeout: Duration = …, read_timeout: Duration = …, write_timeout: Duration = …, max_header_bytes: Int = …, max_body_bytes: Int = …, max_connections: Int = …, on_error: shareable (Exception, HTTP::Request?) -> Void? = …) -> HTTP::Server

A server on a UNIX socket at path, with the options of .new. Its peers have no address and count as trusted proxies, so remote_ip and scheme come from the X-Forwarded-* headers.

<std:http>:1224

Methods

def local_address -> Socket::Address

The address the server listens on (its port, when started on 0).

<std:http>:1237

def serve -> Void

Accepts connections and serves them until #shutdown (or until the calling task is cancelled, which cancels every connection at once).

It returns at once when the server is already serving or was shut down.

<std:http>:1248

def shutdown(timeout: Duration = …) -> Void

Stops the server: it stops accepting, closes the connections waiting for a request, lets those in a handler finish for up to timeout, then cancels them, and #serve returns.

shutdown itself returns at once (so a handler may call it); #wait waits for #serve to return.

<std:http>:1291

def wait -> Void

Waits until #serve has returned (after a #shutdown).

<std:http>:1312

def stopping? -> Bool

Whether #shutdown has been called.

<std:http>:1318