Class: Radio::HTTP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/radio/http/server.rb

Constant Summary collapse

ENV_THREAD_BYPASS =
'radio.thread_bypass'

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



30
31
32
# File 'lib/radio/http/server.rb', line 30

def initialize
  @working_dir = Dir.getwd
end

Instance Method Details

#call(env) ⇒ Array

Rack interface.

Parameters:

  • env (Hash)

    Rack environment.

Returns:

  • (Array)
    status, headers, body


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/radio/http/server.rb', line 61

def call(env)
  # The preprocessing left us with nothing, a response,
  # or a filename that we should try to run.
  case deferred_result = env.delete(ENV_THREAD_BYPASS)
  when String
    filename = deferred_result
    response = Script.new(env, filename).response
    if response.header["X-Cascade"] == 'pass'
      index_response = Script.new(env, filename + '/index').response
      response = index_response unless index_response.header["X-Cascade"] == 'pass'
    end
    response.finish
  when NilClass
    not_found
  else
    deferred_result.finish
  end
end

#deferred?(env) ⇒ Boolean

Thin can run some processes in threads if we provide the logic. Static files are served without deferring to a thread. Everything else is tried in the EventMachine thread pool.

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/radio/http/server.rb', line 37

def deferred? env
  path_info = Rack::Utils.unescape(env['PATH_INFO'])
  return false if path_info =~ /\.(\.|erb$)/ # unsafe '..' and '.erb'
  MOUNTS.each do |path, dir|
    if path_info =~ %r{^#{Regexp.escape(path)}(/.*|)$}
      filename = File.join(dir, $1)
      Dir.chdir @working_dir
      response = FileResponse.new(env, filename)
      if !response.found? and File.extname(path_info) == ''
        response = FileResponse.new(env, filename + '.html')
      end
      if response.found?
        env[ENV_THREAD_BYPASS] = response
        return false
      end
      env[ENV_THREAD_BYPASS] = filename
    end
  end
  return true
end

#not_foundArray

Status 404 with X-Cascade => pass.

Returns:

  • (Array)
    status, headers, body


82
83
84
85
86
87
88
89
90
# File 'lib/radio/http/server.rb', line 82

def not_found
  return @not_found if @not_found
  body = "404 Not Found\n"
  @not_found = [404, {'Content-Type' => 'text/plain',
         'Content-Length' => body.size.to_s,
         'X-Cascade' => 'pass'},
   [body]]
  @not_found
end