HTTP::Server

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

Class Method Detail

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

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

def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler), &handler : Context -> )Source

def self.new(port, handlers : Array(HTTP::Handler), &handler : Context -> )Source

def self.new(host : String, port : Int32, &handler : Context -> )Source

def self.new(port, &handler : Context -> )Source

def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler))Source

def self.new(host : String, port : Int32, handler : HTTP::Handler | HTTP::Handler::Proc)Source

def self.new(port, handlers : Array(HTTP::Handler))Source

def self.new(port, handler)Source

Instance Method Detail

def bind(reuse_port = false)Source

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.

def closeSource

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

def listen(reuse_port = false)Source

Starts the server. Blocks until the server is closed.

See #bind for details on the reuse_port argument.

def portSource

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

def tls : OpenSSL::SSL::Context::Server?

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

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/HTTP/Server.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部