Class: Riddl::Utils::FileServe

Inherits:
Implementation show all
Defined in:
lib/ruby/riddl/utils/fileserve.rb

Instance Method Summary collapse

Methods inherited from Implementation

#headers, #initialize, #status

Constructor Details

This class inherits a constructor from Riddl::Implementation

Instance Method Details

#responseObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby/riddl/utils/fileserve.rb', line 7

def response
  path = File.file?(@a[0]) ? @a[0] : "#{@a[0]}/#{@r[@match.length-1..-1].join('/')}".gsub(/\/+/,'/')

  if File.directory?(path)
    @status = 404
    return []
  end
  if File.exists?(path)
    mtime = File.mtime(path)
    @headers << Riddl::Header.new("Last-Modified",mtime.httpdate)
    @headers << Riddl::Header.new("ETag",Digest::MD5.hexdigest(mtime.httpdate))
    htime = @env["HTTP_IF_MODIFIED_SINCE"].nil? ? Time.at(0) : Time.parse(@env["HTTP_IF_MODIFIED_SINCE"])
    if htime == mtime
      @headers << Riddl::Header.new("Connection","close")
      @status = 304 # Not modified
      return []
    else 
      return Riddl::Parameter::Complex.new("file",MIME::Types.type_for(path).first.to_s,File.open(path,'r'))
    end  
  end
  @status = 404
end