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(8080) do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

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

With non-localhost bind address

require "http/server"

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

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

Add handlers

A series of handlers are chained.

require "http/server"

HTTP::Server.new("127.0.0.1", 8080, [
  HTTP::ErrorHandler.new,
  HTTP::LogHandler.new,
  HTTP::CompressHandler.new,
  HTTP::StaticFileHandler.new("."),
]).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("0.0.0.0", 8080,
  [
    HTTP::ErrorHandler.new,
    HTTP::LogHandler.new,
  ]) do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

server.listen

Defined in:

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

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

==(other : self)
==(other)
==
, dup dup, hash 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

Class 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 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

Class 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) : self from_yaml

Class Method Detail

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

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


[View source]
def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler), &handler : Context -> ) #

[View source]
def self.new(port, handlers : Array(HTTP::Handler), &handler : Context -> ) #

[View source]
def self.new(host : String, port : Int32, &handler : Context -> ) #

[View source]
def self.new(port, &handler : Context -> ) #

[View source]
def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler)) #

[View source]
def self.new(host : String, port : Int32, handler : HTTP::Handler | HTTP::Handler::Proc) #

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

[View source]
def self.new(port, handler) #

[View source]

Instance Method Detail

def bind(reuse_port = false) #

Creates the underlying TCPServer if the doesn't already exist.

You may set reuse_port to true to enable the SO_REUSEPORT socket option, which allows multiple processes to bind to the same port.


[View source]
def close #

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


[View source]
def listen(reuse_port = false) #

Starts the server. Blocks until the server is closed.

See #bind for details on the reuse_port argument.


[View source]
def port #

Returns the TCP port the server is connected to.

For example you may let the system choose a port, then report it:

server = HTTP::Server.new(0) { }
server.bind
server.port # => 12345

[View source]

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