motion/http.rb in bubble-wrap-1.1.0 vs motion/http.rb in bubble-wrap-1.1.1
- old
+ new
@@ -20,23 +20,23 @@
#
def self.get(url, options={}, &block)
options[:action] = block if block_given?
HTTP::Query.new(url, :get, options)
end
-
+
# Make a POST request
def self.post(url, options={}, &block)
options[:action] = block if block_given?
HTTP::Query.new(url, :post, options)
end
-
+
# Make a PUT request
def self.put(url, options={}, &block)
options[:action] = block if block_given?
HTTP::Query.new(url, :put, options)
end
-
+
# Make a DELETE request
def self.delete(url, options={}, &block)
options[:action] = block if block_given?
HTTP::Query.new(url, :delete, options)
end
@@ -153,11 +153,13 @@
download_progress.call(@received_data.length.to_f, response_size)
end
end
def connection(connection, willSendRequest:request, redirectResponse:redirect_response)
- log "HTTP redirected info: #{request} - #{self.description}"
+ @redirection ||= 0
+ @redirection += 1
+ log "##{@redirection} HTTP redirection: #{request} - #{self.description}"
new_request = request.mutableCopy
# new_request.setValue(@credentials.inspect, forHTTPHeaderField:'Authorization') # disabled while we figure this one out
new_request.setAllHTTPHeaderFields(@headers) if @headers
@connection.cancel
@connection = create_connection(new_request, self)
@@ -216,39 +218,56 @@
end
def create_request_body
return nil if (@method == "GET" || @method == "HEAD")
return nil unless (@payload || @files)
- @headers = {"Content-Type" => "multipart/form-data; boundary=#{@boundary}"} if @headers.nil?
+ # if no headers provided, set content-type automatically
+ if @headers.nil?
+ @headers = {"Content-Type" => "multipart/form-data; boundary=#{@boundary}"}
+ # else set content type unless it is specified
+ # if content-type is specified, you should probably also specify
+ # the :boundary key
+ else
+ @headers['Content-Type'] = "multipart/form-data; boundary=#{@boundary}" unless @headers.keys.find {|k| k.downcase == 'content-type'}
+ end
+
body = NSMutableData.data
append_payload(body) if @payload
append_files(body) if @files
- body.appendData("\r\n--#{@boundary}--\r\n".dataUsingEncoding NSUTF8StringEncoding)
-
+ append_body_boundary(body) if @set_body_to_close_boundary
+
log "Built HTTP body: \n #{body.to_str}"
body
end
def append_payload(body)
if @payload.is_a?(NSData)
body.appendData(@payload)
else
append_form_params(body)
end
+ body
end
def append_form_params(body)
- @payload.each do |key, value|
- form_data = NSMutableData.new
- s = "\r\n--#{@boundary}\r\n"
- s += "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
- s += value.to_s
- form_data.appendData(s.dataUsingEncoding NSUTF8StringEncoding)
- body.appendData(form_data)
+ # puts "*** append_form #{@payload}"
+ if @payload.is_a?(String)
+ body.appendData(@payload.dataUsingEncoding NSUTF8StringEncoding)
+ else
+ @payload.each do |key, value|
+ form_data = NSMutableData.new
+ s = "\r\n--#{@boundary}\r\n"
+ s += "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
+ s += value.to_s
+ form_data.appendData(s.dataUsingEncoding NSUTF8StringEncoding)
+ body.appendData(form_data)
+ end
+ @set_body_to_close_boundary = true
end
+ body
end
def append_files(body)
@files.each do |key, value|
file_data = NSMutableData.new
@@ -257,12 +276,18 @@
s += "Content-Type: application/octet-stream\r\n\r\n"
file_data.appendData(s.dataUsingEncoding NSUTF8StringEncoding)
file_data.appendData(value)
body.appendData(file_data)
end
+ @set_body_to_close_boundary = true
+ body
end
+ def append_body_boundary(body)
+ body.appendData("\r\n--#{@boundary}--\r\n".dataUsingEncoding NSUTF8StringEncoding)
+ end
+
def create_url(url_string)
if (@method == "GET" || @method == "HEAD") && @payload
convert_payload_to_url if @payload.is_a?(Hash)
url_string += "?#{@payload}"
end
@@ -299,17 +324,17 @@
end
def escape_line_feeds(hash)
return nil if hash.nil?
escaped_hash = {}
-
+
hash.each{|k,v| escaped_hash[k] = v.gsub("\n", '\\n') }
escaped_hash
end
def patch_nsurl_request(request)
request.instance_variable_set("@done_loading", false)
-
+
def request.done_loading; @done_loading; end
def request.done_loading!; @done_loading = true; end
end
def call_delegator_with_response