5 * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
6 * with the rest of our web framework.
7 * \ingroup WebcitHttpServer
12 #include "webserver.h"
16 * \brief Strip surrounding single or double quotes from a string.
18 * \param s String to be stripped.
20 void stripquotes(char *s)
29 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
37 * \brief Check to see if a META tag has overridden the declared MIME character set.
39 * \param charset Character set name (left unchanged if we don't do anything)
40 * \param meta_http_equiv Content of the "http-equiv" portion of the META tag
41 * \param meta_content Content of the "content" portion of the META tag
43 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
49 if (!meta_http_equiv) return;
50 if (!meta_content) return;
53 if (strcasecmp(meta_http_equiv, "Content-type")) return;
55 ptr = strchr(meta_content, ';');
58 safestrncpy(buf, ++ptr, sizeof buf);
60 if (!strncasecmp(buf, "charset=", 8)) {
61 strcpy(charset, &buf[8]);
68 * \brief Sanitize and enhance an HTML message for display.
69 * Also convert weird character sets to UTF-8 if necessary.
71 * \param supplied_charset the input charset as declared in the MIME headers
73 void output_html(char *supplied_charset, int treat_as_wiki) {
80 size_t converted_alloc = 0;
81 int buffer_length = 1;
83 int content_length = 0;
84 int output_length = 0;
92 iconv_t ic = (iconv_t)(-1) ;
93 char *ibuf; /**< Buffer of characters to be converted */
94 char *obuf; /**< Buffer for converted characters */
95 size_t ibuflen; /**< Length of input buffer */
96 size_t obuflen; /**< Length of output buffer */
97 char *osav; /**< Saved pointer to output buffer */
100 safestrncpy(charset, supplied_charset, sizeof charset);
102 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
104 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
105 line_length = strlen(buf);
106 buffer_length = content_length + line_length + 2;
107 ptr = realloc(msg, buffer_length);
110 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
113 wprintf("</b><br /><br />\n");
114 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
121 strcpy(&msg[content_length], buf);
122 content_length += line_length;
123 strcpy(&msg[content_length], "\n");
127 /** Do a first pass to isolate the message body */
130 msgend = &msg[content_length];
132 while (ptr < msgend) {
134 /** Advance to next tag */
135 ptr = strchr(ptr, '<');
136 if ((ptr == NULL) || (ptr >= msgend)) break;
138 if ((ptr == NULL) || (ptr >= msgend)) break;
141 * Look for META tags. Some messages (particularly in
142 * Asian locales) illegally declare a message's character
143 * set in the HTML instead of in the MIME headers. This
144 * is wrong but we have to work around it anyway.
146 if (!strncasecmp(ptr, "META", 4)) {
152 char *meta_http_equiv;
156 meta_start = &ptr[4];
157 meta_end = strchr(ptr, '>');
158 if ((meta_end != NULL) && (meta_end <= msgend)) {
159 meta_length = meta_end - meta_start + 1;
160 meta = malloc(meta_length + 1);
161 safestrncpy(meta, meta_start, meta_length);
162 meta[meta_length] = 0;
164 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
165 meta_http_equiv = strdup(&meta[11]);
166 spaceptr = strchr(meta_http_equiv, ' ');
167 if (spaceptr != NULL) {
169 meta_content = strdup(++spaceptr);
170 if (!strncasecmp(meta_content, "content=", 8)) {
171 strcpy(meta_content, &meta_content[8]);
172 stripquotes(meta_http_equiv);
173 stripquotes(meta_content);
174 extract_charset_from_meta(charset,
175 meta_http_equiv, meta_content);
179 free(meta_http_equiv);
186 * Any of these tags cause everything up to and including
187 * the tag to be removed.
189 if ( (!strncasecmp(ptr, "HTML", 4))
190 ||(!strncasecmp(ptr, "HEAD", 4))
191 ||(!strncasecmp(ptr, "/HEAD", 5))
192 ||(!strncasecmp(ptr, "BODY", 4)) ) {
193 ptr = strchr(ptr, '>');
194 if ((ptr == NULL) || (ptr >= msgend)) break;
196 if ((ptr == NULL) || (ptr >= msgend)) break;
201 * Any of these tags cause everything including and following
202 * the tag to be removed.
204 if ( (!strncasecmp(ptr, "/HTML", 5))
205 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
214 if (msgstart > msg) {
215 strcpy(msg, msgstart);
218 /** Convert foreign character sets to UTF-8 if necessary. */
220 if ( (strcasecmp(charset, "us-ascii"))
221 && (strcasecmp(charset, "UTF-8"))
222 && (strcasecmp(charset, ""))
224 lprintf(9, "Converting %s to UTF-8\n", charset);
225 ic = ctdl_iconv_open("UTF-8", charset);
226 if (ic == (iconv_t)(-1) ) {
227 lprintf(5, "%s:%d iconv_open() failed: %s\n",
228 __FILE__, __LINE__, strerror(errno));
231 if (ic != (iconv_t)(-1) ) {
233 ibuflen = content_length;
234 obuflen = content_length + (content_length / 2) ;
235 obuf = (char *) malloc(obuflen);
237 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
238 content_length = content_length + (content_length / 2) - obuflen;
239 osav[content_length] = 0;
247 * At this point, the message has been stripped down to
248 * only the content inside the <BODY></BODY> tags, and has
249 * been converted to UTF-8 if it was originally in a foreign
250 * character set. The text is also guaranteed to be null
254 /** Now go through the message, parsing tags as necessary. */
255 converted_alloc = content_length + 8192;
256 converted_msg = malloc(converted_alloc);
257 if (converted_msg == NULL) {
260 strcpy(converted_msg, "");
262 msgend = strchr(msg, 0);
263 while (ptr < msgend) {
266 * Change mailto: links to WebCit mail, by replacing the
267 * link with one that points back to our mail room. Due to
268 * the way we parse URL's, it'll even handle mailto: links
269 * that have "?subject=" in them.
271 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
272 content_length += 64;
273 if (content_length >= converted_alloc) {
274 converted_alloc += 8192;
275 converted_msg = realloc(converted_msg, converted_alloc);
276 if (converted_msg == NULL) {
280 sprintf(&converted_msg[output_length],
281 "<a href=\"display_enter"
282 "?force_room=_MAIL_&recp=");
287 /** Make external links open in a separate window */
288 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
290 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
291 && ((strchr(ptr, '/') < strchr(ptr, '>')))
293 /* open external links to new window */
294 content_length += 64;
295 if (content_length >= converted_alloc) {
296 converted_alloc += 8192;
297 converted_msg = realloc(converted_msg, converted_alloc);
298 if (converted_msg == NULL) {
302 sprintf(&converted_msg[output_length], new_window);
303 output_length += strlen(new_window);
306 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
307 content_length += 64;
308 if (content_length >= converted_alloc) {
309 converted_alloc += 8192;
310 converted_msg = realloc(converted_msg, converted_alloc);
311 if (converted_msg == NULL) {
315 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
320 sprintf(&converted_msg[output_length], "<a href=\"");
326 * Turn anything that looks like a URL into a real link, as long
327 * as it's not inside a tag already
329 else if ( (brak == 0) && (alevel == 0)
330 && (!strncasecmp(ptr, "http://", 7))) {
332 /** Find the end of the link */
333 for (i=0; i<=strlen(ptr); ++i) {
345 if (linklen > 0) break;
348 content_length += (32 + linklen);
349 if (content_length >= converted_alloc) {
350 converted_alloc += 8192;
351 converted_msg = realloc(converted_msg, converted_alloc);
352 if (converted_msg == NULL) {
356 sprintf(&converted_msg[output_length], new_window);
357 output_length += strlen(new_window);
358 converted_msg[output_length] = '\"';
359 converted_msg[++output_length] = 0;
360 for (i=0; i<linklen; ++i) {
361 converted_msg[output_length] = ptr[i];
362 converted_msg[++output_length] = 0;
364 sprintf(&converted_msg[output_length], "\">");
366 for (i=0; i<linklen; ++i) {
367 converted_msg[output_length] = *ptr++;
368 converted_msg[++output_length] = 0;
370 sprintf(&converted_msg[output_length], "</A>");
376 * We need to know when we're inside a tag,
377 * so we don't turn things that look like URL's into
378 * links, when they're already links - or image sources.
380 if (*ptr == '<') ++brak;
381 if (*ptr == '>') --brak;
382 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
383 converted_msg[output_length] = *ptr++;
384 converted_msg[++output_length] = 0;
388 /** uncomment these two lines to override conversion */
389 /** memcpy(converted_msg, msg, content_length); */
390 /** output_length = content_length; */
392 /** Output our big pile of markup */
393 client_write(converted_msg, output_length);
395 /** A little trailing vertical whitespace... */
396 wprintf("<br /><br />\n");
398 /** Now give back the memory */
399 if (converted_msg != NULL) free(converted_msg);
400 if (msg != NULL) free(msg);