Class: Curl::PostField

Inherits:
Object
  • Object
show all
Defined in:
ext/curb_postfield.c

Class Method Summary collapse

Instance Method Summary collapse

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.

Overloads:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'ext/curb_postfield.c', line 210

static VALUE ruby_curl_postfield_new_content(int argc, VALUE *argv, VALUE klass) {
  ruby_curl_postfield *rbcpf = ALLOC(ruby_curl_postfield);
  if (!rbcpf) {
    rb_raise(rb_eNoMemError, "Failed to allocate memory for Curl::PostField");
  }
 
  // 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 Data_Wrap_Struct(cCurlPostField, curl_postfield_mark, curl_postfield_free, rbcpf);
}

.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.

Overloads:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'ext/curb_postfield.c', line 258

static VALUE ruby_curl_postfield_new_file(int argc, VALUE *argv, VALUE klass) {
  // TODO needs to handle content-type too
  ruby_curl_postfield *rbcpf = ALLOC(ruby_curl_postfield);
  if (!rbcpf) {
    rb_raise(rb_eNoMemError, "Failed to allocate memory for Curl::PostField");
  }

  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 Data_Wrap_Struct(cCurlPostField, curl_postfield_mark, curl_postfield_free, rbcpf);
}

Instance Method Details

#contentObject

Obtain the POST field content for this PostField.



334
335
336
# File 'ext/curb_postfield.c', line 334

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.



324
325
326
# File 'ext/curb_postfield.c', line 324

static VALUE ruby_curl_postfield_content_set(VALUE self, VALUE content) {
  CURB_OBJECT_SETTER(ruby_curl_postfield, content);
}

#content_typeObject

Get the POST field Content-type for this PostField.



354
355
356
# File 'ext/curb_postfield.c', line 354

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.



344
345
346
# File 'ext/curb_postfield.c', line 344

static VALUE ruby_curl_postfield_content_type_set(VALUE self, VALUE content_type) {
  CURB_OBJECT_SETTER(ruby_curl_postfield, content_type);
}

#local_fileObject

Get the POST field local filename for this PostField (when performing a file upload).



377
378
379
# File 'ext/curb_postfield.c', line 377

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.



366
367
368
# File 'ext/curb_postfield.c', line 366

static VALUE ruby_curl_postfield_local_file_set(VALUE self, VALUE local_file) {
  CURB_OBJECT_SETTER(ruby_curl_postfield, local_file);
}

#nameObject

Obtain the POST field name for this PostField.



312
313
314
# File 'ext/curb_postfield.c', line 312

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.



302
303
304
# File 'ext/curb_postfield.c', line 302

static VALUE ruby_curl_postfield_name_set(VALUE self, VALUE name) {
  CURB_OBJECT_SETTER(ruby_curl_postfield, name);
}

#local_fileObject

Get the POST field remote filename for this PostField (when performing a file upload).



402
403
404
# File 'ext/curb_postfield.c', line 402

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.



391
392
393
# File 'ext/curb_postfield.c', line 391

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.

Yields:

  • (field)


414
415
416
# File 'ext/curb_postfield.c', line 414

static VALUE ruby_curl_postfield_content_proc_set(int argc, VALUE *argv, VALUE self) {
  CURB_HANDLER_PROC_SETTER(ruby_curl_postfield, content_proc);
}

#to_strObject #to_sObject 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.



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
# File 'ext/curb_postfield.c', line 429

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

  Data_Get_Struct(self, ruby_curl_postfield, 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;
}