patron-new 0.4.19
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.
- checksums.yaml +7 -0
- data/.autotest +15 -0
- data/.gitignore +8 -0
- data/.rspec +0 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +19 -0
- data/README.txt +57 -0
- data/Rakefile +69 -0
- data/ext/patron/.gitignore +4 -0
- data/ext/patron/extconf.rb +49 -0
- data/ext/patron/membuffer.c +85 -0
- data/ext/patron/membuffer.h +78 -0
- data/ext/patron/session_ext.c +769 -0
- data/ext/patron/sglib.h +1952 -0
- data/lib/patron.rb +38 -0
- data/lib/patron/error.rb +55 -0
- data/lib/patron/proxy_type.rb +10 -0
- data/lib/patron/request.rb +168 -0
- data/lib/patron/response.rb +107 -0
- data/lib/patron/session.rb +234 -0
- data/lib/patron/util.rb +50 -0
- data/lib/patron/version.rb +3 -0
- data/patron.gemspec +26 -0
- data/pic.png +0 -0
- data/script/console +11 -0
- data/script/test_server +30 -0
- data/spec/certs/cacert.pem +36 -0
- data/spec/certs/privkey.pem +51 -0
- data/spec/patron_spec.rb +38 -0
- data/spec/request_spec.rb +104 -0
- data/spec/response_spec.rb +62 -0
- data/spec/session_spec.rb +338 -0
- data/spec/session_ssl_spec.rb +275 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/test_server.rb +189 -0
- data/spec/util_spec.rb +92 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: faae4f5916e996609a897c58b816eb85aefa4aaa
|
4
|
+
data.tar.gz: f235825bd133b963b32b4be53e3598667fef273b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b0014227c16f760d95788d885eeef6be92a54545e4a6f139d3555909cf3afe689246bcfc13162b394da6bfc8520d2da71617a3f5e965e858b4b1eaedc7af1392
|
7
|
+
data.tar.gz: 28659d63080f4da203e39bca65bd828a1dbf84100ef09618928ec291bbfc3dc618e57e229982a8008f0d7fba0bb6b695d697c3d9a0f930976e2e08c92ed42784
|
data/.autotest
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Autotest.add_hook :initialize do |at|
|
2
|
+
at.clear_mappings
|
3
|
+
|
4
|
+
at.add_mapping(%r{^spec/.+_spec\.rb$}) do |filename,_|
|
5
|
+
filename
|
6
|
+
end
|
7
|
+
|
8
|
+
at.add_mapping(%r{^lib/patron/(.+)\.rb$}) do |_,match|
|
9
|
+
[ "spec/#{match[1]}_spec.rb" ]
|
10
|
+
end
|
11
|
+
|
12
|
+
at.add_mapping(%r{^spec/spec_helper\.rb$}) do
|
13
|
+
at.files_matching(%r{^spec/.+_spec\.rb$})
|
14
|
+
end
|
15
|
+
end
|
data/.gitignore
ADDED
data/.rspec
ADDED
File without changes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
patron (0.4.18)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rake-compiler (0.7.9)
|
12
|
+
rake
|
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
|
+
diff-lcs (~> 1.1.2)
|
21
|
+
rspec-mocks (2.7.0)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (>= 1.0.0)
|
28
|
+
patron!
|
29
|
+
rake-compiler (>= 0.7.5)
|
30
|
+
rcov (>= 0.9.9)
|
31
|
+
rspec (>= 2.3.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2008 The Hive http://www.thehive.com/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.txt
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= Ruby HTTP Client
|
2
|
+
|
3
|
+
== SYNOPSIS
|
4
|
+
|
5
|
+
Patron is a Ruby HTTP client library based on libcurl. It does not try to expose
|
6
|
+
the full "power" (read complexity) of libcurl but instead tries to provide a
|
7
|
+
sane API while taking advantage of libcurl under the hood.
|
8
|
+
|
9
|
+
|
10
|
+
== USAGE
|
11
|
+
|
12
|
+
Usage is very simple. First, you instantiate a Session object. You can set a few
|
13
|
+
default options on the Session instance that will be used by all subsequent
|
14
|
+
requests:
|
15
|
+
|
16
|
+
sess = Patron::Session.new
|
17
|
+
sess.timeout = 10
|
18
|
+
sess.base_url = "http://myserver.com:9900"
|
19
|
+
sess.headers['User-Agent'] = 'myapp/1.0'
|
20
|
+
sess.enable_debug "/tmp/patron.debug"
|
21
|
+
|
22
|
+
The Session is used to make HTTP requests.
|
23
|
+
|
24
|
+
resp = sess.get("/foo/bar")
|
25
|
+
|
26
|
+
Requests return a Response object:
|
27
|
+
|
28
|
+
if resp.status < 400
|
29
|
+
puts resp.body
|
30
|
+
end
|
31
|
+
|
32
|
+
The GET, HEAD, PUT, POST and DELETE operations are all supported.
|
33
|
+
|
34
|
+
sess.put("/foo/baz", "some data")
|
35
|
+
sess.delete("/foo/baz")
|
36
|
+
|
37
|
+
You can ship custom headers with a single request:
|
38
|
+
|
39
|
+
sess.post("/foo/stuff", "some data", {"Content-Type" => "text/plain"})
|
40
|
+
|
41
|
+
That is pretty much all there is to it.
|
42
|
+
|
43
|
+
|
44
|
+
== REQUIREMENTS
|
45
|
+
|
46
|
+
You need a recent version of libcurl in order to install this gem. On MacOS X
|
47
|
+
the provided libcurl is sufficient. You will have to install the libcurl
|
48
|
+
development packages on Debian or Ubuntu. Other Linux systems are probably
|
49
|
+
similar. Windows users are on your own. Good luck with that.
|
50
|
+
|
51
|
+
|
52
|
+
== INSTALL
|
53
|
+
|
54
|
+
sudo gem install patron
|
55
|
+
|
56
|
+
|
57
|
+
Copyright (c) 2008 The Hive
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) 2008 The Hive http://www.thehive.com/
|
4
|
+
##
|
5
|
+
## Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
## of this software and associated documentation files (the "Software"), to deal
|
7
|
+
## in the Software without restriction, including without limitation the rights
|
8
|
+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
## copies of the Software, and to permit persons to whom the Software is
|
10
|
+
## furnished to do so, subject to the following conditions:
|
11
|
+
##
|
12
|
+
## The above copyright notice and this permission notice shall be included in
|
13
|
+
## all copies or substantial portions of the Software.
|
14
|
+
##
|
15
|
+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
## THE SOFTWARE.
|
22
|
+
##
|
23
|
+
## -------------------------------------------------------------------
|
24
|
+
require 'rake/clean'
|
25
|
+
require 'rake/rdoctask'
|
26
|
+
require 'rake/extensiontask'
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
require 'bundler'
|
29
|
+
|
30
|
+
Rake::ExtensionTask.new do |ext|
|
31
|
+
ext.name = 'session_ext' # indicate the name of the extension.
|
32
|
+
ext.ext_dir = 'ext/patron' # search for 'hello_world' inside it.
|
33
|
+
ext.lib_dir = 'lib/patron' # put binaries into this folder.
|
34
|
+
end
|
35
|
+
|
36
|
+
Bundler::GemHelper.install_tasks
|
37
|
+
|
38
|
+
CLEAN.include FileList["ext/patron/*"].exclude(/^.*\.(rb|c|h)$/)
|
39
|
+
CLOBBER.include %w( doc coverage pkg )
|
40
|
+
|
41
|
+
desc "Start an IRB shell"
|
42
|
+
task :shell => :compile do
|
43
|
+
sh 'irb -I./lib -I./ext -r patron'
|
44
|
+
end
|
45
|
+
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = 'Patron documentation'
|
49
|
+
rdoc.main = 'README.txt'
|
50
|
+
rdoc.rdoc_files.include('README.txt')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
53
|
+
|
54
|
+
desc "Run specs"
|
55
|
+
RSpec::Core::RakeTask.new do |t|
|
56
|
+
t.rspec_opts = %w( --colour --format progress )
|
57
|
+
t.pattern = 'spec/**/*_spec.rb'
|
58
|
+
end
|
59
|
+
|
60
|
+
task :spec => [:compile]
|
61
|
+
|
62
|
+
desc "Run specs with RCov"
|
63
|
+
RSpec::Core::RakeTask.new('spec:rcov') do |t|
|
64
|
+
t.pattern = 'spec/**/*_spec.rb'
|
65
|
+
t.rcov = true
|
66
|
+
t.rcov_opts = %q(--sort coverage --comments --exclude "spec")
|
67
|
+
end
|
68
|
+
|
69
|
+
task :default => :spec
|
@@ -0,0 +1,49 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) 2008 The Hive http://www.thehive.com/
|
4
|
+
##
|
5
|
+
## Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
## of this software and associated documentation files (the "Software"), to deal
|
7
|
+
## in the Software without restriction, including without limitation the rights
|
8
|
+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
## copies of the Software, and to permit persons to whom the Software is
|
10
|
+
## furnished to do so, subject to the following conditions:
|
11
|
+
##
|
12
|
+
## The above copyright notice and this permission notice shall be included in
|
13
|
+
## all copies or substantial portions of the Software.
|
14
|
+
##
|
15
|
+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
## THE SOFTWARE.
|
22
|
+
##
|
23
|
+
## -------------------------------------------------------------------
|
24
|
+
|
25
|
+
require 'mkmf'
|
26
|
+
require 'rbconfig'
|
27
|
+
|
28
|
+
dir_config('curl')
|
29
|
+
|
30
|
+
if find_executable('curl-config')
|
31
|
+
$CFLAGS << " #{`curl-config --cflags`.strip}"
|
32
|
+
$LIBS << " #{`curl-config --libs`.strip}"
|
33
|
+
elsif !have_library('curl') or !have_header('curl/curl.h')
|
34
|
+
fail <<-EOM
|
35
|
+
Can't find libcurl or curl/curl.h
|
36
|
+
|
37
|
+
Try passing --with-curl-dir or --with-curl-lib and --with-curl-include
|
38
|
+
options to extconf.
|
39
|
+
EOM
|
40
|
+
end
|
41
|
+
|
42
|
+
if CONFIG['CC'] =~ /gcc/
|
43
|
+
$CFLAGS << ' -pedantic -Wall'
|
44
|
+
end
|
45
|
+
|
46
|
+
$defs.push("-DUSE_TBR")
|
47
|
+
$defs.push("-DHAVE_TBR") if have_func('rb_thread_blocking_region')
|
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
|
+
|