Beispiel #1
0
LrHandle *
lr_handle_init()
{
    LrHandle *handle;
    CURL *curl = lr_get_curl_handle();

    if (!curl)
        return NULL;

    handle = lr_malloc0(sizeof(LrHandle));
    handle->curl_handle = curl;
    handle->fastestmirrormaxage = LRO_FASTESTMIRRORMAXAGE_DEFAULT;
    handle->mirrorlist_fd = -1;
    handle->metalink_fd = -1;
    handle->checks |= LR_CHECK_CHECKSUM;
    handle->maxparalleldownloads = LRO_MAXPARALLELDOWNLOADS_DEFAULT;
    handle->maxdownloadspermirror = LRO_MAXDOWNLOADSPERMIRROR_DEFAULT;
    handle->lowspeedlimit = LRO_LOWSPEEDLIMIT_DEFAULT;

    return handle;
}
Beispiel #2
0
/** Create list of LrFastestMirror based on input list of URLs.
 */
static gboolean
lr_fastestmirror_prepare(LrHandle *handle,
                         GSList *in_list,
                         GSList **out_list,
                         LrFastestMirrorCache *cache,
                         GError **err)
{
    gboolean ret = TRUE;
    GSList *list = NULL;

    assert(!err || *err == NULL);

    if (!in_list) {
        *out_list = NULL;
        return TRUE;
    }

    gint64 maxage = LRO_FASTESTMIRRORMAXAGE_DEFAULT;
    gint64 current_time = g_get_real_time() / 1000000;

    if (handle)
        maxage = (gint64) handle->fastestmirrormaxage;

    for (GSList *elem = in_list; elem; elem = g_slist_next(elem)) {
        gchar *url = elem->data;
        CURLcode curlcode;
        CURL *curlh;

        // TODO: For prefixed by "file://" - set plain_connect_time to zero

        // Try to find item in the cache
        gint64 ts;
        double connecttime;
        if (lr_fastestmirrorcache_lookup(cache, url, &ts, &connecttime)) {
            if (ts >= (current_time - maxage)) {
                // Use cached entry
                g_debug("%s: Using cached connect time for: %s (%f)",
                        __func__, url, connecttime);
                LrFastestMirror *mirror = lr_lrfastestmirror_new();
                mirror->url = url;
                mirror->curl = NULL;
                mirror->plain_connect_time = connecttime;
                mirror->cached = TRUE;
                list = g_slist_append(list, mirror);
                continue;
            } else {
                g_debug("%s: Cached connect time too old: %s", __func__, url);
            }
        } else {
            g_debug("%s: Not found in cache: %s", __func__, url);
        }

        if (handle)
            curlh = curl_easy_duphandle(handle->curl_handle);
        else
            curlh = lr_get_curl_handle();

        if (!curlh) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                        "Cannot create curl handle");
            ret = FALSE;
            break;
        }

        LrFastestMirror *mirror = lr_lrfastestmirror_new();
        mirror->url = url;
        mirror->curl = curlh;

        curlcode = curl_easy_setopt(curlh, CURLOPT_URL, url);
        if (curlcode != CURLE_OK) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                        "curl_easy_setopt(_, CURLOPT_URL, %s) failed: %s",
                        url, curl_easy_strerror(curlcode));
            ret = FALSE;
            break;
        }

        curlcode = curl_easy_setopt(curlh, CURLOPT_CONNECT_ONLY, 1);
        if (curlcode != CURLE_OK) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                    "curl_easy_setopt(_, CURLOPT_CONNECT_ONLY, 1) failed: %s",
                    curl_easy_strerror(curlcode));
            ret = FALSE;
            break;
        }

        list = g_slist_append(list, mirror);
    }

    if (ret) {
        *out_list = list;
    } else {
        assert(!err || *err);
        g_slist_free_full(list, (GDestroyNotify)lr_lrfastestmirror_free);
        *out_list = NULL;
    }

    return ret;
}