Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split ruby_curl_easy_new function into separate allocate and initiali… #329

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions ext/curb_easy.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ VALUE cCurlEasy;

static VALUE callback_exception(VALUE unused) {
return Qfalse;
}
}

/* These handle both body and header data */
static size_t default_data_handler(char *stream,
Expand Down Expand Up @@ -298,34 +298,44 @@ static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
rbce->callback_active = 0;
}

/*
* Allocate space for a Curl::Easy instance.
*/
static VALUE ruby_curl_easy_allocate(VALUE klass) {
ruby_curl_easy *rbce;
rbce = ALLOC(ruby_curl_easy);
rbce->curl = NULL;
rbce->opts = Qnil;
rbce->multi = Qnil;
ruby_curl_easy_zero(rbce);
return Data_Wrap_Struct(klass, curl_easy_mark, curl_easy_free, rbce);
}

/*
* call-seq:
* Curl::Easy.new => #<Curl::Easy...>
* Curl::Easy.new(url = nil) => #<Curl::Easy...>
* Curl::Easy.new(url = nil) { |self| ... } => #<Curl::Easy...>
*
* Create a new Curl::Easy instance, optionally supplying the URL.
* Initialize a new Curl::Easy instance, optionally supplying the URL.
* The block form allows further configuration to be supplied before
* the instance is returned.
*/
static VALUE ruby_curl_easy_new(int argc, VALUE *argv, VALUE klass) {
static VALUE ruby_curl_easy_initialize(int argc, VALUE *argv, VALUE self) {
CURLcode ecode;
VALUE url, blk;
VALUE new_curl;
ruby_curl_easy *rbce;

rb_scan_args(argc, argv, "01&", &url, &blk);

rbce = ALLOC(ruby_curl_easy);
Data_Get_Struct(self, ruby_curl_easy, rbce);

/* handler */
rbce->curl = curl_easy_init();
if (!rbce->curl) {
rb_raise(eCurlErrFailedInit, "Failed to initialize easy handle");
}

new_curl = Data_Wrap_Struct(klass, curl_easy_mark, curl_easy_free, rbce);

rbce->multi = Qnil;
rbce->opts = Qnil;

Expand All @@ -334,16 +344,16 @@ static VALUE ruby_curl_easy_new(int argc, VALUE *argv, VALUE klass) {
rb_easy_set("url", url);

/* set the new_curl pointer to the curl handle */
ecode = curl_easy_setopt(rbce->curl, CURLOPT_PRIVATE, (void*)new_curl);
ecode = curl_easy_setopt(rbce->curl, CURLOPT_PRIVATE, (void*)self);
if (ecode != CURLE_OK) {
raise_curl_easy_error_exception(ecode);
}

if (blk != Qnil) {
rb_funcall(blk, idCall, 1, new_curl);
rb_funcall(blk, idCall, 1, self);
}

return new_curl;
return self;
}

/*
Expand Down Expand Up @@ -374,7 +384,7 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
* easy.close => nil
*
* Close the Curl::Easy instance. Any open connections are closed
* The easy handle is reinitialized. If a previous multi handle was
* The easy handle is reinitialized. If a previous multi handle was
* open it is set to nil and will be cleared after a GC.
*/
static VALUE ruby_curl_easy_close(VALUE self) {
Expand Down Expand Up @@ -509,7 +519,7 @@ static VALUE ruby_curl_easy_headers_get(VALUE self) {
ruby_curl_easy *rbce;
VALUE headers;
Data_Get_Struct(self, ruby_curl_easy, rbce);
headers = rb_easy_get("headers");//rb_hash_aref(rbce->opts, rb_intern("headers"));
headers = rb_easy_get("headers");//rb_hash_aref(rbce->opts, rb_intern("headers"));
if (headers == Qnil) { headers = rb_easy_set("headers", rb_hash_new()); }
return headers;
}
Expand Down Expand Up @@ -721,29 +731,29 @@ static VALUE ruby_curl_easy_useragent_get(VALUE self) {
/*
* call-seq:
* easy.post_body = "some=form%20data&to=send" => string or nil
*
*
* Sets the POST body of this Curl::Easy instance. This is expected to be
* URL encoded; no additional processing or encoding is done on the string.
* The content-type header will be set to application/x-www-form-urlencoded.
*
*
* This is handy if you want to perform a POST against a Curl::Multi instance.
*/
static VALUE ruby_curl_easy_post_body_set(VALUE self, VALUE post_body) {
ruby_curl_easy *rbce;
CURL *curl;

char *data;
long len;

Data_Get_Struct(self, ruby_curl_easy, rbce);

curl = rbce->curl;

if ( post_body == Qnil ) {
rb_easy_del("postdata_buffer");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
} else {

} else {
if (rb_type(post_body) == T_STRING) {
data = StringValuePtr(post_body);
len = RSTRING_LEN(post_body);
Expand All @@ -756,19 +766,19 @@ static VALUE ruby_curl_easy_post_body_set(VALUE self, VALUE post_body) {
else {
rb_raise(rb_eRuntimeError, "post data must respond_to .to_s");
}
// Store the string, since it has to hang around for the duration of the

// Store the string, since it has to hang around for the duration of the
// request. See CURLOPT_POSTFIELDS in the libcurl docs.
//rbce->postdata_buffer = post_body;
rb_easy_set("postdata_buffer", post_body);

curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);

return post_body;
}

return Qnil;
}

Expand All @@ -785,7 +795,7 @@ static VALUE ruby_curl_easy_post_body_get(VALUE self) {
/*
* call-seq:
* easy.put_data = data => ""
*
*
* Points this Curl::Easy instance to data to be uploaded via PUT. This
* sets the request to a PUT type request - useful if you want to PUT via
* a multi handle.
Expand Down Expand Up @@ -817,7 +827,7 @@ static VALUE ruby_curl_easy_put_data_set(VALUE self, VALUE data) {
curl_easy_setopt(curl, CURLOPT_SEEKDATA, rbce);
#endif

/*
/*
* we need to set specific headers for the PUT to work... so
* convert the internal headers structure to a HASH if one is set
*/
Expand All @@ -831,7 +841,7 @@ static VALUE ruby_curl_easy_put_data_set(VALUE self, VALUE data) {
if (NIL_P(data)) { return data; }

headers = rb_easy_get("headers");
if( headers == Qnil ) {
if( headers == Qnil ) {
headers = rb_hash_new();
}

Expand Down Expand Up @@ -1015,7 +1025,7 @@ static VALUE ruby_curl_easy_proxy_type_get(VALUE self) {
(!strncmp("ntlm",node,4)) ? CURLAUTH_NTLM : \
(!strncmp("anysafe",node,7)) ? CURLAUTH_ANYSAFE : \
(!strncmp("any",node,3)) ? CURLAUTH_ANY : 0
#else
#else
#define CURL_HTTPAUTH_STR_TO_NUM(node) \
(!strncmp("basic",node,5)) ? CURLAUTH_BASIC : \
(!strncmp("digest",node,6)) ? CURLAUTH_DIGEST : \
Expand Down Expand Up @@ -1047,7 +1057,7 @@ static VALUE ruby_curl_easy_http_auth_types_set(int argc, VALUE *argv, VALUE sel

if (len == 1 && (rb_ary_entry(args_ary,0) == Qnil || TYPE(rb_ary_entry(args_ary,0)) == T_FIXNUM ||
TYPE(rb_ary_entry(args_ary,0)) == T_BIGNUM)) {
if (rb_ary_entry(args_ary,0) == Qnil) {
if (rb_ary_entry(args_ary,0) == Qnil) {
rbce->http_auth_types = 0;
}
else {
Expand Down Expand Up @@ -1345,7 +1355,7 @@ static VALUE ruby_curl_easy_username_set(VALUE self, VALUE username) {
/*
* call-seq:
* easy.username => string
*
*
* Get the current username
*/
static VALUE ruby_curl_easy_username_get(VALUE self, VALUE username) {
Expand Down Expand Up @@ -1373,7 +1383,7 @@ static VALUE ruby_curl_easy_password_set(VALUE self, VALUE password) {
/*
* call-seq:
* easy.password => string
*
*
* Get the current password
*/
static VALUE ruby_curl_easy_password_get(VALUE self, VALUE password) {
Expand Down Expand Up @@ -1416,7 +1426,7 @@ static VALUE ruby_curl_easy_ssl_version_get(VALUE self, VALUE ssl_version) {
/*
* call-seq:
* easy.use_ssl = value => fixnum or nil
*
*
* Ensure libcurl uses SSL for FTP connections. Valid options are Curl::CURL_USESSL_NONE,
* Curl::CURL_USESSL_TRY, Curl::CURL_USESSL_CONTROL, and Curl::CURL_USESSL_ALL.
*/
Expand Down Expand Up @@ -1873,7 +1883,7 @@ static VALUE ruby_curl_easy_on_failure_set(int argc, VALUE *argv, VALUE self) {
* To remove a previously-supplied handler, call this method with no attached
* block.
*
* The +on_missing+ handler is called when request is finished with a
* The +on_missing+ handler is called when request is finished with a
* status of 40x
*/
static VALUE ruby_curl_easy_on_missing_set(int argc, VALUE *argv, VALUE self) {
Expand All @@ -1888,7 +1898,7 @@ static VALUE ruby_curl_easy_on_missing_set(int argc, VALUE *argv, VALUE self) {
* To remove a previously-supplied handler, call this method with no attached
* block.
*
* The +on_redirect+ handler is called when request is finished with a
* The +on_redirect+ handler is called when request is finished with a
* status of 30x
*/
static VALUE ruby_curl_easy_on_redirect_set(int argc, VALUE *argv, VALUE self) {
Expand Down Expand Up @@ -2297,7 +2307,7 @@ VALUE ruby_curl_easy_setup(ruby_curl_easy *rbce) {
rb_warn("libcurl is not configured with SSL support");
}
#endif

if (rbce->ftp_filemethod > 0) {
curl_easy_setopt(curl, CURLOPT_FTP_FILEMETHOD, rbce->ftp_filemethod);
}
Expand Down Expand Up @@ -2633,7 +2643,7 @@ static VALUE ruby_curl_easy_response_code_get(VALUE self) {
static VALUE ruby_curl_easy_primary_ip_get(VALUE self) {
ruby_curl_easy *rbce;
char* ip;

Data_Get_Struct(self, ruby_curl_easy, rbce);
curl_easy_getinfo(rbce->curl, CURLINFO_PRIMARY_IP, &ip);

Expand Down Expand Up @@ -2858,7 +2868,7 @@ static VALUE ruby_curl_easy_redirect_count_get(VALUE self) {
* call-seq:
* easy.redirect_url => "http://some.url" or nil
*
* Retrieve the URL a redirect would take you to if you
* Retrieve the URL a redirect would take you to if you
* would enable CURLOPT_FOLLOWLOCATION.
*
* Requires libcurl 7.18.2 or higher, otherwise -1 is always returned.
Expand Down Expand Up @@ -3486,9 +3496,12 @@ void init_curb_easy() {
cCurlEasy = rb_define_class_under(mCurl, "Easy", rb_cObject);

/* Class methods */
rb_define_singleton_method(cCurlEasy, "new", ruby_curl_easy_new, -1);
rb_define_alloc_func(cCurlEasy, ruby_curl_easy_allocate);
rb_define_singleton_method(cCurlEasy, "error", ruby_curl_easy_error_message, 1);

/* Initialize method */
rb_define_method(cCurlEasy, "initialize", ruby_curl_easy_initialize, -1);

/* Attributes for config next perform */
rb_define_method(cCurlEasy, "url", ruby_curl_easy_url_get, 0);
rb_define_method(cCurlEasy, "proxy_url", ruby_curl_easy_proxy_url_get, 0);
Expand Down