patron 0.4.16 → 0.4.17

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -6,19 +6,19 @@ PATH
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.1.2)
10
- rake (0.8.7)
11
- rake-compiler (0.7.5)
9
+ diff-lcs (1.1.3)
10
+ rake (0.9.2.2)
11
+ rake-compiler (0.7.9)
12
12
  rake
13
- rcov (0.9.9)
14
- rspec (2.3.0)
15
- rspec-core (~> 2.3.0)
16
- rspec-expectations (~> 2.3.0)
17
- rspec-mocks (~> 2.3.0)
18
- rspec-core (2.3.1)
19
- rspec-expectations (2.3.0)
13
+ rcov (0.9.11)
14
+ rspec (2.7.0)
15
+ rspec-core (~> 2.7.0)
16
+ rspec-expectations (~> 2.7.0)
17
+ rspec-mocks (~> 2.7.0)
18
+ rspec-core (2.7.1)
19
+ rspec-expectations (2.7.0)
20
20
  diff-lcs (~> 1.1.2)
21
- rspec-mocks (2.3.0)
21
+ rspec-mocks (2.7.0)
22
22
 
23
23
  PLATFORMS
24
24
  ruby
@@ -26,6 +26,6 @@ PLATFORMS
26
26
  DEPENDENCIES
27
27
  bundler (>= 1.0.0)
28
28
  patron!
29
- rake-compiler (~> 0.7.5)
30
- rcov (~> 0.9.9)
31
- rspec (~> 2.3.0)
29
+ rake-compiler (>= 0.7.5)
30
+ rcov (>= 0.9.9)
31
+ rspec (>= 2.3.0)
data/Rakefile CHANGED
@@ -35,7 +35,7 @@ end
35
35
 
36
36
  Bundler::GemHelper.install_tasks
37
37
 
38
- CLEAN.include FileList["ext/patron/*"].exclude(/^.*\.(rb|c)$/)
38
+ CLEAN.include FileList["ext/patron/*"].exclude(/^.*\.(rb|c|h)$/)
39
39
  CLOBBER.include %w( doc coverage pkg )
40
40
 
41
41
  desc "Start an IRB shell"
@@ -40,9 +40,10 @@ elsif !have_library('curl') or !have_header('curl/curl.h')
40
40
  end
41
41
 
42
42
  if CONFIG['CC'] =~ /gcc/
43
- $CFLAGS << ' -Wall'
43
+ $CFLAGS << ' -pedantic -Wall'
44
44
  end
45
45
 
46
+ $defs.push("-DUSE_TBR")
46
47
  $defs.push("-DHAVE_TBR") if have_func('rb_thread_blocking_region')
47
48
 
48
49
  create_makefile 'patron/session_ext'
@@ -0,0 +1,85 @@
1
+ #include <ruby.h>
2
+ #include <assert.h>
3
+ #include "membuffer.h"
4
+
5
+ #define DEFAULT_CAPACITY 4096
6
+ #define MAXVAL(a, b) ((a) > (b) ? (a) : (b))
7
+
8
+ static int membuffer_ensure_capacity( membuffer* m, size_t length ) {
9
+ size_t new_capacity;
10
+ char* tmp_buf;
11
+
12
+ if (m->capacity >= length) { return MB_OK; }
13
+
14
+ new_capacity = MAXVAL(m->capacity, DEFAULT_CAPACITY);
15
+ while (new_capacity < length) { new_capacity *= 2; }
16
+
17
+ tmp_buf = ruby_xrealloc(m->buf, new_capacity+1);
18
+ if (NULL == tmp_buf) { return MB_OUT_OF_MEMORY; }
19
+ else {
20
+ m->buf = tmp_buf;
21
+ m->capacity = new_capacity;
22
+ }
23
+
24
+ return MB_OK;
25
+ }
26
+
27
+ void membuffer_init( membuffer* m ) {
28
+ assert(NULL != m);
29
+
30
+ m->buf = NULL;
31
+ m->length = 0;
32
+ m->capacity = 0;
33
+ }
34
+
35
+ void membuffer_destroy( membuffer* m ) {
36
+ if (NULL == m) { return; }
37
+
38
+ if (NULL != m->buf) { ruby_xfree(m->buf); }
39
+ m->buf = NULL;
40
+ m->length = 0;
41
+ m->capacity = 0;
42
+ }
43
+
44
+ void membuffer_clear( membuffer* m ) {
45
+ assert(NULL != m);
46
+
47
+ if (NULL != m->buf) {
48
+ memset(m->buf, 0, m->capacity+1);
49
+ m->length = 0;
50
+ }
51
+ }
52
+
53
+ int membuffer_insert( membuffer* m, size_t index, const void* src, size_t length ) {
54
+ int rc = MB_OK;
55
+ assert(NULL != m);
56
+
57
+ /* sanity checks on the inputs */
58
+ if (index > m->length) { return MB_OUT_OF_BOUNDS; }
59
+ if (NULL == src || 0 == length) { return MB_OK; }
60
+
61
+ /* increase capacity if needed */
62
+ rc = membuffer_ensure_capacity( m, m->length + length );
63
+ if (MB_OK != rc) { return rc; }
64
+
65
+ /* move data in the buffer to the right of the insertion point */
66
+ memmove( m->buf + index + length, m->buf + index, m->length - index );
67
+
68
+ /* copy date into the insertion point */
69
+ memcpy( m->buf + index, src, length );
70
+ m->length += length;
71
+ m->buf[m->length] = 0; /* null terminate the buffer */
72
+
73
+ return MB_OK;
74
+ }
75
+
76
+ int membuffer_append( membuffer* m, const void* src, size_t length ) {
77
+ assert(NULL != m);
78
+ return membuffer_insert( m, m->length, src, length );
79
+ }
80
+
81
+ VALUE membuffer_to_rb_str( membuffer* m ) {
82
+ assert(NULL != m);
83
+ return rb_str_new(m->buf, m->length);
84
+ }
85
+
@@ -0,0 +1,78 @@
1
+
2
+ #ifndef PATRON_MEMBUFER_H
3
+ #define PATRON_MEMBUFER_H
4
+
5
+ #include <stdlib.h>
6
+
7
+ #define MB_OK 0
8
+ #define MB_OUT_OF_MEMORY 1
9
+ #define MB_OUT_OF_BOUNDS 2
10
+
11
+ /**
12
+ * Implementation of a simple memory buffer for collecting the response body
13
+ * and headers from a curl request. The memory buffer will grow to accomodate
14
+ * the data inserted into it.
15
+ *
16
+ * When the memory buffer needs more capacity, it will reallocate memory from
17
+ * the heap. It will request twice it's current capacity.
18
+ */
19
+ typedef struct {
20
+ char *buf;
21
+ size_t length;
22
+ size_t capacity;
23
+ } membuffer;
24
+
25
+ /**
26
+ * Initialize the memory buffer by setting default values of 0 for the length
27
+ * and capacity.
28
+ */
29
+ void membuffer_init( membuffer* m );
30
+
31
+ /**
32
+ * Free any memory used by the memory buffer.
33
+ */
34
+ void membuffer_destroy( membuffer* m );
35
+
36
+ /**
37
+ * Clear the contents of the memory buffer. The length will be set to zero,
38
+ * but the capacity will remain unchanged - i.e. memory will not be freed by
39
+ * his method.
40
+ */
41
+ void membuffer_clear( membuffer* m );
42
+
43
+ /**
44
+ * Attempt to insert the given _src_ data into the memory buffer at the given
45
+ * _index_. This method will shift data in the memory buffer to the right in
46
+ * order to insert the _src_ data.
47
+ *
48
+ * This method will fail if the _index_ is out of bounds for the memory
49
+ * buffer, if the _src_ is NULL or the _length_ to insert is 0. This method
50
+ * can also fail if the memory buffer needs to expand it's capacity but no
51
+ * memory is available.
52
+ *
53
+ * Return Codes:
54
+ * MB_OK
55
+ * MB_OUT_OF_MEMORY
56
+ * MB_OUT_OF_BOUNDS
57
+ */
58
+ int membuffer_insert( membuffer* m, size_t index, const void* src, size_t length );
59
+
60
+ /**
61
+ * Append the given _src_ data to the end of the memory buffer. This method
62
+ * calls `membuffer_insert` to append the data.
63
+ *
64
+ * Return Codes:
65
+ * MB_OK
66
+ * MB_OUT_OF_MEMORY
67
+ */
68
+ int membuffer_append( membuffer* m, const void* src, size_t length );
69
+
70
+ /**
71
+ * Convert the memory buffer into a Ruby String instance. This method will
72
+ * return an empty String instance if the memory buffer is empty. This method
73
+ * will never return Qnil.
74
+ */
75
+ VALUE membuffer_to_rb_str( membuffer* m );
76
+
77
+ #endif
78
+