httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s.@apache.org
Subject svn commit: r1226509 - in /httpd/mod_mbox/branches/convert-charsets/module-2.0: mod_mbox.h mod_mbox_cte.c mod_mbox_out.c
Date Mon, 02 Jan 2012 18:49:50 GMT
Author: sf
Date: Mon Jan  2 18:49:50 2012
New Revision: 1226509

URL: http://svn.apache.org/viewvc?rev=1226509&view=rev
Log:
Add function to convert to UTF-8 and use it for converting header fields

It makes use of ap_varbuf and therefore needs 2.3.x

Modified:
    httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox.h
    httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_cte.c
    httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_out.c

Modified: httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox.h
URL: http://svn.apache.org/viewvc/httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox.h?rev=1226509&r1=1226508&r2=1226509&view=diff
==============================================================================
--- httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox.h (original)
+++ httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox.h Mon Jan  2 18:49:50 2012
@@ -24,6 +24,7 @@
 #include "http_protocol.h"
 #include "http_request.h"
 #include "util_script.h"
+#include "util_varbuf.h"
 
 #include "apr_date.h"
 #include "apr_strings.h"
@@ -120,8 +121,11 @@ apr_size_t mbox_cte_decode_qp(char *p);
 apr_size_t mbox_cte_decode_b64(char *src);
 apr_size_t mbox_cte_escape_html(apr_pool_t *p, const char *s,
                                 apr_size_t len, char **body);
-char *mbox_cte_decode_rfc2047(apr_pool_t *p, char *src);
+char *mbox_cte_decode_rfc2047(apr_pool_t *p, char *src, struct ap_varbuf *vb);
 char *mbox_cte_decode_header(apr_pool_t *p, char *src);
+apr_status_t mbox_cte_convert_to_utf8(apr_pool_t *p, const char *charset,
+                                      const char *src, apr_size_t len,
+                                      struct ap_varbuf *vb);
 
 /* MIME decoding functions */
 mbox_mime_message_t *mbox_mime_decode_multipart(apr_pool_t *p,

Modified: httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_cte.c
URL: http://svn.apache.org/viewvc/httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_cte.c?rev=1226509&r1=1226508&r2=1226509&view=diff
==============================================================================
--- httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_cte.c (original)
+++ httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_cte.c Mon Jan  2 18:49:50
2012
@@ -244,18 +244,59 @@ apr_size_t mbox_cte_decode_qp(char *p)
     return len;
 }
 
+apr_status_t mbox_cte_convert_to_utf8(apr_pool_t *p, const char *charset,
+                                      const char *src, apr_size_t len,
+                                      struct ap_varbuf *vb)
+{
+    apr_xlate_t *convset;
+    apr_status_t rv = apr_xlate_open(&convset, "UTF-8", charset, p);
+    apr_size_t outbytes_left, inbytes_left = len;
+    char *dst;
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, "could not open convset %s",
charset);
+        return rv;
+    }
+    ap_assert(len > 0);
+
+    while (inbytes_left > 0) {
+        ap_varbuf_grow(vb, vb->strlen + inbytes_left + 8);
+        dst = vb->buf + vb->strlen;
+        outbytes_left = vb->avail - vb->strlen;
+        rv = apr_xlate_conv_buffer(convset, src + len - inbytes_left, &inbytes_left,
+                                   dst, &outbytes_left);
+        if (rv != APR_SUCCESS) {
+            *dst = '\0';
+            goto out;
+        }
+        vb->strlen = vb->avail - outbytes_left;
+    }
+    ap_varbuf_grow(vb, vb->strlen + 8);
+    outbytes_left = vb->avail - vb->strlen;
+    dst = vb->buf + vb->strlen;
+    rv = apr_xlate_conv_buffer(convset, NULL, NULL, dst, &outbytes_left);
+    if (rv != APR_SUCCESS) {
+        *dst = '\0';
+        goto out;
+    }
+    vb->strlen = vb->avail - outbytes_left;
+    vb->buf[vb->strlen] = '\0';
+
+out:
+    apr_xlate_close(convset);
+    return rv;
+}
+
 /* This function performs the decoding of strings like :
  * =?UTF-8?B?QnJhbmtvIMSMaWJlag==?=
  *
  * These strings complies to the following syntax :
  * =?charset?mode?data?= rest
  */
-char *mbox_cte_decode_rfc2047(apr_pool_t *p, char *src)
+char *mbox_cte_decode_rfc2047(apr_pool_t *p, char *src, struct ap_varbuf *vb)
 {
-    apr_xlate_t *xlate;
-
     char *charset, *mode, *data, *rest;
     int i;
+    apr_status_t rv;
 
     /* Look for charset */
     charset = strstr(src, "=?");
@@ -319,29 +360,14 @@ char *mbox_cte_decode_rfc2047(apr_pool_t
     }
 
     /* Charset conversion */
-    if (apr_xlate_open(&xlate, "UTF-8", charset, p) == APR_SUCCESS) {
-        apr_size_t inbytes_left, outbytes_left;
-        apr_size_t outbuf_len = strlen(data);
-
-        char *new_data;
-
-        /* Allocate some memory for our resulting data, and initialize
-           counters. */
-        new_data = apr_palloc(p, outbuf_len);
-        inbytes_left = strlen(data);
-        outbytes_left = strlen(data);
-
-        /* Convert */
-        //      apr_xlate_conv_buffer(xlate, data, &inbytes_left,
-        //                  new_data, &outbytes_left);
-
-        //      new_data[outbuf_len - outbytes_left] = 0;
-        //      data = new_data;
-
-        apr_xlate_close(xlate);
+    vb->strlen = 0;
+    rv = mbox_cte_convert_to_utf8(p, charset, data, strlen(data), vb);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
+                     "conversion from %s to utf-8 failed", charset);
+        return data;
     }
-
-    return data;
+    return vb->buf;
 }
 
 /* MIME header decoding (see RFC 2047). */
@@ -349,6 +375,9 @@ char *mbox_cte_decode_header(apr_pool_t 
 {
     char *start, *end, *part;
     char *result = "";
+    struct ap_varbuf vb;
+    ap_varbuf_init(p, &vb, 100);
+    vb.strlen = 0;
 
     do {
         char c;
@@ -370,7 +399,7 @@ char *mbox_cte_decode_header(apr_pool_t 
         result = apr_psprintf(p, "%s%s", result, src);
         *start = c;
 
-        part = mbox_cte_decode_rfc2047(p, start);
+        part = mbox_cte_decode_rfc2047(p, start, &vb);
         result = apr_psprintf(p, "%s%s", result, part);
 
         src = end + 2;

Modified: httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_out.c
URL: http://svn.apache.org/viewvc/httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_out.c?rev=1226509&r1=1226508&r2=1226509&view=diff
==============================================================================
--- httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_out.c (original)
+++ httpd/mod_mbox/branches/convert-charsets/module-2.0/mod_mbox_out.c Mon Jan  2 18:49:50
2012
@@ -418,22 +418,22 @@ static void display_static_msglist_entry
 {
     mbox_dir_cfg_t *conf;
 
-    char *from;
+    char *tmp;
     int i;
 
     conf = ap_get_module_config(r->per_dir_config, &mbox_module);
 
-    from = ESCAPE_OR_BLANK(r->pool, m->str_from);
-    from = mbox_cte_decode_header(r->pool, from);
-    if (conf->antispam) {
-        from = email_antispam(from);
-    }
-
     /* Message author */
     ap_rputs("   <tr>\n", r);
 
+    tmp = ESCAPE_OR_BLANK(r->pool, m->str_from);
+    tmp = mbox_cte_decode_header(r->pool, tmp);
+    if (conf->antispam) {
+        tmp = email_antispam(tmp);
+    }
+
     if (linked) {
-        ap_rprintf(r, "    <td class=\"author\">%s</td>\n", from);
+        ap_rprintf(r, "    <td class=\"author\">%s</td>\n", tmp);
     }
     else {
         ap_rputs("    <td class=\"author\"></td>\n", r);
@@ -450,7 +450,8 @@ static void display_static_msglist_entry
                    URI_ESCAPE_OR_BLANK(r->pool, m->msgID));
     }
 
-    ap_rprintf(r, "%s", ESCAPE_OR_BLANK(r->pool, m->subject));
+    tmp = mbox_cte_decode_header(r->pool, m->subject);
+    ap_rprintf(r, "%s", ESCAPE_OR_BLANK(r->pool, tmp));
     if (linked) {
         ap_rputs("</a>", r);
     }
@@ -474,7 +475,7 @@ static void display_xml_msglist_entry(re
 {
     mbox_dir_cfg_t *conf;
 
-    char *from;
+    char *from, *subj;
 
     conf = ap_get_module_config(r->per_dir_config, &mbox_module);
 
@@ -482,6 +483,7 @@ static void display_xml_msglist_entry(re
     if (conf->antispam) {
         from = email_antispam(from);
     }
+    subj = mbox_cte_decode_header(r->pool, m->subject);
 
     ap_rprintf(r, " <message linked=\"%d\" depth=\"%d\" id=\"%s\">\n",
                linked, depth, ESCAPE_OR_BLANK(r->pool, m->msgID));
@@ -492,7 +494,7 @@ static void display_xml_msglist_entry(re
                ESCAPE_OR_BLANK(r->pool, m->str_date));
 
     ap_rprintf(r, "  <subject><![CDATA[%s]]></subject>\n",
-               ESCAPE_OR_BLANK(r->pool, m->subject));
+               ESCAPE_OR_BLANK(r->pool, subj));
     ap_rprintf(r, " </message>\n");
 }
 
@@ -1228,8 +1230,7 @@ apr_status_t mbox_xml_message(request_re
 {
     mbox_dir_cfg_t *conf;
     Message *m;
-
-    char *from, *msgID;
+    char *from, *subj, *msgID;
 
     conf = ap_get_module_config(r->per_dir_config, &mbox_module);
 
@@ -1254,6 +1255,7 @@ apr_status_t mbox_xml_message(request_re
         from = email_antispam(from);
     }
     from = ESCAPE_OR_BLANK(r->pool, from);
+    subj = mbox_cte_decode_header(r->pool, m->subject);
 
     ap_rprintf(r, "<mail id=\"%s\">\n"
                " <from><![CDATA[%s]]></from>\n"
@@ -1262,7 +1264,7 @@ apr_status_t mbox_xml_message(request_re
                " <contents><![CDATA[",
                URI_ESCAPE_OR_BLANK(r->pool, m->msgID),
                from,
-               ESCAPE_OR_BLANK(r->pool, m->subject),
+               ESCAPE_OR_BLANK(r->pool, subj),
                ESCAPE_OR_BLANK(r->pool, m->rfc822_date));
 
     ap_rprintf(r, "%s",



Mime
View raw message