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::DeflateHandler.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, 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, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, 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

==(other : Class) ==, ===(other) ===, cast(other) : self cast, clone clone, dup dup, from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |

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 #

[View source]
def close #

[View source]
def listen #

[View source]
def port #

[View source]

def tls=(tls : OpenSSL::SSL::Context::Server | Nil) #