libepc r199 - in trunk: . examples libepc



Author: hasselmm
Date: Fri Jan 18 15:24:00 2008
New Revision: 199
URL: http://svn.gnome.org/viewvc/libepc?rev=199&view=rev

Log:
Support libsoup 2.4, which will be part of GNOME 2.22 (#509699).
Original patch by Dan Winship.

* configure.ac: Also check for libsoup 2.4.
Define HAVE_LIBSOUP22, when still building for libsoup 2.2.
* libepc/consumer.c, libepc/publisher.c: Update for libsoup 2.4.
* examples/simple-publisher.c: Print source code position in debug
message to reduce confusion.


Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/examples/simple-publisher.c
   trunk/libepc/consumer.c
   trunk/libepc/publisher.c

Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac	(original)
+++ trunk/configure.ac	Fri Jan 18 15:24:00 2008
@@ -40,20 +40,32 @@
                               gmodule-2.0  >= 2.0
                               gobject-2.0  >= 2.4
                               gnutls       >= 1.4
-                              libsoup-2.2  >= 2.2
                               uuid         >= 1.36])
 PKG_CHECK_MODULES(LIBEPC_UI, [gtk+-2.0     >= 2.10])
 
-PKG_CHECK_MODULES(GIO,       [gio-2.0      >= 2.15],
+PKG_CHECK_MODULES(LIBSOUP,   [libsoup-2.4  >= 2.3],
+                             [libsoup_api=2.4],[
+PKG_CHECK_MODULES(LIBSOUP,   [libsoup-2.2  >= 2.2],
+                             [libsoup_api=2.2])])
+
+PKG_CHECK_MODULES(GIO,       [gio-2.0      >= 2.15.1],
                              [have_gio=yes],
                              [have_gio=no])
 
+if test "2.2" == "$libsoup_api"
+then
+  AC_DEFINE([HAVE_LIBSOUP22], [1], [Define to 1 if you still want to use libsoup 2.2.])
+fi
+
 if test "yes" == "$have_gio"
 then
   LIBEPC_CFLAGS="$LIBEPC_CFLAGS $GIO_CFLAGS"
   LIBEPC_LIBS="$LIBEPC_LIBS $GIO_LIBS"
 fi
 
+LIBEPC_CFLAGS="$LIBEPC_CFLAGS $LIBSOUP_CFLAGS"
+LIBEPC_LIBS="$LIBEPC_LIBS $LIBSOUP_LIBS"
+
 AC_CHECK_LIB([avahi-ui],
              [aui_service_dialog_set_service_type_name],
              [AC_DEFINE([HAVE_SET_SERVICE_TYPE_NAME], [1],
@@ -74,3 +86,8 @@
 AC_SUBST(GLIB_MKENUMS)
 
 AC_OUTPUT
+
+AC_MSG_NOTICE([============================================])
+AC_MSG_NOTICE([  libsoup API ........................ $libsoup_api])
+AC_MSG_NOTICE([  gio support ........................ $have_gio])
+AC_MSG_NOTICE([============================================])

Modified: trunk/examples/simple-publisher.c
==============================================================================
--- trunk/examples/simple-publisher.c	(original)
+++ trunk/examples/simple-publisher.c	Fri Jan 18 15:24:00 2008
@@ -51,7 +51,7 @@
    * See: EPC_AUTH_PASSWORD_TEXT_NEEDED */
 
   if (NULL != (password = epc_auth_context_get_password (context)))
-    g_debug ("The password: %s", password);
+    g_print ("%s: password='%s'\n", G_STRLOC, password);
 
   /* Check if he password supplied matches @user_data */
 

Modified: trunk/libepc/consumer.c
==============================================================================
--- trunk/libepc/consumer.c	(original)
+++ trunk/libepc/consumer.c	Fri Jan 18 15:24:00 2008
@@ -173,6 +173,8 @@
 
 G_DEFINE_TYPE (EpcConsumer, epc_consumer, G_TYPE_OBJECT);
 
+#ifdef HAVE_LIBSOUP22
+
 static void
 epc_consumer_authenticate_cb (SoupSession  *session G_GNUC_UNUSED,
                               SoupMessage  *message,
@@ -231,6 +233,54 @@
                                   auth_type, username, password, data);
 }
 
+#else
+
+static void
+epc_consumer_authenticate_cb (SoupSession  *session G_GNUC_UNUSED,
+                              SoupMessage  *message,
+                              SoupAuth     *auth,
+                              gboolean      retrying,
+                              gpointer      data)
+{
+  EpcConsumer *self = EPC_CONSUMER (data);
+  const char *username, *password;
+  gboolean handled = FALSE;
+
+  if (EPC_DEBUG_LEVEL (1))
+    g_debug ("%s: path=%s, realm=%s, retrying=%d",
+             G_STRLOC, soup_message_get_uri (message)->path,
+             soup_auth_get_realm (auth), retrying);
+
+  if (retrying)
+    {
+      g_signal_emit (self, signals[SIGNAL_AUTHENTICATE],
+                     0, soup_auth_get_realm (auth), &handled);
+
+      if (EPC_DEBUG_LEVEL (1))
+        g_debug ("%s: path=%s, realm=%s, handled=%d",
+                 G_STRLOC, soup_message_get_uri (message)->path,
+                 soup_auth_get_realm (auth), handled);
+    }
+  else
+    handled = TRUE;
+
+  if (handled)
+    {
+      username = (self->priv->username ? self->priv->username : "");
+      password = (self->priv->password ? self->priv->password : "");
+
+      soup_auth_authenticate (auth, username, password);
+
+      if (EPC_DEBUG_LEVEL (1))
+        g_debug ("%s: path=%s, realm=%s, retrying=%d, username=%s, password=%s",
+                 G_STRLOC, soup_message_get_uri (message)->path,
+                 soup_auth_get_realm (auth), retrying,
+                 username, password);
+    }
+}
+
+#endif
+
 static void
 epc_consumer_init (EpcConsumer *self)
 {
@@ -240,8 +290,10 @@
 
   g_signal_connect (self->priv->session, "authenticate",
                     G_CALLBACK (epc_consumer_authenticate_cb), self);
+#ifdef HAVE_LIBSOUP22
   g_signal_connect (self->priv->session, "reauthenticate",
                     G_CALLBACK (epc_consumer_reauthenticate_cb), self);
+#endif
 }
 
 static void
@@ -959,15 +1011,21 @@
 
   if (SOUP_STATUS_IS_SUCCESSFUL (status))
     {
+#ifdef HAVE_LIBSOUP22
+      const gsize response_length = request->response.length;
+      gconstpointer response_data = request->response.body;
+#else
+      const gsize response_length = request->response_body->length;
+      gconstpointer response_data = request->response_body->data;
+#endif
+
       if (length)
-        *length = request->response.length;
+        *length = response_length;
 
-      contents = g_malloc (request->response.length + 1);
-      contents[request->response.length] = '\0';
+      contents = g_malloc (response_length + 1);
+      contents[response_length] = '\0';
 
-      memcpy (contents,
-              request->response.body,
-              request->response.length);
+      memcpy (contents, response_data, response_length);
     }
   else
     epc_consumer_set_http_error (error, request, status);
@@ -1145,16 +1203,26 @@
                                             G_MARKUP_TREAT_CDATA_AS_TEXT,
                                             &state, NULL);
 
+#ifdef HAVE_LIBSOUP22
       g_markup_parse_context_parse (context,
                                     request->response.body,
                                     request->response.length,
                                     error);
+#else
+      g_markup_parse_context_parse (context,
+                                    request->response_body->data,
+                                    request->response_body->length,
+                                    error);
+#endif
 
       g_markup_parse_context_free (context);
     }
   else
     epc_consumer_set_http_error (error, request, status);
 
+  if (request)
+    g_object_unref (request);
+
   return state.items;
 }
 

Modified: trunk/libepc/publisher.c
==============================================================================
--- trunk/libepc/publisher.c	(original)
+++ trunk/libepc/publisher.c	Fri Jan 18 15:24:00 2008
@@ -28,11 +28,21 @@
 
 #include <glibconfig.h>
 #include <libsoup/soup.h>
+#include <string.h>
+
+#if HAVE_LIBSOUP22
 #include <libsoup/soup-address.h>
 #include <libsoup/soup-server.h>
 #include <libsoup/soup-server-auth.h>
 #include <libsoup/soup-server-message.h>
-#include <string.h>
+
+#define SOUP_MEMORY_COPY SOUP_BUFFER_USER_OWNED
+#define SOUP_MEMORY_TAKE SOUP_BUFFER_SYSTEM_OWNED
+
+#define SoupServerCallback SoupServerCallbackFn
+#define SoupURI            SoupUri
+
+#endif
 
 #if GLIB_CHECK_VERSION(2,15,1)
 #include <gio/gio.h>
@@ -160,10 +170,18 @@
 struct _EpcAuthContext
 {
   /*< private >*/
-  SoupServerAuth *auth;
+  EpcResource    *resource;
   EpcPublisher   *publisher;
   const gchar    *key;
 
+#if HAVE_LIBSOUP22
+  SoupServerAuth *auth;
+#else
+  SoupMessage    *message;
+  const char     *username;
+  const char     *password;
+#endif
+
   /*< public >*/
 };
 
@@ -201,9 +219,14 @@
 
   gboolean               server_started;
   GMainLoop             *server_loop;
-  SoupServerAuthContext  server_auth;
   SoupServer            *server;
 
+#if HAVE_LIBSOUP22
+  SoupServerAuthContext  server_auth;
+#else
+  SoupAuthDomain        *server_auth;
+#endif
+
   GHashTable            *clients;
 
   EpcProtocol            protocol;
@@ -352,14 +375,23 @@
       if (EPC_DEBUG_LEVEL (1))
         g_debug ("%s: writing %d bytes", G_STRLOC, length);
 
-      soup_message_add_chunk (message, SOUP_BUFFER_USER_OWNED, chunk, length);
+#ifdef HAVE_LIBSOUP22
+      soup_message_add_chunk (message, SOUP_MEMORY_COPY, chunk, length);
+#else
+      soup_message_body_append (message->response_body,
+                                SOUP_MEMORY_COPY, chunk, length);
+#endif
     }
   else
     {
       if (EPC_DEBUG_LEVEL (1))
         g_debug ("%s: done", G_STRLOC);
 
+#ifdef HAVE_LIBSOUP22
       soup_message_add_final_chunk (message);
+#else
+      soup_message_body_complete (message->response_body);
+#endif
     }
 }
 
@@ -377,33 +409,36 @@
 
 static gboolean
 epc_publisher_check_client (EpcPublisher      *self,
-                            SoupServerContext *context)
+                            SoupServer        *server,
+                            SoupSocket        *socket)
 {
-  if (context->server == self->priv->server)
+  if (server == self->priv->server)
     return TRUE;
 
   if (EPC_DEBUG_LEVEL (1))
-    epc_publisher_trace_client (G_STRFUNC, "stale client", context->sock);
+    epc_publisher_trace_client (G_STRFUNC, "stale client", socket);
+
+  soup_socket_disconnect (socket);
 
-  soup_socket_disconnect (context->sock);
   return FALSE;
 }
 
-static G_GNUC_WARN_UNUSED_RESULT gboolean
-epc_publisher_track_client (EpcPublisher      *self,
-                            SoupServerContext *context)
+G_GNUC_WARN_UNUSED_RESULT static gboolean
+epc_publisher_track_client (EpcPublisher *self,
+                            SoupServer   *server,
+                            SoupSocket   *socket)
 {
   g_static_rec_mutex_lock (&epc_publisher_lock);
 
-  if (epc_publisher_check_client (self, context))
+  if (epc_publisher_check_client (self, server, socket))
     {
       gpointer tag;
 
-      tag = g_hash_table_lookup (self->priv->clients, context->sock);
+      tag = g_hash_table_lookup (self->priv->clients, socket);
       tag = GINT_TO_POINTER (GPOINTER_TO_INT (tag) + 1);
 
-      g_object_ref (context->sock);
-      g_hash_table_replace (self->priv->clients, context->sock, tag);
+      g_object_ref (socket);
+      g_hash_table_replace (self->priv->clients, socket, tag);
 
       return TRUE;
     }
@@ -414,46 +449,68 @@
 }
 
 static void
-epc_publisher_untrack_client (EpcPublisher      *self,
-                              SoupServerContext *context)
+epc_publisher_untrack_client (EpcPublisher *self,
+                              SoupServer   *server,
+                              SoupSocket   *socket)
 {
-  if (epc_publisher_check_client (self, context))
+  if (epc_publisher_check_client (self, server, socket))
     {
       gpointer tag;
 
-      tag = g_hash_table_lookup (self->priv->clients, context->sock);
+      tag = g_hash_table_lookup (self->priv->clients, socket);
       tag = GINT_TO_POINTER (GPOINTER_TO_INT (tag) - 1);
 
-      g_object_ref (context->sock);
-      g_hash_table_replace (self->priv->clients, context->sock, tag);
+      g_object_ref (socket);
+      g_hash_table_replace (self->priv->clients, socket, tag);
     }
 
   g_static_rec_mutex_unlock (&epc_publisher_lock);
 }
 
 static void
+#ifdef HAVE_LIBSOUP22
 epc_publisher_handle_contents (SoupServerContext *context,
                                SoupMessage       *message,
                                gpointer           data)
+#else
+epc_publisher_handle_contents (SoupServer        *server,
+                               SoupMessage       *message,
+                               const gchar       *path,
+                               GHashTable        *query,
+                               SoupClientContext *context,
+                               gpointer           data)
+#endif
 {
+#ifdef HAVE_LIBSOUP22
+  SoupServer *server = context->server;
+  SoupSocket *socket = context->sock;
+  const gchar *path = context->path;
+#else
+  SoupSocket *socket = soup_client_context_get_socket (context);
+#endif
+
   EpcPublisher *self = EPC_PUBLISHER (data);
   EpcResource *resource = NULL;
   EpcContents *contents = NULL;
   const gchar *key = NULL;
 
   if (EPC_DEBUG_LEVEL (1))
-    g_debug ("%s: method=%s, path=%s", G_STRFUNC, message->method, context->path);
+    g_debug ("%s: method=%s, path=%s", G_STRFUNC, message->method, path);
 
+#ifdef HAVE_LIBSOUP22
   if (SOUP_METHOD_ID_GET != context->method_id)
+#else
+  if (SOUP_METHOD_GET != message->method)
+#endif
     {
       soup_message_set_status (message, SOUP_STATUS_METHOD_NOT_ALLOWED);
       return;
     }
 
-  if (!epc_publisher_track_client (self, context))
+  if (!epc_publisher_track_client (self, server, socket))
     return;
 
-  key = epc_publisher_get_key (context->path);
+  key = epc_publisher_get_key (path);
 
   if (key)
     resource = g_hash_table_lookup (self->priv->resources, key);
@@ -473,7 +530,7 @@
 
       if (data)
         {
-          soup_message_set_response (message, type, SOUP_BUFFER_USER_OWNED, (gpointer) data, length);
+          soup_message_set_response (message, type, SOUP_MEMORY_COPY, (gpointer) data, length);
           soup_message_set_status (message, SOUP_STATUS_OK);
         }
       else if (epc_contents_is_stream (contents))
@@ -481,21 +538,42 @@
           g_signal_connect (message, "wrote-chunk", G_CALLBACK (epc_publisher_chunk_cb), contents);
           g_signal_connect (message, "wrote-headers", G_CALLBACK (epc_publisher_chunk_cb), contents);
 
+#ifdef HAVE_LIBSOUP22
           soup_server_message_set_encoding (SOUP_SERVER_MESSAGE (message), SOUP_TRANSFER_CHUNKED);
+#else
+          soup_message_headers_set_encoding (message->response_headers, SOUP_ENCODING_CHUNKED);
+#endif
           soup_message_set_status (message, SOUP_STATUS_OK);
         }
 
       g_signal_connect_swapped (message, "finished", G_CALLBACK (epc_contents_unref), contents);
     }
 
-  epc_publisher_untrack_client (self, context);
+  epc_publisher_untrack_client (self, server, socket);
 }
 
 static void
+#ifdef HAVE_LIBSOUP22
 epc_publisher_handle_list (SoupServerContext *context,
                            SoupMessage       *message,
                            gpointer           data)
+#else
+epc_publisher_handle_list (SoupServer        *server,
+                           SoupMessage       *message,
+                           const char        *path,
+                           GHashTable        *query,
+                           SoupClientContext *context,
+                           gpointer           data)
+#endif
 {
+#ifdef HAVE_LIBSOUP22
+  SoupServer *server = context->server;
+  SoupSocket *socket = context->sock;
+  const gchar *path = context->path;
+#else
+  SoupSocket *socket = soup_client_context_get_socket (context);
+#endif
+
   const gchar *pattern = NULL;
   EpcPublisher *self = data;
   GList *files = NULL;
@@ -503,11 +581,11 @@
 
   GString *contents = g_string_new (NULL);
 
-  if (!epc_publisher_track_client (self, context))
+  if (!epc_publisher_track_client (self, server, socket))
     return;
 
-  if (g_str_has_prefix (context->path, "/list/") && '\0' != context->path[6])
-    pattern = context->path + 6;
+  if (g_str_has_prefix (path, "/list/") && '\0' != path[6])
+    pattern = path + 6;
 
   files = epc_publisher_list (self, pattern);
   g_string_append (contents, "<list>");
@@ -526,30 +604,42 @@
 
   g_string_append (contents, "</list>");
 
-  soup_message_set_response (message, "text/xml",
-                             SOUP_BUFFER_USER_OWNED,
+  soup_message_set_response (message, "text/xml", SOUP_MEMORY_TAKE,
                              contents->str, contents->len);
   soup_message_set_status (message, SOUP_STATUS_OK);
 
-  g_signal_connect_swapped (message, "finished",
-                            G_CALLBACK (g_free),
-                            contents->str);
-
   g_string_free (contents, FALSE);
   g_list_free (files);
 
-  epc_publisher_untrack_client (self, context);
+  epc_publisher_untrack_client (self, server, socket);
 }
 
 static void
+#ifdef HAVE_LIBSOUP22
 epc_publisher_handle_root (SoupServerContext *context,
                            SoupMessage       *message,
                            gpointer           data)
+#else
+epc_publisher_handle_root (SoupServer        *server,
+                           SoupMessage       *message,
+                           const char        *path,
+                           GHashTable        *query,
+                           SoupClientContext *context,
+                           gpointer           data)
+#endif
 {
+#ifdef HAVE_LIBSOUP22
+  SoupServer *server = context->server;
+  SoupSocket *socket = context->sock;
+  const gchar *path = context->path;
+#else
+  SoupSocket *socket = soup_client_context_get_socket (context);
+#endif
+
   EpcPublisher *self = data;
 
-  if (g_str_equal (context->path, "/") &&
-      epc_publisher_track_client (self, context))
+  if (g_str_equal (path, "/") &&
+      epc_publisher_track_client (self, server, socket))
     {
       GString *contents = g_string_new (NULL);
       gchar *markup;
@@ -607,72 +697,173 @@
 
       g_string_append (contents, "</ul></body></html>");
 
-      soup_message_set_response (message, "text/html",
-                                 SOUP_BUFFER_USER_OWNED,
+      soup_message_set_response (message, "text/html", SOUP_MEMORY_TAKE,
                                  contents->str, contents->len);
       soup_message_set_status (message, SOUP_STATUS_OK);
 
-      g_signal_connect_swapped (message, "finished",
-                                G_CALLBACK (g_free),
-                                contents->str);
-
       g_string_free (contents, FALSE);
       g_list_free (files);
 
-      epc_publisher_untrack_client (self, context);
+      epc_publisher_untrack_client (self, server, socket);
     }
   else
     soup_message_set_status (message, SOUP_STATUS_NOT_FOUND);
 }
 
+static void
+#if HAVE_LIBSOUP22
+epc_auth_context_init (EpcAuthContext *context,
+                       EpcPublisher   *publisher,
+                       SoupMessage    *message,
+                       SoupServerAuth *auth)
+#else
+epc_auth_context_init (EpcAuthContext *context,
+                       EpcPublisher   *publisher,
+                       SoupMessage    *message,
+                       const gchar    *username,
+                       const gchar    *password)
+#endif
+{
+  const SoupURI *uri = soup_message_get_uri (message);
+
+  context->publisher = publisher;
+  context->key = epc_publisher_get_key (uri->path);
+  context->resource = NULL;
+
+#if HAVE_LIBSOUP22
+  context->auth = auth;
+#else
+  context->message  = message;
+  context->username = username;
+  context->password = password;
+#endif
+
+  if (context->key)
+    context->resource = g_hash_table_lookup (publisher->priv->resources, context->key);
+  if (!context->resource)
+    context->resource = publisher->priv->default_resource;
+}
+
+#if HAVE_LIBSOUP22
+
 static gboolean
 epc_publisher_server_auth_cb (SoupServerAuthContext *auth_ctx G_GNUC_UNUSED,
                               SoupServerAuth        *auth,
                               SoupMessage           *message,
                               gpointer               data)
 {
-  EpcResource *resource = NULL;
   gboolean authorized = TRUE;
   const char *user = NULL;
   EpcAuthContext context;
-  const SoupUri *uri;
-
-  uri = soup_message_get_uri (message);
 
   g_static_rec_mutex_lock (&epc_publisher_lock);
-
-  context.auth = auth;
-  context.publisher = EPC_PUBLISHER (data);
-  context.key = epc_publisher_get_key (uri->path);
+  epc_auth_context_init (&context, EPC_PUBLISHER (data), message, auth);
 
   if (NULL != auth)
     user = soup_server_auth_get_user (auth);
-  if (NULL != context.key)
-    resource = g_hash_table_lookup (context.publisher->priv->resources, context.key);
-  if (NULL == resource)
-    resource = context.publisher->priv->default_resource;
 
-  if (resource && resource->auth_handler)
-    authorized = resource->auth_handler (&context, user, resource->auth_user_data);
+  if (context.resource && context.resource->auth_handler)
+    authorized = context.resource->auth_handler (&context, user,
+                                                 context.resource->auth_user_data);
 
   if (EPC_DEBUG_LEVEL (1))
-    g_debug ("%s: path=%s, resource=%p, auth_handler=%p, authorized=%d", G_STRLOC,
-             uri->path, resource, resource ? resource->auth_handler : NULL, authorized);
+    g_debug ("%s: key=%s, resource=%p, auth_handler=%p, authorized=%d", G_STRLOC,
+             context.key, context.resource, context.resource ? context.resource->auth_handler : NULL,
+             authorized);
 
   g_static_rec_mutex_unlock (&epc_publisher_lock);
 
   return authorized;
 }
 
+#else
+
+static gboolean
+epc_publisher_auth_filter (SoupAuthDomain *domain,
+                           SoupMessage    *message,
+                           gpointer        data)
+{
+  gboolean authorized = TRUE;
+  EpcAuthContext context;
+
+  g_static_rec_mutex_lock (&epc_publisher_lock);
+  epc_auth_context_init (&context, EPC_PUBLISHER (data), message, NULL, NULL);
+  authorized = (!context.resource || !context.resource->auth_handler);
+
+  if (EPC_DEBUG_LEVEL (1))
+    g_debug ("%s: key=%s, resource=%p, auth_handler=%p, authorized=%d", G_STRLOC,
+             context.key, context.resource, context.resource ? context.resource->auth_handler : NULL,
+             authorized);
+
+  g_static_rec_mutex_unlock (&epc_publisher_lock);
+
+  return !authorized;
+}
+
+static gboolean
+epc_publisher_basic_auth_cb (SoupAuthDomain *domain,
+                             SoupMessage    *message,
+                             const gchar    *username,
+                             const gchar    *password,
+                             gpointer        data)
+{
+  gboolean authorized = TRUE;
+  EpcAuthContext context;
+
+  g_static_rec_mutex_lock (&epc_publisher_lock);
+  epc_auth_context_init (&context, EPC_PUBLISHER (data), message, username, password);
+
+  if (context.resource && context.resource->auth_handler)
+    authorized = context.resource->auth_handler (&context, username, context.resource->auth_user_data);
+
+  if (EPC_DEBUG_LEVEL (1))
+    g_debug ("%s: key=%s, resource=%p, auth_handler=%p, authorized=%d", G_STRLOC,
+             context.key, context.resource, context.resource ? context.resource->auth_handler : NULL,
+             authorized);
+
+  g_static_rec_mutex_unlock (&epc_publisher_lock);
+
+  return authorized;
+}
+
+static gboolean
+epc_publisher_generic_auth_cb (SoupAuthDomain *domain,
+                               SoupMessage    *message,
+                               const char     *username,
+                               gpointer        data)
+{
+  gboolean authorized = TRUE;
+  EpcAuthContext context;
+
+  g_static_rec_mutex_lock (&epc_publisher_lock);
+  epc_auth_context_init (&context, EPC_PUBLISHER (data), message, username, NULL);
+
+  if (context.resource && context.resource->auth_handler)
+    authorized = context.resource->auth_handler (&context, username, context.resource->auth_user_data);
+
+  if (EPC_DEBUG_LEVEL (1))
+    g_debug ("%s: key=%s, resource=%p, auth_handler=%p, authorized=%d", G_STRLOC,
+             context.key, context.resource, context.resource ? context.resource->auth_handler : NULL,
+             authorized);
+
+  g_static_rec_mutex_unlock (&epc_publisher_lock);
+
+  return authorized;
+}
+
+#endif
+
 static void
 epc_publisher_init (EpcPublisher *self)
 {
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EPC_TYPE_PUBLISHER, EpcPublisherPrivate);
-
   self->priv->protocol = EPC_PROTOCOL_HTTPS;
+
+#if HAVE_LIBSOUP22
   self->priv->server_auth.types = SOUP_AUTH_TYPE_DIGEST;
   self->priv->server_auth.callback = epc_publisher_server_auth_cb;
   self->priv->server_auth.user_data = self;
+#endif
 
   self->priv->resources = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, epc_resource_free);
@@ -881,8 +1072,16 @@
 static void
 epc_publisher_remove_handlers (EpcPublisher *self)
 {
+#if HAVE_LIBSOUP22
   memset (&self->priv->server_auth.digest_info, 0,
           sizeof self->priv->server_auth.digest_info);
+#else
+  if (self->priv->server_auth)
+    {
+      soup_server_remove_auth_domain (self->priv->server, self->priv->server_auth);
+      self->priv->server_auth = NULL;
+    }
+#endif
 
   soup_server_remove_handler (self->priv->server, self->priv->contents_path);
   soup_server_remove_handler (self->priv->server, "/list");
@@ -890,8 +1089,25 @@
 }
 
 static void
+epc_publisher_add_server_callback (EpcPublisher       *self,
+                                   const gchar        *path,
+                                   SoupServerCallback  callback)
+{
+#if HAVE_LIBSOUP22
+  soup_server_add_handler (self->priv->server, path,
+                           &self->priv->server_auth,
+                           callback, NULL, self);
+#else
+  soup_server_add_handler (self->priv->server, path,
+                           callback, self, NULL);
+#endif
+}
+
+static void
 epc_publisher_install_handlers (EpcPublisher *self)
 {
+#if HAVE_LIBSOUP22
+
   memset (&self->priv->server_auth.digest_info, 0,
           sizeof self->priv->server_auth.digest_info);
 
@@ -908,19 +1124,41 @@
         break;
     }
 
-  soup_server_add_handler (self->priv->server,
-                           self->priv->contents_path,
-                           &self->priv->server_auth,
-                           epc_publisher_handle_contents,
-                           NULL, self);
-  soup_server_add_handler (self->priv->server, "/list",
-                           &self->priv->server_auth,
-                           epc_publisher_handle_list,
-                           NULL, self);
-  soup_server_add_handler (self->priv->server, "/",
-                           &self->priv->server_auth,
-                           epc_publisher_handle_root,
-                           NULL, self);
+#else
+
+  g_assert (NULL == self->priv->server_auth);
+
+  if (self->priv->auth_flags & EPC_AUTH_PASSWORD_TEXT_NEEDED)
+    {
+      self->priv->server_auth =
+        soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM,
+                                    self->priv->service_name,
+                                    SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK,
+                                    epc_publisher_basic_auth_cb,
+                                    SOUP_AUTH_DOMAIN_BASIC_AUTH_DATA,
+                                    self, NULL);
+    }
+  else
+    {
+      self->priv->server_auth =
+        soup_auth_domain_digest_new (SOUP_AUTH_DOMAIN_REALM,
+                                     self->priv->service_name,
+                                     SOUP_AUTH_DOMAIN_GENERIC_AUTH_CALLBACK,
+                                     epc_publisher_generic_auth_cb,
+                                     SOUP_AUTH_DOMAIN_GENERIC_AUTH_DATA,
+                                     self, NULL);
+    }
+
+  soup_auth_domain_set_filter (self->priv->server_auth, epc_publisher_auth_filter, self, NULL);
+  soup_auth_domain_add_path (self->priv->server_auth, self->priv->contents_path);
+
+  soup_server_add_auth_domain (self->priv->server, self->priv->server_auth);
+
+#endif
+
+  epc_publisher_add_server_callback (self, self->priv->contents_path, epc_publisher_handle_contents);
+  epc_publisher_add_server_callback (self, "/list", epc_publisher_handle_list);
+  epc_publisher_add_server_callback (self, "/", epc_publisher_handle_root);
 }
 
 static void
@@ -1050,9 +1288,13 @@
   if (self->priv->server)
     epc_publisher_remove_handlers (self);
 
+#if HAVE_LIBSOUP22
   self->priv->server_auth.types =
     flags & EPC_AUTH_PASSWORD_TEXT_NEEDED ?
     SOUP_AUTH_TYPE_BASIC : SOUP_AUTH_TYPE_DIGEST;
+#else
+  self->priv->auth_flags = flags;
+#endif
 
   if (self->priv->server)
     epc_publisher_install_handlers (self);
@@ -2213,7 +2455,9 @@
   if (!self->priv->server_started)
     {
       soup_server_run_async (self->priv->server);
+#if HAVE_LIBSOUP22
       g_object_unref (self->priv->server); /* work arround bug #494128 */
+#endif
       self->priv->server_started = TRUE;
     }
 
@@ -2485,11 +2729,19 @@
 {
   g_return_val_if_fail (NULL != context, NULL);
 
+#if HAVE_LIBSOUP22
+
   if (NULL != context->auth &&
       SOUP_AUTH_TYPE_BASIC == context->auth->type)
     return context->auth->basic.passwd;
 
   return NULL;
+
+#else
+
+  return context->password;
+
+#endif
 }
 
 /**
@@ -2511,10 +2763,19 @@
   g_return_val_if_fail (NULL != context, FALSE);
   g_return_val_if_fail (NULL != password, FALSE);
 
+#if HAVE_LIBSOUP22
+
   return
     NULL != context->auth &&
     soup_server_auth_check_passwd (context->auth, (gchar*) password);
-    /* TODO: libsoup bug #493686 */
+
+#else
+
+  return soup_auth_domain_check_password (context->publisher->priv->server_auth,
+                                          context->message, context->username,
+                                          password);
+
+#endif
 }
 
 /* vim: set sw=2 sta et spl=en spell: */



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