int mget_cookie_suffix_match(const char *domain) { PUBLIC_SUFFIX suffix, *rule; const char *p, *label_bak; unsigned short length_bak; // this function should be called without leading dots, just make shure suffix.label = domain + (*domain == '.'); suffix.length = strlen(suffix.label); suffix.wildcard = 0; suffix.nlabels = 1; for (p = suffix.label; *p; p++) if (*p == '.') suffix.nlabels++; // if domain has enough labels, it won't match rule = mget_vector_get(suffixes, 0); if (!rule || rule->nlabels < suffix.nlabels - 1) return 0; rule = mget_vector_get(suffixes, mget_vector_find(suffixes, &suffix)); if (rule) { // definitely a match, no matter if the found rule is a wildcard or not return 1; } label_bak = suffix.label; length_bak = suffix.length; if ((suffix.label = strchr(suffix.label, '.'))) { suffix.label++; suffix.length = strlen(suffix.label); suffix.nlabels--; rule = mget_vector_get(suffixes, mget_vector_find(suffixes, &suffix)); if (rule) { if (rule->wildcard) { // now that we matched a wildcard, we have to check for an exception suffix.label = label_bak; suffix.length = length_bak; suffix.nlabels++; rule = mget_vector_get(suffix_exceptions, mget_vector_find(suffix_exceptions, &suffix)); if (rule) return 0; return 1; } } } return 0; }
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; }