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