gus-curb 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +51 -0
  3. data/README.markdown +230 -0
  4. data/Rakefile +320 -0
  5. data/doc.rb +42 -0
  6. data/ext/curb.c +997 -0
  7. data/ext/curb.h +48 -0
  8. data/ext/curb_easy.c +3466 -0
  9. data/ext/curb_easy.h +90 -0
  10. data/ext/curb_errors.c +660 -0
  11. data/ext/curb_errors.h +132 -0
  12. data/ext/curb_macros.h +159 -0
  13. data/ext/curb_multi.c +641 -0
  14. data/ext/curb_multi.h +26 -0
  15. data/ext/curb_postfield.c +523 -0
  16. data/ext/curb_postfield.h +40 -0
  17. data/ext/curb_upload.c +80 -0
  18. data/ext/curb_upload.h +30 -0
  19. data/ext/extconf.rb +392 -0
  20. data/lib/curb.rb +1 -0
  21. data/lib/curl.rb +64 -0
  22. data/lib/curl/easy.rb +480 -0
  23. data/lib/curl/multi.rb +248 -0
  24. data/tests/alltests.rb +3 -0
  25. data/tests/bug_crash_on_debug.rb +39 -0
  26. data/tests/bug_crash_on_progress.rb +73 -0
  27. data/tests/bug_curb_easy_blocks_ruby_threads.rb +52 -0
  28. data/tests/bug_curb_easy_post_with_string_no_content_length_header.rb +83 -0
  29. data/tests/bug_instance_post_differs_from_class_post.rb +53 -0
  30. data/tests/bug_issue102.rb +17 -0
  31. data/tests/bug_multi_segfault.rb +14 -0
  32. data/tests/bug_postfields_crash.rb +26 -0
  33. data/tests/bug_postfields_crash2.rb +57 -0
  34. data/tests/bug_require_last_or_segfault.rb +40 -0
  35. data/tests/bugtests.rb +9 -0
  36. data/tests/helper.rb +204 -0
  37. data/tests/mem_check.rb +65 -0
  38. data/tests/require_last_or_segfault_script.rb +36 -0
  39. data/tests/signals.rb +33 -0
  40. data/tests/tc_curl.rb +39 -0
  41. data/tests/tc_curl_download.rb +75 -0
  42. data/tests/tc_curl_easy.rb +1038 -0
  43. data/tests/tc_curl_easy_setopt.rb +31 -0
  44. data/tests/tc_curl_multi.rb +485 -0
  45. data/tests/tc_curl_postfield.rb +143 -0
  46. data/tests/timeout.rb +100 -0
  47. data/tests/timeout_server.rb +33 -0
  48. data/tests/unittests.rb +2 -0
  49. metadata +123 -0
@@ -0,0 +1,53 @@
1
+ # From Vlad Jebelev:
2
+ #
3
+ # - Second thing - I think you just probably didn't have the time to update
4
+ # instance methods yet but when I POST with a reusal of a previous curl
5
+ # instance, it doesnt' work for me, e.g. when I create a curl previously and
6
+ # then issue:
7
+ #
8
+ # c.http_post(login_url, *fields)
9
+ #
10
+ # instead of:
11
+ #
12
+ # c = Curl::Easy.http_post(login_url, *fields) do |curl|
13
+ # ...
14
+ # end
15
+ #
16
+ # then the result I am getting is quite different.
17
+ #
18
+ # ================
19
+ #
20
+ # Update:
21
+ #
22
+ # It seems that class httpost is incorrectly passing arguments down to
23
+ # instance httppost. This bug is intermittent, but results in an
24
+ # exception from the first post when it occurs.
25
+ #
26
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
27
+
28
+ class BugTestInstancePostDiffersFromClassPost < Test::Unit::TestCase
29
+ def test_bug
30
+ 5.times do |i|
31
+ puts "Test ##{i}"
32
+ do_test
33
+ sleep 2
34
+ end
35
+ end
36
+
37
+ def do_test
38
+ c = Curl::Easy.http_post('https://www.google.com/accounts/ServiceLoginAuth',
39
+ Curl::PostField.content('ltmpl','m_blanco'))
40
+ body_c, header_c = c.body_str, c.header_str
41
+
42
+ sleep 2
43
+
44
+ c.http_post('https://www.google.com/accounts/ServiceLoginAuth',
45
+ Curl::PostField.content('ltmpl','m_blanco'))
46
+ body_i, header_i = c.body_str, c.header_str
47
+
48
+ # timestamps will differ, just check first bit. We wont get here if
49
+ # the bug bites anyway...
50
+ assert_equal header_c[0..50], header_i[0..50]
51
+ end
52
+ end
53
+
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class BugIssue102 < Test::Unit::TestCase
4
+
5
+ def test_interface
6
+ test = "https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true"
7
+ ip = "0.0.0.0"
8
+
9
+ c = Curl::Easy.new do |curl|
10
+ curl.url = test
11
+ curl.interface = ip
12
+ end
13
+
14
+ c.perform
15
+ end
16
+
17
+ end
@@ -0,0 +1,14 @@
1
+ # From safis http://github.com/taf2/curb/issues#issue/5
2
+ # irb: require 'curb'
3
+ # irb: multi = Curl::Multi.new
4
+ # irb: exit
5
+ # <main>:47140: [BUG] Bus Error
6
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'..','ext'))
7
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
8
+ require 'curb'
9
+
10
+ class BugMultiSegfault < Test::Unit::TestCase
11
+ def test_bug
12
+ multi = Curl::Multi.new
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ # From GICodeWarrior:
2
+ #
3
+ # $ ruby crash_curb.rb
4
+ # crash_curb.rb:7: [BUG] Segmentation fault
5
+ # ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
6
+ #
7
+ # Aborted
8
+ # crash_curb.rb:
9
+ # #!/usr/bin/ruby
10
+ # require 'rubygems'
11
+ # require 'curb'
12
+ #
13
+ # curl = Curl::Easy.new('http://example.com/')
14
+ # curl.multipart_form_post = true
15
+ # curl.http_post(Curl::PostField.file('test', 'test.xml'){'example data'})
16
+ # Ubuntu 9.10
17
+ # curb gem version 0.6.2.1
18
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
19
+
20
+ class BugPostFieldsCrash < Test::Unit::TestCase
21
+ def test_crash
22
+ curl = Curl::Easy.new('http://example.com/')
23
+ curl.multipart_form_post = true
24
+ curl.http_post(Curl::PostField.file('test', 'test.xml'){'example data'})
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ # Not sure if this is an IRB bug, but thought you guys should know.
2
+ #
3
+ # ** My Ruby version: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
4
+ # ** Version of Rubygems: 1.3.5
5
+ # ** Version of the Curb gem: 0.6.6.0
6
+ #
7
+ #
8
+ # Transcript of IRB session:
9
+ # ------------------------------------------------------------------------------------------------------------------
10
+ # irb(main):001:0> a = {
11
+ # irb(main):002:1* :type => :pie,
12
+ # irb(main):003:1* :series => {
13
+ # irb(main):004:2* :names => [:a,:b],
14
+ # irb(main):005:2* :values => [70,30],
15
+ # irb(main):006:2* :colors => [:red,:green]
16
+ # irb(main):007:2> },
17
+ # irb(main):008:1* :output_format => :png
18
+ # irb(main):009:1> }
19
+ # => {:type=>:pie, :output_format=>:png, :series=>{:names=>[:a, :b], :values=>[70, 30], :colors=>[:red, :green]}}
20
+ # irb(main):010:0> post = []
21
+ # => []
22
+ # irb(main):011:0> require 'rubygems'
23
+ # => true
24
+ # irb(main):012:0> require 'curb'
25
+ # => true
26
+ # irb(main):013:0> include Curl
27
+ # => Object
28
+ # irb(main):014:0> a.each_pair do |k,v|
29
+ # irb(main):015:1* post << PostField.content(k,v)
30
+ # irb(main):016:1> end
31
+ # => {:type=>:pie, :output_format=>:png, :series=>{:names=>[:a, :b], :values=>[70, 30], :colors=>[:red, :green]}}
32
+ # irb(main):017:0> post
33
+ # /usr/lib/ruby/1.8/irb.rb:302: [BUG] Segmentation fault
34
+ # ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
35
+ #
36
+ # Aborted
37
+ # ------------------------------------------------------------------------------------------------------------------
38
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
39
+
40
+ class BugPostFieldsCrash2 < Test::Unit::TestCase
41
+ def test_crash
42
+ a = {
43
+ :type => :pie,
44
+ :series => {
45
+ :names => [:a,:b],
46
+ :values => [70,30],
47
+ :colors => [:red,:green]
48
+ },
49
+ :output_format => :png
50
+ }
51
+ post = []
52
+ a.each_pair do |k,v|
53
+ post << Curl::PostField.content(k,v)
54
+ end
55
+ post.inspect
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ # From Vlad Jebelev:
2
+ #
3
+ # - if I have a require statement after "require 'curb'" and there is a
4
+ # POST with at least 1 field, the script will fail with a segmentation
5
+ # fault, e.g. the following sequence fails every time for me (Ruby 1.8.5):
6
+ # -----------------------------------------------------------------
7
+ # require 'curb'
8
+ # require 'uri'
9
+ #
10
+ # url = 'https://www.google.com/accounts/ServiceLoginAuth'
11
+ #
12
+ # c = Curl::Easy.http_post(
13
+ # 'https://www.google.com/accounts/ServiceLoginAuth',
14
+ # [Curl:: PostField.content('ltmpl','m_blanco')] ) do |curl|
15
+ # end
16
+ # ------------------------------------------------------------------
17
+ # :..dev/util$ ruby seg.rb
18
+ # seg.rb:6: [BUG] Segmentation fault
19
+ # ruby 1.8.5 (2006-08-25) [i686-linux]
20
+ #
21
+ # Aborted
22
+ # ------------------------------------------------------------------
23
+ #
24
+ require 'test/unit'
25
+ require 'rbconfig'
26
+
27
+ $rubycmd = RbConfig::CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
28
+
29
+ class BugTestRequireLastOrSegfault < Test::Unit::TestCase
30
+ def test_bug
31
+ 5.times do |i|
32
+ puts "Test ##{i}"
33
+
34
+ # will be empty string if it segfaults...
35
+ assert_equal 'success', `#$rubycmd #{File.dirname(__FILE__)}/require_last_or_segfault_script.rb`.chomp
36
+ sleep 5
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,9 @@
1
+ $: << $TESTDIR = File.expand_path(File.dirname(__FILE__))
2
+ puts "start"
3
+ begin
4
+ Dir[File.join($TESTDIR, 'bug_*.rb')].each { |lib| require lib }
5
+ rescue Object => e
6
+ puts e.message
7
+ ensure
8
+ puts "done"
9
+ end
@@ -0,0 +1,204 @@
1
+ # DO NOT REMOVE THIS COMMENT - PART OF TESTMODEL.
2
+ # Copyright (c)2006 Ross Bamford. See LICENSE.
3
+ $CURB_TESTING = true
4
+ require 'uri'
5
+
6
+ $TOPDIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
7
+ $EXTDIR = File.join($TOPDIR, 'ext')
8
+ $LIBDIR = File.join($TOPDIR, 'lib')
9
+ $:.unshift($LIBDIR)
10
+ $:.unshift($EXTDIR)
11
+
12
+ require 'curb'
13
+ begin
14
+ require 'test/unit'
15
+ rescue LoadError
16
+ gem 'test/unit'
17
+ require 'test/unit'
18
+ end
19
+ require 'fileutils'
20
+
21
+ $TEST_URL = "file://#{URI.escape(File.expand_path(__FILE__).tr('\\','/').tr(':','|'))}"
22
+
23
+ require 'thread'
24
+ require 'webrick'
25
+
26
+ # set this to true to avoid testing with multiple threads
27
+ # or to test with multiple threads set it to false
28
+ # this is important since, some code paths will change depending
29
+ # on the presence of multiple threads
30
+ TEST_SINGLE_THREADED=false
31
+
32
+ # keep webrick quiet
33
+ class ::WEBrick::HTTPServer
34
+ def access_log(config, req, res)
35
+ # nop
36
+ end
37
+ end
38
+ class ::WEBrick::BasicLog
39
+ def log(level, data)
40
+ # nop
41
+ end
42
+ end
43
+
44
+ #
45
+ # Simple test server to record number of times a request is sent/recieved of a specific
46
+ # request type, e.g. GET,POST,PUT,DELETE
47
+ #
48
+ class TestServlet < WEBrick::HTTPServlet::AbstractServlet
49
+
50
+ def self.port=(p)
51
+ @port = p
52
+ end
53
+
54
+ def self.port
55
+ (@port or 9129)
56
+ end
57
+
58
+ def self.path
59
+ '/methods'
60
+ end
61
+
62
+ def self.url
63
+ "http://127.0.0.1:#{port}#{path}"
64
+ end
65
+
66
+ def respond_with(method,req,res)
67
+ res.body = method.to_s
68
+ $auth_header = req['Authorization']
69
+ res['Content-Type'] = "text/plain"
70
+ end
71
+
72
+ def do_GET(req,res)
73
+ if req.path.match /redirect$/
74
+ res.status = 302
75
+ res['Location'] = '/foo'
76
+ elsif req.path.match /not_here$/
77
+ res.status = 404
78
+ elsif req.path.match /error$/
79
+ res.status = 500
80
+ end
81
+ respond_with("GET#{req.query_string}",req,res)
82
+ end
83
+
84
+ def do_HEAD(req,res)
85
+ res['Location'] = "/nonexistent"
86
+ respond_with("HEAD#{req.query_string}",req,res)
87
+ end
88
+
89
+ def do_POST(req,res)
90
+ if req.query['filename'].nil?
91
+ if req.body
92
+ params = {}
93
+ req.body.split('&').map{|s| k,v=s.split('='); params[k] = v }
94
+ end
95
+ if params and params['s'] == '500'
96
+ res.status = 500
97
+ else
98
+ respond_with("POST\n#{req.body}",req,res)
99
+ end
100
+ else
101
+ respond_with(req.query['filename'],req,res)
102
+ end
103
+ end
104
+
105
+ def do_PUT(req,res)
106
+ res['X-Requested-Content-Type'] = req.content_type
107
+ respond_with("PUT\n#{req.body}",req,res)
108
+ end
109
+
110
+ def do_DELETE(req,res)
111
+ respond_with("DELETE#{req.query_string}",req,res)
112
+ end
113
+
114
+ def do_PURGE(req,res)
115
+ respond_with("PURGE#{req.query_string}",req,res)
116
+ end
117
+
118
+ def do_COPY(req,res)
119
+ respond_with("COPY#{req.query_string}",req,res)
120
+ end
121
+
122
+ def do_PATCH(req,res)
123
+ respond_with("PATCH\n#{req.body}",req,res)
124
+ end
125
+
126
+ def do_OPTIONS(req,res)
127
+ respond_with("OPTIONS#{req.query_string}",req,res)
128
+ end
129
+
130
+ end
131
+
132
+ module TestServerMethods
133
+ def locked_file
134
+ File.join(File.dirname(__FILE__),"server_lock-#{@__port}")
135
+ end
136
+
137
+ def server_setup(port=9129,servlet=TestServlet)
138
+ @__port = port
139
+ if @server.nil? and !File.exist?(locked_file)
140
+
141
+ File.open(locked_file,'w') {|f| f << 'locked' }
142
+ if TEST_SINGLE_THREADED
143
+ rd, wr = IO.pipe
144
+ @__pid = fork do
145
+ rd.close
146
+ rd = nil
147
+
148
+ # start up a webrick server for testing delete
149
+ server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => File.expand_path(File.dirname(__FILE__))
150
+
151
+ server.mount(servlet.path, servlet)
152
+ server.mount("/ext", WEBrick::HTTPServlet::FileHandler, File.join(File.dirname(__FILE__),'..','ext'))
153
+
154
+ trap("INT") { server.shutdown }
155
+ GC.start
156
+ wr.flush
157
+ wr.close
158
+ server.start
159
+ end
160
+ wr.close
161
+ rd.read
162
+ rd.close
163
+ else
164
+ # start up a webrick server for testing delete
165
+ @server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => File.expand_path(File.dirname(__FILE__))
166
+
167
+ @server.mount(servlet.path, servlet)
168
+ @server.mount("/ext", WEBrick::HTTPServlet::FileHandler, File.join(File.dirname(__FILE__),'..','ext'))
169
+ queue = Queue.new # synchronize the thread startup to the main thread
170
+
171
+ @test_thread = Thread.new { queue << 1; @server.start }
172
+
173
+ # wait for the queue
174
+ value = queue.pop
175
+ if !value
176
+ STDERR.puts "Failed to startup test server!"
177
+ exit(1)
178
+ end
179
+
180
+ end
181
+
182
+ exit_code = lambda do
183
+ begin
184
+ if File.exist?(locked_file)
185
+ File.unlink locked_file
186
+ if TEST_SINGLE_THREADED
187
+ Process.kill 'INT', @__pid
188
+ else
189
+ @server.shutdown unless @server.nil?
190
+ end
191
+ end
192
+ #@server.shutdown unless @server.nil?
193
+ rescue Object => e
194
+ puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
195
+ end
196
+ end
197
+
198
+ trap("INT"){exit_code.call}
199
+ at_exit{exit_code.call}
200
+
201
+ end
202
+ rescue Errno::EADDRINUSE
203
+ end
204
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ #require 'rubygems'
3
+ #require 'rmem'
4
+
5
+ #
6
+ # Run some tests to measure the memory usage of curb, these tests require fork and ps
7
+ #
8
+ class TestCurbMemory < Test::Unit::TestCase
9
+
10
+ def test_easy_memory
11
+ easy_avg, easy_std = measure_object_memory(Curl::Easy)
12
+ printf "Easy average: %.2f kilobytes +/- %.2f kilobytes\n", easy_avg.to_f, easy_std.to_f
13
+
14
+ multi_avg, multi_std = measure_object_memory(Curl::Multi)
15
+ printf "Multi average: %.2f kilobytes +/- %.2f kilobytes\n", multi_avg.to_f, multi_std.to_f
16
+
17
+ # now that we have the average size of an easy handle lets see how much a multi request consumes with 10 requests
18
+ end
19
+
20
+ def c_avg(report)
21
+ sum = 0
22
+ report.each {|r| sum += r.last }
23
+ (sum.to_f / report.size)
24
+ end
25
+
26
+ def c_std(report,avg)
27
+ var = 0
28
+ report.each {|r| var += (r.last-avg)*(r.last-avg) }
29
+ Math.sqrt(var / (report.size-1))
30
+ end
31
+
32
+ def measure_object_memory(klass)
33
+ report = []
34
+ 200.times do
35
+ res = mem_check do
36
+ obj = klass.new
37
+ end
38
+ report << res
39
+ end
40
+ avg = c_avg(report)
41
+ std = c_std(report,avg)
42
+ [avg,std]
43
+ end
44
+
45
+ def mem_check
46
+ # see: http://gist.github.com/264060 for inspiration of ps command line
47
+ rd, wr = IO.pipe
48
+ memory_usage = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
49
+ fork do
50
+ before = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
51
+ rd.close
52
+ yield
53
+ after = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
54
+ wr.write((after - before))
55
+ wr.flush
56
+ wr.close
57
+ end
58
+ wr.close
59
+ total = rd.read.to_i
60
+ rd.close
61
+ Process.wait
62
+ # return the delta and the total
63
+ [memory_usage, total]
64
+ end
65
+ end