about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-23 13:07:15 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-23 13:07:56 -0800
commit9b6c5f2fdf5cac77fda7fb506fe09d22cd18b8ab (patch)
tree27fdb8edcefc98cd81703d177955d9f3c60fd109
parent285e93871168720742f5b631a2acce775c671d12 (diff)
downloadmetropolis-9b6c5f2fdf5cac77fda7fb506fe09d22cd18b8ab.tar.gz
fix readonly interface
use 403 to reject PUT/DELETE requests
-rw-r--r--lib/metropolis/common.rb4
-rw-r--r--lib/metropolis/tc/hdb.rb4
-rw-r--r--lib/metropolis/tc/hdb/ro.rb6
-rw-r--r--test/rack_read_write.rb20
-rw-r--r--test/test_tc_hdb.rb8
5 files changed, 31 insertions, 11 deletions
diff --git a/lib/metropolis/common.rb b/lib/metropolis/common.rb
index 3bfbf77..cdae590 100644
--- a/lib/metropolis/common.rb
+++ b/lib/metropolis/common.rb
@@ -22,10 +22,10 @@ module Metropolis::Common
       when "PUT"
         put(key, env)
       else
-        [ 405, {}, [] ]
+        r(405)
       end
     else # OPTIONS
-      [ 405, {}, [] ]
+      r(405)
     end
   end
 end
diff --git a/lib/metropolis/tc/hdb.rb b/lib/metropolis/tc/hdb.rb
index eed46ef..871876d 100644
--- a/lib/metropolis/tc/hdb.rb
+++ b/lib/metropolis/tc/hdb.rb
@@ -41,7 +41,7 @@ module Metropolis::TC::HDB
     @dbv = (0...@nr_slots).to_a.map do |slot|
       path = sprintf(path_pattern, slot)
       hdb = TCHDB.new
-      unless opts[:read_only]
+      unless opts[:readonly]
         hdb.open(path, TCHDB::OWRITER | TCHDB::OCREAT) or ex!(:open, hdb)
         if @optimize
           hdb.optimize(*@optimize) or ex!(:optimize, hdb)
@@ -52,7 +52,7 @@ module Metropolis::TC::HDB
     end
     @rd_flags = TCHDB::OREADER
     @wr_flags = TCHDB::OWRITER
-    if opts[:read_only]
+    if opts[:readonly]
       extend(RO)
     end
   end
diff --git a/lib/metropolis/tc/hdb/ro.rb b/lib/metropolis/tc/hdb/ro.rb
index f348ee4..4114606 100644
--- a/lib/metropolis/tc/hdb/ro.rb
+++ b/lib/metropolis/tc/hdb/ro.rb
@@ -6,7 +6,7 @@ module Metropolis::TC::HDB::RO
       @wr_flags = nil
       @rd_flags |= TokyoCabinet::HDB::ONOLCK
       @dbv.each { |(hdb, path)|
-        hdb.open(path, @rd_flags) or ex!(:open, path)
+        hdb.open(path, @rd_flags) or ex!(:open, hdb)
       }
       @ro_dbv = @dbv.map { |(hdb,_)| hdb }
     end
@@ -21,10 +21,10 @@ module Metropolis::TC::HDB::RO
       when "HEAD"
         head(key)
       else
-        [ 405, {}, [] ]
+        r(403)
       end
     else # OPTIONS
-      [ 405, {}, [] ]
+      r(405)
     end
   end
 
diff --git a/test/rack_read_write.rb b/test/rack_read_write.rb
index f9b0de2..8cbeb04 100644
--- a/test/rack_read_write.rb
+++ b/test/rack_read_write.rb
@@ -38,4 +38,24 @@ module TestRackReadWrite
     assert_equal 404, r.status
     assert_equal "", r.body
   end
+
+  def test_rack_readonly
+    tmp = Metropolis.new(:uri => uri)
+    tmp.close!
+    app = Metropolis.new(:uri => uri, :readonly => true)
+    o = { :lint => true, :fatal => true }
+    req = Rack::MockRequest.new(app)
+
+    r = req.put("/asdf", o.merge(:input=>"ASDF"))
+    assert_equal 403, r.status
+
+    r = req.get("/asdf")
+    assert_equal 404, r.status
+
+    r = req.request("HEAD", "/asdf", {})
+    assert_equal 404, r.status
+
+    r = req.delete("/asdf", {})
+    assert_equal 403, r.status
+  end
 end
diff --git a/test/test_tc_hdb.rb b/test/test_tc_hdb.rb
index ccff861..7a9d548 100644
--- a/test/test_tc_hdb.rb
+++ b/test/test_tc_hdb.rb
@@ -137,7 +137,7 @@ class Test_TC_HDB < Test::Unit::TestCase
     o = Object.new
     o.extend Metropolis::TC::HDB
     assert_nothing_raised do
-      o.setup :path_pattern => @path_pattern, :read_only => true
+      o.setup :path_pattern => @path_pattern, :readonly => true
     end
     %w(PUT DELETE).each do |rm|
       env = {
@@ -145,7 +145,7 @@ class Test_TC_HDB < Test::Unit::TestCase
         "REQUEST_METHOD" => rm,
         "PATH_INFO" => "/#{key}"
       }
-      assert_equal 405, o.call(env)[0]
+      assert_equal 403, o.call(env)[0]
     end
     env = {
       "REQUEST_METHOD" => "GET",
@@ -183,11 +183,11 @@ class Test_TC_HDB < Test::Unit::TestCase
     assert_nothing_raised { obj.get(k) }
     assert_nothing_raised { obj.put(k,{'rack.input' => StringIO.new(data)}) }
 
-    obj = Metropolis.new(:uri => "#{uri}?#{query}", :read_only => true)
+    obj = Metropolis.new(:uri => "#{uri}?#{query}", :readonly => true)
     assert_equal data, obj.get(k)[2].join('')
     obj.close!
 
-    obj = Metropolis.new(:uri => uri, :read_only => true)
+    obj = Metropolis.new(:uri => uri, :readonly => true)
     assert_equal data, obj.get(k)[2].join('')
     obj.close!
     sum = obj.instance_eval {