Class: DeltaAttack::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/delta_attack/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, content = nil) ⇒ Client

Returns a new instance of Client.



30
31
32
33
# File 'lib/delta_attack/client.rb', line 30

def initialize(filename, content=nil)
  @filename = filename
  @content = content
end

Instance Attribute Details

#content_typeObject



43
44
45
# File 'lib/delta_attack/client.rb', line 43

def content_type
  @content_type ||= FiletypeAssumption.new(File.basename(@filename)).content_type
end

Class Method Details

.cast(filename, content_type = nil, host = "localhost", port = 3333) ⇒ Object Also known as: extract



9
10
11
# File 'lib/delta_attack/client.rb', line 9

def cast(filename, content_type = nil, host="localhost", port=3333)
  cast_buf(nil, filename, content_type, host, port)
end

.cast_buf(content, filename = "no-filename", content_type = nil, host = "localhost", port = 3333) ⇒ Object Also known as: extract_buf



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/delta_attack/client.rb', line 14

def cast_buf(content, filename = "no-filename", content_type = nil, host="localhost", port=3333)
  begin
    client = new(filename, content)
    client.content_type = content_type
    res = Net::HTTP.start(host, port){|http| http.request(client.request) }
    raise "Request failed #{res}" unless res.is_a? Net::HTTPOK
    res.body
  rescue Errno::ECONNREFUSED => e
    raise "DeltaAttack Server is down on http://#{host}:#{port}"
  end
end

Instance Method Details

#bodyObject



47
48
49
50
51
52
53
54
# File 'lib/delta_attack/client.rb', line 47

def body
  data = ''
  data << "--#{boundary}\r\n"
  data << "Content-Disposition: form-data; name=\"file\"; filename=\"#{@filename}\"\r\n"
  data << "Content-Type: #{content_type}\r\n\r\n"
  data << content
  data << "\r\n--#{boundary}--\r\n"
end

#boundaryObject



35
36
37
# File 'lib/delta_attack/client.rb', line 35

def boundary
  @boundary ||= Digest::SHA1.hexdigest(File.read(__FILE__))[0,8]
end

#contentObject



39
40
41
# File 'lib/delta_attack/client.rb', line 39

def content
  @content ||= File.open(@filename,"rb"){|f| f.read }
end

#request(path = "/extract") ⇒ Object



56
57
58
59
60
61
62
# File 'lib/delta_attack/client.rb', line 56

def request(path = "/extract" )
  req = Net::HTTP::Post.new(path)
  req.content_type = "multipart/form-data; boundary=#{boundary}"
  req.body = body
  req.content_length = req.body.size
  req
end