Skip to content

Instantly share code, notes, and snippets.

@zlu
Created July 14, 2009 03:33
Show Gist options
  • Save zlu/146687 to your computer and use it in GitHub Desktop.
Save zlu/146687 to your computer and use it in GitHub Desktop.
#Post script
require 'rubygems'
require 'open-uri'
require 'cgi'
require 'net/http'
require 'mime/types'
#Module sprinkling in Multipart for net/http
module Multipart
VERSION = "1.0.0" unless const_defined?(:VERSION)
# Formats a given hash as a multipart form post
# If a hash value responds to :string or :read messages, then it is
# interpreted as a file and processed accordingly; otherwise, it is assumed
# to be a string
class Post
# We have to pretend like we're a web browser...
USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6" unless const_defined?(:USERAGENT)
BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210" unless const_defined?(:BOUNDARY)
CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }" unless const_defined?(:CONTENT_TYPE)
HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT } unless const_defined?(:HEADER)
def self.prepare_query(params)
fp = []
params.each do |k, v|
# Are we trying to make a file parameter?
if v.respond_to?(:path) and v.respond_to?(:read) then
fp.push(FileParam.new(k, v.path, v.read))
# We must be trying to make a regular parameter
else
fp.push(StringParam.new(k, v))
end
end
# Assemble the request body using the special multipart format
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
return query, HEADER
end
end
private
# Formats a basic string key/value pair for inclusion with a multipart post
class StringParam
attr_accessor :k, :v
def initialize(k, v)
@k = k
@v = v
end
def to_multipart
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
end
end
# Formats the contents of a file or string for inclusion with a multipart
# form post
class FileParam
attr_accessor :k, :filename, :content
def initialize(k, filename, content)
@k = k
@filename = filename
@content = content
end
def to_multipart
# If we can tell the possible mime-type from the filename, use the
# first in the list; otherwise, use "application/octet-stream"
mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
"Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
end
end
end
upload_uri = URI.parse "http://sandite.orl.voxeo.net/"
data, headers = Multipart::Post.prepare_query("arrested" => "development", "my_file" => File.open("transcription_test.mp3"))
http = Net::HTTP.new(upload_uri.host, upload_uri.port)
res = http.start {|con| con.post(upload_uri.path, data, headers) }
p res
#Server side log
{"my_file"=>{:type=>"audio/mpeg", :tempfile=>#<File:/tmp/RackMultipart20090714-1027-198umfh-0>, :head=>"Content-Disposition: form-data; name=\"my_file\"; filename=\"transcription_test.mp3\"\r\nContent-Type: audio/mpeg\r\n", :filename=>"transcription_test.mp3", :name=>"my_file"}, "arrested"=>"development"}
71.202.25.141 - - [14/Jul/2009 00:52:44] "POST / " 200 8 0.0024
#Client side log
$ jruby uploader.rb
#<Net::HTTPOK 200 OK readbody=true>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment