コード例 #1
0
static int
trg_http_perform_inner(TrgClient * tc, gchar * reqstr,
                       trg_response * response, gboolean recurse)
{
    TrgClientPrivate *priv = tc->priv;
    TrgPrefs *prefs = trg_client_get_prefs(tc);
    gpointer threadLocalStorage = g_private_get(priv->tlsKey);
    trg_tls *tls;
    long httpCode = 0;
    gchar *session_id;
    struct curl_slist *headers = NULL;

    if (!threadLocalStorage) {
        tls = trg_tls_new(tc);
        g_private_set(priv->tlsKey, tls);
    } else {
        tls = (trg_tls *) threadLocalStorage;
    }

    g_mutex_lock(priv->configMutex);

    if (priv->configSerial > tls->serial)
        trg_tls_update(tc, tls, priv->configSerial);

    session_id = trg_client_get_session_id(tc);
    if (session_id) {
        headers = curl_slist_append(NULL, session_id);
        curl_easy_setopt(tls->curl, CURLOPT_HTTPHEADER, headers);
    }

    curl_easy_setopt(tls->curl, CURLOPT_TIMEOUT,
                     (long) trg_prefs_get_int(prefs, TRG_PREFS_KEY_TIMEOUT,
                                              TRG_PREFS_CONNECTION));

    g_mutex_unlock(priv->configMutex);

    response->size = 0;
    response->raw = NULL;

    curl_easy_setopt(tls->curl, CURLOPT_POSTFIELDS, reqstr);
    curl_easy_setopt(tls->curl, CURLOPT_WRITEDATA, (void *) response);
    response->status = curl_easy_perform(tls->curl);

    if (session_id) {
        g_free(session_id);
        curl_slist_free_all(headers);
    }

    curl_easy_getinfo(tls->curl, CURLINFO_RESPONSE_CODE, &httpCode);

    if (response->status == CURLE_OK) {
        if (httpCode == HTTP_CONFLICT && recurse == TRUE)
            return trg_http_perform_inner(tc, reqstr, response, FALSE);
        else if (httpCode != HTTP_OK)
            response->status = (-httpCode) - 100;
    }

    return response->status;
}
コード例 #2
0
static trg_tls *get_tls(TrgClient *tc) {
	TrgClientPrivate *priv = tc->priv;
	gpointer threadLocalStorage = g_private_get(&priv->tlsKey);
	trg_tls *tls;

    if (!threadLocalStorage) {
        tls = trg_tls_new(tc);
        g_private_set(&priv->tlsKey, tls);
    } else {
        tls = (trg_tls *) threadLocalStorage;
    }

    return tls;
}