* Final cleanup of changes (finally located and fixed the bug)
[citadel.git] / webcit / html2html.c
index fb77905308ccf8f19825d4de7a0a1c00e27835bb..903e9696152cb7b5b8a235770246337948111381 100644 (file)
 #include <signal.h>
 #include "webcit.h"
 #include "vcard.h"
+#include "webserver.h"
 
 
 /*
- * Here we go.  Please note that the buffer may be changed by this function!
  */
-void output_text_html(char *partbuf, int total_length) {
-
+void output_html(void) {
+       char buf[SIZ];
+       char *msg;
        char *ptr;
        char *msgstart;
        char *msgend;
+       int buffer_length = 1;
+       int line_length = 0;
+       int content_length = 0;
+
+       msg = strdup("");
+
+       while (serv_gets(buf), strcmp(buf, "000")) {
+               line_length = strlen(buf);
+               buffer_length = content_length + line_length + 2;
+               msg = realloc(msg, buffer_length);
+               if (msg == NULL) {
+                       wprintf("<B>realloc() error!  "
+                               "couldn't get %d bytes: %s</B><BR><BR>\n",
+                               buffer_length + 1,
+                               strerror(errno));
+                       return;
+               }
+               strcpy(&msg[content_length], buf);
+               content_length += line_length;
+               strcpy(&msg[content_length], "\n");
+               content_length += 1;
+       }
 
-       ptr = partbuf;
-       msgstart = partbuf;
-       msgend = &partbuf[total_length];
+       ptr = msg;
+       msgstart = msg;
+       msgend = &msg[content_length];
 
        while (ptr < msgend) {
 
                /* Advance to next tag */
                ptr = strchr(ptr, '<');
+               if ((ptr == NULL) || (ptr >= msgend)) break;
                ++ptr;
+               if ((ptr == NULL) || (ptr >= msgend)) break;
 
                /* Any of these tags cause everything up to and including
                 * the tag to be removed.
@@ -55,7 +80,9 @@ void output_text_html(char *partbuf, int total_length) {
                   ||(!strncasecmp(ptr, "/HEAD", 5))
                   ||(!strncasecmp(ptr, "BODY", 4)) ) {
                        ptr = strchr(ptr, '>');
+                       if ((ptr == NULL) || (ptr >= msgend)) break;
                        ++ptr;
+                       if ((ptr == NULL) || (ptr >= msgend)) break;
                        msgstart = ptr;
                }
 
@@ -74,5 +101,12 @@ void output_text_html(char *partbuf, int total_length) {
        }
 
        write(WC->http_sock, msgstart, strlen(msgstart));
+
+       /* A little trailing vertical whitespace... */
+       wprintf("<BR><BR>\n");
+
+       /* Now give back the memory */
+       free(msg);
+
 }