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