1 # -*- encoding: binary -*-
2 # here are internal implementation details, do not use them in your code
5 require 'mogilefs/backend'
8 # HTTPFile wraps up the new file operations for storing files onto an HTTP
11 # You really don't want to create an HTTPFile by hand. Instead you want to
12 # create a new file using MogileFS::MogileFS.new_file.
14 class MogileFS::HTTPFile < StringIO
15 class EmptyResponseError < MogileFS::Error; end
16 class BadResponseError < MogileFS::Error; end
17 class UnparseableResponseError < MogileFS::Error; end
18 class NoStorageNodesError < MogileFS::Error
19 def message; 'Unable to open socket to storage node'; end
23 # The URI this file will be stored to.
30 # The big_io name in case we have file > 256M
34 attr_accessor :streaming_io
37 # Creates a new HTTPFile with MogileFS-specific data. Use
38 # MogileFS::MogileFS#new_file instead of this method.
40 def initialize(dests, content_length)
42 @streaming_io = @big_io = @uri = @devid = nil
48 # Writes an HTTP PUT request to +sock+ to upload the file and
49 # returns file size if the socket finished writing
50 def upload(devid, uri) # :nodoc:
52 sock = MogileFS::Socket.tcp(uri.host, uri.port)
55 file_size = @streaming_io.length
56 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
57 "Content-Length: #{file_size}\r\n\r\n")
58 @streaming_io.call(Proc.new do |data_to_write|
59 sock.write(data_to_write)
62 # Don't try to run out of memory
63 File.open(@big_io) do |fp|
64 file_size = fp.stat.size
65 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
66 "Content-Length: #{file_size}\r\n\r\n")
67 MogileFS::X.copy_stream(fp, sock)
70 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
71 "Content-Length: #{length}\r\n\r\n#{string}")
74 case line = sock.timed_read(23, "")
75 when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
78 raise EmptyResponseError, 'Unable to read response line from server'
79 when %r{^HTTP/\d\.\d\s+(\d+)}
80 raise BadResponseError, "HTTP response status from upload: #$1"
82 raise UnparseableResponseError, "Response line not understood: #{line}"
85 sock.close if sock && ! sock.closed?
90 @dests.each do |devid, path|
93 bytes_uploaded = upload(devid, uri)
94 @devid, @uri = devid, uri
98 errors << "#{path} failed with #{e.message} (#{e.class})"
102 raise NoStorageNodesError,
103 "all paths failed with PUT: #{errors.join(', ')}", []