Sha256: 066c55686b511bafc4ce2aa20fa43de8bda4b1e5579bfcbbd7ed61a1d00ee451

Contents?: true

Size: 1.43 KB

Versions: 23

Compression:

Stored size: 1.43 KB

Contents

module Faye
  class StaticServer

    def initialize(directory, path_regex)
      @directory  = directory
      @path_regex = path_regex
      @path_map   = {}
      @index      = {}
    end

    def map(request_path, filename)
      @path_map[request_path] = filename
    end

    def =~(pathname)
      @path_regex =~ pathname
    end

    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

  end
end

Version data entries

23 entries across 23 versions & 4 rubygems

Version Path
faye-1.4.0 lib/faye/adapters/static_server.rb
faye-1.3.0 lib/faye/adapters/static_server.rb
faye-1.2.5 lib/faye/adapters/static_server.rb
faye-1.1.3 lib/faye/adapters/static_server.rb
faye-1.0.4 lib/faye/adapters/static_server.rb
faye-1.2.4 lib/faye/adapters/static_server.rb
faye-1.2.3 lib/faye/adapters/static_server.rb
faye-1.2.2 lib/faye/adapters/static_server.rb
faye-1.2.1 lib/faye/adapters/static_server.rb
faye-1.2.0 lib/faye/adapters/static_server.rb
faye-ouvrages-1.1.2.1 lib/faye/adapters/static_server.rb
faye-ouvrages-1.1.2 lib/faye/adapters/static_server.rb
faye-1.1.2 lib/faye/adapters/static_server.rb
faye-1.1.1 lib/faye/adapters/static_server.rb
faye-1.1.0 lib/faye/adapters/static_server.rb
faye-huboard-1.0.5 lib/faye/adapters/static_server.rb
faye-huboard-1.0.4 lib/faye/adapters/static_server.rb
faye-0.8.11 lib/faye/adapters/static_server.rb
faye-1.0.3 lib/faye/adapters/static_server.rb
faye-1.0.1 lib/faye/adapters/static_server.rb