Class: DeltaAttack::Client
- Inherits:
-
Object
- Object
- DeltaAttack::Client
- Defined in:
- lib/delta_attack/client.rb
Instance Attribute Summary collapse
Class Method Summary collapse
- .cast(filename, content_type = nil, host = "localhost", port = 3333) ⇒ Object (also: extract)
- .cast_buf(content, filename = "no-filename", content_type = nil, host = "localhost", port = 3333) ⇒ Object (also: extract_buf)
Instance Method Summary collapse
- #body ⇒ Object
- #boundary ⇒ Object
- #content ⇒ Object
-
#initialize(filename, content = nil) ⇒ Client
constructor
A new instance of Client.
- #request(path = "/extract") ⇒ Object
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_type ⇒ Object
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
#body ⇒ Object
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 |
#boundary ⇒ Object
35 36 37 |
# File 'lib/delta_attack/client.rb', line 35 def boundary @boundary ||= Digest::SHA1.hexdigest(File.read(__FILE__))[0,8] end |
#content ⇒ Object
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 |