def send_file(req_path, request, response, header_only=false)
stat = File.stat(req_path)
mtime = stat.mtime
etag = Const::ETAG_FORMAT % [mtime.to_i, stat.size, stat.ino]
modified_since = request.params[Const::HTTP_IF_MODIFIED_SINCE]
none_match = request.params[Const::HTTP_IF_NONE_MATCH]
same_response = case
when modified_since && !last_response_time = Time.httpdate(modified_since) rescue nil : false
when modified_since && last_response_time > Time.now : false
when modified_since && mtime > last_response_time : false
when none_match && none_match == '*' : false
when none_match && !none_match.strip.split(/\s*,\s*/).include?(etag) : false
else modified_since || none_match
end
header = response.header
header[Const::ETAG] = etag
if same_response
response.start(304) {}
else
response.status = 200
header[Const::LAST_MODIFIED] = mtime.httpdate
dot_at = req_path.rindex('.')
if dot_at
header[Const::CONTENT_TYPE] = MIME_TYPES[req_path[dot_at .. -1]] || @default_content_type
else
header[Const::CONTENT_TYPE] = @default_content_type
end
response.send_status(stat.size)
response.send_header
if not header_only
response.send_file(req_path, stat.size < Const::CHUNK_SIZE * 2)
end
end
end