6673172ba1119e076d0bd08565f5c316f9d8c31a
[citadel.git] / webcit / dav_delete.c
1 /*
2  * Handles GroupDAV DELETE requests.
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "dav.h"
24
25
26 /*
27  * The pathname is always going to be /groupdav/room_name/euid
28  */
29 void dav_delete(void) 
30 {
31         wcsession *WCC = WC;
32         char dav_uid[SIZ];
33         long dav_msgnum = (-1);
34         char buf[SIZ];
35         int n = 0;
36         StrBuf *dav_roomname = NewStrBuf();
37         
38         /* Now extract the message euid */
39         n = StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/');
40         extract_token(dav_uid, ChrPtr(WCC->Hdr->HR.ReqLine), n-1, '/', sizeof dav_uid);
41         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
42
43         ///* What's left is the room name.  Remove trailing slashes. */
44         //len = StrLength(WCC->Hdr->HR.ReqLine);
45         //if ((len > 0) && (ChrPtr(WCC->Hdr->HR.ReqLinee)[len-1] == '/')) {
46         //      StrBufCutRight(WCC->Hdr->HR.ReqLine, 1);
47         //}
48         //StrBufCutLeft(WCC->Hdr->HR.ReqLine, 1);
49
50         /* Go to the correct room. */
51         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
52                 gotoroom(dav_roomname);
53         }
54         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
55                 hprintf("HTTP/1.1 404 not found\r\n");
56                 dav_common_headers();
57                 hprintf("Content-Length: 0\r\n\r\n");
58                 begin_burst();
59                 end_burst();
60                 FreeStrBuf(&dav_roomname);
61                 return;
62         }
63
64         dav_msgnum = locate_message_by_uid(dav_uid);
65
66         /*
67          * If no item exists with the requested uid ... simple error.
68          */
69         if (dav_msgnum < 0L) {
70                 hprintf("HTTP/1.1 404 Not Found\r\n");
71                 dav_common_headers();
72                 hprintf("Content-Length: 0\r\n\r\n");
73                 begin_burst();
74                 end_burst();
75                 FreeStrBuf(&dav_roomname);
76                 return;
77         }
78
79         /*
80          * It's there ... check the ETag and make sure it matches
81          * the message number.
82          */
83         if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
84                 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != dav_msgnum) {
85                         hprintf("HTTP/1.1 412 Precondition Failed\r\n");
86                         dav_common_headers();
87                         hprintf("Content-Length: 0\r\n\r\n");
88                         begin_burst();
89                         end_burst();
90                         FreeStrBuf(&dav_roomname);
91                         return;
92                 }
93         }
94
95         /*
96          * Ok, attempt to delete the item.
97          */
98         serv_printf("DELE %ld", dav_msgnum);
99         serv_getln(buf, sizeof buf);
100         if (buf[0] == '2') {
101                 hprintf("HTTP/1.1 204 No Content\r\n"); /* success */
102                 dav_common_headers();
103                 hprintf("Content-Length: 0\r\n\r\n");
104                 begin_burst();
105                 end_burst();
106         }
107         else {
108                 hprintf("HTTP/1.1 403 Forbidden\r\n");  /* access denied */
109                 dav_common_headers();
110                 hprintf("Content-Length: 0\r\n\r\n");
111                 begin_burst();
112                 end_burst();
113         }
114         FreeStrBuf(&dav_roomname);
115         return;
116 }