libsoup r1241 - in trunk: . libsoup



Author: xan
Date: Thu Feb 19 14:00:52 2009
New Revision: 1241
URL: http://svn.gnome.org/viewvc/libsoup?rev=1241&view=rev

Log:
2009-02-19  Xan Lopez  <xan gnome org>

        * libsoup/soup-cookie-jar-sqlite.c:
        (callback):
        (try_create_table):
        (exec_query_with_try_create_table):
        (load):
        (changed):

        Fix a series of issues that prevented SoupCookieJarSQLite from working:

        - Try to create cookies table on errors. The table was never created before.
        - Store max_age in a gulong, not int.
        - Parse correctly boolean values from query. It's 0/1, not FALSE/TRUE.
	- The host is stored as host in the table, not as domain.

        #572409

Modified:
   trunk/ChangeLog
   trunk/libsoup/soup-cookie-jar-sqlite.c

Modified: trunk/libsoup/soup-cookie-jar-sqlite.c
==============================================================================
--- trunk/libsoup/soup-cookie-jar-sqlite.c	(original)
+++ trunk/libsoup/soup-cookie-jar-sqlite.c	Thu Feb 19 14:00:52 2009
@@ -157,8 +157,9 @@
 }
 
 #define QUERY_ALL "SELECT * FROM moz_cookies;"
+#define CREATE_TABLE "CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER)"
 #define QUERY_INSERT "INSERT INTO moz_cookies VALUES(NULL, %Q, %Q, %Q, %Q, %d, NULL, %d, %d);"
-#define QUERY_DELETE "DELETE FROM moz_cookies WHERE name=%Q AND domain=%Q;"
+#define QUERY_DELETE "DELETE FROM moz_cookies WHERE name=%Q AND host=%Q;"
 
 enum {
 	COL_ID,
@@ -194,8 +195,8 @@
 	if (max_age <= 0)
 		return 0;
 
-	http_only = (strcmp (argv[COL_HTTP_ONLY], "FALSE") != 0);
-	secure = (strcmp (argv[COL_SECURE], "FALSE") != 0);
+	http_only = (strcmp (argv[COL_HTTP_ONLY], "1") == 0);
+	secure = (strcmp (argv[COL_SECURE], "1") == 0);
 
 	cookie = soup_cookie_new (name, value, host, path, max_age);
 
@@ -210,23 +211,54 @@
 }
 
 static void
+try_create_table (sqlite3 *db)
+{
+	char *error = 0;
+
+	if (sqlite3_exec (db, CREATE_TABLE, NULL, NULL, &error)) {
+		g_warning ("Failed to execute query: %s", error);
+		sqlite3_free (error);
+	}
+}
+
+static void
+exec_query_with_try_create_table (sqlite3 *db,
+				  const char *sql,
+				  int (*callback)(void*,int,char**,char**),
+				  void *argument)
+{
+	char *error = 0;
+	gboolean try_create = TRUE;
+
+try_exec:
+	if (sqlite3_exec (db, sql, callback, argument, &error)) {
+		if (try_create) {
+			try_create = FALSE;
+			try_create_table (db);
+			sqlite3_free (error);
+			goto try_exec;
+		} else {
+			g_warning ("Failed to execute query: %s", error);
+			sqlite3_free (error);
+		}
+	}
+}
+					 
+static void
 load (SoupCookieJar *jar)
 {
 	SoupCookieJarSqlitePrivate *priv =
 		SOUP_COOKIE_JAR_SQLITE_GET_PRIVATE (jar);
 
 	sqlite3 *db;
-	char *error = 0;
 
 	if (sqlite3_open (priv->filename, &db)) {
 		sqlite3_close (db);
-		g_debug ("Can't open %s", priv->filename);
+		g_warning ("Can't open %s", priv->filename);
+		return;
 	}
 
-	if (sqlite3_exec (db, QUERY_ALL, callback, (void *)jar, &error)) {
-		g_debug ("Failed to execute query: %s", error);
-		sqlite3_free (error);
-	}
+	exec_query_with_try_create_table (db, QUERY_ALL, callback, jar);
 
 	sqlite3_close (db);
 }
@@ -239,7 +271,6 @@
 	SoupCookieJarSqlitePrivate *priv =
 		SOUP_COOKIE_JAR_SQLITE_GET_PRIVATE (jar);
 	sqlite3 *db;
-	char *error = NULL;
 	char *query;
 
 	if (sqlite3_open (priv->filename, &db)) {
@@ -252,21 +283,14 @@
 		query = sqlite3_mprintf (QUERY_DELETE,
 					 old_cookie->name,
 					 old_cookie->domain);
-		if (sqlite3_exec (db, query, NULL, NULL, &error)) {
-			g_warning ("Failed to execute query: %s", error);
-			sqlite3_free (error);
-		}
+		exec_query_with_try_create_table (db, query, NULL, NULL);
 		sqlite3_free (query);
 	}
 
-	if (new_cookie) {
-		int expires;
-
-		if (new_cookie->expires)
-			expires = soup_date_to_time_t (new_cookie->expires);
-		else
-			expires = 0;
-
+	if (new_cookie && new_cookie->expires) {
+		gulong expires;
+		
+		expires = (gulong)soup_date_to_time_t (new_cookie->expires);
 		query = sqlite3_mprintf (QUERY_INSERT, 
 					 new_cookie->name,
 					 new_cookie->value,
@@ -275,10 +299,7 @@
 					 expires,
 					 new_cookie->secure,
 					 new_cookie->http_only);
-		if (sqlite3_exec (db, query, NULL, NULL, &error)) {
-			g_warning ("Failed to execute query: %s", error);
-			sqlite3_free (error);
-		}
+		exec_query_with_try_create_table (db, query, NULL, NULL);
 		sqlite3_free (query);
 	}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]