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 staus 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"

Server.new("127.0.0.1", 8080, [
  ErrorHandler.new,
  LogHandler.new,
  DeflateHandler.new,
  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,
  [
    ErrorHandler.new,
    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/server.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

! !, ==(other)
==(other : self)
==
, hash hash, inspect(io : IO) : Nil inspect, nil? nil?, object_id object_id, same?(other : Nil)
same?(other : Reference)
same?
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
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, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

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

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


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

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

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

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

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

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

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

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

[View source]

Instance Method Detail

def close #

[View source]
def listen #

[View source]
def ssl #

def ssl=(ssl) #