[PATCH] Converted Charset Fix

Jonathan Houser <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
      This patch is to ensure the encoding= of the <?xml block gets 
changed if the charset is converted for compatability with the handset. 
  This was patched to fix specifically accessing:

    http://tagtag.com/site/wapdir/index.php

      What happens without the patch is that the charset is converted to 
UTF-8 for compatability with the handset, but the encoding= is left as 
ISO-8859-2.  The handset can't handle ISO-8859-2, so it fails to load 
the site.  The patch obviously fixes this.

Jon
charset_convert.diff (text/plain, 5 KB)
Index: gw/wap-appl.c
===================================================================
RCS file: /home/cvs/gateway/gw/wap-appl.c,v
retrieving revision 1.114
diff -u -p -d -r1.114 wap-appl.c
--- gw/wap-appl.c	7 Mar 2005 14:14:21 -0000	1.114
+++ gw/wap-appl.c	14 Apr 2005 13:09:35 -0000
@@ -142,6 +142,7 @@ struct content {
     Octstr *charset;
     Octstr *url;
     Octstr *version;
+    int was_converted;
 };
 
 
@@ -725,6 +726,7 @@ static void return_reply(int status, Oct
     content.url = url;
     content.body = content_body;
     content.version = content.type = content.charset = NULL;
+    content.was_converted = 0;
     server = ua = NULL;
 
     /* Get session machine for this session. If this was a connection-less
@@ -870,7 +872,7 @@ static void return_reply(int status, Oct
                                         octstr_get_cstr(charset), "UTF-8") >= 0) {
                         octstr_destroy(content.charset);
                         content.charset = octstr_create("UTF-8");
-                        /* XXX it might be good idea to change <?xml...encoding?> */
+                        content.was_converted = 1;
                     }
                  }
             }
@@ -890,7 +892,7 @@ static void return_reply(int status, Oct
                                         octstr_get_cstr(charset), "ISO-8859-1") >= 0) {
                         octstr_destroy(content.charset);
                         content.charset = octstr_create("ISO-8859-1");
-                        /* XXX it might be good idea to change <?xml...encoding?> */
+                        content.was_converted = 1;
                     }
                 }
             }
@@ -1315,7 +1317,7 @@ static Octstr *convert_wml_to_wmlc(struc
    
     /* content->charset is passed from the HTTP header parsing */
     ret = wml_compile(content->body, content->charset, &wmlc, 
-                      content->version);
+                      content->version, content->was_converted);
 
     /* wmlc is created implicitely in wml_compile() */
     if (ret == 0)
Index: gw/wap_push_ppg.c
===================================================================
RCS file: /home/cvs/gateway/gw/wap_push_ppg.c,v
retrieving revision 1.68
diff -u -p -d -r1.68 wap_push_ppg.c
--- gw/wap_push_ppg.c	11 Feb 2005 15:35:48 -0000	1.68
+++ gw/wap_push_ppg.c	14 Apr 2005 13:09:35 -0000
@@ -1928,7 +1928,7 @@ static Octstr *convert_wml_to_wmlc(struc
 {
     Octstr *wmlc;
 
-    if (wml_compile(content->body, content->charset, &wmlc, NULL) == 0)
+    if (wml_compile(content->body, content->charset, &wmlc, NULL, 0) == 0)
         return wmlc;
     warning(0, "PPG: wml compilation failed");
     return NULL;
Index: gw/wml_compiler.c
===================================================================
RCS file: /home/cvs/gateway/gw/wml_compiler.c,v
retrieving revision 1.119
diff -u -p -d -r1.119 wml_compiler.c
--- gw/wml_compiler.c	11 Feb 2005 15:35:48 -0000	1.119
+++ gw/wml_compiler.c	14 Apr 2005 13:09:35 -0000
@@ -328,7 +328,7 @@ static void string_table_output(Octstr *
  * For more information, look wml_compiler.h. 
  */
 int wml_compile(Octstr *wml_text, Octstr *charset, Octstr **wml_binary,
-                Octstr *version)
+                Octstr *version, int was_converted)
 {
     int ret = 0;
     size_t size;
@@ -368,7 +368,14 @@ int wml_compile(Octstr *wml_text, Octstr
      * We will trust the xml preamble encoding more then the HTTP header 
      * charset definition.
      */
-    if ((encoding = find_charset_encoding(wml_text)) != NULL) {
+    if (was_converted) {
+        /*
+         * the charset was converted by the upstream caller. we need to
+         * ensure the encoding= block matches the new charset.
+         */
+        encoding = octstr_duplicate(charset);
+    }
+    else if ((encoding = find_charset_encoding(wml_text)) != NULL) {
         /* ok, we rely on the xml preamble encoding */
     } else if (charset && octstr_len(charset) > 0) {
         /* we had a HTTP response charset, use this */
Index: gw/wml_compiler.h
===================================================================
RCS file: /home/cvs/gateway/gw/wml_compiler.h,v
retrieving revision 1.15
diff -u -p -d -r1.15 wml_compiler.h
--- gw/wml_compiler.h	11 Feb 2005 15:35:48 -0000	1.15
+++ gw/wml_compiler.h	14 Apr 2005 13:09:35 -0000
@@ -91,7 +91,8 @@
 int wml_compile(Octstr *wml_text,
 		Octstr *charset,
 		Octstr **wml_binary,
-		Octstr *version);
+		Octstr *version,
+		int was_converted);
 
 /*
  * A function to initialize the wml compiler for use. Allocates memory 
Index: test/wml_tester.c
===================================================================
RCS file: /home/cvs/gateway/test/wml_tester.c,v
retrieving revision 1.26
diff -u -p -d -r1.26 wml_tester.c
--- test/wml_tester.c	11 Feb 2005 15:35:49 -0000	1.26
+++ test/wml_tester.c	14 Apr 2005 13:09:35 -0000
@@ -177,7 +177,7 @@ int main(int argc, char **argv)
 	    set_zero(wml_text);
 
 	for (i = 0; i <= num; i++) {
-	    ret = wml_compile(wml_text, charset, &wml_binary, NULL);
+	    ret = wml_compile(wml_text, charset, &wml_binary, NULL, 0);
 	    if (i < num)
 		octstr_destroy(wml_binary);
 	}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.