* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / groupdav_get.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV GET 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_get(char *dav_pathname) {
17         char dav_roomname[SIZ];
18         char dav_uid[SIZ];
19         long dav_msgnum = (-1);
20         char buf[SIZ];
21         int n = 0;
22         int in_body = 0;
23         int found_content_type = 0;
24
25         /* First, break off the "/groupdav/" prefix */
26         remove_token(dav_pathname, 0, '/');
27         remove_token(dav_pathname, 0, '/');
28
29         /* Now extract the message euid */
30         n = num_tokens(dav_pathname, '/');
31         extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
32         remove_token(dav_pathname, n-1, '/');
33
34         /* What's left is the room name.  Remove trailing slashes. */
35         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
36                 dav_pathname[strlen(dav_pathname)-1] = 0;
37         }
38         strcpy(dav_roomname, dav_pathname);
39
40         /* Go to the correct room. */
41         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
42                 gotoroom(dav_roomname);
43         }
44         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
45                 wprintf("HTTP/1.1 404 not found\r\n");
46                 groupdav_common_headers();
47                 wprintf(
48                         "Content-Type: text/plain\r\n"
49                         "\r\n"
50                         "There is no folder called \"%s\" on this server.\r\n",
51                         dav_roomname
52                 );
53                 return;
54         }
55
56         dav_msgnum = locate_message_by_uid(dav_uid);
57         serv_printf("MSG2 %ld", dav_msgnum);
58         serv_getln(buf, sizeof buf);
59         if (buf[0] != '1') {
60                 wprintf("HTTP/1.1 404 not found\r\n");
61                 groupdav_common_headers();
62                 wprintf(
63                         "Content-Type: text/plain\r\n"
64                         "\r\n"
65                         "Object \"%s\" was not found in the \"%s\" folder.\r\n",
66                         dav_uid,
67                         dav_roomname
68                 );
69                 return;
70         }
71
72         wprintf("HTTP/1.1 200 OK\r\n");
73         groupdav_common_headers();
74         wprintf("etag: \"%ld\"\r\n", dav_msgnum);
75         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
76                 if (!strncasecmp(buf, "Date: ", 6)) {
77                         wprintf("%s\r\n", buf);
78                 }
79                 if (!strncasecmp(buf, "Content-type: ", 14)) {
80                         wprintf("%s\r\n", buf);
81                         found_content_type = 1;
82                 }
83                 if ((strlen(buf) == 0) && (in_body == 0)) {
84                         if (!found_content_type) {
85                                 wprintf("Content-type: text/plain\r\n");
86                         }
87                         in_body = 1;
88                 }
89                 if (in_body) {
90                         wprintf("%s\r\n", buf);
91                 }
92         }
93 }