CGI frontend now requests a feed or an entry depending on the request
[cascardo/atompub.git] / frontend / cgi / cgi.c
index 238ac39..270c1d7 100644 (file)
@@ -33,35 +33,74 @@ cgi_serve_request (AtomCtx *ctx)
     return;
   if (path == NULL)
     {
-      path = atom_config_get_str (ctx, "cgi", "index");
+      if ((path = atom_config_get_str (ctx, "cgi", "index")) == NULL)
+        {
+          /* We do not want to disclose our configuration is empty.
+           * The requester cannot distinguish an empty configuration
+           * from a non-existent index.
+           */
+          fprintf (stdout, "Status: 404 Not Found\n\n");
+          return;
+        }
     }
   if (!strcmp (method, "GET"))
     {
-      IRI *iri = iri_new ();
-      Atom *atom;
-      GError *error;
-      iri_set_path (iri, path);
-      atom = atom_retrieve_resource (ctx, iri);
-      iri_delete (iri);
-      if (atom)
+      AtomID *id;
+      AtomEntry *entry = NULL;
+      AtomFeed *feed = NULL;
+      AtomError *error;
+      char *req;
+      if (atom_is_feed (ctx, path))
+        {
+          feed = atom_retrieve_feed (ctx);
+        }
+      else
+        {
+          /* 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 (entry || feed)
        {
-         char *header = "Content-type: application/atom+xml\n\n";
+         char * str;
+         size_t len;
+          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));
-         write (1, atom_string (atom), atom_len (atom));
-         atom_delete (atom);
+         write (1, str, len);
+         g_free (str);
        }
       else if ((error = atom_error_get (ctx)) != NULL)
        {
-         if (error->domain == G_FILE_ERROR &&
-             (error->code == G_FILE_ERROR_EXIST ||
-              error->code == G_FILE_ERROR_ACCES ||
-              error->code == G_FILE_ERROR_PERM))
-           fprintf (stdout, "Status: 403 %s\n\n", error->message);
-         else if (error->domain == G_FILE_ERROR &&
-                  error->code == G_FILE_ERROR_NOENT)
-           fprintf (stdout, "Status: 404 %s\n\n", error->message);
-         else
-           fprintf (stdout, "Status: 500 %s\n\n", error->message);
+         int code = atom_error_code (error);
+         char *message = atom_error_message (error);
+         fprintf (stdout, "Status: %d %s\n\n%s\n", code, message, message);
+       }
+      else
+        {
+         fprintf (stdout, "Status: 500 Server error\n\nServer error\n");
        }
     }
   else
@@ -69,3 +108,19 @@ cgi_serve_request (AtomCtx *ctx)
       fprintf (stdout, "Status: 501 Not Implemented\n\n");
     }
 }
+
+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;
+}