4 * Output an HTML message, modifying it slightly to make sure it plays nice
5 * with the rest of our web framework.
15 #include <sys/types.h>
17 #include <sys/socket.h>
19 #include <netinet/in.h>
29 #include "webserver.h"
34 void output_html(void) {
41 int buffer_length = 1;
43 int content_length = 0;
44 int output_length = 0;
51 sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
53 while (serv_gets(buf), strcmp(buf, "000")) {
54 line_length = strlen(buf);
55 buffer_length = content_length + line_length + 2;
56 msg = realloc(msg, buffer_length);
58 wprintf("<B>realloc() error! "
59 "couldn't get %d bytes: %s</B><BR><BR>\n",
64 strcpy(&msg[content_length], buf);
65 content_length += line_length;
66 strcpy(&msg[content_length], "\n");
72 msgend = &msg[content_length];
74 while (ptr < msgend) {
76 /* Advance to next tag */
77 ptr = strchr(ptr, '<');
78 if ((ptr == NULL) || (ptr >= msgend)) break;
80 if ((ptr == NULL) || (ptr >= msgend)) break;
82 /* Any of these tags cause everything up to and including
83 * the tag to be removed.
85 if ( (!strncasecmp(ptr, "HTML", 4))
86 ||(!strncasecmp(ptr, "HEAD", 4))
87 ||(!strncasecmp(ptr, "/HEAD", 5))
88 ||(!strncasecmp(ptr, "BODY", 4)) ) {
89 ptr = strchr(ptr, '>');
90 if ((ptr == NULL) || (ptr >= msgend)) break;
92 if ((ptr == NULL) || (ptr >= msgend)) break;
96 /* Any of these tags cause everything including and following
97 * the tag to be removed.
99 if ( (!strncasecmp(ptr, "/HTML", 5))
100 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
110 converted_msg = malloc(content_length);
111 strcpy(converted_msg, "");
113 while (ptr < msgend) {
114 /* Make links open in a separate window */
115 if (!strncasecmp(ptr, "<A HREF=", 8)) {
116 content_length += 64;
117 converted_msg = realloc(converted_msg, content_length);
118 sprintf(&converted_msg[output_length], new_window);
119 output_length += strlen(new_window);
123 /* Turn loose URL's into hot links */
124 else if ( (brak == 0)
125 && (!strncasecmp(ptr, "http://", 7))) {
127 /* Find the end of the link */
128 for (i=0; i<=strlen(ptr); ++i) {
137 if (linklen > 0) break;
140 content_length += (32 + linklen);
141 converted_msg = realloc(converted_msg, content_length);
142 sprintf(&converted_msg[output_length], new_window);
143 output_length += strlen(new_window);
144 converted_msg[output_length] = '\"';
145 converted_msg[++output_length] = 0;
146 for (i=0; i<linklen; ++i) {
147 converted_msg[output_length] = ptr[i];
148 converted_msg[++output_length] = 0;
150 sprintf(&converted_msg[output_length], "\">");
152 for (i=0; i<linklen; ++i) {
153 converted_msg[output_length] = *ptr++;
154 converted_msg[++output_length] = 0;
156 sprintf(&converted_msg[output_length], "</A>");
161 if (*ptr == '<') ++brak;
162 if (*ptr == '>') --brak;
163 converted_msg[output_length] = *ptr++;
164 converted_msg[++output_length] = 0;
168 /* Output our big pile of markup */
169 http_write(WC->http_sock, converted_msg, output_length);
171 /* A little trailing vertical whitespace... */
172 wprintf("<BR><BR>\n");
174 /* Now give back the memory */