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
@@ -0,0 +1,769 @@
|
|
1
|
+
/* -------------------------------------------------------------------------- *\
|
2
|
+
*
|
3
|
+
* Patron HTTP Client: Interface to libcurl
|
4
|
+
* Copyright (c) 2008 The Hive http://www.thehive.com/
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*
|
24
|
+
\* -------------------------------------------------------------------------- */
|
25
|
+
#include <ruby.h>
|
26
|
+
#include <curl/curl.h>
|
27
|
+
#include "membuffer.h"
|
28
|
+
#include "sglib.h" /* Simple Generic Library -> http://sglib.sourceforge.net */
|
29
|
+
|
30
|
+
#define UNUSED_ARGUMENT(x) (void)x
|
31
|
+
|
32
|
+
static VALUE mPatron = Qnil;
|
33
|
+
static VALUE mProxyType = Qnil;
|
34
|
+
static VALUE cSession = Qnil;
|
35
|
+
static VALUE cRequest = Qnil;
|
36
|
+
static VALUE ePatronError = Qnil;
|
37
|
+
static VALUE eUnsupportedProtocol = Qnil;
|
38
|
+
static VALUE eURLFormatError = Qnil;
|
39
|
+
static VALUE eHostResolutionError = Qnil;
|
40
|
+
static VALUE eConnectionFailed = Qnil;
|
41
|
+
static VALUE ePartialFileError = Qnil;
|
42
|
+
static VALUE eTimeoutError = Qnil;
|
43
|
+
static VALUE eTooManyRedirects = Qnil;
|
44
|
+
|
45
|
+
|
46
|
+
struct curl_state {
|
47
|
+
CURL* handle;
|
48
|
+
char* upload_buf;
|
49
|
+
FILE* download_file;
|
50
|
+
FILE* upload_file;
|
51
|
+
FILE* debug_file;
|
52
|
+
char error_buf[CURL_ERROR_SIZE];
|
53
|
+
struct curl_slist* headers;
|
54
|
+
struct curl_httppost* post;
|
55
|
+
struct curl_httppost* last;
|
56
|
+
membuffer header_buffer;
|
57
|
+
membuffer body_buffer;
|
58
|
+
int interrupt;
|
59
|
+
};
|
60
|
+
|
61
|
+
|
62
|
+
/*----------------------------------------------------------------------------*/
|
63
|
+
/* Curl Callbacks */
|
64
|
+
|
65
|
+
/* Takes data streamed from libcurl and writes it to a Ruby string buffer. */
|
66
|
+
static size_t session_write_handler(char* stream, size_t size, size_t nmemb, membuffer* buf) {
|
67
|
+
int rc = membuffer_append(buf, stream, size * nmemb);
|
68
|
+
|
69
|
+
/* return 0 to signal that we could not append data to our buffer */
|
70
|
+
if (MB_OK != rc) { return 0; }
|
71
|
+
|
72
|
+
/* otherwise, return the number of bytes appended */
|
73
|
+
return size * nmemb;
|
74
|
+
}
|
75
|
+
|
76
|
+
static size_t session_read_handler(char* stream, size_t size, size_t nmemb, char **buffer) {
|
77
|
+
size_t result = 0;
|
78
|
+
|
79
|
+
if (buffer != NULL && *buffer != NULL) {
|
80
|
+
size_t len = size * nmemb;
|
81
|
+
char *s1 = strncpy(stream, *buffer, len);
|
82
|
+
result = strlen(s1);
|
83
|
+
*buffer += result;
|
84
|
+
}
|
85
|
+
|
86
|
+
return result;
|
87
|
+
}
|
88
|
+
|
89
|
+
/* A non-zero return value from the progress handler will terminate the current
|
90
|
+
* request. We use this fact in order to interrupt any request when either the
|
91
|
+
* user calls the "interrupt" method on the session or when the Ruby interpreter
|
92
|
+
* is attempting to exit.
|
93
|
+
*/
|
94
|
+
static int session_progress_handler(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
|
95
|
+
struct curl_state* state = (struct curl_state*) clientp;
|
96
|
+
UNUSED_ARGUMENT(dltotal);
|
97
|
+
UNUSED_ARGUMENT(dlnow);
|
98
|
+
UNUSED_ARGUMENT(ultotal);
|
99
|
+
UNUSED_ARGUMENT(ulnow);
|
100
|
+
return state->interrupt;
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
/*----------------------------------------------------------------------------*/
|
105
|
+
/* List of active curl sessions */
|
106
|
+
|
107
|
+
struct curl_state_list {
|
108
|
+
struct curl_state *state;
|
109
|
+
struct curl_state_list *next;
|
110
|
+
};
|
111
|
+
|
112
|
+
#define CS_LIST_COMPARATOR(p, _state_) (p->state == _state_)
|
113
|
+
|
114
|
+
static struct curl_state_list *cs_list = NULL;
|
115
|
+
|
116
|
+
static void cs_list_append( struct curl_state *state ) {
|
117
|
+
struct curl_state_list *item = NULL;
|
118
|
+
|
119
|
+
assert(state != NULL);
|
120
|
+
item = ruby_xmalloc(sizeof(struct curl_state_list));
|
121
|
+
item->state = state;
|
122
|
+
item->next = NULL;
|
123
|
+
|
124
|
+
SGLIB_LIST_ADD(struct curl_state_list, cs_list, item, next);
|
125
|
+
}
|
126
|
+
|
127
|
+
static void cs_list_remove(struct curl_state *state) {
|
128
|
+
struct curl_state_list *item = NULL;
|
129
|
+
|
130
|
+
assert(state != NULL);
|
131
|
+
SGLIB_LIST_FIND_MEMBER(struct curl_state_list, cs_list, state, CS_LIST_COMPARATOR, next, item);
|
132
|
+
if (item) {
|
133
|
+
SGLIB_LIST_DELETE(struct curl_state_list, cs_list, item, next);
|
134
|
+
ruby_xfree(item);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
static void cs_list_interrupt(VALUE data) {
|
139
|
+
UNUSED_ARGUMENT(data);
|
140
|
+
|
141
|
+
SGLIB_LIST_MAP_ON_ELEMENTS(struct curl_state_list, cs_list, item, next, {
|
142
|
+
item->state->interrupt = 1;
|
143
|
+
});
|
144
|
+
}
|
145
|
+
|
146
|
+
|
147
|
+
/*----------------------------------------------------------------------------*/
|
148
|
+
/* Object allocation */
|
149
|
+
|
150
|
+
static void session_close_debug_file(struct curl_state *curl) {
|
151
|
+
if (curl->debug_file && stderr != curl->debug_file) {
|
152
|
+
fclose(curl->debug_file);
|
153
|
+
}
|
154
|
+
curl->debug_file = NULL;
|
155
|
+
}
|
156
|
+
|
157
|
+
/* Cleans up the Curl handle when the Session object is garbage collected. */
|
158
|
+
void session_free(struct curl_state *curl) {
|
159
|
+
if (curl->handle) {
|
160
|
+
curl_easy_cleanup(curl->handle);
|
161
|
+
curl->handle = NULL;
|
162
|
+
}
|
163
|
+
|
164
|
+
session_close_debug_file(curl);
|
165
|
+
|
166
|
+
membuffer_destroy( &curl->header_buffer );
|
167
|
+
membuffer_destroy( &curl->body_buffer );
|
168
|
+
|
169
|
+
cs_list_remove(curl);
|
170
|
+
|
171
|
+
free(curl);
|
172
|
+
}
|
173
|
+
|
174
|
+
/* Allocates curl_state data needed for a new Session object. */
|
175
|
+
VALUE session_alloc(VALUE klass) {
|
176
|
+
struct curl_state* curl;
|
177
|
+
VALUE obj = Data_Make_Struct(klass, struct curl_state, NULL, session_free, curl);
|
178
|
+
|
179
|
+
membuffer_init( &curl->header_buffer );
|
180
|
+
membuffer_init( &curl->body_buffer );
|
181
|
+
cs_list_append(curl);
|
182
|
+
|
183
|
+
return obj;
|
184
|
+
}
|
185
|
+
|
186
|
+
/* Return the curl_state from the ruby VALUE which is the Session instance. */
|
187
|
+
static struct curl_state* get_curl_state(VALUE self) {
|
188
|
+
struct curl_state *state;
|
189
|
+
Data_Get_Struct(self, struct curl_state, state);
|
190
|
+
|
191
|
+
if (NULL == state->handle) {
|
192
|
+
state->handle = curl_easy_init();
|
193
|
+
curl_easy_setopt(state->handle, CURLOPT_NOSIGNAL, 1);
|
194
|
+
curl_easy_setopt(state->handle, CURLOPT_NOPROGRESS, 0);
|
195
|
+
curl_easy_setopt(state->handle, CURLOPT_PROGRESSFUNCTION, &session_progress_handler);
|
196
|
+
curl_easy_setopt(state->handle, CURLOPT_PROGRESSDATA, state);
|
197
|
+
}
|
198
|
+
|
199
|
+
return state;
|
200
|
+
}
|
201
|
+
|
202
|
+
|
203
|
+
/*----------------------------------------------------------------------------*/
|
204
|
+
/* Method implementations */
|
205
|
+
|
206
|
+
/* call-seq:
|
207
|
+
* Patron.libcurl_version -> version string
|
208
|
+
*
|
209
|
+
* Returns the version of the embedded libcurl as a string.
|
210
|
+
*/
|
211
|
+
static VALUE libcurl_version(VALUE klass) {
|
212
|
+
char* value = curl_version();
|
213
|
+
UNUSED_ARGUMENT(klass);
|
214
|
+
return rb_str_new2(value);
|
215
|
+
}
|
216
|
+
|
217
|
+
/* call-seq:
|
218
|
+
* Session.escape( string ) -> escaped string
|
219
|
+
*
|
220
|
+
* URL escapes the provided string.
|
221
|
+
*/
|
222
|
+
static VALUE session_escape(VALUE self, VALUE value) {
|
223
|
+
struct curl_state *state = get_curl_state(self);
|
224
|
+
VALUE string = StringValue(value);
|
225
|
+
char* escaped = NULL;
|
226
|
+
VALUE retval = Qnil;
|
227
|
+
|
228
|
+
escaped = curl_easy_escape(state->handle,
|
229
|
+
RSTRING_PTR(string),
|
230
|
+
(int) RSTRING_LEN(string));
|
231
|
+
|
232
|
+
retval = rb_str_new2(escaped);
|
233
|
+
curl_free(escaped);
|
234
|
+
|
235
|
+
return retval;
|
236
|
+
}
|
237
|
+
|
238
|
+
/* call-seq:
|
239
|
+
* Session.unescape( string ) -> unescaped string
|
240
|
+
*
|
241
|
+
* Unescapes the provided string.
|
242
|
+
*/
|
243
|
+
static VALUE session_unescape(VALUE self, VALUE value) {
|
244
|
+
struct curl_state *state = get_curl_state(self);
|
245
|
+
VALUE string = StringValue(value);
|
246
|
+
char* unescaped = NULL;
|
247
|
+
VALUE retval = Qnil;
|
248
|
+
|
249
|
+
unescaped = curl_easy_unescape(state->handle,
|
250
|
+
RSTRING_PTR(string),
|
251
|
+
(int) RSTRING_LEN(string),
|
252
|
+
NULL);
|
253
|
+
|
254
|
+
retval = rb_str_new2(unescaped);
|
255
|
+
curl_free(unescaped);
|
256
|
+
|
257
|
+
return retval;
|
258
|
+
}
|
259
|
+
|
260
|
+
/* Callback used to iterate over the HTTP headers and store them in an slist. */
|
261
|
+
static int each_http_header(VALUE header_key, VALUE header_value, VALUE self) {
|
262
|
+
struct curl_state *state = get_curl_state(self);
|
263
|
+
CURL* curl = state->handle;
|
264
|
+
|
265
|
+
VALUE name = rb_obj_as_string(header_key);
|
266
|
+
VALUE value = rb_obj_as_string(header_value);
|
267
|
+
VALUE header_str = Qnil;
|
268
|
+
|
269
|
+
if (rb_str_cmp(name, rb_str_new2("Accept-Encoding")) == 0) {
|
270
|
+
if (rb_funcall(value, rb_intern("include?"), 1, rb_str_new2("gzip"))) {
|
271
|
+
#ifdef CURLOPT_ACCEPT_ENCODING
|
272
|
+
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
|
273
|
+
#elif defined CURLOPT_ENCODING
|
274
|
+
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
|
275
|
+
#else
|
276
|
+
rb_raise(rb_eArgError,
|
277
|
+
"The libcurl version installed doesn't support 'gzip'.");
|
278
|
+
#endif
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
header_str = rb_str_plus(name, rb_str_new2(": "));
|
283
|
+
header_str = rb_str_plus(header_str, value);
|
284
|
+
|
285
|
+
state->headers = curl_slist_append(state->headers, StringValuePtr(header_str));
|
286
|
+
|
287
|
+
return 0;
|
288
|
+
}
|
289
|
+
|
290
|
+
static int formadd_values(VALUE data_key, VALUE data_value, VALUE self) {
|
291
|
+
struct curl_state *state = get_curl_state(self);
|
292
|
+
VALUE name = rb_obj_as_string(data_key);
|
293
|
+
VALUE value = rb_obj_as_string(data_value);
|
294
|
+
|
295
|
+
curl_formadd(&state->post, &state->last, CURLFORM_PTRNAME, RSTRING_PTR(name),
|
296
|
+
CURLFORM_PTRCONTENTS, RSTRING_PTR(value), CURLFORM_END);
|
297
|
+
|
298
|
+
return 0;
|
299
|
+
}
|
300
|
+
|
301
|
+
static int formadd_files(VALUE data_key, VALUE data_value, VALUE self) {
|
302
|
+
struct curl_state *state = get_curl_state(self);
|
303
|
+
VALUE name = rb_obj_as_string(data_key);
|
304
|
+
VALUE value = rb_obj_as_string(data_value);
|
305
|
+
|
306
|
+
curl_formadd(&state->post, &state->last, CURLFORM_PTRNAME, RSTRING_PTR(name),
|
307
|
+
CURLFORM_FILE, RSTRING_PTR(value), CURLFORM_END);
|
308
|
+
|
309
|
+
return 0;
|
310
|
+
}
|
311
|
+
|
312
|
+
static void set_chunked_encoding(struct curl_state *state) {
|
313
|
+
state->headers = curl_slist_append(state->headers, "Transfer-Encoding: chunked");
|
314
|
+
}
|
315
|
+
|
316
|
+
static FILE* open_file(VALUE filename, const char* perms) {
|
317
|
+
FILE* handle = fopen(StringValuePtr(filename), perms);
|
318
|
+
if (!handle) {
|
319
|
+
rb_raise(rb_eArgError, "Unable to open specified file.");
|
320
|
+
}
|
321
|
+
|
322
|
+
return handle;
|
323
|
+
}
|
324
|
+
|
325
|
+
/* Set the options on the Curl handle from a Request object. Takes each field
|
326
|
+
* in the Request object and uses it to set the appropriate option on the Curl
|
327
|
+
* handle.
|
328
|
+
*/
|
329
|
+
static void set_options_from_request(VALUE self, VALUE request) {
|
330
|
+
struct curl_state *state = get_curl_state(self);
|
331
|
+
CURL* curl = state->handle;
|
332
|
+
|
333
|
+
ID action = Qnil;
|
334
|
+
VALUE headers = Qnil;
|
335
|
+
VALUE url = Qnil;
|
336
|
+
VALUE timeout = Qnil;
|
337
|
+
VALUE redirects = Qnil;
|
338
|
+
VALUE proxy = Qnil;
|
339
|
+
VALUE proxy_type = Qnil;
|
340
|
+
VALUE credentials = Qnil;
|
341
|
+
VALUE ignore_content_length = Qnil;
|
342
|
+
VALUE insecure = Qnil;
|
343
|
+
VALUE cacert = Qnil;
|
344
|
+
VALUE buffer_size = Qnil;
|
345
|
+
VALUE action_name = rb_iv_get(request, "@action");
|
346
|
+
|
347
|
+
headers = rb_iv_get(request, "@headers");
|
348
|
+
if (!NIL_P(headers)) {
|
349
|
+
if (rb_type(headers) != T_HASH) {
|
350
|
+
rb_raise(rb_eArgError, "Headers must be passed in a hash.");
|
351
|
+
}
|
352
|
+
|
353
|
+
rb_hash_foreach(headers, each_http_header, self);
|
354
|
+
}
|
355
|
+
|
356
|
+
|
357
|
+
action = rb_to_id(action_name);
|
358
|
+
if (action == rb_intern("GET")) {
|
359
|
+
VALUE data = rb_iv_get(request, "@upload_data");
|
360
|
+
VALUE download_file = rb_iv_get(request, "@file_name");
|
361
|
+
|
362
|
+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
363
|
+
if (!NIL_P(data)) {
|
364
|
+
long len = RSTRING_LEN(data);
|
365
|
+
state->upload_buf = StringValuePtr(data);
|
366
|
+
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
|
367
|
+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
|
368
|
+
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &session_read_handler);
|
369
|
+
curl_easy_setopt(curl, CURLOPT_READDATA, &state->upload_buf);
|
370
|
+
curl_easy_setopt(curl, CURLOPT_INFILESIZE, len);
|
371
|
+
}
|
372
|
+
if (!NIL_P(download_file)) {
|
373
|
+
state->download_file = open_file(download_file, "wb");
|
374
|
+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, state->download_file);
|
375
|
+
} else {
|
376
|
+
state->download_file = NULL;
|
377
|
+
}
|
378
|
+
} else if (action == rb_intern("POST") || action == rb_intern("PUT")) {
|
379
|
+
VALUE data = rb_iv_get(request, "@upload_data");
|
380
|
+
VALUE filename = rb_iv_get(request, "@file_name");
|
381
|
+
VALUE multipart = rb_iv_get(request, "@multipart");
|
382
|
+
|
383
|
+
if (!NIL_P(data) && NIL_P(multipart)) {
|
384
|
+
long len = RSTRING_LEN(data);
|
385
|
+
|
386
|
+
state->upload_buf = StringValuePtr(data);
|
387
|
+
|
388
|
+
if (action == rb_intern("POST")) {
|
389
|
+
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
390
|
+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, state->upload_buf);
|
391
|
+
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
|
392
|
+
} else {
|
393
|
+
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
|
394
|
+
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &session_read_handler);
|
395
|
+
curl_easy_setopt(curl, CURLOPT_READDATA, &state->upload_buf);
|
396
|
+
curl_easy_setopt(curl, CURLOPT_INFILESIZE, len);
|
397
|
+
}
|
398
|
+
} else if (!NIL_P(filename) && NIL_P(multipart)) {
|
399
|
+
set_chunked_encoding(state);
|
400
|
+
|
401
|
+
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
|
402
|
+
|
403
|
+
if (action == rb_intern("POST")) {
|
404
|
+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
|
405
|
+
}
|
406
|
+
|
407
|
+
state->upload_file = open_file(filename, "rb");
|
408
|
+
curl_easy_setopt(curl, CURLOPT_READDATA, state->upload_file);
|
409
|
+
} else if (!NIL_P(multipart)) {
|
410
|
+
if (action == rb_intern("POST")) {
|
411
|
+
if(!NIL_P(data) && !NIL_P(filename)) {
|
412
|
+
if (rb_type(data) == T_HASH && rb_type(filename) == T_HASH) {
|
413
|
+
rb_hash_foreach(data, formadd_values, self);
|
414
|
+
rb_hash_foreach(filename, formadd_files, self);
|
415
|
+
} else { rb_raise(rb_eArgError, "Data and Filename must be passed in a hash.");}
|
416
|
+
}
|
417
|
+
curl_easy_setopt(curl, CURLOPT_HTTPPOST, state->post);
|
418
|
+
|
419
|
+
} else {
|
420
|
+
rb_raise(rb_eArgError, "Multipart PUT not supported");
|
421
|
+
}
|
422
|
+
|
423
|
+
} else {
|
424
|
+
rb_raise(rb_eArgError, "Must provide either data or a filename when doing a PUT or POST");
|
425
|
+
}
|
426
|
+
|
427
|
+
// support for data passed with a DELETE request (e.g.: used by elasticsearch)
|
428
|
+
} else if (action == rb_intern("DELETE")) {
|
429
|
+
VALUE data = rb_iv_get(request, "@upload_data");
|
430
|
+
|
431
|
+
if (!NIL_P(data)) {
|
432
|
+
long len = RSTRING_LEN(data);
|
433
|
+
state->upload_buf = StringValuePtr(data);
|
434
|
+
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
435
|
+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, state->upload_buf);
|
436
|
+
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
|
437
|
+
}
|
438
|
+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
439
|
+
|
440
|
+
} else if (action == rb_intern("HEAD")) {
|
441
|
+
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
|
442
|
+
} else {
|
443
|
+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, StringValuePtr(action_name));
|
444
|
+
}
|
445
|
+
|
446
|
+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, state->headers);
|
447
|
+
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, state->error_buf);
|
448
|
+
|
449
|
+
url = rb_iv_get(request, "@url");
|
450
|
+
if (NIL_P(url)) {
|
451
|
+
rb_raise(rb_eArgError, "Must provide a URL");
|
452
|
+
}
|
453
|
+
curl_easy_setopt(curl, CURLOPT_URL, StringValuePtr(url));
|
454
|
+
|
455
|
+
timeout = rb_iv_get(request, "@timeout");
|
456
|
+
if (!NIL_P(timeout)) {
|
457
|
+
curl_easy_setopt(curl, CURLOPT_TIMEOUT, FIX2INT(timeout));
|
458
|
+
}
|
459
|
+
|
460
|
+
timeout = rb_iv_get(request, "@connect_timeout");
|
461
|
+
if (!NIL_P(timeout)) {
|
462
|
+
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, FIX2INT(timeout));
|
463
|
+
}
|
464
|
+
|
465
|
+
redirects = rb_iv_get(request, "@max_redirects");
|
466
|
+
if (!NIL_P(redirects)) {
|
467
|
+
int r = FIX2INT(redirects);
|
468
|
+
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, r == 0 ? 0 : 1);
|
469
|
+
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, r);
|
470
|
+
}
|
471
|
+
|
472
|
+
proxy = rb_iv_get(request, "@proxy");
|
473
|
+
if (!NIL_P(proxy)) {
|
474
|
+
curl_easy_setopt(curl, CURLOPT_PROXY, StringValuePtr(proxy));
|
475
|
+
}
|
476
|
+
|
477
|
+
proxy_type = rb_iv_get(request, "@proxy_type");
|
478
|
+
if (!NIL_P(proxy_type)) {
|
479
|
+
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, FIX2INT(proxy_type));
|
480
|
+
}
|
481
|
+
|
482
|
+
credentials = rb_funcall(request, rb_intern("credentials"), 0);
|
483
|
+
if (!NIL_P(credentials)) {
|
484
|
+
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, FIX2INT(rb_iv_get(request, "@auth_type")));
|
485
|
+
curl_easy_setopt(curl, CURLOPT_USERPWD, StringValuePtr(credentials));
|
486
|
+
}
|
487
|
+
|
488
|
+
ignore_content_length = rb_iv_get(request, "@ignore_content_length");
|
489
|
+
if (!NIL_P(ignore_content_length)) {
|
490
|
+
curl_easy_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, 1);
|
491
|
+
}
|
492
|
+
|
493
|
+
insecure = rb_iv_get(request, "@insecure");
|
494
|
+
if(!NIL_P(insecure)) {
|
495
|
+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
496
|
+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
497
|
+
}
|
498
|
+
|
499
|
+
cacert = rb_iv_get(request, "@cacert");
|
500
|
+
if(!NIL_P(cacert)) {
|
501
|
+
curl_easy_setopt(curl, CURLOPT_CAINFO, StringValuePtr(cacert));
|
502
|
+
}
|
503
|
+
|
504
|
+
buffer_size = rb_iv_get(request, "@buffer_size");
|
505
|
+
if (!NIL_P(buffer_size)) {
|
506
|
+
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, FIX2INT(buffer_size));
|
507
|
+
}
|
508
|
+
|
509
|
+
if(state->debug_file) {
|
510
|
+
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
511
|
+
curl_easy_setopt(curl, CURLOPT_STDERR, state->debug_file);
|
512
|
+
}
|
513
|
+
}
|
514
|
+
|
515
|
+
/* Use the info in a Curl handle to create a new Response object. */
|
516
|
+
static VALUE create_response(VALUE self, CURL* curl, VALUE header_buffer, VALUE body_buffer) {
|
517
|
+
VALUE args[6] = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil };
|
518
|
+
char* effective_url = NULL;
|
519
|
+
long code = 0;
|
520
|
+
long count = 0;
|
521
|
+
|
522
|
+
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
|
523
|
+
args[0] = rb_str_new2(effective_url);
|
524
|
+
|
525
|
+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
526
|
+
args[1] = INT2NUM(code);
|
527
|
+
|
528
|
+
curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &count);
|
529
|
+
args[2] = INT2NUM(count);
|
530
|
+
|
531
|
+
args[3] = header_buffer;
|
532
|
+
args[4] = body_buffer;
|
533
|
+
args[5] = rb_iv_get(self, "@default_response_charset");
|
534
|
+
|
535
|
+
return rb_class_new_instance(6, args, rb_const_get(mPatron, rb_intern("Response")));
|
536
|
+
}
|
537
|
+
|
538
|
+
/* Raise an exception based on the Curl error code. */
|
539
|
+
static VALUE select_error(CURLcode code) {
|
540
|
+
VALUE error = Qnil;
|
541
|
+
switch (code) {
|
542
|
+
case CURLE_UNSUPPORTED_PROTOCOL: error = eUnsupportedProtocol; break;
|
543
|
+
case CURLE_URL_MALFORMAT: error = eURLFormatError; break;
|
544
|
+
case CURLE_COULDNT_RESOLVE_HOST: error = eHostResolutionError; break;
|
545
|
+
case CURLE_COULDNT_CONNECT: error = eConnectionFailed; break;
|
546
|
+
case CURLE_PARTIAL_FILE: error = ePartialFileError; break;
|
547
|
+
case CURLE_OPERATION_TIMEDOUT: error = eTimeoutError; break;
|
548
|
+
case CURLE_TOO_MANY_REDIRECTS: error = eTooManyRedirects; break;
|
549
|
+
|
550
|
+
default: error = ePatronError;
|
551
|
+
}
|
552
|
+
|
553
|
+
return error;
|
554
|
+
}
|
555
|
+
|
556
|
+
/* Perform the actual HTTP request by calling libcurl. */
|
557
|
+
static VALUE perform_request(VALUE self) {
|
558
|
+
struct curl_state *state = get_curl_state(self);
|
559
|
+
CURL* curl = state->handle;
|
560
|
+
membuffer* header_buffer = NULL;
|
561
|
+
membuffer* body_buffer = NULL;
|
562
|
+
CURLcode ret = 0;
|
563
|
+
|
564
|
+
state->interrupt = 0; /* clear any interrupt flags */
|
565
|
+
|
566
|
+
header_buffer = &state->header_buffer;
|
567
|
+
body_buffer = &state->body_buffer;
|
568
|
+
|
569
|
+
membuffer_clear(header_buffer);
|
570
|
+
membuffer_clear(body_buffer);
|
571
|
+
|
572
|
+
/* headers */
|
573
|
+
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &session_write_handler);
|
574
|
+
curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_buffer);
|
575
|
+
|
576
|
+
/* body */
|
577
|
+
if (!state->download_file) {
|
578
|
+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &session_write_handler);
|
579
|
+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_buffer);
|
580
|
+
}
|
581
|
+
|
582
|
+
#if defined(HAVE_TBR) && defined(USE_TBR)
|
583
|
+
ret = (CURLcode) rb_thread_blocking_region(
|
584
|
+
(rb_blocking_function_t*) curl_easy_perform, curl,
|
585
|
+
RUBY_UBF_IO, 0
|
586
|
+
);
|
587
|
+
#else
|
588
|
+
ret = curl_easy_perform(curl);
|
589
|
+
#endif
|
590
|
+
|
591
|
+
if (CURLE_OK == ret) {
|
592
|
+
VALUE header_str = membuffer_to_rb_str(header_buffer);
|
593
|
+
VALUE body_str = Qnil;
|
594
|
+
if (!state->download_file) { body_str = membuffer_to_rb_str(body_buffer); }
|
595
|
+
|
596
|
+
return create_response(self, curl, header_str, body_str);
|
597
|
+
} else {
|
598
|
+
rb_raise(select_error(ret), "%s", state->error_buf);
|
599
|
+
}
|
600
|
+
}
|
601
|
+
|
602
|
+
/* Cleanup after each request by resetting the Curl handle and deallocating
|
603
|
+
* all request related objects such as the header slist.
|
604
|
+
*/
|
605
|
+
static VALUE cleanup(VALUE self) {
|
606
|
+
struct curl_state *state = get_curl_state(self);
|
607
|
+
curl_easy_reset(state->handle);
|
608
|
+
|
609
|
+
if (state->headers) {
|
610
|
+
curl_slist_free_all(state->headers);
|
611
|
+
state->headers = NULL;
|
612
|
+
}
|
613
|
+
|
614
|
+
if (state->download_file) {
|
615
|
+
fclose(state->download_file);
|
616
|
+
state->download_file = NULL;
|
617
|
+
}
|
618
|
+
|
619
|
+
if (state->upload_file) {
|
620
|
+
fclose(state->upload_file);
|
621
|
+
state->upload_file = NULL;
|
622
|
+
}
|
623
|
+
|
624
|
+
state->upload_buf = NULL;
|
625
|
+
|
626
|
+
return Qnil;
|
627
|
+
}
|
628
|
+
|
629
|
+
/* call-seq:
|
630
|
+
* session.handle_request( request ) -> response
|
631
|
+
*
|
632
|
+
* Peform the actual HTTP request by calling libcurl. Each filed in the
|
633
|
+
* +request+ object will be used to set the appropriate option on the libcurl
|
634
|
+
* library. After the request completes, a Response object will be created and
|
635
|
+
* returned.
|
636
|
+
*
|
637
|
+
* In the event of an error in the libcurl library, a Ruby exception will be
|
638
|
+
* created and raised. The exception will return the libcurl error code and
|
639
|
+
* error message.
|
640
|
+
*/
|
641
|
+
static VALUE session_handle_request(VALUE self, VALUE request) {
|
642
|
+
set_options_from_request(self, request);
|
643
|
+
return rb_ensure(&perform_request, self, &cleanup, self);
|
644
|
+
}
|
645
|
+
|
646
|
+
/* call-seq:
|
647
|
+
* session.reset -> session
|
648
|
+
*
|
649
|
+
* Reset the underlying cURL session. This effectively closes all open
|
650
|
+
* connections and disables debug output.
|
651
|
+
*/
|
652
|
+
static VALUE session_reset(VALUE self) {
|
653
|
+
struct curl_state *state;
|
654
|
+
Data_Get_Struct(self, struct curl_state, state);
|
655
|
+
|
656
|
+
if (NULL != state->handle) {
|
657
|
+
cleanup(self);
|
658
|
+
curl_easy_cleanup(state->handle);
|
659
|
+
state->handle = NULL;
|
660
|
+
session_close_debug_file(state);
|
661
|
+
}
|
662
|
+
|
663
|
+
return self;
|
664
|
+
}
|
665
|
+
|
666
|
+
/* call-seq:
|
667
|
+
* session.interrupt -> session
|
668
|
+
*
|
669
|
+
* Interrupt any currently executing request. This will cause the current
|
670
|
+
* request to error and raise an exception.
|
671
|
+
*/
|
672
|
+
static VALUE session_interrupt(VALUE self) {
|
673
|
+
struct curl_state *state = get_curl_state(self);
|
674
|
+
state->interrupt = 1;
|
675
|
+
return self;
|
676
|
+
}
|
677
|
+
|
678
|
+
/* call-seq:
|
679
|
+
* session.enable_cookie_session( file ) -> session
|
680
|
+
*
|
681
|
+
* Turn on cookie handling for this session, storing them in memory by
|
682
|
+
* default or in +file+ if specified. The +file+ must be readable and
|
683
|
+
* writable. Calling multiple times will add more files.
|
684
|
+
*/
|
685
|
+
static VALUE enable_cookie_session(VALUE self, VALUE file) {
|
686
|
+
struct curl_state *state = get_curl_state(self);
|
687
|
+
CURL* curl = state->handle;
|
688
|
+
char* file_path = NULL;
|
689
|
+
|
690
|
+
file_path = RSTRING_PTR(file);
|
691
|
+
if (file_path != NULL && strlen(file_path) != 0) {
|
692
|
+
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, file_path);
|
693
|
+
}
|
694
|
+
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, file_path);
|
695
|
+
|
696
|
+
return self;
|
697
|
+
}
|
698
|
+
|
699
|
+
/* call-seq:
|
700
|
+
* session.set_debug_file( file ) -> session
|
701
|
+
*
|
702
|
+
* Enable debug output to stderr or to specified +file+.
|
703
|
+
*/
|
704
|
+
static VALUE set_debug_file(VALUE self, VALUE file) {
|
705
|
+
struct curl_state *state = get_curl_state(self);
|
706
|
+
char* file_path = RSTRING_PTR(file);
|
707
|
+
|
708
|
+
session_close_debug_file(state);
|
709
|
+
|
710
|
+
if(file_path != NULL && strlen(file_path) != 0) {
|
711
|
+
state->debug_file = open_file(file, "wb");
|
712
|
+
} else {
|
713
|
+
state->debug_file = stderr;
|
714
|
+
}
|
715
|
+
|
716
|
+
return self;
|
717
|
+
}
|
718
|
+
|
719
|
+
|
720
|
+
/*----------------------------------------------------------------------------*/
|
721
|
+
/* Extension initialization */
|
722
|
+
|
723
|
+
void Init_session_ext() {
|
724
|
+
curl_global_init(CURL_GLOBAL_ALL);
|
725
|
+
rb_require("patron/error");
|
726
|
+
|
727
|
+
rb_set_end_proc(&cs_list_interrupt, Qnil);
|
728
|
+
|
729
|
+
mPatron = rb_define_module("Patron");
|
730
|
+
|
731
|
+
ePatronError = rb_const_get(mPatron, rb_intern("Error"));
|
732
|
+
|
733
|
+
eUnsupportedProtocol = rb_const_get(mPatron, rb_intern("UnsupportedProtocol"));
|
734
|
+
eURLFormatError = rb_const_get(mPatron, rb_intern("URLFormatError"));
|
735
|
+
eHostResolutionError = rb_const_get(mPatron, rb_intern("HostResolutionError"));
|
736
|
+
eConnectionFailed = rb_const_get(mPatron, rb_intern("ConnectionFailed"));
|
737
|
+
ePartialFileError = rb_const_get(mPatron, rb_intern("PartialFileError"));
|
738
|
+
eTimeoutError = rb_const_get(mPatron, rb_intern("TimeoutError"));
|
739
|
+
eTooManyRedirects = rb_const_get(mPatron, rb_intern("TooManyRedirects"));
|
740
|
+
|
741
|
+
rb_define_module_function(mPatron, "libcurl_version", libcurl_version, 0);
|
742
|
+
|
743
|
+
cSession = rb_define_class_under(mPatron, "Session", rb_cObject);
|
744
|
+
cRequest = rb_define_class_under(mPatron, "Request", rb_cObject);
|
745
|
+
rb_define_alloc_func(cSession, session_alloc);
|
746
|
+
|
747
|
+
rb_define_method(cSession, "escape", session_escape, 1);
|
748
|
+
rb_define_method(cSession, "unescape", session_unescape, 1);
|
749
|
+
rb_define_method(cSession, "handle_request", session_handle_request, 1);
|
750
|
+
rb_define_method(cSession, "reset", session_reset, 0);
|
751
|
+
rb_define_method(cSession, "interrupt", session_interrupt, 0);
|
752
|
+
rb_define_method(cSession, "enable_cookie_session", enable_cookie_session, 1);
|
753
|
+
rb_define_method(cSession, "set_debug_file", set_debug_file, 1);
|
754
|
+
rb_define_alias(cSession, "urlencode", "escape");
|
755
|
+
rb_define_alias(cSession, "urldecode", "unescape");
|
756
|
+
|
757
|
+
rb_define_const(cRequest, "AuthBasic", INT2FIX(CURLAUTH_BASIC));
|
758
|
+
rb_define_const(cRequest, "AuthDigest", INT2FIX(CURLAUTH_DIGEST));
|
759
|
+
rb_define_const(cRequest, "AuthAny", INT2FIX(CURLAUTH_ANY));
|
760
|
+
|
761
|
+
mProxyType = rb_define_module_under(mPatron, "ProxyType");
|
762
|
+
rb_define_const(mProxyType, "HTTP", INT2FIX(CURLPROXY_HTTP));
|
763
|
+
rb_define_const(mProxyType, "HTTP_1_0", INT2FIX(CURLPROXY_HTTP_1_0));
|
764
|
+
rb_define_const(mProxyType, "SOCKS4", INT2FIX(CURLPROXY_SOCKS4));
|
765
|
+
rb_define_const(mProxyType, "SOCKS5", INT2FIX(CURLPROXY_SOCKS5));
|
766
|
+
rb_define_const(mProxyType, "SOCKS4A", INT2FIX(CURLPROXY_SOCKS4A));
|
767
|
+
rb_define_const(mProxyType, "SOCKS5_HOSTNAME", INT2FIX(CURLPROXY_SOCKS5_HOSTNAME));
|
768
|
+
}
|
769
|
+
|