Skip to content

Instantly share code, notes, and snippets.

@bigcurl
Created May 17, 2011 10:51
Show Gist options
  • Save bigcurl/976287 to your computer and use it in GitHub Desktop.
Save bigcurl/976287 to your computer and use it in GitHub Desktop.
Upload a file to a webserver via socket
require 'thread'
require 'net/http'
require 'base64'
require 'openssl'
class Producer
def initialize
@mutex = Mutex.new
@body = ''
end
def read(size)
@mutex.synchronize {
@body.slice!(0,size)
}
end
def produce(str)
@mutex.synchronize {
@body << str
}
end
end
data = "--60079\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test.file\"\r\nContent-Type: application/x-ruby\r\n\r\nthis is just a test\r\n--60079--\r\n"
req = Net::HTTP::Post.new('/')
producer = Producer.new
req.body_stream = producer
req.content_length = data.length
req.content_type = "multipart/form-data; boundary=60079"
t1 = Thread.new do
producer.produce(data)
end
res = Net::HTTP.new('127.0.0.1', 9000).start {|http| http.request(req) }
puts res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment