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 int buffer_length = 1;
82 int content_length = 0;
83 int output_length = 0;
91 iconv_t ic = (iconv_t)(-1) ;
92 char *ibuf; /**< Buffer of characters to be converted */
93 char *obuf; /**< Buffer for converted characters */
94 size_t ibuflen; /**< Length of input buffer */
95 size_t obuflen; /**< Length of output buffer */
96 char *osav; /**< Saved pointer to output buffer */
99 safestrncpy(charset, supplied_charset, sizeof charset);
101 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
103 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
104 line_length = strlen(buf);
105 buffer_length = content_length + line_length + 2;
106 ptr = realloc(msg, buffer_length);
109 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
112 wprintf("</b><br /><br />\n");
113 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
120 strcpy(&msg[content_length], buf);
121 content_length += line_length;
122 strcpy(&msg[content_length], "\n");
126 /** Do a first pass to isolate the message body */
129 msgend = &msg[content_length];
131 while (ptr < msgend) {
133 /** Advance to next tag */
134 ptr = strchr(ptr, '<');
135 if ((ptr == NULL) || (ptr >= msgend)) break;
137 if ((ptr == NULL) || (ptr >= msgend)) break;
140 * Look for META tags. Some messages (particularly in
141 * Asian locales) illegally declare a message's character
142 * set in the HTML instead of in the MIME headers. This
143 * is wrong but we have to work around it anyway.
145 if (!strncasecmp(ptr, "META", 4)) {
151 char *meta_http_equiv;
155 meta_start = &ptr[4];
156 meta_end = strchr(ptr, '>');
157 if ((meta_end != NULL) && (meta_end <= msgend)) {
158 meta_length = meta_end - meta_start + 1;
159 meta = malloc(meta_length + 1);
160 safestrncpy(meta, meta_start, meta_length);
161 meta[meta_length] = 0;
163 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
164 meta_http_equiv = strdup(&meta[11]);
165 spaceptr = strchr(meta_http_equiv, ' ');
166 if (spaceptr != NULL) {
168 meta_content = strdup(++spaceptr);
169 if (!strncasecmp(meta_content, "content=", 8)) {
170 strcpy(meta_content, &meta_content[8]);
171 stripquotes(meta_http_equiv);
172 stripquotes(meta_content);
173 extract_charset_from_meta(charset,
174 meta_http_equiv, meta_content);
178 free(meta_http_equiv);
185 * Any of these tags cause everything up to and including
186 * the tag to be removed.
188 if ( (!strncasecmp(ptr, "HTML", 4))
189 ||(!strncasecmp(ptr, "HEAD", 4))
190 ||(!strncasecmp(ptr, "/HEAD", 5))
191 ||(!strncasecmp(ptr, "BODY", 4)) ) {
192 ptr = strchr(ptr, '>');
193 if ((ptr == NULL) || (ptr >= msgend)) break;
195 if ((ptr == NULL) || (ptr >= msgend)) break;
200 * Any of these tags cause everything including and following
201 * the tag to be removed.
203 if ( (!strncasecmp(ptr, "/HTML", 5))
204 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
213 if (msgstart > msg) {
214 strcpy(msg, msgstart);
217 /** Convert foreign character sets to UTF-8 if necessary. */
219 if ( (strcasecmp(charset, "us-ascii"))
220 && (strcasecmp(charset, "UTF-8"))
221 && (strcasecmp(charset, ""))
223 lprintf(9, "Converting %s to UTF-8\n", charset);
224 ic = ctdl_iconv_open("UTF-8", charset);
225 if (ic == (iconv_t)(-1) ) {
226 lprintf(5, "%s:%d iconv_open() failed: %s\n",
227 __FILE__, __LINE__, strerror(errno));
230 if (ic != (iconv_t)(-1) ) {
232 ibuflen = content_length;
233 obuflen = content_length + (content_length / 2) ;
234 obuf = (char *) malloc(obuflen);
236 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
237 content_length = content_length + (content_length / 2) - obuflen;
238 osav[content_length] = 0;
246 * At this point, the message has been stripped down to
247 * only the content inside the <BODY></BODY> tags, and has
248 * been converted to UTF-8 if it was originally in a foreign
249 * character set. The text is also guaranteed to be null
253 /** Now go through the message, parsing tags as necessary. */
254 converted_msg = malloc(content_length);
255 strcpy(converted_msg, "");
257 msgend = strchr(msg, 0);
258 while (ptr < msgend) {
261 * Change mailto: links to WebCit mail, by replacing the
262 * link with one that points back to our mail room. Due to
263 * the way we parse URL's, it'll even handle mailto: links
264 * that have "?subject=" in them.
266 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
267 content_length += 64;
268 converted_msg = realloc(converted_msg, content_length);
269 sprintf(&converted_msg[output_length],
270 "<a href=\"display_enter"
271 "?force_room=_MAIL_&recp=");
276 /** Make external links open in a separate window */
277 else if (!strncasecmp(ptr, "<a href=", 8)) {
279 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
280 && ((strchr(ptr, '/') < strchr(ptr, '>')))
282 /* open external links to new window */
283 content_length += 64;
284 converted_msg = realloc(converted_msg, content_length);
285 sprintf(&converted_msg[output_length], new_window);
286 output_length += strlen(new_window);
289 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
290 content_length += 64;
291 converted_msg = realloc(converted_msg, content_length);
292 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
297 sprintf(&converted_msg[output_length], "<a href=");
303 * Turn anything that looks like a URL into a real link, as long
304 * as it's not inside a tag already
306 else if ( (brak == 0) && (alevel == 0)
307 && (!strncasecmp(ptr, "http://", 7))) {
309 /** Find the end of the link */
310 for (i=0; i<=strlen(ptr); ++i) {
322 if (linklen > 0) break;
325 content_length += (32 + linklen);
326 converted_msg = realloc(converted_msg, content_length);
327 sprintf(&converted_msg[output_length], new_window);
328 output_length += strlen(new_window);
329 converted_msg[output_length] = '\"';
330 converted_msg[++output_length] = 0;
331 for (i=0; i<linklen; ++i) {
332 converted_msg[output_length] = ptr[i];
333 converted_msg[++output_length] = 0;
335 sprintf(&converted_msg[output_length], "\">");
337 for (i=0; i<linklen; ++i) {
338 converted_msg[output_length] = *ptr++;
339 converted_msg[++output_length] = 0;
341 sprintf(&converted_msg[output_length], "</A>");
347 * We need to know when we're inside a tag,
348 * so we don't turn things that look like URL's into
349 * links, when they're already links - or image sources.
351 if (*ptr == '<') ++brak;
352 if (*ptr == '>') --brak;
353 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
354 converted_msg[output_length] = *ptr++;
355 converted_msg[++output_length] = 0;
359 /** uncomment these two lines to override conversion */
360 /** memcpy(converted_msg, msg, content_length); */
361 /** output_length = content_length; */
363 /** Output our big pile of markup */
364 client_write(converted_msg, output_length);
366 /** A little trailing vertical whitespace... */
367 wprintf("<br /><br />\n");
369 /** Now give back the memory */