Handle POST requests in the core
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 26 Oct 2008 17:07:37 +0000 (15:07 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 26 Oct 2008 17:08:26 +0000 (15:08 -0200)
atom/core.c

index e9199b2..b2289b4 100644 (file)
@@ -48,6 +48,45 @@ void atom_get (AtomCtx *ctx, AtomRequest *request)
      }
 }
 
+static void
+atom_post (AtomCtx *ctx, AtomRequest *request)
+{
+  AtomError *error;
+  /* Publish requests go to the same IRI as the feed IRI */
+  if (!atom_is_feed (ctx, atom_request_request (request)))
+    {
+      error = atom_error_new ();
+      atom_error_code_set (error, 400);
+      atom_error_message_set (error, "Bad Request");
+      atom_error_set (ctx, error);
+    }
+  else
+    {
+      char *content = NULL;
+      size_t len = 0;
+      AtomEntry *entry;
+      atom_request_content (request, &content, &len);
+      /* TODO: If the content is not an Atom Entry, consider it a media
+       * publish request */
+      entry = atom_entry_new_data_len (content, len);
+      if (entry == NULL)
+        {
+          error = atom_error_new ();
+          atom_error_code_set (error, 400);
+          atom_error_message_set (error, "Bad Request");
+          atom_error_set (ctx, error);
+        }
+      else
+        {
+          atom_publish_entry (ctx, atom_request_request (request), entry);
+          /* TODO: Entry should be the backend representation and more
+           * information should be given to the frontend */
+          if (!atom_error_get (ctx))
+            atom_handle_publish (ctx, entry);
+        }
+    }
+}
+
 void
 atom_serve_request (AtomCtx *ctx)
 {
@@ -56,12 +95,16 @@ atom_serve_request (AtomCtx *ctx)
   request = atom_get_request (ctx);
   if (request != NULL)
     {
-      if (atom_request_type (request) == ATOM_REQUEST_GET)
+      int type = atom_request_type (request);
+      switch (type)
         {
+        case ATOM_REQUEST_GET:
           atom_get (ctx, request);
-        }
-      else
-        {
+          break;
+        case ATOM_REQUEST_POST:
+          atom_post (ctx, request);
+          break;
+        default:
           error = atom_error_new ();
           atom_error_code_set (error, 501);
           atom_error_message_set (error, "Not Implemented");