19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/faye/adapters/static_server.rb', line 19
def call(env)
pathname = env['PATH_INFO']
pathname = File.basename(pathname)
pathname = @path_map[pathname] || pathname
cache = @index[pathname] ||= {}
fullpath = File.join(@directory, pathname)
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
= type.dup
ims = env['HTTP_IF_MODIFIED_SINCE']
no_content_length = env[RackAdapter::HTTP_X_NO_CONTENT_LENGTH]
['Content-Length'] = '0' unless no_content_length
['ETag'] = cache[:digest]
['Last-Modified'] = cache[:mtime].httpdate
if env['HTTP_IF_NONE_MATCH'] == cache[:digest]
[304, , ['']]
elsif ims and cache[:mtime] <= Time.httpdate(ims)
[304, , ['']]
else
['Content-Length'] = cache[:content].bytesize.to_s unless no_content_length
[200, , [cache[:content]]]
end
end
|