Skip to content

Commit

Permalink
Fix bugs. Ok, so it didn't work before now.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksieger committed Aug 12, 2008
1 parent 4c2db18 commit e3451f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/composite_io.rb
Expand Up @@ -54,5 +54,6 @@ def local_path
"#{local_path}" "#{local_path}"
end end
EOS EOS
io
end end
end end
22 changes: 18 additions & 4 deletions lib/net/http/post/multipart.rb
Expand Up @@ -18,8 +18,8 @@ def length


def build_part(boundary, name, value) def build_part(boundary, name, value)
part = '' part = ''
part << "#{boundary}\r\n" part << "--#{boundary}\r\n"
part << "Content-Disposition: form-data; name=\"#{CGI::escape name.to_s}\"\r\n" part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n"
part << "\r\n" part << "\r\n"
part << "#{value}\r\n" part << "#{value}\r\n"
end end
Expand Down Expand Up @@ -47,8 +47,8 @@ def length


def build_head(boundary, name, filename, type) def build_head(boundary, name, filename, type)
part = '' part = ''
part << "#{boundary}\r\n" part << "--#{boundary}\r\n"
part << "Content-Disposition: form-data; name=\"#{CGI::escape name.to_s}\"; filename=\"#{filename}\"\r\n" part << "Content-Disposition: form-data; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n"
part << "Content-Type: #{type}\r\n" part << "Content-Type: #{type}\r\n"
part << "Content-Transfer-Encoding: binary\r\n" part << "Content-Transfer-Encoding: binary\r\n"
part << "\r\n" part << "\r\n"
Expand All @@ -59,6 +59,19 @@ def to_io
end end
end end


class ClosingBoundary
def initialize(boundary)
@part = "--#{boundary}--\r\n"
@io = StringIO.new(@part)
end
def length
@part.length
end
def to_io
@io
end
end

class Part class Part
def self.new(boundary, name, value) def self.new(boundary, name, value)
if value.respond_to? :content_type if value.respond_to? :content_type
Expand All @@ -75,6 +88,7 @@ class Multipart < Post
def initialize(path, params, boundary = DEFAULT_BOUNDARY) def initialize(path, params, boundary = DEFAULT_BOUNDARY)
super(path) super(path)
parts = params.map {|k,v| Part.new(boundary, k, v)} parts = params.map {|k,v| Part.new(boundary, k, v)}
parts << ClosingBoundary.new(boundary)
ios = parts.map{|p| p.to_io } ios = parts.map{|p| p.to_io }
self.set_content_type("multipart/form-data", { "boundary" => boundary }) self.set_content_type("multipart/form-data", { "boundary" => boundary })
self.content_length = parts.inject(0) {|sum,i| sum + i.length } self.content_length = parts.inject(0) {|sum,i| sum + i.length }
Expand Down

0 comments on commit e3451f5

Please sign in to comment.