Class: OvirtSDK4::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object


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;
}

Instance Method Details

#closeObject

Define the methods:


249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/ovirtsdk4c/ov_http_client.c', line 249

static VALUE ov_http_client_close(VALUE self) {
    ov_http_client_object* ptr;

    /* Get the pointer to the native object and check that it isn't closed: */
    ov_http_client_ptr(self, ptr);
    ov_http_client_check_closed(ptr);

    /* Release the resources used by libcurl: */
    curl_multi_cleanup(ptr->handle);
    ptr->handle = NULL;

    return Qnil;
}

#inspectObject


1147
1148
1149
1150
1151
1152
# File 'ext/ovirtsdk4c/ov_http_client.c', line 1147

static VALUE ov_http_client_inspect(VALUE self) {
    ov_http_client_object* ptr;

    ov_http_client_ptr(self, ptr);
    return rb_sprintf("#<%"PRIsVALUE":%p>", ov_http_client_class, ptr);
}

#send(request) ⇒ Object


1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'ext/ovirtsdk4c/ov_http_client.c', line 1084

static VALUE ov_http_client_send(VALUE self, VALUE request) {
    ov_http_client_object* ptr;

    /* Get the pointer to the native object and check that it isn't closed: */
    ov_http_client_ptr(self, ptr);
    ov_http_client_check_closed(ptr);

    /* If the limit hasn't been reached then submit the request directly to libcurl, otherwise put it in the queue: */
    if (RHASH_SIZE(ptr->pending) < ptr->limit) {
        ov_http_client_submit(self, request);
    }
    else {
        rb_ary_push(ptr->queue, request);
    }

    return Qnil;
}

#to_sObject


1147
1148
1149
1150
1151
1152
# File 'ext/ovirtsdk4c/ov_http_client.c', line 1147

static VALUE ov_http_client_inspect(VALUE self) {
    ov_http_client_object* ptr;

    ov_http_client_ptr(self, ptr);
    return rb_sprintf("#<%"PRIsVALUE":%p>", ov_http_client_class, ptr);
}

#wait(request) ⇒ Object


1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
# File 'ext/ovirtsdk4c/ov_http_client.c', line 1102

static VALUE ov_http_client_wait(VALUE self, VALUE request) {
    VALUE next;
    VALUE result;
    ov_http_client_object* ptr;
    ov_http_client_wait_context context;

    /* Get the pointer to the native object and check that it isn't closed: */
    ov_http_client_ptr(self, ptr);
    ov_http_client_check_closed(ptr);

    /* Work till the transfer has been completed. */
    context.handle = ptr->handle;
    context.code = CURLE_OK;
    context.cancel = false;
    for (;;) {
        /* Move requests from the queue to libcurl: */
        while (RARRAY_LEN(ptr->queue) > 0 && RHASH_SIZE(ptr->pending) < ptr->limit) {
            next = rb_ary_shift(ptr->queue);
            ov_http_client_submit(self, next);
        }

        /* Check if the response is already available, if so then return it: */
        result = rb_hash_delete(ptr->completed, request);
        if (!NIL_P(result)) {
            return result;
        }

        /* If the response isn't available yet, then do some real work: */
        rb_thread_call_without_gvl(
            ov_http_client_wait_task,
            &context,
            ov_http_client_wait_cancel,
            &context
        );
        if (context.cancel) {
            return Qnil;
        }
        if (context.code != CURLE_OK) {
            rb_raise(ov_error_class, "Unexpected error while waiting: %s", curl_easy_strerror(context.code));
        }
    }

    return Qnil;
}