Class: Reel::Rack::Server

Inherits:
Server::HTTP
  • Object
show all
Includes:
Celluloid::Internals::Logger
Defined in:
lib/reel/rack/server.rb

Constant Summary collapse

CONTENT_LENGTH_HEADER =

Compile the regex once

%r{^content-length$}i
NO_PREFIX_HEADERS =

Those headers must not start with ‘HTTP_’.

%w[CONTENT_TYPE CONTENT_LENGTH].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
# File 'lib/reel/rack/server.rb', line 13

def initialize(app, options)
  raise ArgumentError, "no host given" unless options[:Host]
  raise ArgumentError, "no port given" unless options[:Port]

  info  "A Reel good HTTP server! (Codename \"#{::Reel::CODENAME}\")"
  info "Listening on http://#{options[:Host]}:#{options[:Port]}"

  super(options[:Host], options[:Port], options, &method(:on_connection))
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/reel/rack/server.rb', line 11

def app
  @app
end

Instance Method Details

#convert_headers(headers) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reel/rack/server.rb', line 86

def convert_headers(headers)
  Hash[headers.map { |key, value|
    header = key.upcase.gsub('-','_')

    if NO_PREFIX_HEADERS.member?(header)
      [header, value]
    else
      ['HTTP_' + header, value]
    end
  }]
end

#default_server_port(env) ⇒ Object



114
115
116
# File 'lib/reel/rack/server.rb', line 114

def default_server_port(env)
  env['HTTP_X_FORWARDED_PROTO'] == 'https' ? 443 : 80
end

#normalize_env(env) ⇒ Object

Copied from lib/puma/server.rb



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/reel/rack/server.rb', line 99

def normalize_env(env)
  if ( host = env["HTTP_HOST"] )
    if ( colon = host.index(":") )
      env["SERVER_NAME"] = host[0, colon]
      env["SERVER_PORT"] = host[colon+1, host.bytesize]
    else
      env["SERVER_NAME"] = host
      env["SERVER_PORT"] = default_server_port(env)
    end
  else
    env["SERVER_NAME"] = "localhost"
    env["SERVER_PORT"] = default_server_port(env)
  end
end

#on_connection(connection) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reel/rack/server.rb', line 24

def on_connection(connection)
  connection.each_request do |request|
    if request.websocket?
      connection.detach
      route_websocket request
      return  nil
    else
      route_request request
    end
  end
end

#route_request(request) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/reel/rack/server.rb', line 52

def route_request(request)
  options = {
    :method       => request.method,
    :input        => request.body.to_s,
    "REMOTE_ADDR" => request.remote_addr
  }.merge(convert_headers(request.headers))

  normalize_env(options)

  status, headers, body = app.call ::Rack::MockRequest.env_for(request.url, options)

  if body.respond_to? :each
    # If Content-Length was specified we can send the response all at once
    if headers.keys.detect { |h| h =~ CONTENT_LENGTH_HEADER }
      # Can't use collect here because Rack::BodyProxy/Rack::Lint isn't a real Enumerable
      full_body = ''
      body.each { |b| full_body << b }
      request.respond status_symbol(status), headers, full_body
    else
      request.respond status_symbol(status), headers.merge(:transfer_encoding => :chunked)
      body.each { |chunk| request << chunk }
      request.finish_response
    end
  else
    Logger.error("don't know how to render: #{request.url}")
    request.respond :internal_server_error, "An error occurred processing your request"
  end

  body.close if body.respond_to? :close
end

#route_websocket(request) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/reel/rack/server.rb', line 36

def route_websocket( request )
  options = {
    :method       => request.method,
    :input        => request.body.to_s,
    "websocket"   => request.websocket,
  }.merge(convert_headers(request.headers))

  normalize_env(options)

  app.call  ::Rack::MockRequest.env_for(request.url, options)
  nil
end

#status_symbol(status) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/reel/rack/server.rb', line 118

def status_symbol(status)
  if status.is_a?(Integer)
    Reel::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym
  else
    status.to_sym
  end
end