Index: servers/google/libgdata-google/ChangeLog
===================================================================
--- servers/google/libgdata-google/ChangeLog	(revision 8974)
+++ servers/google/libgdata-google/ChangeLog	(working copy)
@@ -1,3 +1,16 @@
+2008-06-13  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #521921 (Patch by Sebastian Keller)
+
+	* gdata-google-service.c: (send_and_handle_google_redirection),
+	(gdata_google_service_insert_entry),
+	(gdata_google_service_delete_entry),
+	(gdata_google_service_update_entry_with_link):
+	Handle redirects in a way that works with Google API. Fixes some cases
+	of entries not being created or deleted.
+	* gdata-google-service.c: (gdata_google_service_authenticate),
+	(gdata_google_service_get_feed): Code cleanup.
+
 2008-06-13  Wang Xin  <jedy.wang@sun.com>
 
 	** Fix for bug #527544.
Index: servers/google/libgdata-google/gdata-google-service.c
===================================================================
--- servers/google/libgdata-google/gdata-google-service.c	(revision 8974)
+++ servers/google/libgdata-google/gdata-google-service.c	(working copy)
@@ -89,6 +89,36 @@ gdata_google_service_set_proxy (GDataSer
 		g_object_set (priv->soup_session, SOUP_SESSION_PROXY_URI, proxy, NULL);
 }
 
+/* send a message without redirection and if it was required, then redirects itself */
+static void
+send_and_handle_google_redirection (SoupSession *soup_session, SoupMessage *msg)
+{
+	soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
+	soup_session_send_message (soup_session, msg);
+	soup_message_set_flags (msg, 0);
+
+	if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
+		SoupURI *new_uri;
+		const char *new_loc;
+
+		new_loc = soup_message_headers_get (msg->response_headers, "Location");
+		g_return_if_fail (new_loc != NULL);
+
+		new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
+		if (!new_uri) {
+			soup_message_set_status_full (msg,
+						      SOUP_STATUS_MALFORMED,
+						      "Invalid Redirect URL");
+			return;
+		}
+
+		soup_message_set_uri (msg, new_uri);
+		soup_uri_free (new_uri);
+
+		soup_session_send_message (soup_session, msg);
+	}
+}
+
 static void
 gdata_google_service_set_credentials (GDataService *service, const gchar *username, const gchar *password)
 {
@@ -129,7 +159,6 @@ gdata_google_service_authenticate (GData
 	GHashTable *request_form;
 	gchar *request_body;
 	gchar *token = NULL;
-	int  http_status;
 
 	priv = GDATA_GOOGLE_SERVICE_GET_PRIVATE(service);
 	auth = (GDataGoogleServiceAuth *)priv->auth;
@@ -149,11 +178,11 @@ gdata_google_service_authenticate (GData
 				  SOUP_MEMORY_TAKE,
 				  request_body, strlen(request_body));
 
-	http_status = soup_session_send_message (priv->soup_session, msg);
+	soup_session_send_message (priv->soup_session, msg);
 
-    	if (http_status != 200) {
+    	if (msg->status_code != 200) {
 		g_set_error (error, SOUP_HTTP_ERROR,
-					 http_status, soup_status_get_phrase (http_status));
+					 msg->status_code, msg->reason_phrase);
 		g_object_unref(msg);
 		return (NULL != token);
 	}
@@ -202,7 +231,6 @@ gdata_google_service_get_feed (GDataServ
 	GDataGoogleServiceAuth *auth;
 	SoupSession *soup_session;
 	SoupMessage *msg;
-	int http_status;
 
 	g_return_val_if_fail(service != NULL, NULL);
 	g_return_val_if_fail(GDATA_IS_GOOGLE_SERVICE(service),NULL);
@@ -221,10 +249,10 @@ gdata_google_service_get_feed (GDataServ
 	soup_message_headers_append(msg->request_headers,
 			"Authorization", (gchar *)g_strdup_printf("GoogleLogin auth=%s", auth->token));
 
-	http_status = soup_session_send_message(soup_session, msg);
-	if (http_status != 200) {
+	soup_session_send_message (soup_session, msg);
+	if (msg->status_code != 200) {
 		g_set_error (error, SOUP_HTTP_ERROR,
-					 http_status, soup_status_get_phrase (http_status));
+					 msg->status_code, msg->reason_phrase);
 		g_object_unref (msg);
 		return NULL;
 	}
@@ -263,7 +291,6 @@ gdata_google_service_insert_entry (GData
 	SoupSession *soup_session;
 	SoupMessage *msg;
 	gchar *entry_xml;
-	int http_status;
 
 	g_return_val_if_fail(service != NULL, NULL);
 	g_return_val_if_fail(GDATA_IS_GOOGLE_SERVICE(service), NULL);
@@ -292,10 +319,12 @@ gdata_google_service_insert_entry (GData
 				entry_xml,
 				strlen(entry_xml));
 
-	http_status = soup_session_send_message(soup_session, msg);
-    	if (http_status != 201) {
+	/* Handle redirects ourself, since soup does not behave like google-api expects */
+	send_and_handle_google_redirection (soup_session, msg);
+
+	if (msg->status_code != 201) {
 		g_set_error (error, SOUP_HTTP_ERROR,
-					 http_status, soup_status_get_phrase (http_status));
+					 msg->status_code, msg->reason_phrase);
 		g_object_unref (msg);
 		return NULL;
 	}
@@ -331,7 +360,6 @@ gdata_google_service_delete_entry (GData
 	SoupSession *soup_session;
 	SoupMessage *msg;
 	const gchar *entry_edit_url;
-	int http_status;
 	gboolean retval = FALSE;
 
 	g_return_val_if_fail (service !=NULL, FALSE);
@@ -352,11 +380,13 @@ gdata_google_service_delete_entry (GData
 				     "Authorization",
 				     (gchar *)g_strdup_printf ("GoogleLogin auth=%s",
 				     auth->token));
-	http_status = soup_session_send_message (soup_session, msg);
 
-    	if (http_status != 200) {
+	/* Handle redirects ourself */
+	send_and_handle_google_redirection (soup_session, msg);
+
+    	if (msg->status_code != 200) {
 		g_set_error (error, SOUP_HTTP_ERROR,
-					 http_status, soup_status_get_phrase (http_status));
+					 msg->status_code, msg->reason_phrase);
 	} else {
 		retval = TRUE;
 	}
@@ -406,7 +436,6 @@ gdata_google_service_update_entry_with_l
 	SoupSession *soup_session;
 	SoupMessage *msg;
 	gchar *entry_xml;
-	int http_status;
 	GDataEntry *updated_entry = NULL;
 
 	g_return_val_if_fail (service !=NULL, FALSE);
@@ -436,11 +465,12 @@ gdata_google_service_update_entry_with_l
 				entry_xml,
 				strlen(entry_xml));
 
-	http_status = soup_session_send_message (soup_session, msg);
+	/* Handle redirects ourself */
+	send_and_handle_google_redirection (soup_session, msg);
 
-    	if (http_status != 200) {
+    	if (msg->status_code != 200) {
 		g_set_error (error, SOUP_HTTP_ERROR,
-					 http_status, soup_status_get_phrase (http_status));
+					 msg->status_code, msg->reason_phrase);
 		g_object_unref (msg);
 		return updated_entry;
 	}
