Class: MogileFS::HTTPFile

Inherits:
StringIO
  • Object
show all
Defined in:
lib/mogilefs/httpfile.rb

Overview

HTTPFile wraps up the new file operations for storing files onto an HTTP storage node.

You really don’t want to create an HTTPFile by hand. Instead you want to create a new file using MogileFS::MogileFS.new_file.

WARNING! HTTP mode is completely untested as I cannot make it work on FreeBSD. Please send patches/tests if you find bugs. – TODO dup’d content in MogileFS::NFSFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mg, fid, path, devid, klass, key, dests, content_length) ⇒ HTTPFile

Creates a new HTTPFile with MogileFS-specific data. Use MogileFS::MogileFS#new_file instead of this method.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mogilefs/httpfile.rb', line 57

def initialize(mg, fid, path, devid, klass, key, dests, content_length)
  super ''
  @mg = mg
  @fid = fid
  @path = path
  @devid = devid
  @klass = klass
  @key = key

  @dests = dests.map { |(_,u)| URI.parse u }
  @tried = {}

  @socket = nil
end

Instance Attribute Details

#classObject (readonly)

The class of this file.



35
36
37
# File 'lib/mogilefs/httpfile.rb', line 35

def class
  @class
end

#keyObject (readonly)

The key for this file. This key won’t represent a real file until you’ve called #close.



30
31
32
# File 'lib/mogilefs/httpfile.rb', line 30

def key
  @key
end

#pathObject (readonly)

The path this file will be stored to.



24
25
26
# File 'lib/mogilefs/httpfile.rb', line 24

def path
  @path
end

Class Method Details

.open(*args) ⇒ Object

Works like File.open. Use MogileFS::MogileFS#new_file instead of this method.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mogilefs/httpfile.rb', line 41

def self.open(*args)
  fp = new(*args)

  return fp unless block_given?

  begin
    yield fp
  ensure
    fp.close
  end
end

Instance Method Details

#closeObject

Closes the file handle and marks it as closed in MogileFS.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mogilefs/httpfile.rb', line 75

def close
  connect_socket

  @socket.write "PUT #{@path.request_uri} HTTP/1.0\r\nContent-length: #{length}\r\n\r\n#{string}"

  if connected? then
    line = @socket.gets
    raise 'Unable to read response line from server' if line.nil?

    if line =~ %r%^HTTP/\d+\.\d+\s+(\d+)% then
      status = Integer $1
      case status
      when 200..299 then # success!
      else
        found_header = false
        body = []
        line = @socket.gets
        until line.nil? do
          line.strip
          found_header = true if line.nil?
          next unless found_header
          body << " #{line}"
        end
        body = body[0, 512] if body.length > 512
        raise "HTTP response status from upload: #{body}"
      end
    else
      raise "Response line not understood: #{line}"
    end
    @socket.close
  end

  @mg.backend.create_close(:fid => @fid, :devid => @devid,
                           :domain => @mg.domain, :key => @key,
                           :path => @path, :size => length)
  return nil
end