void mget_cookie_store_cookie(MGET_COOKIE *cookie) { MGET_COOKIE *old; int pos; debug_printf("got cookie %s=%s\n", cookie->name, cookie->value); if (!cookie->normalized) return; pthread_mutex_lock(&cookies_mutex); if (!cookies) { cookies = mget_vector_create(128, -2, (int(*)(const void *, const void *))compare_cookie); old = NULL; } else old = mget_vector_get(cookies, pos = mget_vector_find(cookies, cookie)); if (old) { debug_printf("replace old cookie %s=%s\n", cookie->name, cookie->value); cookie->creation = old->creation; mget_cookie_free_cookie(old); mget_vector_replace(cookies, cookie, sizeof(*cookie), pos); } else { debug_printf("store new cookie %s=%s\n", cookie->name, cookie->value); mget_vector_insert_sorted(cookies, cookie, sizeof(*cookie)); } pthread_mutex_unlock(&cookies_mutex); }
int mget_cookie_store_cookie(mget_cookie_db_t *cookie_db, mget_cookie_t *cookie) { mget_cookie_t *old; int pos; if (!cookie_db) { mget_cookie_deinit(cookie); return -1; } debug_printf("got cookie %s=%s\n", cookie->name, cookie->value); if (!cookie->normalized) { mget_cookie_deinit(cookie); return -1; } if (mget_cookie_check_psl(cookie_db, cookie) != 0) { debug_printf("cookie '%s' dropped, domain '%s' is a public suffix\n", cookie->name, cookie->domain); mget_cookie_deinit(cookie); return -1; } mget_thread_mutex_lock(&cookie_db->mutex); old = mget_vector_get(cookie_db->cookies, pos = mget_vector_find(cookie_db->cookies, cookie)); if (old) { debug_printf("replace old cookie %s=%s\n", cookie->name, cookie->value); cookie->creation = old->creation; mget_vector_replace(cookie_db->cookies, cookie, sizeof(*cookie), pos); } else { debug_printf("store new cookie %s=%s\n", cookie->name, cookie->value); mget_vector_insert_sorted(cookie_db->cookies, cookie, sizeof(*cookie)); } mget_thread_mutex_unlock(&cookie_db->mutex); return 0; }
static void test_vector(void) { struct ENTRY *tmp, txt_sorted[5] = { {""}, {"four"}, {"one"}, {"three"}, {"two"} }, *txt[countof(txt_sorted)]; mget_vector_t *v = mget_vector_create(2, -2, (int(*)(const void *, const void *))compare_txt); unsigned it; int n; // copy for (it = 0; it < countof(txt); it++) txt[it] = &txt_sorted[it]; // shuffle txt for (it = 0; it < countof(txt); it++) { n = rand() % countof(txt); tmp = txt[n]; txt[n] = txt[it]; txt[it] = tmp; } for (it = 0; it < countof(txt); it++) { mget_vector_insert_sorted(v, txt[it], sizeof(struct ENTRY)); } for (it = 0; it < countof(txt); it++) { struct ENTRY *e = mget_vector_get(v, it); if (!strcmp(e->txt,txt_sorted[it].txt)) ok++; else failed++; } mget_vector_free(&v); }