* Final cleanup of changes (finally located and fixed the bug)
[citadel.git] / webcit / html2html.c
1 /*
2  * $Id$
3  *
4  * Output an HTML message, modifying it slightly to make sure it plays nice
5  * with the rest of our web framework.
6  *
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <limits.h>
19 #include <netinet/in.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include "webcit.h"
28 #include "vcard.h"
29 #include "webserver.h"
30
31
32 /*
33  */
34 void output_html(void) {
35         char buf[SIZ];
36         char *msg;
37         char *ptr;
38         char *msgstart;
39         char *msgend;
40         int buffer_length = 1;
41         int line_length = 0;
42         int content_length = 0;
43
44         msg = strdup("");
45
46         while (serv_gets(buf), strcmp(buf, "000")) {
47                 line_length = strlen(buf);
48                 buffer_length = content_length + line_length + 2;
49                 msg = realloc(msg, buffer_length);
50                 if (msg == NULL) {
51                         wprintf("<B>realloc() error!  "
52                                 "couldn't get %d bytes: %s</B><BR><BR>\n",
53                                 buffer_length + 1,
54                                 strerror(errno));
55                         return;
56                 }
57                 strcpy(&msg[content_length], buf);
58                 content_length += line_length;
59                 strcpy(&msg[content_length], "\n");
60                 content_length += 1;
61         }
62
63         ptr = msg;
64         msgstart = msg;
65         msgend = &msg[content_length];
66
67         while (ptr < msgend) {
68
69                 /* Advance to next tag */
70                 ptr = strchr(ptr, '<');
71                 if ((ptr == NULL) || (ptr >= msgend)) break;
72                 ++ptr;
73                 if ((ptr == NULL) || (ptr >= msgend)) break;
74
75                 /* Any of these tags cause everything up to and including
76                  * the tag to be removed.
77                  */     
78                 if ( (!strncasecmp(ptr, "HTML", 4))
79                    ||(!strncasecmp(ptr, "HEAD", 4))
80                    ||(!strncasecmp(ptr, "/HEAD", 5))
81                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
82                         ptr = strchr(ptr, '>');
83                         if ((ptr == NULL) || (ptr >= msgend)) break;
84                         ++ptr;
85                         if ((ptr == NULL) || (ptr >= msgend)) break;
86                         msgstart = ptr;
87                 }
88
89                 /* Any of these tags cause everything including and following
90                  * the tag to be removed.
91                  */
92                 if ( (!strncasecmp(ptr, "/HTML", 5))
93                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
94                         --ptr;
95                         msgend = ptr;
96                         strcpy(ptr, "");
97                         
98                 }
99
100                 ++ptr;
101         }
102
103         write(WC->http_sock, msgstart, strlen(msgstart));
104
105         /* A little trailing vertical whitespace... */
106         wprintf("<BR><BR>\n");
107
108         /* Now give back the memory */
109         free(msg);
110
111 }
112