extras/try_gzip_static.rb in yahns-1.12.5 vs extras/try_gzip_static.rb in yahns-1.13.0
- old
+ new
@@ -47,11 +47,11 @@
if ims = env["HTTP_IF_MODIFIED_SINCE"]
return [ 304, {}, [] ] if st.mtime.httpdate == ims
end
size = st.size
- ranges = Rack::Utils.byte_ranges(env, size)
+ ranges = byte_ranges(env, size)
if ranges.nil? || ranges.length > 1
[ 200 ] # serve the whole thing, possibly with static gzip \o/
elsif ranges.empty?
res = r(416)
res[1]["Content-Range"] = "bytes */#{size}"
@@ -90,11 +90,16 @@
end
def stat_path(env)
path = fspath(env) or return r(403)
begin
- st = File.stat(path)
+ st = File.lstat(path)
+ if st.symlink?
+ path = File.readlink(path)
+ path[0] == '/'.freeze or path = "#@root/#{path}"
+ st = File.stat(path)
+ end
return r(404) unless st.file?
return r(403) unless st.readable?
[ path, st ]
rescue Errno::ENOENT, Errno::ENOTDIR
r(404)
@@ -201,18 +206,27 @@
if env && exc && logger = env["rack.logger"]
msg = exc.message if exc.respond_to?(:message)
msg = msg.dump if /[[:cntrl:]]/ =~ msg # prevent code injection
logger.warn("#{env['REQUEST_METHOD']} #{env['PATH_INFO']} " \
"#{code} #{msg}")
- if exc.respond_to?(:backtrace)
+ if exc.respond_to?(:backtrace) && !(SystemCallError === exc)
exc.backtrace.each { |line| logger.warn(line) }
end
end
if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(code)
[ code, {}, [] ]
else
- h = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
- [ code, h, [] ]
+ msg = "#{code} #{Rack::Utils::HTTP_STATUS_CODES[code.to_i]}\n"
+ h = { 'Content-Type' => 'text/plain', 'Content-Length' => msg.size.to_s }
+ [ code, h, [ msg ] ]
end
+ end
+
+ if Rack::Utils.respond_to?(:get_byte_ranges) # rack 2.0+
+ def byte_ranges(env, size)
+ Rack::Utils.get_byte_ranges(env['HTTP_RANGE'], size)
+ end
+ else # rack 1.x
+ def byte_ranges(env, size); Rack::Utils.byte_ranges(env, size); end
end
end