Implement new frontend functions for CGI frontend
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 13 Oct 2008 03:17:08 +0000 (00:17 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 13 Oct 2008 03:45:06 +0000 (00:45 -0300)
frontend/cgi/cgi.c

index 270c1d7..76380ba 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
-void
-cgi_serve_request (AtomCtx *ctx)
+static AtomRequest *
+cgi_get_request (AtomCtx *ctx)
 {
+  AtomError *error;
   char *method = getenv ("REQUEST_METHOD");
   char *path = getenv ("PATH_INFO");
+  int type;
+  char *request;
   if (method == NULL)
-    return;
-  if (path == NULL)
     {
-      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;
-        }
+      error = atom_error_new ();
+      atom_error_code_set (error, 400);
+      atom_error_message_set (error, "Bad Request");
+      atom_error_set (ctx, error);
+      return NULL;
     }
+  if (path == NULL || *path == '\0')
+    path = "/";
   if (!strcmp (method, "GET"))
     {
-      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 * 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, str, len);
-         g_free (str);
-       }
-      else if ((error = atom_error_get (ctx)) != NULL)
-       {
-         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");
-       }
+      /* Remove the leading slash before mapping */
+      return atom_request_new (ATOM_REQUEST_GET, path + 1);
+    }
+  error = atom_error_new ();
+  atom_error_code_set (error, 501);
+  atom_error_message_set (error, "Not Implemented");
+  atom_error_set (ctx, error);
+  return NULL;
+}
+
+static void
+cgi_handle_error (AtomCtx *ctx)
+{
+  AtomError *error;
+  if ((error = atom_error_get (ctx)) != NULL)
+    {
+      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: 501 Not Implemented\n\n");
+      fprintf (stdout, "Status: 500 Server error\n\nServer error\n");
     }
 }
 
+static void
+cgi_write_header (void)
+{
+  char *header;
+  header = "Content-type: application/atom+xml\n\n";
+  write (1, header, strlen (header));
+}
+
+static void
+cgi_handle_entry (AtomCtx *ctx, AtomEntry *entry)
+{
+  char * str;
+  size_t len;
+  cgi_write_header ();
+  atom_entry_string (entry, &str, &len);
+  atom_entry_delete (entry);
+  write (1, str, len);
+  g_free (str);
+}
+
+static void
+cgi_handle_feed (AtomCtx *ctx, AtomFeed *feed)
+{
+  char * str;
+  size_t len;
+  cgi_write_header ();
+  atom_feed_string (feed, &str, &len);
+  atom_feed_delete (feed);
+  write (1, str, len);
+  g_free (str);
+}
+
 static int
 cgi_is_feed (AtomCtx *ctx, char *req)
 {
-  return (strcmp (req, "/") == 0);
+  return (req == NULL || *req == '\0');
 }
 
 AtomFrontend *
@@ -122,5 +115,9 @@ cgi_frontend (void)
   frontend = atom_frontend_new ();
   atom_frontend_map_entries_set (frontend, atom_map_frontend_requests);
   atom_frontend_is_feed_set (frontend, cgi_is_feed);
+  atom_frontend_get_request_set (frontend, cgi_get_request);
+  atom_frontend_handle_error_set (frontend, cgi_handle_error);
+  atom_frontend_handle_entry_set (frontend, cgi_handle_entry);
+  atom_frontend_handle_feed_set (frontend, cgi_handle_feed);
   return frontend;
 }