Back to index

webcit  8.12-dfsg
dav_options.c
Go to the documentation of this file.
00001 /*
00002  * Handles DAV OPTIONS requests (experimental -- not required by GroupDAV)
00003  *
00004  * Copyright (c) 2005-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 #include "webserver.h"
00017 #include "dav.h"
00018 
00019 /*
00020  * The pathname is always going to be /groupdav/room_name/msg_num
00021  */
00022 void dav_options(void)
00023 {
00024        wcsession *WCC = WC;
00025        StrBuf *dav_roomname;
00026        StrBuf *dav_uid;
00027        long dav_msgnum = (-1);
00028        char datestring[256];
00029        time_t now;
00030 
00031        now = time(NULL);
00032        http_datestring(datestring, sizeof datestring, now);
00033 
00034        dav_roomname = NewStrBuf();
00035        dav_uid = NewStrBuf();
00036        StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
00037        StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
00038 
00039        syslog(LOG_DEBUG, "\033[35m%s (logged_in=%d)\033[0m", ChrPtr(WCC->Hdr->HR.ReqLine), WC->logged_in);
00040        /*
00041         * If the room name is blank, the client is doing an OPTIONS on the root.
00042         */
00043        if (StrLength(dav_roomname) == 0) {
00044               syslog(LOG_DEBUG, "\033[36mOPTIONS requested for root\033[0m");
00045               hprintf("HTTP/1.1 200 OK\r\n");
00046               dav_common_headers();
00047               hprintf("Date: %s\r\n", datestring);
00048               hprintf("DAV: 1\r\n");
00049               hprintf("Allow: OPTIONS, PROPFIND\r\n");
00050               hprintf("\r\n");
00051               begin_burst();
00052               end_burst();
00053               FreeStrBuf(&dav_roomname);
00054               FreeStrBuf(&dav_uid);
00055               return;
00056        }
00057 
00058        /* Go to the correct room. */
00059        if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
00060               gotoroom(dav_roomname);
00061        }
00062 
00063        if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
00064               syslog(LOG_DEBUG, "\033[36mOPTIONS requested for invalid item\033[0m");
00065               hprintf("HTTP/1.1 404 not found\r\n");
00066               dav_common_headers();
00067               hprintf("Date: %s\r\n", datestring);
00068               hprintf(
00069                      "Content-Type: text/plain\r\n");
00070               begin_burst();
00071               wc_printf(
00072                      "There is no folder called \"%s\" on this server.\r\n",
00073                      ChrPtr(dav_roomname)
00074               );
00075               end_burst();
00076               FreeStrBuf(&dav_roomname);
00077               FreeStrBuf(&dav_uid);
00078               return;
00079        }
00080 
00081        /* If dav_uid is non-empty, client is requesting an OPTIONS on
00082         * a specific item in the room.
00083         */
00084        if (StrLength(dav_uid) != 0) {
00085               syslog(LOG_DEBUG, "\033[36mOPTIONS requested for specific item\033[0m");
00086               dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
00087               if (dav_msgnum < 0) {
00088                      hprintf("HTTP/1.1 404 not found\r\n");
00089                      dav_common_headers();
00090                      hprintf("Content-Type: text/plain\r\n");
00091                      begin_burst();
00092                      wc_printf(
00093                             "Object \"%s\" was not found in the \"%s\" folder.\r\n",
00094                             ChrPtr(dav_uid),
00095                             ChrPtr(dav_roomname)
00096                      );
00097                      FreeStrBuf(&dav_roomname);
00098                      FreeStrBuf(&dav_uid);
00099                      end_burst();return;
00100               }
00101 
00102               hprintf("HTTP/1.1 200 OK\r\n");
00103               dav_common_headers();
00104               hprintf("Date: %s\r\n", datestring);
00105               hprintf("DAV: 1\r\n");
00106               hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
00107               
00108               begin_burst();
00109               end_burst();
00110               FreeStrBuf(&dav_roomname);
00111               FreeStrBuf(&dav_uid);
00112               return;
00113        }
00114 
00115        FreeStrBuf(&dav_roomname);
00116        FreeStrBuf(&dav_uid);
00117 
00118        /*
00119         * We got to this point, which means that the client is requesting
00120         * an OPTIONS on the room itself.
00121         */
00122        syslog(LOG_DEBUG, "\033[36mOPTIONS requested for room '%s' (%slogged in)\033[0m",
00123               ChrPtr(WC->CurRoom.name),
00124               ((WC->logged_in) ? "" : "not ")
00125        );
00126        hprintf("HTTP/1.1 200 OK\r\n");
00127        dav_common_headers();
00128        hprintf("Date: %s\r\n", datestring);
00129 
00130        /*
00131         * Offer CalDAV (RFC 4791) if this is a calendar room
00132         */
00133        if ( (WC->CurRoom.view == VIEW_CALENDAR) || (WC->CurRoom.view == VIEW_CALBRIEF) ) {
00134               hprintf("DAV: 1, calendar-access\r\n");
00135               syslog(LOG_DEBUG, "\033[36mDAV: 1, calendar-access\033[0m");
00136        }
00137        else {
00138               hprintf("DAV: 1\r\n");
00139               syslog(LOG_DEBUG, "\033[36mDAV: 1\033[0m");
00140        }
00141 
00142        hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, REPORT\r\n");
00143        begin_burst();
00144        end_burst();
00145 }