kopug memo

名古屋で働くとあるWebエンジニアの覚書。

DoCoMoでxhtml(MIME TYPE application/xhtml+xml)使用時でもmb_output_handlerを働くようにする

これ大分昔にPHPのソースに独自パッチを当てて対応したのですが、覚書として残しておきます。

1. まずはPHP本体を解凍し、オリジナルのソースをコピーしておく

$ tar zxvf php-5.2.9.tar.gz
$ cp php-5.2.9/ext/mbstring/mbstring.c php-5.2.9/ext/mbstring/mbstring_org.c

2. mbstring.cを修正する

$ vim php-5.2.9/ext/mbstring/mbstring.c

before

/* analyze mime type */
if (SG(sapi_headers).mimetype &&
    strncmp(SG(sapi_headers).mimetype, "text/", 5) == 0) {
    if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){
        mimetype = estrdup(SG(sapi_headers).mimetype);
    } else {
        mimetype = estrndup(SG(sapi_headers).mimetype,s-SG(sapi_headers).mimetype);
    }
    send_text_mimetype = 1;
} else if (SG(sapi_headers).send_default_content_type) {
    mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
}

after

/* analyze mime type */
if (SG(sapi_headers).mimetype &&
    (strncmp(SG(sapi_headers).mimetype, "text/", 5) == 0 || strncmp(SG(sapi_headers).mimetype, "application/xhtml+xml", 21) == 0)) {
    if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){
        mimetype = estrdup(SG(sapi_headers).mimetype);
    } else {
        mimetype = estrndup(SG(sapi_headers).mimetype,s-SG(sapi_headers).mimetype);
    }
    send_text_mimetype = 1;
} else if (SG(sapi_headers).send_default_content_type) {
    mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
}

PHPマニュアルにも書いてある通り、MIMEが"text/"の時しか動かないようになっているので、ここのif文に"application/xhtml+xml"を足してあげるだけです。

3. patchの作成

$ diff -u php-5.2.9/ext/mbstring/mbstring_org.c php-5.2.9/ext/mbstring/mbstring.c > php-5.2.9-mbstring.patch

といった具合でpatchが完成です。

--- php-5.2.9/ext/mbstring/mbstring_org.c       2009-03-22 01:45:25.000000000 +0900
+++ php-5.2.9/ext/mbstring/mbstring.c   2009-03-22 01:46:51.000000000 +0900
@@ -1526,7 +1526,7 @@

                /* analyze mime type */
                if (SG(sapi_headers).mimetype &&
-                       strncmp(SG(sapi_headers).mimetype, "text/", 5) == 0) {
+                       (strncmp(SG(sapi_headers).mimetype, "text/", 5) == 0 || strncmp(SG(sapi_headers).mimetype, "application/xhtml+xml", 21) == 0)) {
                        if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){
                                mimetype = estrdup(SG(sapi_headers).mimetype);
                        } else {

うちはRPMで全て管理をしているので、この後SPECファイルにpatchを追加し、rpmbuid -ba してバイナリとソースRPMを作成しています。