server: handle "permission denied" error

This commit is contained in:
0x1eef 2023-06-26 05:34:58 -03:00
parent 3e640c22a2
commit e0fffe0ac8
3 changed files with 16 additions and 5 deletions

View file

@ -10,8 +10,12 @@ class Server::Dir
headers = Rack::Headers.new(env)
h, body = read(local_path(req.path))
[200, headers.merge!(h), body]
rescue Errno::EPERM, Errno::EACCES
body = "Permission denied"
[403, {"content-length" => body.bytesize, "content-type" => "text/plain"}, [body]]
rescue Errno::ENOENT
not_found
body = "The requested URL was not found"
[404, {"content-length" => body.bytesize, "content-type" => "text/plain"}, [body]]
end
private
@ -31,8 +35,4 @@ class Server::Dir
lpath = File.join root, File.expand_path(req_path)
File.directory?(lpath) ? File.join(lpath, "index.html") : lpath
end
def not_found
[404, {"content-type" => "text/plain"}, ["The requested URL was not found"]]
end
end

View file

@ -11,6 +11,17 @@ class ServerDirTest < Test::Unit::TestCase
assert_equal bytesize("./test/fakeweb/index.html"), last_response.content_length
end
def test_permission_denied
File.chmod 0, "./test/fakeweb/permission_denied.html"
get "/permission_denied.html"
assert_equal 403, last_response.status
assert_equal "text/plain", last_response.content_type
assert_equal "Permission denied".bytesize, last_response.content_length
assert_equal "Permission denied", last_response.body
ensure
File.chmod 0440, "./test/fakeweb/permission_denied.html"
end
private
def app