03367f53695b745db3937e500b56a2678a3526b5
[citadel.git] / webcit / groupdav_get.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgraup GroupdavGet Handle GroupDAV GET requests.
6  *
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "webserver.h"
11 #include "groupdav.h"
12
13
14 /**
15  * \briefThe pathname is always going to be /groupdav/room_name/euid
16  * \param dav_pathname the pathname to print
17  */
18 void groupdav_get(char *dav_pathname) {
19         char dav_roomname[SIZ];
20         char dav_uid[SIZ];
21         long dav_msgnum = (-1);
22         char buf[SIZ];
23         int n = 0;
24         int in_body = 0;
25         int found_content_type = 0;
26
27         /** First, break off the "/groupdav/" prefix */
28         remove_token(dav_pathname, 0, '/');
29         remove_token(dav_pathname, 0, '/');
30
31         /** Now extract the message euid */
32         n = num_tokens(dav_pathname, '/');
33         extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
34         remove_token(dav_pathname, n-1, '/');
35
36         /** What's left is the room name.  Remove trailing slashes. */
37         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
38                 dav_pathname[strlen(dav_pathname)-1] = 0;
39         }
40         strcpy(dav_roomname, dav_pathname);
41
42         /** Go to the correct room. */
43         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
44                 gotoroom(dav_roomname);
45         }
46         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
47                 wprintf("HTTP/1.1 404 not found\r\n");
48                 groupdav_common_headers();
49                 wprintf(
50                         "Content-Type: text/plain\r\n"
51                         "\r\n"
52                         "There is no folder called \"%s\" on this server.\r\n",
53                         dav_roomname
54                 );
55                 return;
56         }
57
58         dav_msgnum = locate_message_by_uid(dav_uid);
59         serv_printf("MSG2 %ld", dav_msgnum);
60         serv_getln(buf, sizeof buf);
61         if (buf[0] != '1') {
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                         "Object \"%s\" was not found in the \"%s\" folder.\r\n",
68                         dav_uid,
69                         dav_roomname
70                 );
71                 return;
72         }
73
74         wprintf("HTTP/1.1 200 OK\r\n");
75         groupdav_common_headers();
76         wprintf("etag: \"%ld\"\r\n", dav_msgnum);
77         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
78                 if (!strncasecmp(buf, "Date: ", 6)) {
79                         wprintf("%s\r\n", buf);
80                 }
81                 if (!strncasecmp(buf, "Content-type: ", 14)) {
82                         wprintf("%s\r\n", buf);
83                         found_content_type = 1;
84                 }
85                 if ((strlen(buf) == 0) && (in_body == 0)) {
86                         if (!found_content_type) {
87                                 wprintf("Content-type: text/plain\r\n");
88                         }
89                         in_body = 1;
90                 }
91                 if (in_body) {
92                         wprintf("%s\r\n", buf);
93                 }
94         }
95 }
96
97
98 /*@}*/