Handle POST requests in the core
[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   if (!atom_is_feed (ctx, atom_request_request (request)))
57     {
58       error = atom_error_new ();
59       atom_error_code_set (error, 400);
60       atom_error_message_set (error, "Bad Request");
61       atom_error_set (ctx, error);
62     }
63   else
64     {
65       char *content = NULL;
66       size_t len = 0;
67       AtomEntry *entry;
68       atom_request_content (request, &content, &len);
69       /* TODO: If the content is not an Atom Entry, consider it a media
70        * publish request */
71       entry = atom_entry_new_data_len (content, len);
72       if (entry == NULL)
73         {
74           error = atom_error_new ();
75           atom_error_code_set (error, 400);
76           atom_error_message_set (error, "Bad Request");
77           atom_error_set (ctx, error);
78         }
79       else
80         {
81           atom_publish_entry (ctx, atom_request_request (request), entry);
82           /* TODO: Entry should be the backend representation and more
83            * information should be given to the frontend */
84           if (!atom_error_get (ctx))
85             atom_handle_publish (ctx, entry);
86         }
87     }
88 }
89
90 void
91 atom_serve_request (AtomCtx *ctx)
92 {
93   AtomRequest *request;
94   AtomError *error;
95   request = atom_get_request (ctx);
96   if (request != NULL)
97     {
98       int type = atom_request_type (request);
99       switch (type)
100         {
101         case ATOM_REQUEST_GET:
102           atom_get (ctx, request);
103           break;
104         case ATOM_REQUEST_POST:
105           atom_post (ctx, request);
106           break;
107         default:
108           error = atom_error_new ();
109           atom_error_code_set (error, 501);
110           atom_error_message_set (error, "Not Implemented");
111           atom_error_set (ctx, error);
112         }
113     }
114   if (atom_error_get (ctx))
115     {
116       atom_handle_error (ctx);
117       atom_error_set (ctx, NULL);
118     }
119 }