Change lookup method to find if content is of XML type.
[cascardo/atompub.git] / atom / core.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <atompub/atom.h>
21
22 #include <glib.h>
23
24 #include <string.h>
25
26 static
27 void atom_get (AtomCtx *ctx, AtomRequest *request)
28 {
29   if (atom_is_feed (ctx, atom_request_request (request)))
30     {
31       AtomFeed *feed;
32       feed = atom_retrieve_feed (ctx);
33       if (feed != NULL)
34         {
35           atom_handle_feed (ctx, feed);
36           atom_feed_delete (feed);
37         }
38     }
39   else
40      {
41        AtomEntry *entry;
42        entry = atom_retrieve_entry (ctx, atom_request_request (request));
43        if (entry != NULL)
44          {
45            atom_handle_entry (ctx, entry);
46            atom_entry_delete (entry);
47          }
48      }
49 }
50
51 static void
52 atom_post (AtomCtx *ctx, AtomRequest *request)
53 {
54   AtomError *error;
55   /* Publish requests go to the same IRI as the feed IRI */
56   char *content = NULL;
57   size_t len = 0;
58   AtomEntry *entry;
59   atom_request_content (request, &content, &len);
60   /* TODO: If the content is not an Atom Entry, consider it a media
61    * publish request */
62   entry = atom_entry_new_data_len (content, len);
63   if (entry == NULL)
64     {
65       error = atom_error_new ();
66       atom_error_code_set (error, 400);
67       atom_error_message_set (error, "Bad Request");
68       atom_error_set (ctx, error);
69     }
70   else
71     {
72       atom_publish_entry (ctx, atom_request_request (request), entry);
73       /* TODO: Entry should be the backend representation and more
74        * information should be given to the frontend */
75       if (!atom_error_get (ctx))
76         atom_handle_publish (ctx, entry);
77     }
78 }
79
80 void
81 atom_serve_request (AtomCtx *ctx)
82 {
83   AtomRequest *request;
84   AtomError *error;
85   request = atom_get_request (ctx);
86   if (request != NULL)
87     {
88       int type = atom_request_type (request);
89       switch (type)
90         {
91         case ATOM_REQUEST_GET:
92           atom_get (ctx, request);
93           break;
94         case ATOM_REQUEST_POST:
95           atom_post (ctx, request);
96           break;
97         default:
98           error = atom_error_new ();
99           atom_error_code_set (error, 501);
100           atom_error_message_set (error, "Not Implemented");
101           atom_error_set (ctx, error);
102         }
103     }
104   if (atom_error_get (ctx))
105     {
106       atom_handle_error (ctx);
107       atom_error_set (ctx, NULL);
108     }
109 }