* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / groupdav_put.c
index c83be3f615080990de8da5f9c719ad520f8e6e9f..a968ccd57e626986d417f3c89af4b162cd525238 100644 (file)
@@ -5,22 +5,6 @@
  *
  */
 
-#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
  */
 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
-               char *supplied_content_type, char *dav_content
+               char *dav_content_type, char *dav_content
 ) {
        char dav_roomname[SIZ];
        char dav_uid[SIZ];
-       char dav_content_type[SIZ];
        long new_msgnum = (-2L);
        long old_msgnum = (-1L);
        char buf[SIZ];
@@ -46,7 +29,7 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
 
        /* Now extract the message euid */
        n = num_tokens(dav_pathname, '/');
-       extract_token(dav_uid, dav_pathname, n-1, '/');
+       extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
        remove_token(dav_pathname, n-1, '/');
 
        /* What's left is the room name.  Remove trailing slashes. */
@@ -60,36 +43,17 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
                gotoroom(dav_roomname);
        }
        if (strcasecmp(WC->wc_roomname, dav_roomname)) {
-               wprintf("HTTP/1.1 404 not found\n");
+               wprintf("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",
+                       "Content-Type: text/plain\r\n"
+                       "\r\n"
+                       "There is no folder called \"%s\" on this server.\r\n",
                        dav_roomname
                );
                return;
        }
 
-       /* Ugly hack to mess with the content type.  KOrganizer is either
-        * not supplying one, or supplying the wrong one.
-        * FIXME - remove this after KOrg gets fixed.
-        */
-       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
@@ -98,11 +62,15 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
         * version, so we fail...
         */
        if (strlen(dav_ifmatch) > 0) {
+               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");
+                       wprintf("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");
+                       wprintf("Content-Length: 0\r\n\r\n");
                        return;
                }
        }
@@ -113,13 +81,13 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
         * 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");
+               wprintf("HTTP/1.1 502 Bad Gateway\r\n");
                groupdav_common_headers();
-               wprintf("Content-type: text/plain\n"
-                       "\n"
-                       "%s\n", &buf[4]
+               wprintf("Content-type: text/plain\r\n"
+                       "\r\n"
+                       "%s\r\n", &buf[4]
                );
                return;
        }
@@ -132,13 +100,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,21 +117,23 @@ 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");
+               wprintf("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
+               wprintf("Content-type: text/plain\r\n"
+                       "\r\n"
+                       "new_msgnum is %ld\r\n"
+                       "\r\n", new_msgnum
                );
                return;
        }
 
        /* We created this item for the first time. */
        if (old_msgnum < 0L) {
-               wprintf("HTTP/1.1 201 Created\n");
+               wprintf("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("etag: \"%ld\"\r\n", new_msgnum);
+               wprintf("Content-Length: 0\r\n");
                wprintf("Location: ");
                if (strlen(WC->http_host) > 0) {
                        wprintf("%s://%s",
@@ -171,21 +142,23 @@ void groupdav_put(char *dav_pathname, char *dav_ifmatch,
                }
                wprintf("/groupdav/");
                urlescputs(dav_roomname);
-               wprintf("/%s\n", dav_uid);
-               wprintf("\n");
+               wprintf("/%s\r\n", dav_uid);
+               wprintf("\r\n");
                return;
        }
 
        /* We modified an existing item. */
-       wprintf("HTTP/1.1 204 No Content\n");
+       wprintf("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");
+       wprintf("etag: \"%ld\"\r\n", new_msgnum);
+       wprintf("Content-Length: 0\r\n\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);
 
        return;
 }