Class: ServerHere::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/server_here/core.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/server_here/core.rb', line 4

def call env
  req = Rack::Request.new env
  path = File.join('.', req.path_info)
  
  if not File.exists? path
    return [404, {'Content-Type' => 'text/plain'}, ["#{path} does not exist"] ]
  end
  
  if File.directory? path
    return [200, {'Content-Type' => 'text/plain'}, [`ls -al #{path}`] ]
  end
  
  file = FileWrapper.new path
  
  if_mod_since = req.get_header 'HTTP_IF_MODIFIED_SINCE'
  if if_mod_since and file.last_mod == if_mod_since
    return [304, {}, []]
  end
  
  header = {
    'Content-Type' => file.content_type,
    'Last-Modified' => file.last_mod
  }
  
  [200, header, file.to_body]
end