From e0fffe0ac869ee69bea5c95a2c2561003b0dccef Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Mon, 26 Jun 2023 05:34:58 -0300 Subject: [PATCH] server: handle "permission denied" error --- packages/ruby/server/lib/server/dir.rb | 10 +++++----- .../ruby/server/test/fakeweb/permission_denied.html | 0 packages/ruby/server/test/server_dir_test.rb | 11 +++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 packages/ruby/server/test/fakeweb/permission_denied.html diff --git a/packages/ruby/server/lib/server/dir.rb b/packages/ruby/server/lib/server/dir.rb index 1262908..537a6b7 100644 --- a/packages/ruby/server/lib/server/dir.rb +++ b/packages/ruby/server/lib/server/dir.rb @@ -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 diff --git a/packages/ruby/server/test/fakeweb/permission_denied.html b/packages/ruby/server/test/fakeweb/permission_denied.html new file mode 100644 index 0000000..e69de29 diff --git a/packages/ruby/server/test/server_dir_test.rb b/packages/ruby/server/test/server_dir_test.rb index 561e127..2f82b88 100644 --- a/packages/ruby/server/test/server_dir_test.rb +++ b/packages/ruby/server/test/server_dir_test.rb @@ -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