Print error messages as plain content in http body too
[cascardo/atompub.git] / frontend / cgi / cgi.c
index 84481b9..472d3ec 100644 (file)
 
 #include <atompub/atom.h>
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 void
 cgi_serve_request (AtomCtx *ctx)
@@ -37,15 +39,33 @@ cgi_serve_request (AtomCtx *ctx)
     {
       IRI *iri = iri_new ();
       Atom *atom;
+      GError *error;
       iri_set_path (iri, path);
       atom = atom_retrieve_resource (ctx, iri);
       iri_delete (iri);
       if (atom)
        {
-         fprintf (stdout, "Content-Type: application/atom+xml\n\n");
-         write (stdout, atom_string (atom), atom_len (atom));
+         char *header = "Content-type: application/atom+xml\n\n";
+         write (1, header, strlen (header));
+         write (1, atom_string (atom), atom_len (atom));
          atom_delete (atom);
        }
+      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%s\n", error->message,
+                    error->message);
+         else if (error->domain == G_FILE_ERROR &&
+                  error->code == G_FILE_ERROR_NOENT)
+           fprintf (stdout, "Status: 404 %s\n\n%s\n", error->message,
+                    error->message);
+         else
+           fprintf (stdout, "Status: 500 %s\n\n%s\n", error->message,
+                    error->message);
+       }
     }
   else
     {