From fbbeaec63038ec44cc2cebe4726019dec245e259 Mon Sep 17 00:00:00 2001 From: Rui Hirokawa Date: Sat, 27 Jul 2002 13:58:16 +0000 Subject: [PATCH] fixed: output encoding translation by mb_output_handler() in ext/mbstring was not usable when Content-Type is set by header(). --- ext/mbstring/mbstring.c | 24 +++++++++++++++++++++--- main/SAPI.c | 7 +++++++ main/SAPI.h | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 1ab727123f..1dd682db32 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -1728,10 +1728,12 @@ PHP_FUNCTION(mb_output_handler) size_t arg_string_len; long arg_status; mbfl_string string, result; - const char *mimetype, *charset; + const char *charset; char *p; enum mbfl_no_encoding encoding; int last_feed, len; + unsigned char send_text_mimetype = 0; + char *s, *mimetype = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &arg_string, &arg_string_len, &arg_status) == FAILURE) { WRONG_PARAM_COUNT; @@ -1749,9 +1751,22 @@ PHP_FUNCTION(mb_output_handler) if (encoding == mbfl_no_encoding_pass) { RETURN_STRINGL(arg_string, arg_string_len, 1); } - /* if content-type is not yet set, set it and activate the converter */ - if (SG(sapi_headers).send_default_content_type ) { + + /* 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; + } + + /* if content-type is not yet set, set it and activate the converter */ + if (SG(sapi_headers).send_default_content_type || send_text_mimetype) { charset = mbfl_no2preferred_mime_name(encoding); if (charset) { len = (sizeof ("Content-Type:")-1) + strlen(mimetype) + (sizeof (";charset=")-1) + strlen(charset) + 1; @@ -1765,6 +1780,9 @@ PHP_FUNCTION(mb_output_handler) } /* activate the converter */ MBSTRG(outconv) = mbfl_buffer_converter_new(MBSTRG(current_internal_encoding), encoding, 0 TSRMLS_CC); + if (send_text_mimetype){ + efree(mimetype); + } } } diff --git a/main/SAPI.c b/main/SAPI.c index 1aa931db60..8f53ff8539 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -490,6 +490,10 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg TSRMLS_DC) } mimetype = estrdup(ptr); newlen = sapi_apply_default_charset(&mimetype, len TSRMLS_CC); + if (!SG(sapi_headers).mimetype){ + SG(sapi_headers).mimetype = estrdup(mimetype); + } + if (newlen != 0) { newlen += sizeof("Content-type: "); newheader = emalloc(newlen); @@ -677,6 +681,9 @@ SAPI_API int sapi_send_headers(TSRMLS_D) if (SG(sapi_headers).http_status_line) { efree(SG(sapi_headers).http_status_line); } + if (SG(sapi_headers).mimetype) { + efree(SG(sapi_headers).mimetype); + } return ret; } diff --git a/main/SAPI.h b/main/SAPI.h index dd2753d9cd..2610ae7cb0 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -51,6 +51,7 @@ typedef struct { zend_llist headers; int http_response_code; unsigned char send_default_content_type; + char *mimetype; char *http_status_line; } sapi_headers_struct; -- 2.40.0