* Completed GroupDAV PUT. Untested.
[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 *dav_content_type, char *dav_content
34 ) {
35         char dav_roomname[SIZ];
36         char dav_uid[SIZ];
37         long new_msgnum = (-1L);
38         long old_msgnum = (-1L);
39         char buf[SIZ];
40         int n = 0;
41
42         /* First, break off the "/groupdav/" prefix */
43         remove_token(dav_pathname, 0, '/');
44         remove_token(dav_pathname, 0, '/');
45
46         /* Now extract the message euid */
47         n = num_tokens(dav_pathname, '/');
48         extract_token(dav_uid, dav_pathname, n-1, '/');
49         remove_token(dav_pathname, n-1, '/');
50
51         /* What's left is the room name.  Remove trailing slashes. */
52         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
53                 dav_pathname[strlen(dav_pathname)-1] = 0;
54         }
55         strcpy(dav_roomname, dav_pathname);
56
57         /* Go to the correct room. */
58         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
59                 gotoroom(dav_roomname);
60         }
61         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
62                 wprintf("HTTP/1.1 404 not found\n");
63                 groupdav_common_headers();
64                 wprintf(
65                         "Content-Type: text/plain\n"
66                         "\n"
67                         "There is no folder called \"%s\" on this server.\n",
68                         dav_roomname
69                 );
70                 return;
71         }
72
73         /*
74          * If an HTTP If-Match: header is present, the client is attempting
75          * to replace an existing item.  We have to check to see if the
76          * message number associated with the supplied uid matches what the
77          * client is expecting.  If not, the server probably contains a newer
78          * version, so we fail...
79          */
80         if (strlen(dav_ifmatch) > 0) {
81                 old_msgnum = locate_message_by_uid(dav_uid);
82                 if (atol(dav_ifmatch) != old_msgnum) {
83                         wprintf("HTTP/1.1 412 Precondition Failed\n");
84                         groupdav_common_headers();
85                         wprintf("Content-Length: 0\n\n");
86                         return;
87                 }
88         }
89
90         /*
91          * We are cleared for upload!  We use the new calling syntax for ENT0
92          * which allows a confirmation to be sent back to us.  That's how we
93          * extract the message ID.
94          */
95         serv_puts("ENT0 1|||4|||1|");
96         serv_gets(buf);
97         if (buf[0] != '8') {
98                 wprintf("HTTP/1.1 502 Bad Gateway\n");
99                 groupdav_common_headers();
100                 wprintf("Content-type: text/plain\n"
101                         "\n"
102                         "%s\n", &buf[4]
103                 );
104                 return;
105         }
106
107         /* Send the content to the Citadel server */
108         serv_printf("Content-type: %s\n\n", dav_content_type);
109         serv_puts(dav_content);
110         serv_puts("\n000\n");
111
112         /* Fetch the reply from the Citadel server */
113         n = 0;
114         strcpy(dav_uid, "");
115         while (serv_gets(buf), strcmp(buf, "000")) {
116                 switch(n++) {
117                         case 0:
118                                 new_msgnum = atol(buf);
119                                 break;
120                         case 2:
121                                 strcpy(dav_uid, buf);
122                         default:
123                                 break;
124                 }
125         }
126
127         /* Tell the client what happened. */
128
129         /* Citadel failed in some way? */
130         if (new_msgnum < 0L) {
131                 wprintf("HTTP/1.1 502 Bad Gateway\n");
132                 groupdav_common_headers();
133                 wprintf("Content-length: 0\n\n");
134                 return;
135         }
136
137         /* We created this item for the first time. */
138         if (old_msgnum < 0L) {
139                 wprintf("HTTP/1.1 201 Created\n");
140                 groupdav_common_headers();
141                 wprintf("Content-Length: 0\n");
142                 wprintf("Location: ");
143                 if (strlen(WC->http_host) > 0) {
144                         wprintf("%s://%s",
145                                 (is_https ? "https" : "http"),
146                                 WC->http_host);
147                 }
148                 wprintf("/groupdav/");
149                 urlescputs(dav_roomname);
150                 wprintf("/%s\n", dav_uid);
151                 wprintf("\n");
152                 return;
153         }
154
155         /* We modified an existing item. */
156         wprintf("HTTP/1.1 204 No Content\n");
157         groupdav_common_headers();
158         wprintf("Content-Length: 0\n\n");
159
160         /* The item we replaced has probably already been deleted by
161          * the Citadel server, but we'll do this anyway, just in case.
162          */
163         serv_printf("DELE %ld", old_msgnum);
164         serv_gets(buf);
165
166         return;
167 }