* Doxygen groups. Sorted the files into groups. so now we have a nice structure
[citadel.git] / webcit / groupdav_put.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup GroupdavPut Handles GroupDAV PUT requests.
6  * \ingroup WebcitHttpServerGDav
7  *
8  */
9 /*@{*/
10 #include "webcit.h"
11 #include "webserver.h"
12 #include "groupdav.h"
13
14
15 /**
16  * \brief GroupDAV PUT an item to the server
17  * \param dav_pathname The pathname is always going to be /groupdav/room_name/euid
18  * \param dav_ifmatch ETag of the item we think we're replacing
19  * \param dav_content_type the mime type
20  * \param dav_content the actual data
21  */
22 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
23                 char *dav_content_type, char *dav_content
24 ) {
25         char dav_roomname[SIZ];
26         char dav_uid[SIZ];
27         long new_msgnum = (-2L);
28         long old_msgnum = (-1L);
29         char buf[SIZ];
30         int n = 0;
31
32         /** First, break off the "/groupdav/" prefix */
33         remove_token(dav_pathname, 0, '/');
34         remove_token(dav_pathname, 0, '/');
35
36         /** Now extract the message euid */
37         n = num_tokens(dav_pathname, '/');
38         extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
39         remove_token(dav_pathname, n-1, '/');
40
41         /** What's left is the room name.  Remove trailing slashes. */
42         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
43                 dav_pathname[strlen(dav_pathname)-1] = 0;
44         }
45         strcpy(dav_roomname, dav_pathname);
46
47         /** Go to the correct room. */
48         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
49                 gotoroom(dav_roomname);
50         }
51         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
52                 wprintf("HTTP/1.1 404 not found\r\n");
53                 groupdav_common_headers();
54                 wprintf(
55                         "Content-Type: text/plain\r\n"
56                         "\r\n"
57                         "There is no folder called \"%s\" on this server.\r\n",
58                         dav_roomname
59                 );
60                 return;
61         }
62
63         /**
64          * If an HTTP If-Match: header is present, the client is attempting
65          * to replace an existing item.  We have to check to see if the
66          * message number associated with the supplied uid matches what the
67          * client is expecting.  If not, the server probably contains a newer
68          * version, so we fail...
69          */
70         if (strlen(dav_ifmatch) > 0) {
71                 old_msgnum = locate_message_by_uid(dav_uid);
72                 if (atol(dav_ifmatch) != old_msgnum) {
73                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
74                         lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
75                                 atol(dav_ifmatch), old_msgnum);
76                         groupdav_common_headers();
77                         wprintf("Content-Length: 0\r\n\r\n");
78                         return;
79                 }
80         }
81
82         /**
83          * We are cleared for upload!  We use the new calling syntax for ENT0
84          * which allows a confirmation to be sent back to us.  That's how we
85          * extract the message ID.
86          */
87         serv_puts("ENT0 1|||4|||1|");
88         serv_getln(buf, sizeof buf);
89         if (buf[0] != '8') {
90                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
91                 groupdav_common_headers();
92                 wprintf("Content-type: text/plain\r\n"
93                         "\r\n"
94                         "%s\r\n", &buf[4]
95                 );
96                 return;
97         }
98
99         /** Send the content to the Citadel server */
100         serv_printf("Content-type: %s\n\n", dav_content_type);
101         serv_puts(dav_content);
102         serv_puts("\n000");
103
104         /** Fetch the reply from the Citadel server */
105         n = 0;
106         strcpy(dav_uid, "");
107         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
108                 switch(n++) {
109                         case 0: new_msgnum = atol(buf);
110                                 break;
111                         case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
112                                 break;
113                         case 2: strcpy(dav_uid, buf);
114                                 break;
115                         default:
116                                 break;
117                 }
118         }
119
120         /** Tell the client what happened. */
121
122         /** Citadel failed in some way? */
123         if (new_msgnum < 0L) {
124                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
125                 groupdav_common_headers();
126                 wprintf("Content-type: text/plain\r\n"
127                         "\r\n"
128                         "new_msgnum is %ld\r\n"
129                         "\r\n", new_msgnum
130                 );
131                 return;
132         }
133
134         /** We created this item for the first time. */
135         if (old_msgnum < 0L) {
136                 wprintf("HTTP/1.1 201 Created\r\n");
137                 lprintf(9, "HTTP/1.1 201 Created\r\n");
138                 groupdav_common_headers();
139                 wprintf("etag: \"%ld\"\r\n", new_msgnum);
140                 wprintf("Content-Length: 0\r\n");
141                 wprintf("Location: ");
142                 if (strlen(WC->http_host) > 0) {
143                         wprintf("%s://%s",
144                                 (is_https ? "https" : "http"),
145                                 WC->http_host);
146                 }
147                 wprintf("/groupdav/");
148                 urlescputs(dav_roomname);
149                 wprintf("/%s\r\n", dav_uid);
150                 wprintf("\r\n");
151                 return;
152         }
153
154         /** We modified an existing item. */
155         wprintf("HTTP/1.1 204 No Content\r\n");
156         lprintf(9, "HTTP/1.1 204 No Content\r\n");
157         groupdav_common_headers();
158         wprintf("etag: \"%ld\"\r\n", new_msgnum);
159         wprintf("Content-Length: 0\r\n\r\n");
160
161         /**
162          * The item we replaced has probably already been deleted by
163          * the Citadel server, but we'll do this anyway, just in case.
164          */
165         serv_printf("DELE %ld", old_msgnum);
166         serv_getln(buf, sizeof buf);
167
168         return;
169 }
170
171
172 /*@}*/