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;
52 sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
54 while (serv_gets(buf), strcmp(buf, "000")) {
55 line_length = strlen(buf);
56 buffer_length = content_length + line_length + 2;
57 msg = realloc(msg, buffer_length);
59 wprintf("<B>realloc() error! "
60 "couldn't get %d bytes: %s</B><br /><br />\n",
65 strcpy(&msg[content_length], buf);
66 content_length += line_length;
67 strcpy(&msg[content_length], "\n");
73 msgend = &msg[content_length];
75 while (ptr < msgend) {
77 /* Advance to next tag */
78 ptr = strchr(ptr, '<');
79 if ((ptr == NULL) || (ptr >= msgend)) break;
81 if ((ptr == NULL) || (ptr >= msgend)) break;
83 /* Any of these tags cause everything up to and including
84 * the tag to be removed.
86 if ( (!strncasecmp(ptr, "HTML", 4))
87 ||(!strncasecmp(ptr, "HEAD", 4))
88 ||(!strncasecmp(ptr, "/HEAD", 5))
89 ||(!strncasecmp(ptr, "BODY", 4)) ) {
90 ptr = strchr(ptr, '>');
91 if ((ptr == NULL) || (ptr >= msgend)) break;
93 if ((ptr == NULL) || (ptr >= msgend)) break;
97 /* Any of these tags cause everything including and following
98 * the tag to be removed.
100 if ( (!strncasecmp(ptr, "/HTML", 5))
101 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
111 converted_msg = malloc(content_length);
112 strcpy(converted_msg, "");
114 while (ptr < msgend) {
115 /* Change mailto: links to WebCit mail, by replacing the
116 * link with one that points back to our mail room. Due to
117 * the way we parse URL's, it'll even handle mailto: links
118 * that have "?subject=" in them.
120 if (!strncasecmp(ptr, "<A HREF=\"mailto:", 16)) {
121 content_length += 64;
122 converted_msg = realloc(converted_msg, content_length);
123 sprintf(&converted_msg[output_length],
124 "<A HREF=\"/display_enter"
125 "?force_room=_MAIL_&recp=");
130 /* Make links open in a separate window */
131 else if (!strncasecmp(ptr, "<A HREF=", 8)) {
132 content_length += 64;
133 converted_msg = realloc(converted_msg, content_length);
134 sprintf(&converted_msg[output_length], new_window);
135 output_length += strlen(new_window);
139 /* Turn anything that looks like a URL into a real link, as long
140 * as it's not inside a tag already
142 else if ( (brak == 0) && (alevel == 0)
143 && (!strncasecmp(ptr, "http://", 7))) {
145 /* Find the end of the link */
146 for (i=0; i<=strlen(ptr); ++i) {
158 if (linklen > 0) break;
161 content_length += (32 + linklen);
162 converted_msg = realloc(converted_msg, content_length);
163 sprintf(&converted_msg[output_length], new_window);
164 output_length += strlen(new_window);
165 converted_msg[output_length] = '\"';
166 converted_msg[++output_length] = 0;
167 for (i=0; i<linklen; ++i) {
168 converted_msg[output_length] = ptr[i];
169 converted_msg[++output_length] = 0;
171 sprintf(&converted_msg[output_length], "\">");
173 for (i=0; i<linklen; ++i) {
174 converted_msg[output_length] = *ptr++;
175 converted_msg[++output_length] = 0;
177 sprintf(&converted_msg[output_length], "</A>");
183 * We need to know when we're inside a tag,
184 * so we don't turn things that look like URL's into
185 * links, when they're already links - or image sources.
187 if (*ptr == '<') ++brak;
188 if (*ptr == '>') --brak;
189 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
190 converted_msg[output_length] = *ptr++;
191 converted_msg[++output_length] = 0;
195 /* Output our big pile of markup */
196 client_write(converted_msg, output_length);
198 /* A little trailing vertical whitespace... */
199 wprintf("<br /><br />\n");
201 /* Now give back the memory */