about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-11-08 09:44:15 +0000
committerEric Wong <normalperson@yhbt.net>2011-11-08 09:44:15 +0000
commit0d908e6ba1af6e0cc229dc4d66a2a0e97c07164c (patch)
treec2cc15a390a87d9bf9a7a85e1438115f1859f661
parent9be9c8b2ca91938025d9066ebde5c832da04f0ef (diff)
downloadmogilefs-client-0d908e6ba1af6e0cc229dc4d66a2a0e97c07164c.tar.gz
GET is all we need and we can save some code this way.
If we ever need to use HEAD much again, we can use
net/http/persistent since our internal HTTP classes
are only optimized for large responses.
-rw-r--r--lib/mogilefs/bigfile.rb4
-rw-r--r--lib/mogilefs/http_reader.rb25
-rw-r--r--lib/mogilefs/mogilefs.rb2
3 files changed, 11 insertions, 20 deletions
diff --git a/lib/mogilefs/bigfile.rb b/lib/mogilefs/bigfile.rb
index cfd6580..8d5c75f 100644
--- a/lib/mogilefs/bigfile.rb
+++ b/lib/mogilefs/bigfile.rb
@@ -33,7 +33,7 @@ module MogileFS::Bigfile
       next if part_nr == 0 # info[:parts][0] is always empty
 
       begin
-        sock = MogileFS::HTTPReader.first(part[:paths], "GET", t)
+        sock = MogileFS::HTTPReader.first(part[:paths], t)
       rescue
         # part[:paths] may not be valid anymore due to rebalancing, however we
         # can get_keys on key,<part_nr> and retry paths if all paths fail
@@ -42,7 +42,7 @@ module MogileFS::Bigfile
         paths.empty? and
           raise MogileFS::Backend::NoDevices,
                 "no device for key=#{part_key.inspect}", []
-        sock = MogileFS::HTTPReader.first(paths, "GET", t)
+        sock = MogileFS::HTTPReader.first(paths, t)
       end
 
       w = MogileFS::X.copy_stream(sock, wr_io)
diff --git a/lib/mogilefs/http_reader.rb b/lib/mogilefs/http_reader.rb
index 76ba328..ca0e46b 100644
--- a/lib/mogilefs/http_reader.rb
+++ b/lib/mogilefs/http_reader.rb
@@ -19,30 +19,30 @@ class MogileFS::HTTPReader < MogileFS::Socket
           "read=#{buf.size} bytes, expected=#@content_length from #@uri", []
   end
 
-  def self.first(paths, http_method, timeout)
+  def self.first(paths, timeout)
     errors = nil
     paths.each do |path|
       begin
-        sock = try(path, http_method, timeout) and return sock
+        sock = try(path, timeout) and return sock
       rescue => e
         errors ||= []
         errors << "#{path} - #{e.message} (#{e.class})"
       end
     end
     raise MogileFS::Error,
-          "all paths failed with #{http_method}: #{errors.join(', ')}", []
+          "all paths failed with GET: #{errors.join(', ')}", []
   end
 
   # given a path, this returns a readable socket with ready data from the
   # body of the response.
-  def self.try(path, http_method, timeout)
+  def self.try(path, timeout)
     uri = URI.parse(path)
     sock = tcp(uri.host, uri.port, timeout)
-    buf = "#{http_method} #{uri.request_uri} HTTP/1.0\r\n\r\n" # no chunking
+    buf = "GET #{uri.request_uri} HTTP/1.0\r\n\r\n" # no chunking
     sock.timed_write(buf, timeout)
 
     sock.timed_peek(2048, buf, timeout) or
-      raise MogileFS::InvalidResponseError, "EOF on #{http_method} #{uri}", []
+      raise MogileFS::InvalidResponseError, "EOF while reading header", []
 
     head, _ = buf.split(/\r\n\r\n/, 2)
 
@@ -52,19 +52,10 @@ class MogileFS::HTTPReader < MogileFS::Socket
        head =~ %r{^Content-Length:\s*(\d+)}i
       sock.content_length = $1.to_i
       sock.uri = uri
-
-      case http_method
-      when "HEAD"
-        sock.close
-      else # "GET"
-        # slice off the top of the socket buffer to allow IO.copy_stream
-        # to work
-        sock.timed_read(head.bytesize + 4, buf, 0)
-      end
+      sock.timed_read(head.bytesize + 4, buf, 0)
       return sock
     end
-    raise MogileFS::InvalidResponseError,
-          "#{http_method} on #{uri} returned: #{head.inspect}", []
+    raise MogileFS::InvalidResponseError, "header=#{head.inspect}", []
   rescue
     sock.close if sock && ! sock.closed?
     raise
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index e253de5..c5fc96b 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -58,7 +58,7 @@ class MogileFS::MogileFS < MogileFS::Client
 
   def get_file_data(key, dest = nil)
     paths = get_paths(key)
-    sock = MogileFS::HTTPReader.first(paths, "GET", @get_file_data_timeout)
+    sock = MogileFS::HTTPReader.first(paths, @get_file_data_timeout)
     if dest
       MogileFS::X.copy_stream(sock, dest)
     elsif block_given?