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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
# File 'ext/ovirtsdk4c/ov_http_client.c', line 502
static VALUE ov_http_client_initialize(int argc, VALUE* argv, VALUE self) {
VALUE opt;
VALUE opts;
long connections;
long pipeline;
ov_http_client_object* ptr;
/* Get the pointer to the native object: */
ov_http_client_ptr(self, ptr);
/* Check the number of arguments: */
if (argc > 1) {
rb_raise(ov_error_class, "Expected at most one argument, 'opts', but received %d", argc);
}
opts = argc > 0? argv[0]: Qnil;
if (NIL_P(opts)) {
opts = rb_hash_new();
}
else {
Check_Type(opts, T_HASH);
}
/* Get the value of the 'ca_file' parameter: */
opt = rb_hash_aref(opts, CA_FILE_SYMBOL);
if (NIL_P(opt)) {
ptr->ca_file = NULL;
}
else {
Check_Type(opt, T_STRING);
ptr->ca_file = ov_string_dup(opt);
}
/* Get the value of the 'insecure' parameter: */
opt = rb_hash_aref(opts, INSECURE_SYMBOL);
ptr->insecure = NIL_P(opt)? false: RTEST(opt);
/* Get the value of the 'debug' parameter: */
opt = rb_hash_aref(opts, DEBUG_SYMBOL);
ptr->debug = NIL_P(opt)? false: RTEST(opt);
/* Get the value of the 'compress' parameter: */
opt = rb_hash_aref(opts, COMPRESS_SYMBOL);
ptr->compress = NIL_P(opt)? true: RTEST(opt);
/* Get the value of the 'timeout' parameter: */
opt = rb_hash_aref(opts, TIMEOUT_SYMBOL);
if (NIL_P(opt)) {
ptr->timeout = 0;
}
else {
Check_Type(opt, T_FIXNUM);
ptr->timeout = NUM2INT(opt);
}
/* Get the value of the 'connect_timeout' parameter: */
opt = rb_hash_aref(opts, CONNECT_TIMEOUT_SYMBOL);
if (NIL_P(opt)) {
ptr->connect_timeout = 0;
}
else {
Check_Type(opt, T_FIXNUM);
ptr->connect_timeout = NUM2INT(opt);
}
/* Get the value of the 'proxy_url' parameter: */
opt = rb_hash_aref(opts, PROXY_URL_SYMBOL);
if (NIL_P(opt)) {
ptr->proxy_url = NULL;
}
else {
Check_Type(opt, T_STRING);
ptr->proxy_url = ov_string_dup(opt);
}
/* Get the value of the 'proxy_username' parameter: */
opt = rb_hash_aref(opts, PROXY_USERNAME_SYMBOL);
if (NIL_P(opt)) {
ptr->proxy_username = NULL;
}
else {
Check_Type(opt, T_STRING);
ptr->proxy_username = ov_string_dup(opt);
}
/* Get the value of the 'proxy_password' parameter: */
opt = rb_hash_aref(opts, PROXY_PASSWORD_SYMBOL);
if (NIL_P(opt)) {
ptr->proxy_password = NULL;
}
else {
Check_Type(opt, T_STRING);
ptr->proxy_password = ov_string_dup(opt);
}
/* Get the value of the 'log' parameter: */
opt = rb_hash_aref(opts, LOG_SYMBOL);
ptr->log = opt;
/* Get the value of the 'pipeline' parameter: */
opt = rb_hash_aref(opts, PIPELINE_SYMBOL);
if (NIL_P(opt)) {
pipeline = 0;
}
else {
Check_Type(opt, T_FIXNUM);
pipeline = NUM2LONG(opt);
}
if (pipeline < 0) {
rb_raise(rb_eArgError, "The maximum pipeline length can't be %ld, minimum is 0.", pipeline);
}
/* Get the value of the 'connections' parameter: */
opt = rb_hash_aref(opts, CONNECTIONS_SYMBOL);
if (NIL_P(opt)) {
connections = 1;
}
else {
Check_Type(opt, T_FIXNUM);
connections = NUM2LONG(opt);
}
if (connections < 1) {
rb_raise(rb_eArgError, "The maximum number of connections can't be %ld, minimum is 1.", connections);
}
/* Get the value of the 'cookies' parameter. If it is a string it will be used as the path of the file where the
cookies will be stored. If it is any other thing it will be treated as a boolean flag indicating if cookies
should be enabled but not loaded/saved from/to any file. */
opt = rb_hash_aref(opts, COOKIES_SYMBOL);
if (TYPE(opt) == T_STRING) {
ptr->cookies = ov_string_dup(opt);
}
else if (RTEST(opt)) {
ptr->cookies = ov_string_dup(rb_str_new2(""));
}
else {
ptr->cookies = NULL;
}
/* Create the queue that contains requests that haven't been sent to libcurl yet: */
ptr->queue = rb_ary_new();
/* Create the hash that contains the transfers are pending an completed. Both use the identity of the request
as key. */
ptr->completed = rb_funcall(rb_hash_new(), COMPARE_BY_IDENTITY_ID, 0);
ptr->pending = rb_funcall(rb_hash_new(), COMPARE_BY_IDENTITY_ID, 0);
/* Calculate the max number of requests that can be handled by libcurl simultaneously. For versions of libcurl
newer than 7.30.0 the limit can be increased when using pipelining. For older versions it can't be increased
because libcurl would create additional connections for the requests that can't be pipelined. */
ptr->limit = connections;
if (pipeline > 0 && libcurl_version->version_num >= 0x071e00 /* 7.30.0 */) {
ptr->limit *= pipeline;
}
/* Create the libcurl multi handle: */
ptr->handle = curl_multi_init();
if (ptr->handle == NULL) {
rb_raise(ov_error_class, "Can't create libcurl multi object");
}
/* Create the libcurl share handle in order to share cookie data: */
ptr->share = curl_share_init();
if (ptr->share == NULL) {
rb_raise(ov_error_class, "Can't create libcurl share object");
}
if (ptr->cookies != NULL) {
curl_share_setopt(ptr->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
}
/* Enable pipelining: */
if (pipeline > 0) {
curl_multi_setopt(ptr->handle, CURLMOPT_PIPELINING, CURLPIPE_HTTP1);
if (libcurl_version->version_num >= 0x071e00 /* 7.30.0 */) {
curl_multi_setopt(ptr->handle, CURLMOPT_MAX_PIPELINE_LENGTH, pipeline);
}
else {
ov_http_client_log_warn(
ptr->log,
"Can't set maximum pipeline length to %d, it isn't supported by libcurl %s. Upgrade to 7.30.0 or "
"newer to avoid this issue.",
pipeline,
libcurl_version->version
);
}
}
/* Set the max number of connections: */
if (connections > 0) {
if (libcurl_version->version_num >= 0x071e00 /* 7.30.0 */) {
curl_multi_setopt(ptr->handle, CURLMOPT_MAX_HOST_CONNECTIONS, connections);
curl_multi_setopt(ptr->handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, connections);
}
else {
ov_http_client_log_warn(
ptr->log,
"Can't set maximum number of connections to %d, it isn't supported by libcurl %s. Upgrade to 7.30.0 "
"or newer to avoid this issue.",
connections,
libcurl_version->version
);
}
if (libcurl_version->version_num >= 0x070f03 /* 7.16.3 */) {
curl_multi_setopt(ptr->handle, CURLMOPT_MAXCONNECTS, connections);
}
else {
ov_http_client_log_warn(
ptr->log,
"Can't set total maximum connection cache size to %d, it isn't supported by libcurl %s. Upgrade to "
"7.16.3 or newer to avoid this issue.",
connections,
libcurl_version->version
);
}
}
return self;
}
|