class HTTP::Server

Overview

An HTTP server.

A server is given a handler that receives an HTTP::Server::Context that holds the HTTP::Request to process and must in turn configure and write to an HTTP::Server::Response.

The HTTP::Server::Response object has status and headers properties that can be configured before writing the response body. Once response output is written, changing the status and headers properties has no effect.

The HTTP::Server::Response is also a write-only IO, so all IO methods are available in it.

The handler given to a server can simply be a block that receives an HTTP::Server::Context, or it can be an HTTP::Handler. An HTTP::Handler has an optional next handler, so handlers can be chained. For example, an initial handler may handle exceptions in a subsequent handler and return a 500 status code (see HTTP::ErrorHandler), the next handler might log the incoming request (see HTTP::LogHandler), and the final handler deals with routing and application logic.

Simple Setup

A handler is given with a block.

require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

server.bind_tcp 8080
puts "Listening on http://127.0.0.1:8080"
server.listen

With non-localhost bind address

require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

server.bind_tcp "0.0.0.0", 8080
puts "Listening on http://0.0.0.0:8080"
server.listen

Add handlers

A series of handlers are chained.

require "http/server"

server = HTTP::Server.new([
  HTTP::ErrorHandler.new,
  HTTP::LogHandler.new,
  HTTP::CompressHandler.new,
  HTTP::StaticFileHandler.new("."),
])

server.bind_tcp "127.0.0.1", 8080
server.listen

Add handlers and block

A series of handlers is chained, the last one being the given block.

require "http/server"

server = HTTP::Server.new([
  HTTP::ErrorHandler.new,
  HTTP::LogHandler.new,
]) do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

server.bind_tcp "0.0.0.0", 8080
server.listen

Defined in:

http/server/context.cr
http/server/response.cr
http/server.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup, hash(hasher) hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference)
same?(other : Nil)
same?
, to_s(io : IO) : Nil to_s

Constructor methods inherited from class Reference

new new

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.new(handlers : Array(HTTP::Handler), &handler : HTTP::Handler::Proc) : self #

Creates a new HTTP server with a handler chain constructed from the handlers array and the given block.


[View source]
def self.new(&handler : HTTP::Handler::Proc) : self #

Creates a new HTTP server with the given block as handler.


[View source]
def self.new(handlers : Array(HTTP::Handler)) : self #

Creates a new HTTP server with the handlers array as handler chain.


[View source]
def self.new(handler : HTTP::Handler | HTTP::Handler::Proc) #

Creates a new HTTP server with the given handler.


[View source]

Class Method Detail

def self.build_middleware(handlers, last_handler : Context -> ? = nil) #

Builds all handlers as the middleware for HTTP::Server.


[View source]

Instance Method Detail

def addresses : Array(Socket::Address) #

[View source]
def bind(socket : Socket::Server) : Nil #

Adds a Socket::Server socket to this server.


[View source]
def bind_tcp(host : String, port : Int32, reuse_port : Bool = false) : Socket::IPAddress #

Creates a TCPServer listenting on host:port and adds it as a socket, returning the local address and port the server listens on.

server = HTTP::Server.new { }
server.bind_tcp("127.0.0.100", 8080) # => Socket::IPAddress.new("127.0.0.100", 8080)

If reuse_port is true, it enables the SO_REUSEPORT socket option, which allows multiple processes to bind to the same port.


[View source]
def bind_tcp(port : Int32, reuse_port : Bool = false) : Socket::IPAddress #

Creates a TCPServer listenting on 127.0.0.1:port and adds it as a socket, returning the local address and port the server listens on.

server = HTTP::Server.new { }
server.bind_tcp(8080) # => Socket::IPAddress.new("127.0.0.1", 8080)

If reuse_port is true, it enables the SO_REUSEPORT socket option, which allows multiple processes to bind to the same port.


[View source]
def bind_unix(path : String) : Socket::UNIXAddress #

Creates a UNIXServer bound to path and adds it as a socket.

server = HTTP::Server.new { }
server.bind_unix "/tmp/my-socket.sock"

[View source]
def bind_unused_port(host : String = "127.0.0.1", reuse_port : Bool = false) : Socket::IPAddress #

Creates a TCPServer listening on an unused port and adds it as a socket.

Returns the Socket::IPAddress with the determined port number.

server = HTTP::Server.new { }
server.bind_unused_port # => Socket::IPAddress.new("127.0.0.1", 12345)

[View source]
def close #

Gracefully terminates the server. It will process currently accepted requests, but it won't accept new connections.


[View source]
def closed? : Bool #

Returns true if this server is closed.


[View source]
def each_address(&block : Socket::Address -> ) #

Enumerates all addresses this server is bound to.


[View source]
def listen(port : Int32, reuse_port : Bool = false) #

Creates a TCPServer listenting on 127.0.0.1:port, adds it as a socket and starts the server. Blocks until the server is closed.

See #bind(port : Int32) for details.


[View source]
def listen(host : String, port : Int32, reuse_port : Bool = false) #

Creates a TCPServer listenting on host:port, adds it as a socket and starts the server. Blocks until the server is closed.

See #bind(host : String, port : Int32) for details.


[View source]
def listen #

Starts the server. Blocks until the server is closed.


[View source]
def listening? : Bool #

Returns true if this server is listening on it's sockets.


[View source]

def tls=(tls : OpenSSL::SSL::Context::Server?) #