| 1 |
module ActionController |
|---|
| 2 |
module Integration |
|---|
| 3 |
class Session |
|---|
| 4 |
def multipart_post(url, parameters, headers = {}) |
|---|
| 5 |
boundary = "----------XnJLe9ZIbbGUYtzPQJ16u1" |
|---|
| 6 |
post url, multipart_body(parameters, boundary), headers.merge({"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}"}) |
|---|
| 7 |
end |
|---|
| 8 |
|
|---|
| 9 |
def multipart_requestify(params) |
|---|
| 10 |
returning p = {} do |
|---|
| 11 |
params.each do |key, value| |
|---|
| 12 |
if Hash === value |
|---|
| 13 |
value.each do |subkey, subvalue| |
|---|
| 14 |
p["#{CGI.escape(key.to_s)}[#{CGI.escape(subkey.to_s)}]"] = subvalue |
|---|
| 15 |
end |
|---|
| 16 |
else |
|---|
| 17 |
p[CGI.escape(key.to_s)] = value |
|---|
| 18 |
end |
|---|
| 19 |
end |
|---|
| 20 |
end |
|---|
| 21 |
end |
|---|
| 22 |
|
|---|
| 23 |
def multipart_body(params, boundary) |
|---|
| 24 |
multipart_requestify(params).map do |key, value| |
|---|
| 25 |
if value.respond_to?(:original_filename) |
|---|
| 26 |
File.open(value.path) do |f| |
|---|
| 27 |
<<-EOF |
|---|
| 28 |
-- |
|---|
| 29 |
Content-Disposition: form-data; name="#{key}"; filename="#{CGI.escape(value.original_filename)}"\r |
|---|
| 30 |
Content-Type: |
|---|
| 31 |
Content-Length: |
|---|
| 32 |
\r |
|---|
| 33 |
|
|---|
| 34 |
EOF |
|---|
| 35 |
end |
|---|
| 36 |
else |
|---|
| 37 |
<<-EOF |
|---|
| 38 |
-- |
|---|
| 39 |
Content-Disposition: form-data; name="#{key}"\r |
|---|
| 40 |
\r |
|---|
| 41 |
|
|---|
| 42 |
EOF |
|---|
| 43 |
end |
|---|
| 44 |
end.join("")+"--#{boundary}--\r" |
|---|
| 45 |
end |
|---|
| 46 |
end |
|---|
| 47 |
end |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|