Class: Waitress::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/waitress/handlers/handler.rb

Overview

The Handler class is responsible for handling incoming HTTP requests for a given URL Path. This default class works by matching a regular expression to the request path, however subclasses may choose to use their own matching methods (see Waitress::DirHandler).

Each Handler acts off of a priority system, where if multiple handlers can respond to the request, the one with the highest priority will be chosen.

Direct Known Subclasses

DirHandler, ErrorHandler, LibraryHandler, REST

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regex = nil, priority = 50, &action) ⇒ Handler

Create a new Regex-Based Handler Params:

regex

The regex pattern to match against the request path

priority

Priority of the handler. Default: 50

action

The block to call when a match is reached. Should take args

request, response, client and vhost.



20
21
22
23
24
# File 'lib/waitress/handlers/handler.rb', line 20

def initialize regex=nil, priority=50, &action
  @regex = regex
  @action = action
  @priority = priority
end

Instance Attribute Details

#priorityObject

Returns the value of attribute priority.



12
13
14
# File 'lib/waitress/handlers/handler.rb', line 12

def priority
  @priority
end

Instance Method Details

#respond?(request, vhost) ⇒ Boolean

Returns true if this handler is valid for the given request

Returns:

  • (Boolean)


27
28
29
# File 'lib/waitress/handlers/handler.rb', line 27

def respond? request, vhost
  (request.path =~ @regex) != nil
end

#serve(request, response, client, vhost) ⇒ Object

If we can respond to the request, this method is called to serve a response based on this handler. Do your response logic here. Params:

request

The Waitress::Request object

response

The Waitress::Response object

client

The client socket

vhost

The Virtual Host responsible for the connection



44
45
46
# File 'lib/waitress/handlers/handler.rb', line 44

def serve request, response, client, vhost
  @action.call(request, response, client, vhost) unless @action.nil?
end

#serve!(request, response, client, vhost) ⇒ Object

Don’t touch this – this adds Kernel bindings



32
33
34
35
# File 'lib/waitress/handlers/handler.rb', line 32

def serve! request, response, client, vhost
  kernel_prepare
  serve request, response, client, vhost
end