class Faye::StaticServer

Public Class Methods

new(directory, path_regex) click to toggle source
# File lib/faye/adapters/static_server.rb, line 4
def initialize(directory, path_regex)
  @directory  = directory
  @path_regex = path_regex
  @path_map   = {}
  @index      = {}
end

Public Instance Methods

=~(pathname) click to toggle source
# File lib/faye/adapters/static_server.rb, line 15
def =~(pathname)
  @path_regex =~ pathname
end
call(env) click to toggle source
# File lib/faye/adapters/static_server.rb, line 19
def call(env)
  filename = File.basename(env['PATH_INFO'])
  filename = @path_map[filename] || filename

  cache = @index[filename] ||= {}
  fullpath = File.join(@directory, filename)

  begin
    cache[:content] ||= File.read(fullpath)
    cache[:digest]  ||= Digest::SHA1.hexdigest(cache[:content])
    cache[:mtime]   ||= File.mtime(fullpath)
  rescue
    return [404, {}, []]
  end

  type = /\.js$/ =~ fullpath ? RackAdapter::TYPE_SCRIPT : RackAdapter::TYPE_JSON
  ims  = env['HTTP_IF_MODIFIED_SINCE']

  no_content_length = env[RackAdapter::HTTP_X_NO_CONTENT_LENGTH]

  headers = {
    'ETag'          => cache[:digest],
    'Last-Modified' => cache[:mtime].httpdate
  }

  if env['HTTP_IF_NONE_MATCH'] == cache[:digest]
    [304, headers, ['']]
  elsif ims and cache[:mtime] <= Time.httpdate(ims)
    [304, headers, ['']]
  else
    headers['Content-Length'] = cache[:content].bytesize.to_s unless no_content_length
    headers.update(type)
    [200, headers, [cache[:content]]]
  end
end
map(request_path, filename) click to toggle source
# File lib/faye/adapters/static_server.rb, line 11
def map(request_path, filename)
  @path_map[request_path] = filename
end