* use strbuffer as wprintf backend
[citadel.git] / webcit / groupdav_put.c
index 45729c7bf3874b487d9e52a4766330fe29c1f551..dc5121f47c4069ad812db89a77ed1cc9b18975fc 100644 (file)
@@ -5,91 +5,95 @@
  *
  */
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <limits.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <time.h>
-#include <pthread.h>
 #include "webcit.h"
 #include "webserver.h"
 #include "groupdav.h"
 
 
 /*
- * The pathname is always going to be /groupdav/room_name/euid
+ * This function is for uploading an ENTIRE calendar, not just one
+ * component.  This would be for webcal:// 'publish' operations, not
+ * for GroupDAV.
+ */
+void groupdav_put_bigics(char *dav_content, int dav_content_length)
+{
+       char buf[1024];
+
+       /*
+        * Tell the server that when we save a calendar event, we
+        * do *not* want the server to generate invitations. 
+        */
+       serv_puts("ICAL sgi|0");
+       serv_getln(buf, sizeof buf);
+
+       serv_puts("ICAL putics");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] != '4') {
+               hprintf("HTTP/1.1 502 Bad Gateway\r\n");
+               groupdav_common_headers();
+               hprintf("Content-type: text/plain\r\n");
+               wprintf("%s\r\n", &buf[4]);
+               end_burst();
+               return;
+       }
+
+       serv_write(dav_content, dav_content_length);
+       serv_printf("\n000");
+
+       /* Report success and not much else. */
+       hprintf("HTTP/1.1 204 No Content\r\n");
+       lprintf(9, "HTTP/1.1 204 No Content\r\n");
+       groupdav_common_headers();
+       hprintf("Content-Length: 0\r\n");
+}
+
+
+
+/*
+ * The pathname is always going to take one of two formats:
+ * /groupdav/room_name/euid    (GroupDAV)
+ * /groupdav/room_name         (webcal)
  */
 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
-               char *supplied_content_type, char *dav_content
+               char *dav_content_type, char *dav_content,
+               int dav_content_length
 ) {
-       char dav_roomname[SIZ];
-       char dav_uid[SIZ];
-       char dav_content_type[SIZ];
+       char dav_roomname[1024];
+       char dav_uid[1024];
        long new_msgnum = (-2L);
        long old_msgnum = (-1L);
        char buf[SIZ];
        int n = 0;
 
-       /* First, break off the "/groupdav/" prefix */
-       remove_token(dav_pathname, 0, '/');
-       remove_token(dav_pathname, 0, '/');
-
-       /* Now extract the message euid */
-       n = num_tokens(dav_pathname, '/');
-       extract_token(dav_uid, dav_pathname, n-1, '/');
-       remove_token(dav_pathname, n-1, '/');
+       if (num_tokens(dav_pathname, '/') < 3) {
+               hprintf("HTTP/1.1 404 not found\r\n");
+               groupdav_common_headers();
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf("The object you requested was not found.\r\n");
+               end_burst();
+               return;
+       }
 
-       /* What's left is the room name.  Remove trailing slashes. */
-       if (dav_pathname[strlen(dav_pathname)-1] == '/') {
-               dav_pathname[strlen(dav_pathname)-1] = 0;
+       extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
+       extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
+       if ((!strcasecmp(dav_uid, "ics")) || (!strcasecmp(dav_uid, "calendar.ics"))) {
+               strcpy(dav_uid, "");
        }
-       strcpy(dav_roomname, dav_pathname);
 
        /* Go to the correct room. */
        if (strcasecmp(WC->wc_roomname, dav_roomname)) {
                gotoroom(dav_roomname);
        }
        if (strcasecmp(WC->wc_roomname, dav_roomname)) {
-               wprintf("HTTP/1.1 404 not found\n");
+               hprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
-               wprintf(
-                       "Content-Type: text/plain\n"
-                       "\n"
-                       "There is no folder called \"%s\" on this server.\n",
-                       dav_roomname
-               );
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf("There is no folder called \"%s\" on this server.\r\n",
+                       dav_roomname);
+               end_burst();
                return;
        }
 
-       /* Ugly hack to mess with the content type.  KOrganizer is either
-        * not supplying one, or supplying the wrong one.  FIXME remove this
-        * after getting clarification or a fix from Reinhold.
-        */
-       strcpy(dav_content_type, supplied_content_type);
-       switch (WC->wc_view) {
-               case VIEW_ADDRESSBOOK:
-                       strcpy(dav_content_type, "text/x-vcard");
-                       break;
-               case VIEW_CALENDAR:
-                       strcpy(dav_content_type, "text/calendar");
-                       break;
-               case VIEW_TASKS:
-                       strcpy(dav_content_type, "text/calendar");
-                       break;
-               default:
-                       break;
-       }
-
        /*
         * If an HTTP If-Match: header is present, the client is attempting
         * to replace an existing item.  We have to check to see if the
@@ -97,30 +101,42 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
         * client is expecting.  If not, the server probably contains a newer
         * version, so we fail...
         */
-       if (strlen(dav_ifmatch) > 0) {
+       if (!IsEmptyStr(dav_ifmatch)) {
+               lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
                old_msgnum = locate_message_by_uid(dav_uid);
+               lprintf(9, "old_msgnum:  %ld\n", old_msgnum);
                if (atol(dav_ifmatch) != old_msgnum) {
-                       wprintf("HTTP/1.1 412 Precondition Failed\n");
+                       hprintf("HTTP/1.1 412 Precondition Failed\r\n");
+                       lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
+                               atol(dav_ifmatch), old_msgnum);
                        groupdav_common_headers();
-                       wprintf("Content-Length: 0\n\n");
+                       hprintf("Content-Length: 0\r\n");
+                       end_burst();
                        return;
                }
        }
 
+       /** PUT on the collection itself uploads an ICS of the entire collection.
+        */
+       if (!strcasecmp(dav_uid, "")) {
+               groupdav_put_bigics(dav_content, dav_content_length);
+               return;
+       }
+
        /*
         * We are cleared for upload!  We use the new calling syntax for ENT0
         * which allows a confirmation to be sent back to us.  That's how we
         * extract the message ID.
         */
        serv_puts("ENT0 1|||4|||1|");
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '8') {
-               wprintf("HTTP/1.1 502 Bad Gateway\n");
+               hprintf("HTTP/1.1 502 Bad Gateway\r\n");
                groupdav_common_headers();
-               wprintf("Content-type: text/plain\n"
-                       "\n"
-                       "%s\n", &buf[4]
-               );
+               hprintf("Content-type: text/plain\r\n");
+
+               wprintf("%s\r\n", &buf[4]);
+               end_burst();
                return;
        }
 
@@ -132,13 +148,14 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
        /* Fetch the reply from the Citadel server */
        n = 0;
        strcpy(dav_uid, "");
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                switch(n++) {
-                       case 0:
-                               new_msgnum = atol(buf);
+                       case 0: new_msgnum = atol(buf);
+                               break;
+                       case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
+                               break;
+                       case 2: strcpy(dav_uid, buf);
                                break;
-                       case 2:
-                               strcpy(dav_uid, buf);
                        default:
                                break;
                }
@@ -148,44 +165,44 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
 
        /* Citadel failed in some way? */
        if (new_msgnum < 0L) {
-               wprintf("HTTP/1.1 502 Bad Gateway\n");
+               hprintf("HTTP/1.1 502 Bad Gateway\r\n");
                groupdav_common_headers();
-               wprintf("Content-type: text/plain\n"
-                       "\n"
-                       "new_msgnum is %ld\n"
-                       "\n", new_msgnum
-               );
+               hprintf("Content-type: text/plain\r\n");
+               wprintf("new_msgnum is %ld\r\n"
+                       "\r\n", new_msgnum);
+               end_burst();
                return;
        }
 
        /* We created this item for the first time. */
        if (old_msgnum < 0L) {
-               wprintf("HTTP/1.1 201 Created\n");
+               hprintf("HTTP/1.1 201 Created\r\n");
+               lprintf(9, "HTTP/1.1 201 Created\r\n");
                groupdav_common_headers();
-               wprintf("Content-Length: 0\n");
-               wprintf("Location: ");
-               if (strlen(WC->http_host) > 0) {
-                       wprintf("%s://%s",
-                               (is_https ? "https" : "http"),
-                               WC->http_host);
-               }
-               wprintf("/groupdav/");
+               hprintf("etag: \"%ld\"\r\n", new_msgnum);
+               hprintf("Content-Length: 0\r\n");
+               hprintf("Location: ");
+               groupdav_identify_host();
+               hprintf("/groupdav/");/////TODO
                urlescputs(dav_roomname);
-               wprintf("/%s\n", dav_uid);
-               wprintf("\n");
+               char escaped_uid[1024];
+               euid_escapize(escaped_uid, dav_uid);
+               wprintf("/%s\r\n", escaped_uid);
                return;
        }
 
        /* We modified an existing item. */
-       wprintf("HTTP/1.1 204 No Content\n");
+       hprintf("HTTP/1.1 204 No Content\r\n");
+       lprintf(9, "HTTP/1.1 204 No Content\r\n");
        groupdav_common_headers();
-       wprintf("Content-Length: 0\n\n");
+       hprintf("etag: \"%ld\"\r\n", new_msgnum);
+       hprintf("Content-Length: 0\r\n");
 
        /* The item we replaced has probably already been deleted by
         * the Citadel server, but we'll do this anyway, just in case.
         */
        serv_printf("DELE %ld", old_msgnum);
-       serv_gets(buf);
-
+       serv_getln(buf, sizeof buf);
+       end_burst();
        return;
 }