CGI frontend now requests a feed or an entry depending on the request
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 13 Oct 2008 01:06:59 +0000 (22:06 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 13 Oct 2008 01:36:30 +0000 (22:36 -0300)
frontend/cgi/cgi.c

index 736fb68..270c1d7 100644 (file)
@@ -46,34 +46,51 @@ cgi_serve_request (AtomCtx *ctx)
   if (!strcmp (method, "GET"))
     {
       AtomID *id;
-      AtomEntry *atom;
+      AtomEntry *entry = NULL;
+      AtomFeed *feed = NULL;
       AtomError *error;
       char *req;
-      id = atom_id_new_from_frontend (ctx, path);
-      if (id == NULL || (req = atom_id_to_backend (ctx, id)) == NULL)
+      if (atom_is_feed (ctx, path))
         {
-          atom = NULL;
-          error = atom_error_new ();
-          atom_error_code_set (error, 404);
-          atom_error_message_set (error, "Resource not found");
-          atom_error_set (ctx, error);
+          feed = atom_retrieve_feed (ctx);
         }
       else
         {
-          atom = atom_retrieve_entry (ctx, req);
+          /* Remove the leading slash before mapping */
+          id = atom_id_new_from_frontend (ctx, path + 1);
+          if (id == NULL || (req = atom_id_to_backend (ctx, id)) == NULL)
+            {
+              error = atom_error_new ();
+              atom_error_code_set (error, 404);
+              atom_error_message_set (error, "Resource not found");
+              atom_error_set (ctx, error);
+            }
+          else
+            {
+              entry = atom_retrieve_entry (ctx, req);
+            }
+          if (id != NULL)
+            atom_id_delete (id);
         }
-      if (id != NULL)
-        atom_id_delete (id);
-      if (atom)
+      if (entry || feed)
        {
          char * str;
          size_t len;
-         char *header = "Content-type: application/atom+xml\n\n";
+          char *header;
+          if (entry)
+            {
+             atom_entry_string (entry, &str, &len);
+              atom_entry_delete (entry);
+            }
+          else
+            {
+             atom_feed_string (feed, &str, &len);
+              atom_feed_delete (feed);
+            }
+         header = "Content-type: application/atom+xml\n\n";
          write (1, header, strlen (header));
-         atom_entry_string (atom, &str, &len);
          write (1, str, len);
          g_free (str);
-         atom_entry_delete (atom);
        }
       else if ((error = atom_error_get (ctx)) != NULL)
        {
@@ -92,11 +109,18 @@ cgi_serve_request (AtomCtx *ctx)
     }
 }
 
+static int
+cgi_is_feed (AtomCtx *ctx, char *req)
+{
+  return (strcmp (req, "/") == 0);
+}
+
 AtomFrontend *
 cgi_frontend (void)
 {
   AtomFrontend *frontend;
   frontend = atom_frontend_new ();
   atom_frontend_map_entries_set (frontend, atom_map_frontend_requests);
+  atom_frontend_is_feed_set (frontend, cgi_is_feed);
   return frontend;
 }