X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=frontend%2Fcgi%2Fcgi.c;h=3c5d72aaece358ef19794cf1285cd4103696702a;hb=f98c599316357e226405a39d12c39fa6ddda1498;hp=84481b9a4083a7cbe5da8b1541fd99d8452ff32a;hpb=f5f94660160dbd2046b9d1f48a8fc2808aa5bd22;p=cascardo%2Fatompub.git diff --git a/frontend/cgi/cgi.c b/frontend/cgi/cgi.c index 84481b9..3c5d72a 100644 --- a/frontend/cgi/cgi.c +++ b/frontend/cgi/cgi.c @@ -19,8 +19,10 @@ #include +#include #include #include +#include void cgi_serve_request (AtomCtx *ctx) @@ -31,20 +33,44 @@ 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; - iri_set_path (iri, path); - atom = atom_retrieve_resource (ctx, iri); - iri_delete (iri); + AtomID *id; + AtomResource *atom; + AtomError *error; + id = atom_id_new (path); + atom = atom_retrieve_resource (ctx, id); + atom_id_delete (id); if (atom) { - fprintf (stdout, "Content-Type: application/atom+xml\n\n"); - write (stdout, atom_string (atom), atom_len (atom)); - atom_delete (atom); + char * str; + size_t len; + char *header = "Content-type: application/atom+xml\n\n"; + write (1, header, strlen (header)); + atom_resource_string (atom, &str, &len); + write (1, str, len); + g_free (str); + atom_resource_delete (atom); + } + 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"); } } else @@ -52,3 +78,12 @@ cgi_serve_request (AtomCtx *ctx) fprintf (stdout, "Status: 501 Not Implemented\n\n"); } } + +AtomFrontend * +cgi_frontend (void) +{ + AtomFrontend *frontend; + frontend = atom_frontend_new (); + atom_frontend_map_entries_set (frontend, atom_map_frontend_requests); + return frontend; +}