def send_request args
args[:verb] ||= args[:method]
args[:verb] ||= :get
verb = args[:verb].to_s.upcase
unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
set_deferred_status :failed, {:status => 0}
return
end
request = args[:request] || "/"
unless request[0,1] == "/"
request = "/" + request
end
qs = args[:query_string] || ""
if qs.length > 0 and qs[0,1] != '?'
qs = "?" + qs
end
version = args[:version] || "1.1"
host = args[:host_header] || args[:host] || "_"
port = args[:port]
postcontenttype = args[:contenttype] || "application/octet-stream"
postcontent = args[:content] || ""
raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength
req = [
"#{verb} #{request}#{qs} HTTP/#{version}",
"Host: #{host}:#{port}",
"User-agent: Ruby EventMachine",
]
if verb == "POST" || verb == "PUT"
req << "Content-type: #{postcontenttype}"
req << "Content-length: #{postcontent.length}"
end
if args[:cookie]
req << "Cookie: #{args[:cookie]}"
end
args[:custom_headers].each do |k,v|
req << "#{k}: #{v}"
end if args[:custom_headers]
if args[:basic_auth]
basic_auth_string = ["#{args[:basic_auth][:username]}:#{args[:basic_auth][:password]}"].pack('m').strip.gsub(/\n/,'')
req << "Authorization: Basic #{basic_auth_string}"
end
req << ""
reqstring = req.map {|l| "#{l}\r\n"}.join
send_data reqstring
if verb == "POST" || verb == "PUT"
send_data postcontent
end
end