Method: Curl::PostField#to_str

Defined in:
ext/curb_postfield.c

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



423
424
425
426
427
428
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
# File 'ext/curb_postfield.c', line 423

static VALUE ruby_curl_postfield_to_str(VALUE self) {
  // FIXME This is using the deprecated curl_escape func
  ruby_curl_postfield *rbcpf;
  VALUE result = Qnil;
  
  Data_Get_Struct(self, ruby_curl_postfield, rbcpf);

  if ((rbcpf->local_file == Qnil) && (rbcpf->remote_file == Qnil)) {
    if (rbcpf->name != Qnil) {
      char *tmpchrs;
      
      if ((tmpchrs = curl_escape(StringValuePtr(rbcpf->name), RSTRING_LEN(rbcpf->name))) == NULL) {
        rb_raise(eCurlErrInvalidPostField, "Failed to url-encode name `%s'", tmpchrs);
      } else {
        VALUE tmpcontent = Qnil;
        VALUE escd_name = rb_str_new2(tmpchrs);
        curl_free(tmpchrs);
        
        if (rbcpf->content_proc != Qnil) {
          tmpcontent = rb_funcall(rbcpf->content_proc, idCall, 1, self);
        } else if (rbcpf->content != Qnil) {
          tmpcontent = rbcpf->content;
        } else {
          tmpcontent = rb_str_new2("");
        }
        
        if ((tmpchrs = curl_escape(StringValuePtr(tmpcontent), RSTRING_LEN(tmpcontent))) == NULL) {
          rb_raise(eCurlErrInvalidPostField, "Failed to url-encode content `%s'", tmpchrs);
        } else {
          VALUE escd_content = rb_str_new2(tmpchrs);
          curl_free(tmpchrs);
          
          result = escd_name;
          rb_str_cat(result, "=", 1);
          rb_str_concat(result, escd_content); 
        }            
      }
    } else {
      rb_raise(eCurlErrInvalidPostField, "Cannot convert unnamed field to string");
    }      
  } else {
    rb_raise(eCurlErrInvalidPostField, "Cannot convert non-content field to string");
  }  
  
  return result;
}