about summary refs log tree commit homepage
path: root/lib/mogilefs/http_file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mogilefs/http_file.rb')
-rw-r--r--lib/mogilefs/http_file.rb73
1 files changed, 4 insertions, 69 deletions
diff --git a/lib/mogilefs/http_file.rb b/lib/mogilefs/http_file.rb
index 0d6faf5..bd0b53b 100644
--- a/lib/mogilefs/http_file.rb
+++ b/lib/mogilefs/http_file.rb
@@ -1,10 +1,7 @@
 # -*- encoding: binary -*-
 # here are internal implementation details, do not use them in your code
-require 'socket'
 require 'stringio'
-require 'uri'
-require 'digest/md5'
-require 'mogilefs/chunker'
+require 'mogilefs/new_file_common'
 
 ##
 # HTTPFile wraps up the new file operations for storing files onto an HTTP
@@ -14,16 +11,7 @@ require 'mogilefs/chunker'
 # create a new file using MogileFS::MogileFS.new_file.
 #
 class MogileFS::HTTPFile < StringIO
-  class RetryableError < MogileFS::Error; end
-  class EmptyResponseError < RetryableError; end
-  class BadResponseError < RetryableError; end
-  class UnparseableResponseError < RetryableError; end
-  class NoStorageNodesError < MogileFS::Error
-    def message; 'Unable to open socket to storage node'; end
-  end
-  class NonRetryableError < MogileFS::Error; end
-
-  MD5_TRAILER_NODES = {} # :nodoc: # EXPERIMENTAL
+  include MogileFS::NewFileCommon
 
   ##
   # The big_io name in case we have file > 256M
@@ -132,18 +120,8 @@ class MogileFS::HTTPFile < StringIO
       request_put(sock, uri, file_size, self)
     end
 
-    # mostly relying on SO_KEEPALIVE to timeout
-    case line = sock.timed_read(23, "", 7200)
-    when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
-      file_size
-    when nil
-      raise EmptyResponseError, 'Unable to read response line from server'
-    when %r{^HTTP/\d\.\d\s+(\d+)}
-      raise BadResponseError, "HTTP response status from upload: #$1"
-    else
-      raise UnparseableResponseError,
-            "Response line not understood: #{line.inspect}"
-    end
+    read_response(sock) # raises on errors
+    file_size
     rescue SystemCallError, RetryableError => err
       rewind_or_raise!(uri, err)
       raise
@@ -169,51 +147,8 @@ class MogileFS::HTTPFile < StringIO
           "all paths failed with PUT: #{errors.join(', ')}", []
   end
 
-  def create_close(devid, uri, bytes_uploaded)
-    args = {
-      :fid => @opts[:fid],
-      :devid => devid,
-      :key => @opts[:key],
-      :domain => @opts[:domain],
-      :size => bytes_uploaded,
-      :path => uri.to_s,
-    }
-    if @md5
-      args[:checksum] = "MD5:#{@md5.hexdigest}"
-    elsif String === @opts[:content_md5]
-      hex = @opts[:content_md5].unpack('m')[0].unpack('H*')[0]
-      args[:checksum] = "MD5:#{hex}"
-    end
-    args[:checksumverify] = 1 if @opts[:checksumverify]
-    @opts[:backend].create_close(args)
-    bytes_uploaded
-  end
-
   def close
     commit
     super
   end
-
-  # :stopdoc:
-  # aggressive keepalive settings on Linux + Ruby 1.9.2+
-  TCP_KEEPALIVE = {
-    :TCP_KEEPIDLE => 60, # seconds time before keepalive packet is sent
-    :TCP_KEEPINTVL => 5,
-    :TCP_KEEPCNT => 2,  # number of retries
-  }
-
-  req_consts = TCP_KEEPALIVE.keys
-  if (Socket.constants & req_consts).size == req_consts.size
-    def set_socket_options(sock)
-      sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 1)
-      TCP_KEEPALIVE.each do |k,v|
-        sock.setsockopt(:IPPROTO_TCP, k, v)
-      end
-    end
-  else
-    def set_socket_options(sock)
-      sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
-    end
-  end
-  # :startdoc:
 end