Esempio n. 1
0
static void
do_cookies_accept_policy_test (void)
{
	SoupSession *session;
	SoupMessage *msg;
	SoupURI *uri;
	SoupCookieJar *jar;
	GSList *l, *p;
	int i;

	debug_printf (1, "SoupCookieJarAcceptPolicy test\n");

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
	soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
	jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));

	for (i = 0; i < G_N_ELEMENTS (validResults); i++) {
		soup_cookie_jar_set_accept_policy (jar, validResults[i].policy);

		uri = soup_uri_new_with_base (first_party_uri, "/index.html");
		msg = soup_message_new_from_uri ("GET", uri);
		soup_message_set_first_party (msg, first_party_uri);
		soup_session_send_message (session, msg);
		soup_uri_free (uri);
		g_object_unref (msg);

		/* We can't use two servers due to limitations in
		 * test_server, so let's swap first and third party here
		 * to simulate a cookie coming from a third party.
		 */
		uri = soup_uri_new_with_base (first_party_uri, "/foo.jpg");
		msg = soup_message_new_from_uri ("GET", uri);
		soup_message_set_first_party (msg, third_party_uri);
		soup_session_send_message (session, msg);
		soup_uri_free (uri);
		g_object_unref (msg);

		l = soup_cookie_jar_all_cookies (jar);
		if (g_slist_length (l) < validResults[i].n_cookies) {
			debug_printf (1, " accepted less cookies than it should have\n");
			errors++;
		} else if (g_slist_length (l) > validResults[i].n_cookies) {
			debug_printf (1, " accepted more cookies than it should have\n");
			errors++;
		}

		for (p = l; p; p = p->next) {
			soup_cookie_jar_delete_cookie (jar, p->data);
			soup_cookie_free (p->data);
		}

		g_slist_free (l);
	}

	soup_test_session_abort_unref (session);
}
Esempio n. 2
0
void deleteAllCookies(NetworkingContext* context)
{
    SoupCookieJar* cookieJar = context ? cookieJarForContext(context) : soupCookieJar();
    GOwnPtr<GSList> cookies(soup_cookie_jar_all_cookies(cookieJar));
    for (GSList* item = cookies.get(); item; item = g_slist_next(item)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(item->data);
        soup_cookie_jar_delete_cookie(cookieJar, cookie);
        soup_cookie_free(cookie);
    }
}
Esempio n. 3
0
void deleteAllCookies(const NetworkStorageSession& session)
{
    SoupCookieJar* cookieJar = cookieJarForSession(session);
    GUniquePtr<GSList> cookies(soup_cookie_jar_all_cookies(cookieJar));
    for (GSList* item = cookies.get(); item; item = g_slist_next(item)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(item->data);
        soup_cookie_jar_delete_cookie(cookieJar, cookie);
        soup_cookie_free(cookie);
    }
}
Esempio n. 4
0
static void
delete_cookie (const char *filename, SoupCookie *cookie)
{
	char *contents = NULL, *line, *p;
	gsize length = 0;
	FILE *f;
	SoupCookie *c;
	time_t now = time (NULL);

	if (!g_file_get_contents (filename, &contents, &length, NULL))
		return;

	f = fopen (filename, "w");
	if (!f) {
		g_free (contents);
		return;
	}

	line = contents;
	for (p = contents; *p; p++) {
		/* \r\n comes out as an extra empty line and gets ignored */
		if (*p == '\r' || *p == '\n') {
			*p = '\0';
			c = parse_cookie (line, now);
			line = p + 1;
			if (!c)
				continue;
			if (!soup_cookie_equal (cookie, c))
				write_cookie (f, c);
			soup_cookie_free (c);
		}
	}
	c = parse_cookie (line, now);
	if (c) {
		if (!soup_cookie_equal (cookie, c))
			write_cookie (f, c);
		soup_cookie_free (c);
	}

	g_free (contents);
	fclose (f);
}
Esempio n. 5
0
static gboolean
katze_http_cookies_update_jar (KatzeHttpCookies* http_cookies)
{
    gint fn = 0;
    FILE* f = NULL;
    gchar* temporary_filename = NULL;
    GSList* cookies;

    http_cookies->timeout = 0;

    temporary_filename = g_strconcat (http_cookies->filename, ".XXXXXX", NULL);
    if ((fn = g_mkstemp (temporary_filename)) == -1)
        goto failed;
    if (!((f = fdopen (fn, "wb"))))
        goto failed;

    cookies = soup_cookie_jar_all_cookies (http_cookies->jar);
    for (; cookies != NULL; cookies = g_slist_next (cookies))
    {
        SoupCookie* cookie = cookies->data;
        if (cookie->expires && !soup_date_is_past (cookie->expires))
            write_cookie (f, cookie);
        soup_cookie_free (cookie);
    }
    g_slist_free (cookies);

    if (fclose (f) != 0)
    {
        f = NULL;
        goto failed;
    }
    f = NULL;

    if (g_rename (temporary_filename, http_cookies->filename) == -1)
        goto failed;
    g_free (temporary_filename);

    if (g_getenv ("MIDORI_COOKIES_DEBUG") != NULL)
    {
        g_print ("KatzeHttpCookies: %d cookies changed\n", http_cookies->counter);
        http_cookies->counter = 0;
    }
    return FALSE;

failed:
    if (f)
        fclose (f);
    g_unlink (temporary_filename);
    g_free (temporary_filename);
    if (g_getenv ("MIDORI_COOKIES_DEBUG") != NULL)
        g_print ("KatzeHttpCookies: Failed to write '%s'\n",
                 http_cookies->filename);
    return FALSE;
}
Esempio n. 6
0
void getHostnamesWithCookies(const NetworkStorageSession& session, HashSet<String>& hostnames)
{
    SoupCookieJar* cookieJar = cookieJarForSession(session);
    GUniquePtr<GSList> cookies(soup_cookie_jar_all_cookies(cookieJar));
    for (GSList* item = cookies.get(); item; item = g_slist_next(item)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(item->data);
        if (cookie->domain)
            hostnames.add(String::fromUTF8(cookie->domain));
        soup_cookie_free(cookie);
    }
}
Esempio n. 7
0
void deleteCookiesForHostname(const NetworkStorageSession& session, const String& hostname)
{
    CString hostNameString = hostname.utf8();
    SoupCookieJar* cookieJar = cookieJarForSession(session);
    GUniquePtr<GSList> cookies(soup_cookie_jar_all_cookies(cookieJar));
    for (GSList* item = cookies.get(); item; item = g_slist_next(item)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(item->data);
        if (soup_cookie_domain_matches(cookie, hostNameString.data()))
            soup_cookie_jar_delete_cookie(cookieJar, cookie);
        soup_cookie_free(cookie);
    }
}
Esempio n. 8
0
void deleteCookiesForHostname(NetworkingContext* context, const String& hostname)
{
    CString hostNameString = hostname.utf8();
    SoupCookieJar* cookieJar = context ? cookieJarForContext(context) : soupCookieJar();
    GOwnPtr<GSList> cookies(soup_cookie_jar_all_cookies(cookieJar));
    for (GSList* item = cookies.get(); item; item = g_slist_next(item)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(item->data);
        if (soup_cookie_domain_matches(cookie, hostNameString.data()))
            soup_cookie_jar_delete_cookie(cookieJar, cookie);
        soup_cookie_free(cookie);
    }
}
Esempio n. 9
0
gboolean
ossifer_session_delete_cookie (const gchar *name, const gchar *domain, const gchar *path)
{
    SoupCookie *cookie = ossifer_session_get_cookie (name, domain, path);

    if (cookie != NULL) {
        soup_cookie_jar_delete_cookie (ossifer_session_get_cookie_jar (), cookie);
        soup_cookie_free (cookie);
        return TRUE;
    }

    return FALSE;
}
Esempio n. 10
0
static void cookie_manager_free_cookie_list(CookieManager *cm)
{
	CookieManagerPrivate *priv = cm->priv;

	if (priv->cookies != NULL)
	{
		GSList *l;

		for (l = priv->cookies; l != NULL; l = g_slist_next(l))
			soup_cookie_free(l->data);
		g_slist_free(priv->cookies);
		priv->cookies = NULL;
	}
}
Esempio n. 11
0
static GSList*
cookies_from_table(lua_State *L, gint idx)
{
    GSList *cookies = NULL;
    SoupCookie *cookie;
    gchar *error;

    /* bring a copy of the table to the top of the stack */
    lua_pushvalue(L, idx);

    /* push first index */
    lua_pushnil(L);

    /* iterate over cookies table */
    while(luaH_next(L, -2)) {
        /* create soup cookie from table */
        if ((cookie = cookie_new_from_table(L, -1, &error)))
            cookies = g_slist_prepend(cookies, cookie);

        /* bad cookie, raise error */
        else if (error) {
            /* free cookies */
            for (GSList *p = cookies; p; p = g_slist_next(p))
                soup_cookie_free(p->data);
            g_slist_free(cookies);

            /* push & raise error */
            lua_pushfstring(L, "bad cookie in cookies table (%s)", error);
            g_free(error);
            lua_error(L);
        }

        /* remove cookie table */
        lua_pop(L, 1);
    }

    /* remove copy of the table */
    lua_pop(L, 1);

    return cookies;
}
Esempio n. 12
0
void ewk_cookies_cookie_del(Ewk_Cookie* cookie)
{
    EINA_SAFETY_ON_NULL_RETURN(cookie);
    GSList* list;
    GSList* p;
    SoupCookieJar* cookieJar = WebCore::soupCookieJar();
    SoupCookie* cookie1 = soup_cookie_new(
        cookie->name, cookie->value, cookie->domain, cookie->path, -1);

    list = soup_cookie_jar_all_cookies(cookieJar);
    for (p = list; p; p = p->next) {
        SoupCookie* cookie2 = static_cast<SoupCookie*>(p->data);
        if (soup_cookie_equal(cookie1, cookie2)) {
            soup_cookie_jar_delete_cookie(cookieJar, cookie2);
            break;
        }
    }

    soup_cookie_free(cookie1);
    soup_cookies_free(list);
}
Esempio n. 13
0
static void
create_cookie_jar_for_domain (const char *address, const char *directory)
{
  GSList *cookies, *p;
  SoupCookieJar *current_jar, *new_jar;
  char *domain, *filename;
  SoupURI *uri;

  /* Create the new cookie jar */
  filename = g_build_filename (directory, "cookies.sqlite", NULL);
  new_jar = (SoupCookieJar*)soup_cookie_jar_sqlite_new (filename, FALSE);
  g_free (filename);

  /* The app domain for the current view */
  uri = soup_uri_new (address);
  domain = uri->host;

  /* The current cookies */
  current_jar = get_current_cookie_jar ();
  if (!current_jar) {
    soup_uri_free (uri);
    return;
  }

  cookies = soup_cookie_jar_all_cookies (current_jar);

  for (p = cookies; p; p = p->next) {
    SoupCookie *cookie = (SoupCookie*)p->data;

    if (soup_cookie_domain_matches (cookie, domain))
      soup_cookie_jar_add_cookie (new_jar, cookie);
    else
      soup_cookie_free (cookie);
  }

  soup_uri_free (uri);
  g_slist_free (cookies);
  g_object_unref (current_jar);
  g_object_unref (new_jar);
}
Esempio n. 14
0
void deleteCookie(const NetworkStorageSession& session, const URL& url, const String& name)
{
    SoupCookieJar* jar = cookieJarForSession(session);
    if (!jar)
        return;

    GUniquePtr<SoupURI> uri = url.createSoupURI();
    GUniquePtr<GSList> cookies(soup_cookie_jar_get_cookie_list(jar, uri.get(), TRUE));
    if (!cookies)
        return;

    CString cookieName = name.utf8();
    bool wasDeleted = false;
    for (GSList* iter = cookies.get(); iter; iter = g_slist_next(iter)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(iter->data);
        if (!wasDeleted && cookieName == cookie->name) {
            soup_cookie_jar_delete_cookie(jar, cookie);
            wasDeleted = true;
        }
        soup_cookie_free(cookie);
    }
}
Esempio n. 15
0
bool getRawCookies(const NetworkStorageSession& session, const URL& /*firstParty*/, const URL& url, Vector<Cookie>& rawCookies)
{
    rawCookies.clear();
    SoupCookieJar* jar = cookieJarForSession(session);
    if (!jar)
        return false;

    GUniquePtr<SoupURI> uri = url.createSoupURI();
    GUniquePtr<GSList> cookies(soup_cookie_jar_get_cookie_list(jar, uri.get(), TRUE));
    if (!cookies)
        return false;

    for (GSList* iter = cookies.get(); iter; iter = g_slist_next(iter)) {
        SoupCookie* cookie = static_cast<SoupCookie*>(iter->data);
        rawCookies.append(Cookie(String::fromUTF8(cookie->name), String::fromUTF8(cookie->value), String::fromUTF8(cookie->domain),
            String::fromUTF8(cookie->path), cookie->expires ? static_cast<double>(soup_date_to_time_t(cookie->expires)) * 1000 : 0,
            cookie->http_only, cookie->secure, !cookie->expires));
        soup_cookie_free(cookie);
    }

    return true;
}
Esempio n. 16
0
/**
 * Deletes a cookie from the cookie jar.
 *
 * Note that the fields name, value, domain and path are used to match this
 * cookie in the cookie jar.
 *
 * @param cookie an @c Ewk_Cookie that has the info relative to that cookie.
 */
void ewk_cookies_cookie_del(Ewk_Cookie *cookie)
{
#if USE(SOUP)
    EINA_SAFETY_ON_NULL_RETURN(cookie);
    GSList* l;
    GSList* p;
    SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
    SoupCookie* c1 = soup_cookie_new(
        cookie->name, cookie->value, cookie->domain, cookie->path, -1);

    l = soup_cookie_jar_all_cookies(cookieJar);
    for (p = l; p; p = p->next) {
        SoupCookie* c2 = static_cast<SoupCookie*>(p->data);
        if (soup_cookie_equal(c1, c2)) {
            soup_cookie_jar_delete_cookie(cookieJar, c2);
            break;
        }
    }

    soup_cookie_free(c1);
    soup_cookies_free(l);
#endif
}
Esempio n. 17
0
template <> void freeOwnedGPtr<SoupCookie>(SoupCookie* ptr)
{
    if (ptr)
        soup_cookie_free(ptr);
}
Esempio n. 18
0
void
ossifer_cookie_free (SoupCookie *cookie)
{
    soup_cookie_free (cookie);
}
Esempio n. 19
0
/* We received the HTTP headers of the request and it contains cookie-managing headers */
static void _cookie_permission_manager_on_response_received(WebKitWebView *inView,
															WebKitWebFrame *inFrame,
															WebKitWebResource *inResource,
															WebKitNetworkResponse *inResponse,
															gpointer inUserData)
{
	g_return_if_fail(IS_COOKIE_PERMISSION_MANAGER(inUserData));

	CookiePermissionManager			*self=COOKIE_PERMISSION_MANAGER(inUserData);
	CookiePermissionManagerPrivate	*priv=self->priv;
	GSList							*newCookies, *cookie;
	GSList							*unknownCookies=NULL, *acceptedCookies=NULL;
	SoupURI							*firstParty;
	SoupCookieJarAcceptPolicy		cookiePolicy;
	gint							unknownCookiesPolicy;
	SoupMessage						*message;

	/* If policy is to deny all cookies return immediately */
	cookiePolicy=soup_cookie_jar_get_accept_policy(priv->cookieJar);
	if(cookiePolicy==SOUP_COOKIE_JAR_ACCEPT_NEVER) return;

	/* Get SoupMessage */
	message=webkit_network_response_get_message(inResponse);
	if(!message || !SOUP_IS_MESSAGE(message)) return;

	/* Iterate through cookies in response and check if they should be
	 * blocked (remove from cookies list) or accepted (added to cookie jar).
	 * If we could not determine what to do collect these cookies and
	 * ask user
	 */
	newCookies=soup_cookies_from_response(message);
	firstParty=soup_message_get_first_party(message);
	for(cookie=newCookies; cookie; cookie=cookie->next)
	{
		switch(_cookie_permission_manager_get_policy(self, cookie->data))
		{
			case COOKIE_PERMISSION_MANAGER_POLICY_BLOCK:
				soup_cookie_free(cookie->data);
				break;

			case COOKIE_PERMISSION_MANAGER_POLICY_ACCEPT:
			case COOKIE_PERMISSION_MANAGER_POLICY_ACCEPT_FOR_SESSION:
				if((cookiePolicy==SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY &&
						firstParty!=NULL &&
						firstParty->host &&
						soup_cookie_domain_matches(cookie->data, firstParty->host)) ||
						cookiePolicy==SOUP_COOKIE_JAR_ACCEPT_ALWAYS)
				{
					acceptedCookies=g_slist_prepend(acceptedCookies, cookie->data);
				}
					else soup_cookie_free(cookie->data);
				break;

			case COOKIE_PERMISSION_MANAGER_POLICY_UNDETERMINED:
			default:
				if((cookiePolicy==SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY &&
						firstParty!=NULL &&
						firstParty->host &&
						soup_cookie_domain_matches(cookie->data, firstParty->host)) ||
						cookiePolicy==SOUP_COOKIE_JAR_ACCEPT_ALWAYS)
				{
					unknownCookies=g_slist_prepend(unknownCookies, cookie->data);
				}
					else soup_cookie_free(cookie->data);
				break;
		}
	}

	/* Prepending an item to list is the fastest method but the order of cookies
	 * is reversed now and may be added to cookie jar in the wrong order. So we
	 * need to reverse list now of both - undetermined and accepted cookies
	 */
	unknownCookies=g_slist_reverse(unknownCookies);
	acceptedCookies=g_slist_reverse(acceptedCookies);

	/* Ask user for his decision what to do with cookies whose policy is undetermined
	 * But only ask if there is any undetermined one
	 */
	if(g_slist_length(unknownCookies)>0)
	{
		/* Get view */
		MidoriView					*view;

		view=MIDORI_VIEW(g_object_get_data(G_OBJECT(inView), "midori-view"));

		/* Ask for user's decision */
		unknownCookiesPolicy=_cookie_permission_manager_ask_for_policy(self, view, message, unknownCookies);
		if(unknownCookiesPolicy==COOKIE_PERMISSION_MANAGER_POLICY_ACCEPT ||
			unknownCookiesPolicy==COOKIE_PERMISSION_MANAGER_POLICY_ACCEPT_FOR_SESSION)
		{
			/* Add accepted undetermined cookies to cookie jar */
			for(cookie=unknownCookies; cookie; cookie=cookie->next)
			{
				soup_cookie_jar_add_cookie(priv->cookieJar, (SoupCookie*)cookie->data);
			}
		}
			else
			{
				/* Free cookies because they should be blocked */
				for(cookie=unknownCookies; cookie; cookie=cookie->next)
				{
					soup_cookie_free((SoupCookie*)cookie->data);
				}
			}
	}

	/* Add accepted cookies to cookie jar */
	for(cookie=acceptedCookies; cookie; cookie=cookie->next)
	{
		soup_cookie_jar_add_cookie(priv->cookieJar, (SoupCookie*)cookie->data);
	}

	/* Free list of cookies */
	g_slist_free(unknownCookies);
	g_slist_free(acceptedCookies);
	g_slist_free(newCookies);
}
Esempio n. 20
0
/* FIXME: moar tests! */
static void
do_cookies_parsing_test (void)
{
	SoupSession *session;
	SoupMessage *msg;
	SoupCookieJar *jar;
	GSList *cookies, *iter;
	SoupCookie *cookie;
	gboolean got1, got2, got3;

	debug_printf (1, "\nSoupCookie parsing test\n");

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
	soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
	jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));

	/* "httponly" is case-insensitive, and its value (if any) is ignored */
	msg = soup_message_new_from_uri ("GET", first_party_uri);
	soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
				     "one=1; httponly; max-age=100");
	soup_session_send_message (session, msg);
	g_object_unref (msg);

	msg = soup_message_new_from_uri ("GET", first_party_uri);
	soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
				     "two=2; HttpOnly; max-age=100");
	soup_session_send_message (session, msg);
	g_object_unref (msg);

	msg = soup_message_new_from_uri ("GET", first_party_uri);
	soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
				     "three=3; httpONLY=Wednesday; max-age=100");
	soup_session_send_message (session, msg);
	g_object_unref (msg);

	cookies = soup_cookie_jar_get_cookie_list (jar, first_party_uri, TRUE);
	got1 = got2 = got3 = FALSE;

	for (iter = cookies; iter; iter = iter->next) {
		cookie = iter->data;

		if (!strcmp (soup_cookie_get_name (cookie), "one")) {
			got1 = TRUE;
			if (!soup_cookie_get_http_only (cookie)) {
				debug_printf (1, "  cookie 1 is not HttpOnly!\n");
				errors++;
			}
			if (!soup_cookie_get_expires (cookie)) {
				debug_printf (1, "  cookie 1 did not fully parse!\n");
				errors++;
			}
		} else if (!strcmp (soup_cookie_get_name (cookie), "two")) {
			got2 = TRUE;
			if (!soup_cookie_get_http_only (cookie)) {
				debug_printf (1, "  cookie 2 is not HttpOnly!\n");
				errors++;
			}
			if (!soup_cookie_get_expires (cookie)) {
				debug_printf (1, "  cookie 3 did not fully parse!\n");
				errors++;
			}
		} else if (!strcmp (soup_cookie_get_name (cookie), "three")) {
			got3 = TRUE;
			if (!soup_cookie_get_http_only (cookie)) {
				debug_printf (1, "  cookie 3 is not HttpOnly!\n");
				errors++;
			}
			if (!soup_cookie_get_expires (cookie)) {
				debug_printf (1, "  cookie 3 did not fully parse!\n");
				errors++;
			}
		} else {
			debug_printf (1, "  got unexpected cookie '%s'\n",
				      soup_cookie_get_name (cookie));
			errors++;
		}

		soup_cookie_free (cookie);
	}
	g_slist_free (cookies);

	if (!got1) {
		debug_printf (1, "  didn't get cookie 1\n");
		errors++;
	}
	if (!got2) {
		debug_printf (1, "  didn't get cookie 2\n");
		errors++;
	}
	if (!got3) {
		debug_printf (1, "  didn't get cookie 3\n");
		errors++;
	}

	soup_test_session_abort_unref (session);
}