]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_put.c
*** empty log message ***
[citadel.git] / webcit / groupdav_put.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV PUT requests.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <time.h>
23 #include <pthread.h>
24 #include "webcit.h"
25 #include "webserver.h"
26 #include "groupdav.h"
27
28
29 /*
30  * The pathname is always going to be /groupdav/room_name/euid
31  */
32 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
33                 char *supplied_content_type, char *dav_content
34 ) {
35         char dav_roomname[SIZ];
36         char dav_uid[SIZ];
37         char dav_content_type[SIZ];
38         long new_msgnum = (-2L);
39         long old_msgnum = (-1L);
40         char buf[SIZ];
41         int n = 0;
42
43         /* First, break off the "/groupdav/" prefix */
44         remove_token(dav_pathname, 0, '/');
45         remove_token(dav_pathname, 0, '/');
46
47         /* Now extract the message euid */
48         n = num_tokens(dav_pathname, '/');
49         extract_token(dav_uid, dav_pathname, n-1, '/');
50         remove_token(dav_pathname, n-1, '/');
51
52         /* What's left is the room name.  Remove trailing slashes. */
53         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
54                 dav_pathname[strlen(dav_pathname)-1] = 0;
55         }
56         strcpy(dav_roomname, dav_pathname);
57
58         /* Go to the correct room. */
59         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
60                 gotoroom(dav_roomname);
61         }
62         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
63                 wprintf("HTTP/1.1 404 not found\r\n");
64                 groupdav_common_headers();
65                 wprintf(
66                         "Content-Type: text/plain\r\n"
67                         "\r\n"
68                         "There is no folder called \"%s\" on this server.\r\n",
69                         dav_roomname
70                 );
71                 return;
72         }
73
74         /* Ugly hack to mess with the content type.  KOrganizer is either
75          * not supplying one, or supplying the wrong one.
76          * FIXME - remove this after KOrg gets fixed.
77          */
78         strcpy(dav_content_type, supplied_content_type);
79         /* lprintf(9, "Supplied content type: %s\n", dav_content_type); */
80         switch (WC->wc_view) {
81                 case VIEW_ADDRESSBOOK:
82                         strcpy(dav_content_type, "text/x-vcard");
83                         break;
84                 case VIEW_CALENDAR:
85                         strcpy(dav_content_type, "text/calendar");
86                         break;
87                 case VIEW_TASKS:
88                         strcpy(dav_content_type, "text/calendar");
89                         break;
90                 default:
91                         break;
92         }
93         /* lprintf(9, "  Forced content type: %s\n", dav_content_type); */
94
95         /*
96          * If an HTTP If-Match: header is present, the client is attempting
97          * to replace an existing item.  We have to check to see if the
98          * message number associated with the supplied uid matches what the
99          * client is expecting.  If not, the server probably contains a newer
100          * version, so we fail...
101          */
102         if (strlen(dav_ifmatch) > 0) {
103                 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
104                 old_msgnum = locate_message_by_uid(dav_uid);
105                 lprintf(9, "old_msgnum:  %ld\n", old_msgnum);
106                 if (atol(dav_ifmatch) != old_msgnum) {
107                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
108                         lprintf(9, "HTTP/1.1 412 Precondition Failed\r\n");
109                         groupdav_common_headers();
110                         wprintf("Content-Length: 0\r\n\r\n");
111                         return;
112                 }
113         }
114
115         /*
116          * We are cleared for upload!  We use the new calling syntax for ENT0
117          * which allows a confirmation to be sent back to us.  That's how we
118          * extract the message ID.
119          */
120         serv_puts("ENT0 1|||4|||1|");
121         serv_gets(buf);
122         if (buf[0] != '8') {
123                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
124                 groupdav_common_headers();
125                 wprintf("Content-type: text/plain\r\n"
126                         "\r\n"
127                         "%s\r\n", &buf[4]
128                 );
129                 return;
130         }
131
132         /* Send the content to the Citadel server */
133         serv_printf("Content-type: %s\n\n", dav_content_type);
134         serv_puts(dav_content);
135         serv_puts("\n000");
136
137         /* Fetch the reply from the Citadel server */
138         n = 0;
139         strcpy(dav_uid, "");
140         while (serv_gets(buf), strcmp(buf, "000")) {
141                 switch(n++) {
142                         case 0: new_msgnum = atol(buf);
143                                 break;
144                         case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
145                                 break;
146                         case 2: strcpy(dav_uid, buf);
147                                 break;
148                         default:
149                                 break;
150                 }
151         }
152
153         /* Tell the client what happened. */
154
155         /* Citadel failed in some way? */
156         if (new_msgnum < 0L) {
157                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
158                 groupdav_common_headers();
159                 wprintf("Content-type: text/plain\r\n"
160                         "\r\n"
161                         "new_msgnum is %ld\r\n"
162                         "\r\n", new_msgnum
163                 );
164                 return;
165         }
166
167         /* We created this item for the first time. */
168         if (old_msgnum < 0L) {
169                 wprintf("HTTP/1.1 201 Created\r\n");
170                 lprintf(9, "HTTP/1.1 201 Created\r\n");
171                 groupdav_common_headers();
172                 wprintf("Content-Length: 0\r\n");
173                 wprintf("Location: ");
174                 if (strlen(WC->http_host) > 0) {
175                         wprintf("%s://%s",
176                                 (is_https ? "https" : "http"),
177                                 WC->http_host);
178                 }
179                 wprintf("/groupdav/");
180                 urlescputs(dav_roomname);
181                 wprintf("/%s\r\n", dav_uid);
182                 wprintf("\r\n");
183                 return;
184         }
185
186         /* We modified an existing item. */
187         wprintf("HTTP/1.1 204 No Content\r\n");
188         lprintf(9, "HTTP/1.1 204 No Content\r\n");
189         groupdav_common_headers();
190         wprintf("Content-Length: 0\r\n\r\n");
191
192         /* The item we replaced has probably already been deleted by
193          * the Citadel server, but we'll do this anyway, just in case.
194          */
195         serv_printf("DELE %ld", old_msgnum);
196         serv_gets(buf);
197
198         return;
199 }