* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / groupdav_get.c
index b61ffae83db59074f650061e1300cc157ba0ed9f..70f75bbd5ef04bf8d8079b734f8000fe13dc7174 100644 (file)
@@ -5,37 +5,22 @@
  *
  */
 
-#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/msg_num
+ * The pathname is always going to be /groupdav/room_name/euid
  */
 void groupdav_get(char *dav_pathname) {
        char dav_roomname[SIZ];
        char dav_uid[SIZ];
        long dav_msgnum = (-1);
        char buf[SIZ];
-       int found_content_type = 0;
        int n = 0;
+       int in_body = 0;
+       int found_content_type = 0;
 
        /* First, break off the "/groupdav/" prefix */
        remove_token(dav_pathname, 0, '/');
@@ -43,7 +28,7 @@ void groupdav_get(char *dav_pathname) {
 
        /* 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. */
@@ -57,12 +42,12 @@ void groupdav_get(char *dav_pathname) {
                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;
@@ -70,30 +55,39 @@ void groupdav_get(char *dav_pathname) {
 
        dav_msgnum = locate_message_by_uid(dav_uid);
        serv_printf("MSG2 %ld", dav_msgnum);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
-               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"
-                       "Object \"%s\" was not found in the \"%s\" folder.\n",
+                       "Content-Type: text/plain\r\n"
+                       "\r\n"
+                       "Object \"%s\" was not found in the \"%s\" folder.\r\n",
                        dav_uid,
                        dav_roomname
                );
                return;
        }
 
-       wprintf("HTTP/1.1 200 OK\n");
+       wprintf("HTTP/1.1 200 OK\r\n");
        groupdav_common_headers();
-       wprintf("ETag: \"%ld\"\n", dav_msgnum);
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       wprintf("etag: \"%ld\"\r\n", dav_msgnum);
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+               if (!strncasecmp(buf, "Date: ", 6)) {
+                       wprintf("%s\r\n", buf);
+               }
                if (!strncasecmp(buf, "Content-type: ", 14)) {
+                       wprintf("%s\r\n", buf);
                        found_content_type = 1;
                }
-               if ((strlen(buf) == 0) && (found_content_type == 0)) {
-                       wprintf("Content-type: text/plain\n");
+               if ((strlen(buf) == 0) && (in_body == 0)) {
+                       if (!found_content_type) {
+                               wprintf("Content-type: text/plain\r\n");
+                       }
+                       in_body = 1;
+               }
+               if (in_body) {
+                       wprintf("%s\r\n", buf);
                }
-               wprintf("%s\n", buf);
        }
 }