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 /* Change mailto: links to WebCit mail, by replacing the
115 * link with one that points back to our mail room. Due to
116 * the way we parse URL's, it'll even handle mailto: links
117 * that have "?subject=" in them.
119 if (!strncasecmp(ptr, "<A HREF=\"mailto:", 16)) {
120 content_length += 64;
121 converted_msg = realloc(converted_msg, content_length);
122 sprintf(&converted_msg[output_length],
123 "<A HREF=\"/display_enter"
124 "?force_room=_MAIL_&recp=");
129 /* Make links open in a separate window */
130 else if (!strncasecmp(ptr, "<A HREF=", 8)) {
131 content_length += 64;
132 converted_msg = realloc(converted_msg, content_length);
133 sprintf(&converted_msg[output_length], new_window);
134 output_length += strlen(new_window);
138 /* Turn anything that looks like a URL into a real link, as long
139 * as it's not inside a tag already
141 else if ( (brak == 0)
142 && (!strncasecmp(ptr, "http://", 7))) {
144 /* Find the end of the link */
145 for (i=0; i<=strlen(ptr); ++i) {
154 if (linklen > 0) break;
157 content_length += (32 + linklen);
158 converted_msg = realloc(converted_msg, content_length);
159 sprintf(&converted_msg[output_length], new_window);
160 output_length += strlen(new_window);
161 converted_msg[output_length] = '\"';
162 converted_msg[++output_length] = 0;
163 for (i=0; i<linklen; ++i) {
164 converted_msg[output_length] = ptr[i];
165 converted_msg[++output_length] = 0;
167 sprintf(&converted_msg[output_length], "\">");
169 for (i=0; i<linklen; ++i) {
170 converted_msg[output_length] = *ptr++;
171 converted_msg[++output_length] = 0;
173 sprintf(&converted_msg[output_length], "</A>");
179 * We need to know when we're inside a tag,
180 * so we don't turn things that look like URL's into
181 * links, when they're already links - or image sources.
183 if (*ptr == '<') ++brak;
184 if (*ptr == '>') --brak;
185 converted_msg[output_length] = *ptr++;
186 converted_msg[++output_length] = 0;
190 /* Output our big pile of markup */
191 client_write(converted_msg, output_length);
193 /* A little trailing vertical whitespace... */
194 wprintf("<BR><BR>\n");
196 /* Now give back the memory */