dmitry Wed, 14 Jul 2010 15:20:44 +
Revision: http://svn.php.net/viewvc?view=revision&revision=301263
Log:
Optimized defaut Content-Type HTTP header processing
Changed paths:
U php/php-src/trunk/main/SAPI.c
U php/php-src/trunk/main/main.c
Modified: php/php-src/trunk/main/SAPI.c
===
--- php/php-src/trunk/main/SAPI.c 2010-07-14 10:27:08 UTC (rev 301262)
+++ php/php-src/trunk/main/SAPI.c 2010-07-14 15:20:44 UTC (rev 301263)
@@ -224,35 +224,61 @@
}
-SAPI_API char *sapi_get_default_content_type(TSRMLS_D)
+static inline char *get_default_content_type(uint prefix_len, uint *len
TSRMLS_DC)
{
char *mimetype, *charset, *content_type;
+ uint mimetype_len, charset_len;
- mimetype = SG(default_mimetype) ? SG(default_mimetype) :
SAPI_DEFAULT_MIMETYPE;
- charset = SG(default_charset) ? SG(default_charset) :
SAPI_DEFAULT_CHARSET;
+ if (SG(default_mimetype)) {
+ mimetype = SG(default_mimetype);
+ mimetype_len = strlen(SG(default_mimetype));
+ } else {
+ mimetype = SAPI_DEFAULT_MIMETYPE;
+ mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1;
+ }
+ if (SG(default_charset)) {
+ charset = SG(default_charset);
+ charset_len = strlen(SG(default_charset));
+ } else {
+ charset = SAPI_DEFAULT_CHARSET;
+ charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1;
+ }
- if (strncasecmp(mimetype, "text/", 5) == 0 && *charset) {
- int len = strlen(mimetype) + sizeof("; charset=") +
strlen(charset); /* sizeof() includes \0 */
- content_type = emalloc(len);
- snprintf(content_type, len, "%s; charset=%s", mimetype,
charset);
+ if (*charset && strncasecmp(mimetype, "text/", 5) == 0) {
+ char *p;
+
+ *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 +
charset_len;
+ content_type = (char*)emalloc(*len + 1);
+ p = content_type + prefix_len;
+ memcpy(p, mimetype, mimetype_len);
+ p += mimetype_len;
+ memcpy(p, "; charset=", sizeof("; charset=") - 1);
+ p += sizeof("; charset=") - 1;
+ memcpy(p, charset, charset_len + 1);
} else {
- content_type = estrdup(mimetype);
+ *len = prefix_len + mimetype_len;
+ content_type = (char*)emalloc(*len + 1);
+ memcpy(content_type + prefix_len, mimetype, mimetype_len + 1);
}
return content_type;
}
+SAPI_API char *sapi_get_default_content_type(TSRMLS_D)
+{
+ uint len;
+
+ return get_default_content_type(0, &len TSRMLS_CC);
+}
+
+
SAPI_API void sapi_get_default_content_type_header(sapi_header_struct
*default_header TSRMLS_DC)
{
- char *default_content_type = sapi_get_default_content_type(TSRMLS_C);
- int default_content_type_len = strlen(default_content_type);
+uint len;
- default_header->header_len = (sizeof("Content-type: ")-1) +
default_content_type_len;
- default_header->header = emalloc(default_header->header_len+1);
- memcpy(default_header->header, "Content-type: ", sizeof("Content-type:
"));
- memcpy(default_header->header+sizeof("Content-type: ")-1,
default_content_type, default_content_type_len);
- default_header->header[default_header->header_len] = 0;
- efree(default_content_type);
+ default_header->header = get_default_content_type(sizeof("Content-type:
")-1, &len TSRMLS_CC);
+ default_header->header_len = len;
+ memcpy(default_header->header, "Content-type: ", sizeof("Content-type:
") - 1);
}
/*
@@ -521,6 +547,27 @@
return r;
}
+static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct
*sapi_header TSRMLS_DC)
+{
+ if (!sapi_module.header_handler ||
+ (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op,
&SG(sapi_headers) TSRMLS_CC))) {
+ if (op == SAPI_HEADER_REPLACE) {
+ char *colon_offset = strchr(sapi_header->header, ':');
+
+ if (colon_offset) {
+ char sav = *colon_offset;
+
+ *colon_offset = 0;
+
zend_llist_del_element(&SG(sapi_headers).headers, sapi_header->header,
(int(*)(void*, void*))sapi_find_matching_header);
+ *colon_offset = sav;
+ }
+ }
+ zend_llist_add_element(&SG(sapi_headers).headers, (void *)
sapi_header);
+ } else {
+ sapi_free_header(sapi_header);
+ }
+}
+
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg TSRMLS_DC)
{
int retval;
@@ -577,8 +624,12 @@
header_line = estrndup(header_line, h