Class: Curl::PostField
- Inherits:
-
Object
- Object
- Curl::PostField
- Defined in:
- ext/curb_postfield.c
Class Method Summary collapse
-
.content(*args) ⇒ Object
Create a new Curl::PostField, supplying the field name, content, and, optionally, Content-type (curl will attempt to determine this if not specified).
-
.file(*args) ⇒ Object
Create a new Curl::PostField for a file upload field, supplying the local filename to read from, and optionally the remote filename (defaults to the local name).
Instance Method Summary collapse
-
#content ⇒ Object
Obtain the POST field content for this PostField.
-
#content=(content) ⇒ Object
Set the POST field content for this PostField.
-
#content_type ⇒ Object
Get the POST field Content-type for this PostField.
-
#content_type=(content_type) ⇒ Object
Set the POST field Content-type for this PostField.
-
#local_file ⇒ Object
Get the POST field local filename for this PostField (when performing a file upload).
-
#local_file=(local_file) ⇒ Object
Set the POST field local filename for this PostField (when performing a file upload).
-
#name ⇒ Object
Obtain the POST field name for this PostField.
-
#name=(name) ⇒ Object
Set the POST field name for this PostField.
-
#local_file ⇒ Object
Get the POST field remote filename for this PostField (when performing a file upload).
-
#remote_file=(remote_file) ⇒ Object
Set the POST field remote filename for this PostField (when performing a file upload).
-
#set_content_proc {|field| ... } ⇒ Object
Set a content proc for this field.
-
#to_str ⇒ Object
(also: #to_s)
Obtain a String representation of this PostField in url-encoded format.
Class Method Details
.Curl::PostField.content(name, content) ⇒ #<Curl::PostField... .Curl::PostField.content(name, content, content_type = nil) ⇒ #<Curl::PostField... .Curl::PostField.content(name, content_type = nil) {|field| ... } ⇒ #<Curl::PostField...
Create a new Curl::PostField, supplying the field name, content, and, optionally, Content-type (curl will attempt to determine this if not specified).
The block form allows a block to supply the content for this field, called during the perform. The block should return a ruby string with the field data.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'ext/curb_postfield.c', line 234
static VALUE ruby_curl_postfield_new_content(int argc, VALUE *argv, VALUE klass) {
VALUE self;
ruby_curl_postfield *rbcpf;
self = TypedData_Make_Struct(klass, ruby_curl_postfield, &ruby_curl_postfield_data_type, rbcpf);
MEMZERO(rbcpf, ruby_curl_postfield, 1);
// wierdness - we actually require two args, unless a block is provided, but
// we have to work that out below.
rb_scan_args(argc, argv, "12&", &rbcpf->name, &rbcpf->content, &rbcpf->content_type, &rbcpf->content_proc);
// special handling if theres a block, second arg is actually content_type
if (rbcpf->content_proc != Qnil) {
if (rbcpf->content != Qnil) {
// we were given a content-type
rbcpf->content_type = rbcpf->content;
rbcpf->content = Qnil;
} else {
// default content type
rbcpf->content_type = Qnil;
}
} else {
// no block, so make sure content was provided
if (rbcpf->content == Qnil) {
rb_raise(rb_eArgError, "Incorrect number of arguments (expected 2 or 3)");
}
}
/* assoc objects */
rbcpf->local_file = Qnil;
rbcpf->remote_file = Qnil;
rbcpf->buffer_str = Qnil;
return self;
}
|
.Curl::PostField.file(name, local_file_name) ⇒ #<Curl::PostField... .Curl::PostField.file(name, local_file_name, remote_file_name = local_file_name) ⇒ #<Curl::PostField... .Curl::PostField.file(name, remote_file_name) {|field| ... } ⇒ #<Curl::PostField...
Create a new Curl::PostField for a file upload field, supplying the local filename to read from, and optionally the remote filename (defaults to the local name).
The block form allows a block to supply the content for this field, called during the perform. The block should return a ruby string with the field data.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'ext/curb_postfield.c', line 283
static VALUE ruby_curl_postfield_new_file(int argc, VALUE *argv, VALUE klass) {
// TODO needs to handle content-type too
VALUE self;
ruby_curl_postfield *rbcpf;
self = TypedData_Make_Struct(klass, ruby_curl_postfield, &ruby_curl_postfield_data_type, rbcpf);
MEMZERO(rbcpf, ruby_curl_postfield, 1);
rb_scan_args(argc, argv, "21&", &rbcpf->name, &rbcpf->local_file, &rbcpf->remote_file, &rbcpf->content_proc);
// special handling if theres a block, second arg is actually remote name.
if (rbcpf->content_proc != Qnil) {
if (rbcpf->local_file != Qnil) {
// we were given a local file
if (rbcpf->remote_file == Qnil) {
// we weren't given a remote, so local is actually remote
// (correct block call form)
rbcpf->remote_file = rbcpf->local_file;
}
// Shouldn't get a local file, so can ignore it.
rbcpf->local_file = Qnil;
}
} else {
if (rbcpf->remote_file == Qnil) {
rbcpf->remote_file = rbcpf->local_file;
}
}
/* assoc objects */
rbcpf->content = Qnil;
rbcpf->content_type = Qnil;
rbcpf->buffer_str = Qnil;
return self;
}
|
Instance Method Details
#content ⇒ Object
Obtain the POST field content for this PostField.
360 361 362 |
# File 'ext/curb_postfield.c', line 360 static VALUE ruby_curl_postfield_content_get(VALUE self) { CURB_OBJECT_GETTER(ruby_curl_postfield, content); } |
#content=(content) ⇒ Object
Set the POST field content for this PostField. Ignored when a
content_proc is supplied via either Curl::PostField.file or
set_content_proc.
350 351 352 |
# File 'ext/curb_postfield.c', line 350
static VALUE ruby_curl_postfield_content_set(VALUE self, VALUE content) {
CURB_OBJECT_SETTER(ruby_curl_postfield, content);
}
|
#content_type ⇒ Object
Get the POST field Content-type for this PostField.
380 381 382 |
# File 'ext/curb_postfield.c', line 380 static VALUE ruby_curl_postfield_content_type_get(VALUE self) { CURB_OBJECT_GETTER(ruby_curl_postfield, content_type); } |
#content_type=(content_type) ⇒ Object
Set the POST field Content-type for this PostField.
370 371 372 |
# File 'ext/curb_postfield.c', line 370
static VALUE ruby_curl_postfield_content_type_set(VALUE self, VALUE content_type) {
CURB_OBJECT_SETTER(ruby_curl_postfield, content_type);
}
|
#local_file ⇒ Object
Get the POST field local filename for this PostField (when performing a file upload).
403 404 405 |
# File 'ext/curb_postfield.c', line 403 static VALUE ruby_curl_postfield_local_file_get(VALUE self) { CURB_OBJECT_GETTER(ruby_curl_postfield, local_file); } |
#local_file=(local_file) ⇒ Object
Set the POST field local filename for this PostField (when performing
a file upload). Ignored when a content_proc is supplied via either
Curl::PostField.file or set_content_proc.
392 393 394 |
# File 'ext/curb_postfield.c', line 392
static VALUE ruby_curl_postfield_local_file_set(VALUE self, VALUE local_file) {
CURB_OBJECT_SETTER(ruby_curl_postfield, local_file);
}
|
#name ⇒ Object
Obtain the POST field name for this PostField.
338 339 340 |
# File 'ext/curb_postfield.c', line 338 static VALUE ruby_curl_postfield_name_get(VALUE self) { CURB_OBJECT_GETTER(ruby_curl_postfield, name); } |
#name=(name) ⇒ Object
Set the POST field name for this PostField.
328 329 330 |
# File 'ext/curb_postfield.c', line 328
static VALUE ruby_curl_postfield_name_set(VALUE self, VALUE name) {
CURB_OBJECT_SETTER(ruby_curl_postfield, name);
}
|
#local_file ⇒ Object
Get the POST field remote filename for this PostField (when performing a file upload).
428 429 430 |
# File 'ext/curb_postfield.c', line 428 static VALUE ruby_curl_postfield_remote_file_get(VALUE self) { CURB_OBJECT_GETTER(ruby_curl_postfield, remote_file); } |
#remote_file=(remote_file) ⇒ Object
Set the POST field remote filename for this PostField (when performing a file upload). If no remote filename is provided, and no content_proc is supplied, the local filename is used. If no remote filename is specified when a content_proc is used, an exception will be raised during the perform.
417 418 419 |
# File 'ext/curb_postfield.c', line 417
static VALUE ruby_curl_postfield_remote_file_set(VALUE self, VALUE remote_file) {
CURB_OBJECT_SETTER(ruby_curl_postfield, remote_file);
}
|
#set_content_proc {|field| ... } ⇒ Object
Set a content proc for this field. This proc will be called during the
perform to supply the content for this field, overriding any setting
of content or local_file.
440 441 442 |
# File 'ext/curb_postfield.c', line 440
static VALUE ruby_curl_postfield_content_proc_set(int argc, VALUE *argv, VALUE self) {
CURB_HANDLER_PROC_SETTER(ruby_curl_postfield, content_proc);
}
|
#to_str ⇒ Object #to_s ⇒ Object Also known as: to_s
Obtain a String representation of this PostField in url-encoded format. This is used to construct the post data for non-multipart POSTs.
Only content fields may be converted to strings.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'ext/curb_postfield.c', line 455
static VALUE ruby_curl_postfield_to_str(VALUE self) {
ruby_curl_postfield *rbcpf;
VALUE result = Qnil;
VALUE name = Qnil;
char *tmpchrs;
#ifdef HAVE_CURL_EASY_ESCAPE
CURL *curl_handle = NULL;
#endif
TypedData_Get_Struct(self, ruby_curl_postfield, &ruby_curl_postfield_data_type, rbcpf);
if (rbcpf->name != Qnil) {
name = rbcpf->name;
if (TYPE(name) != T_STRING) {
if (rb_respond_to(name, rb_intern("to_s")))
name = rb_funcall(name, rb_intern("to_s"), 0);
else
name = Qnil;
}
}
if (name == Qnil) {
rb_raise(eCurlErrInvalidPostField,
"Cannot convert unnamed field to string %s:%d, make sure your field name responds_to :to_s",
__FILE__, __LINE__);
}
/* Force field name to UTF-8 before escaping */
VALUE name_utf8 = rb_str_export_to_enc(name, rb_utf8_encoding());
#ifdef HAVE_CURL_EASY_ESCAPE
curl_handle = curl_easy_init();
if (!curl_handle) {
rb_raise(eCurlErrInvalidPostField, "Failed to initialize curl handle for escaping");
}
tmpchrs = curl_easy_escape(curl_handle, StringValuePtr(name_utf8), (int)RSTRING_LEN(name_utf8));
if (!tmpchrs) {
curl_easy_cleanup(curl_handle);
rb_raise(eCurlErrInvalidPostField, "Failed to url-encode name");
}
#else
tmpchrs = curl_escape(StringValuePtr(name_utf8), (int)RSTRING_LEN(name_utf8));
if (!tmpchrs) {
rb_raise(eCurlErrInvalidPostField, "Failed to url-encode name");
}
#endif
VALUE escd_name = rb_str_new2(tmpchrs);
#ifdef HAVE_CURL_EASY_ESCAPE
curl_free(tmpchrs);
#else
curl_free(tmpchrs);
#endif
VALUE tmpcontent = Qnil;
if (rbcpf->content_proc != Qnil) {
tmpcontent = rb_funcall(rbcpf->content_proc, idCall, 1, self);
} else if (rbcpf->content != Qnil) {
tmpcontent = rbcpf->content;
} else if (rbcpf->local_file != Qnil) {
tmpcontent = rbcpf->local_file;
} else if (rbcpf->remote_file != Qnil) {
tmpcontent = rbcpf->remote_file;
} else {
tmpcontent = rb_str_new2("");
}
if (TYPE(tmpcontent) != T_STRING) {
if (rb_respond_to(tmpcontent, rb_intern("to_s")))
tmpcontent = rb_funcall(tmpcontent, rb_intern("to_s"), 0);
else {
#ifdef HAVE_CURL_EASY_ESCAPE
curl_easy_cleanup(curl_handle);
#endif
rb_raise(rb_eRuntimeError,
"postfield(%s) is not a string and does not respond_to to_s",
RSTRING_PTR(escd_name));
}
}
/* Force content to UTF-8 before escaping */
VALUE content_utf8 = rb_str_export_to_enc(tmpcontent, rb_utf8_encoding());
#ifdef HAVE_CURL_EASY_ESCAPE
tmpchrs = curl_easy_escape(curl_handle, StringValuePtr(content_utf8), (int)RSTRING_LEN(content_utf8));
if (!tmpchrs) {
curl_easy_cleanup(curl_handle);
rb_raise(eCurlErrInvalidPostField, "Failed to url-encode content");
}
#else
tmpchrs = curl_escape(StringValuePtr(content_utf8), (int)RSTRING_LEN(content_utf8));
if (!tmpchrs) {
rb_raise(eCurlErrInvalidPostField, "Failed to url-encode content");
}
#endif
VALUE escd_content = rb_str_new2(tmpchrs);
#ifdef HAVE_CURL_EASY_ESCAPE
curl_free(tmpchrs);
curl_easy_cleanup(curl_handle);
#else
curl_free(tmpchrs);
#endif
result = escd_name;
rb_str_cat(result, "=", 1);
rb_str_concat(result, escd_content);
return result;
}
|