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 msg = realloc(msg, buffer_length);
109 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
112 wprintf("</b><br /><br />\n");
115 strcpy(&msg[content_length], buf);
116 content_length += line_length;
117 strcpy(&msg[content_length], "\n");
121 /** Do a first pass to isolate the message body */
124 msgend = &msg[content_length];
126 while (ptr < msgend) {
128 /** Advance to next tag */
129 ptr = strchr(ptr, '<');
130 if ((ptr == NULL) || (ptr >= msgend)) break;
132 if ((ptr == NULL) || (ptr >= msgend)) break;
135 * Look for META tags. Some messages (particularly in
136 * Asian locales) illegally declare a message's character
137 * set in the HTML instead of in the MIME headers. This
138 * is wrong but we have to work around it anyway.
140 if (!strncasecmp(ptr, "META", 4)) {
146 char *meta_http_equiv;
150 meta_start = &ptr[4];
151 meta_end = strchr(ptr, '>');
152 if ((meta_end != NULL) && (meta_end <= msgend)) {
153 meta_length = meta_end - meta_start + 1;
154 meta = malloc(meta_length + 1);
155 safestrncpy(meta, meta_start, meta_length);
156 meta[meta_length] = 0;
158 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
159 meta_http_equiv = strdup(&meta[11]);
160 spaceptr = strchr(meta_http_equiv, ' ');
161 if (spaceptr != NULL) {
163 meta_content = strdup(++spaceptr);
164 if (!strncasecmp(meta_content, "content=", 8)) {
165 strcpy(meta_content, &meta_content[8]);
166 stripquotes(meta_http_equiv);
167 stripquotes(meta_content);
168 extract_charset_from_meta(charset,
169 meta_http_equiv, meta_content);
173 free(meta_http_equiv);
180 * Any of these tags cause everything up to and including
181 * the tag to be removed.
183 if ( (!strncasecmp(ptr, "HTML", 4))
184 ||(!strncasecmp(ptr, "HEAD", 4))
185 ||(!strncasecmp(ptr, "/HEAD", 5))
186 ||(!strncasecmp(ptr, "BODY", 4)) ) {
187 ptr = strchr(ptr, '>');
188 if ((ptr == NULL) || (ptr >= msgend)) break;
190 if ((ptr == NULL) || (ptr >= msgend)) break;
195 * Any of these tags cause everything including and following
196 * the tag to be removed.
198 if ( (!strncasecmp(ptr, "/HTML", 5))
199 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
208 if (msgstart > msg) {
209 strcpy(msg, msgstart);
212 /** Convert foreign character sets to UTF-8 if necessary. */
214 if ( (strcasecmp(charset, "us-ascii"))
215 && (strcasecmp(charset, "UTF-8"))
216 && (strcasecmp(charset, ""))
218 lprintf(9, "Converting %s to UTF-8\n", charset);
219 ic = ctdl_iconv_open("UTF-8", charset);
220 if (ic == (iconv_t)(-1) ) {
221 lprintf(5, "%s:%d iconv_open() failed: %s\n",
222 __FILE__, __LINE__, strerror(errno));
225 if (ic != (iconv_t)(-1) ) {
227 ibuflen = content_length;
228 obuflen = content_length + (content_length / 2) ;
229 obuf = (char *) malloc(obuflen);
231 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
232 content_length = content_length + (content_length / 2) - obuflen;
233 osav[content_length] = 0;
241 * At this point, the message has been stripped down to
242 * only the content inside the <BODY></BODY> tags, and has
243 * been converted to UTF-8 if it was originally in a foreign
244 * character set. The text is also guaranteed to be null
248 /** Now go through the message, parsing tags as necessary. */
249 converted_msg = malloc(content_length);
250 strcpy(converted_msg, "");
252 msgend = strchr(msg, 0);
253 while (ptr < msgend) {
256 * Change mailto: links to WebCit mail, by replacing the
257 * link with one that points back to our mail room. Due to
258 * the way we parse URL's, it'll even handle mailto: links
259 * that have "?subject=" in them.
261 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
262 content_length += 64;
263 converted_msg = realloc(converted_msg, content_length);
264 sprintf(&converted_msg[output_length],
265 "<a href=\"display_enter"
266 "?force_room=_MAIL_&recp=");
271 /** Make external links open in a separate window */
272 else if (!strncasecmp(ptr, "<a href=", 8)) {
274 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
275 && ((strchr(ptr, '/') < strchr(ptr, '>')))
277 /* open external links to new window */
278 content_length += 64;
279 converted_msg = realloc(converted_msg, content_length);
280 sprintf(&converted_msg[output_length], new_window);
281 output_length += strlen(new_window);
284 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
285 content_length += 64;
286 converted_msg = realloc(converted_msg, content_length);
287 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
292 sprintf(&converted_msg[output_length], "<a href=");
298 * Turn anything that looks like a URL into a real link, as long
299 * as it's not inside a tag already
301 else if ( (brak == 0) && (alevel == 0)
302 && (!strncasecmp(ptr, "http://", 7))) {
304 /** Find the end of the link */
305 for (i=0; i<=strlen(ptr); ++i) {
317 if (linklen > 0) break;
320 content_length += (32 + linklen);
321 converted_msg = realloc(converted_msg, content_length);
322 sprintf(&converted_msg[output_length], new_window);
323 output_length += strlen(new_window);
324 converted_msg[output_length] = '\"';
325 converted_msg[++output_length] = 0;
326 for (i=0; i<linklen; ++i) {
327 converted_msg[output_length] = ptr[i];
328 converted_msg[++output_length] = 0;
330 sprintf(&converted_msg[output_length], "\">");
332 for (i=0; i<linklen; ++i) {
333 converted_msg[output_length] = *ptr++;
334 converted_msg[++output_length] = 0;
336 sprintf(&converted_msg[output_length], "</A>");
342 * We need to know when we're inside a tag,
343 * so we don't turn things that look like URL's into
344 * links, when they're already links - or image sources.
346 if (*ptr == '<') ++brak;
347 if (*ptr == '>') --brak;
348 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
349 converted_msg[output_length] = *ptr++;
350 converted_msg[++output_length] = 0;
354 /** uncomment these two lines to override conversion */
355 /** memcpy(converted_msg, msg, content_length); */
356 /** output_length = content_length; */
358 /** Output our big pile of markup */
359 client_write(converted_msg, output_length);
361 /** A little trailing vertical whitespace... */
362 wprintf("<br /><br />\n");
364 /** Now give back the memory */