a6ba5ef59ffc64d81e392f73a5d3c54de30bb4f6
[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 = (-2L);
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, '/', sizeof dav_uid);
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\r\n");
63                 groupdav_common_headers();
64                 wprintf(
65                         "Content-Type: text/plain\r\n"
66                         "\r\n"
67                         "There is no folder called \"%s\" on this server.\r\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                 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
82                 old_msgnum = locate_message_by_uid(dav_uid);
83                 lprintf(9, "old_msgnum:  %ld\n", old_msgnum);
84                 if (atol(dav_ifmatch) != old_msgnum) {
85                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
86                         lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
87                                 atol(dav_ifmatch), old_msgnum);
88                         groupdav_common_headers();
89                         wprintf("Content-Length: 0\r\n\r\n");
90                         return;
91                 }
92         }
93
94         /*
95          * We are cleared for upload!  We use the new calling syntax for ENT0
96          * which allows a confirmation to be sent back to us.  That's how we
97          * extract the message ID.
98          */
99         serv_puts("ENT0 1|||4|||1|");
100         serv_getln(buf, sizeof buf);
101         if (buf[0] != '8') {
102                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
103                 groupdav_common_headers();
104                 wprintf("Content-type: text/plain\r\n"
105                         "\r\n"
106                         "%s\r\n", &buf[4]
107                 );
108                 return;
109         }
110
111         /* Send the content to the Citadel server */
112         serv_printf("Content-type: %s\n\n", dav_content_type);
113         serv_puts(dav_content);
114         serv_puts("\n000");
115
116         /* Fetch the reply from the Citadel server */
117         n = 0;
118         strcpy(dav_uid, "");
119         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
120                 switch(n++) {
121                         case 0: new_msgnum = atol(buf);
122                                 break;
123                         case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
124                                 break;
125                         case 2: strcpy(dav_uid, buf);
126                                 break;
127                         default:
128                                 break;
129                 }
130         }
131
132         /* Tell the client what happened. */
133
134         /* Citadel failed in some way? */
135         if (new_msgnum < 0L) {
136                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
137                 groupdav_common_headers();
138                 wprintf("Content-type: text/plain\r\n"
139                         "\r\n"
140                         "new_msgnum is %ld\r\n"
141                         "\r\n", new_msgnum
142                 );
143                 return;
144         }
145
146         /* We created this item for the first time. */
147         if (old_msgnum < 0L) {
148                 wprintf("HTTP/1.1 201 Created\r\n");
149                 lprintf(9, "HTTP/1.1 201 Created\r\n");
150                 groupdav_common_headers();
151                 wprintf("etag: \"%ld\"\r\n", new_msgnum);
152                 wprintf("Content-Length: 0\r\n");
153                 wprintf("Location: ");
154                 if (strlen(WC->http_host) > 0) {
155                         wprintf("%s://%s",
156                                 (is_https ? "https" : "http"),
157                                 WC->http_host);
158                 }
159                 wprintf("/groupdav/");
160                 urlescputs(dav_roomname);
161                 wprintf("/%s\r\n", dav_uid);
162                 wprintf("\r\n");
163                 return;
164         }
165
166         /* We modified an existing item. */
167         wprintf("HTTP/1.1 204 No Content\r\n");
168         lprintf(9, "HTTP/1.1 204 No Content\r\n");
169         groupdav_common_headers();
170         wprintf("etag: \"%ld\"\r\n", new_msgnum);
171         wprintf("Content-Length: 0\r\n\r\n");
172
173         /* The item we replaced has probably already been deleted by
174          * the Citadel server, but we'll do this anyway, just in case.
175          */
176         serv_printf("DELE %ld", old_msgnum);
177         serv_getln(buf, sizeof buf);
178
179         return;
180 }