1 # -*- encoding: binary -*-
2 # here are internal implementation details, do not use them in your code
5 require 'mogilefs/chunker'
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
21 class NonRetryableError < MogileFS::Error; end
24 # The URI this file will be stored to.
31 # The big_io name in case we have file > 256M
35 attr_accessor :streaming_io
38 # Creates a new HTTPFile with MogileFS-specific data. Use
39 # MogileFS::MogileFS#new_file instead of this method.
41 def initialize(dests, content_length)
43 @streaming_io = @big_io = @uri = @devid = @active = nil
47 def request_put(sock, uri, file_size, input = nil)
49 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
50 "Content-Length: #{file_size}\r\n\r\n")
51 input ? MogileFS::X.copy_stream(@active = input, sock) : yield(sock)
53 sock.write("PUT #{uri.request_uri} HTTP/1.1\r\n" \
54 "Host: #{uri.host}:#{uri.port}\r\n" \
55 "Transfer-Encoding: chunked\r\n\r\n")
56 tmp = MogileFS::Chunker.new(sock)
57 rv = input ? MogileFS::X.copy_stream(@active = input, tmp) : yield(tmp)
63 def put_streaming_io(sock, uri) # unlikely to be used
64 file_size = @streaming_io.length
66 request_put(sock, uri, file_size) do |wr|
67 @streaming_io.call(Proc.new do |data_to_write|
68 written += wr.write(data_to_write)
71 file_size ? file_size : written
74 def rewind_or_raise!(uri, err)
75 @active.rewind if @active
77 msg = "#{uri} failed with #{err.message} (#{err.class}) and " \
78 "retrying is impossible as rewind on " \
79 "#{@active.inspect} failed with: #{e.message} (#{e.class})"
80 raise NonRetryableError, msg, e.backtrace
84 # Writes an HTTP PUT request to +sock+ to upload the file and
85 # returns file size if the socket finished writing
86 def upload(devid, uri) # :nodoc:
87 sock = MogileFS::Socket.tcp(uri.host, uri.port)
91 file_size = put_streaming_io(sock, uri)
93 if String === @big_io || @big_io.respond_to?(:to_path)
94 file ||= File.open(@big_io)
96 file_size = request_put(sock, uri, stat.file? ? stat.size : nil, file)
99 if @big_io.respond_to?(:stat)
101 size = stat.size if stat.file?
102 elsif @big_io.respond_to?(:size)
105 file_size = request_put(sock, uri, size, @big_io)
108 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
109 "Content-Length: #{file_size}\r\n\r\n#{string}")
112 case line = sock.timed_read(23, "")
113 when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
116 raise EmptyResponseError, 'Unable to read response line from server'
117 when %r{^HTTP/\d\.\d\s+(\d+)}
118 raise BadResponseError, "HTTP response status from upload: #$1"
120 raise UnparseableResponseError, "Response line not understood: #{line}"
123 rewind_or_raise!(uri, err)
132 @dests.each do |devid, path|
134 uri = URI.parse(path)
135 bytes_uploaded = upload(devid, uri)
136 @devid, @uri = devid, uri
137 return bytes_uploaded
138 rescue NonRetryableError
142 errors << "#{path} - #{e.message} (#{e.class})"
146 raise NoStorageNodesError,
147 "all paths failed with PUT: #{errors.join(', ')}", []