Permalink
Browse files

allows http_post(array_of_fields) when using multipart form posts

  • Loading branch information...
1 parent c33bf15 commit 2c9f36da6d2332b8ced99c6d0bfc03b4e7f76946 @taf2 committed Jan 20, 2012
Showing with 72 additions and 11 deletions.
  1. +3 −3 curb.gemspec
  2. +16 −7 ext/curb_easy.c
  3. +39 −0 tests/bug_crash_on_debug.rb
  4. +14 −1 tests/tc_curl_easy.rb
View
@@ -1,8 +1,8 @@
Gem::Specification.new do |s|
s.name = "curb"
s.authors = ["Ross Bamford", "Todd A. Fisher"]
- s.version = '0.7.18'
- s.date = '2012-01-16'
+ s.version = '0.8.0'
+ s.date = '2012-01-19'
s.description = %q{Curb (probably CUrl-RuBy or something) provides Ruby-language bindings for the libcurl(3), a fully-featured client-side URL transfer library. cURL and libcurl live at http://curl.haxx.se/}
s.email = 'todd.fisher@gmail.com'
s.extra_rdoc_files = ['LICENSE', 'README']
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.require_paths = ['lib','ext']
s.rubyforge_project = 'curb'
s.summary = %q{Ruby libcurl bindings}
- s.test_files = ["tests/alltests.rb", "tests/bug_crash_on_progress.rb", "tests/bug_curb_easy_blocks_ruby_threads.rb", "tests/bug_curb_easy_post_with_string_no_content_length_header.rb", "tests/bug_instance_post_differs_from_class_post.rb", "tests/bug_multi_segfault.rb", "tests/bug_postfields_crash.rb", "tests/bug_postfields_crash2.rb", "tests/bug_require_last_or_segfault.rb", "tests/bugtests.rb", "tests/helper.rb", "tests/mem_check.rb", "tests/require_last_or_segfault_script.rb", "tests/tc_curl_download.rb", "tests/tc_curl_easy.rb", "tests/tc_curl_easy_setopt.rb", "tests/tc_curl_multi.rb", "tests/tc_curl_postfield.rb", "tests/timeout.rb", "tests/timeout_server.rb", "tests/unittests.rb"]
+ s.test_files = ["tests/alltests.rb", "tests/bug_crash_on_debug.rb", "tests/bug_crash_on_progress.rb", "tests/bug_curb_easy_blocks_ruby_threads.rb", "tests/bug_curb_easy_post_with_string_no_content_length_header.rb", "tests/bug_instance_post_differs_from_class_post.rb", "tests/bug_multi_segfault.rb", "tests/bug_postfields_crash.rb", "tests/bug_postfields_crash2.rb", "tests/bug_require_last_or_segfault.rb", "tests/bugtests.rb", "tests/helper.rb", "tests/mem_check.rb", "tests/require_last_or_segfault_script.rb", "tests/tc_curl_download.rb", "tests/tc_curl_easy.rb", "tests/tc_curl_easy_setopt.rb", "tests/tc_curl_multi.rb", "tests/tc_curl_postfield.rb", "tests/timeout.rb", "tests/timeout_server.rb", "tests/unittests.rb"]
s.extensions << 'ext/extconf.rb'
View
@@ -1887,7 +1887,6 @@ VALUE ruby_curl_easy_setup( ruby_curl_easy *rbce ) {
url = rb_check_string_type(_url);
- // Need to configure the handler as per settings in rbce
curl_easy_setopt(curl, CURLOPT_URL, StringValuePtr(url));
// network stuff and auth
@@ -2322,6 +2321,17 @@ static VALUE ruby_curl_easy_perform_post(int argc, VALUE *argv, VALUE self) {
for (i = 0; i < argc; i++) {
if (rb_obj_is_instance_of(argv[i], cCurlPostField)) {
append_to_form(argv[i], &first, &last);
+ } else if (rb_type(argv[i]) == T_ARRAY) {
+ // see: https://github.com/rvanlieshout/curb/commit/8bcdefddc0162484681ebd1a92d52a642666a445
+ int c = 0, argv_len = (int)RARRAY_LEN(argv[i]);
+ for (; c < argv_len; ++c) {
+ if (rb_obj_is_instance_of(rb_ary_entry(argv[i],c), cCurlPostField)) {
+ append_to_form(rb_ary_entry(argv[i],c), &first, &last);
+ } else {
+ rb_raise(eCurlErrInvalidPostField, "You must use PostFields only with multipart form posts");
+ return Qnil;
+ }
+ }
} else {
rb_raise(eCurlErrInvalidPostField, "You must use PostFields only with multipart form posts");
return Qnil;
@@ -2395,10 +2405,9 @@ static VALUE ruby_curl_easy_set_head_option(VALUE self, VALUE onoff) {
Data_Get_Struct(self, ruby_curl_easy, rbce);
- if( onoff == Qtrue ) {
+ if (onoff == Qtrue) {
curl_easy_setopt(rbce->curl, CURLOPT_NOBODY, 1);
- }
- else {
+ } else {
curl_easy_setopt(rbce->curl, CURLOPT_NOBODY, 0);
}
@@ -2415,13 +2424,13 @@ static VALUE ruby_curl_easy_set_head_option(VALUE self, VALUE onoff) {
static VALUE ruby_curl_easy_perform_put(VALUE self, VALUE data) {
ruby_curl_easy *rbce;
CURL *curl;
-
+
Data_Get_Struct(self, ruby_curl_easy, rbce);
curl = rbce->curl;
-
+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, NULL);
ruby_curl_easy_put_data_set(self, data);
-
+
return rb_funcall(self, rb_intern("perform"), 0);
}
@@ -0,0 +1,39 @@
+require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
+
+require 'webrick'
+class ::WEBrick::HTTPServer ; def access_log(config, req, res) ; end ; end
+class ::WEBrick::BasicLog ; def log(level, data) ; end ; end
+
+require 'curl'
+
+class BugCrashOnDebug < Test::Unit::TestCase
+
+ def test_on_debug
+ server = WEBrick::HTTPServer.new( :Port => 9999 )
+ server.mount_proc("/test") do|req,res|
+ res.body = "hi"
+ res['Content-Type'] = "text/html"
+ end
+ puts 'a'
+ thread = Thread.new(server) do|srv|
+ srv.start
+ end
+ puts 'b'
+ c = Curl::Easy.new('http://localhost:9999/test')
+ c.on_debug do|x|
+ puts x.inspect
+ raise "error" # this will get swallowed
+ end
+ c.perform
+ puts 'c'
+ ensure
+ puts 'd'
+ server.shutdown
+ puts 'e'
+ puts thread.exit
+ puts 'f'
+ end
+
+end
+
+#test_on_debug
View
@@ -621,6 +621,19 @@ def test_post_remote_is_easy_handle
end
end
end
+
+ # see: https://github.com/rvanlieshout/curb/commit/8bcdefddc0162484681ebd1a92d52a642666a445
+ def test_post_multipart_array_remote
+ curl = Curl::Easy.new(TestServlet.url)
+ curl.multipart_form_post = true
+ fields = [
+ Curl::PostField.file('foo', File.expand_path(File.join(File.dirname(__FILE__),'..','README'))),
+ Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README')))
+ ]
+ curl.http_post(fields)
+ assert_match /HTTP POST file upload/, curl.body_str
+ assert_match /Content-Disposition: form-data/, curl.body_str
+ end
def test_post_with_body_remote
curl = Curl::Easy.new(TestServlet.url)
@@ -724,7 +737,7 @@ def test_put_nil_data_no_crash
def test_put_remote_file
curl = Curl::Easy.new(TestServlet.url)
- File.open(__FILE__,'r') do|f|
+ File.open(__FILE__,'rb') do|f|
assert curl.http_put(f)
end
assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str

0 comments on commit 2c9f36d

Please sign in to comment.