Skip to content

Commit d8bddb9

Browse files
committed
In some SAPI (e.g. FastCGI) we don't need to setup and reset libxml callbacks on each request, we con do it only once. Probably the list of such SAPI may be extended.
1 parent caffc1c commit d8bddb9

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

ext/libxml/libxml.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include "php.h"
29+
#include "SAPI.h"
2930

3031
#define PHP_XML_INTERNAL
3132
#include "zend_variables.h"
@@ -53,6 +54,7 @@
5354

5455
/* a true global for initialization */
5556
static int _php_libxml_initialized = 0;
57+
static int _php_libxml_per_request_initialization = 1;
5658

5759
typedef struct _php_libxml_func_handler {
5860
php_libxml_export_node export_func;
@@ -636,22 +638,55 @@ static PHP_MINIT_FUNCTION(libxml)
636638
INIT_CLASS_ENTRY(ce, "LibXMLError", NULL);
637639
libxmlerror_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
638640

641+
if (sapi_module.name) {
642+
static const char * const supported_sapis[] = {
643+
"cgi-fcgi",
644+
"fpm-fcgi",
645+
"litespeed",
646+
NULL
647+
};
648+
const char * const *sapi_name;
649+
650+
for (sapi_name = supported_sapis; *sapi_name; sapi_name++) {
651+
if (strcmp(sapi_module.name, *sapi_name) == 0) {
652+
_php_libxml_per_request_initialization = 0;
653+
break;
654+
}
655+
}
656+
}
657+
658+
if (!_php_libxml_per_request_initialization) {
659+
/* report errors via handler rather than stderr */
660+
xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
661+
xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
662+
xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
663+
}
664+
639665
return SUCCESS;
640666
}
641667

642668

643669
static PHP_RINIT_FUNCTION(libxml)
644670
{
645-
/* report errors via handler rather than stderr */
646-
xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
647-
xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
648-
xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
671+
if (_php_libxml_per_request_initialization) {
672+
/* report errors via handler rather than stderr */
673+
xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
674+
xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
675+
xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
676+
}
649677
return SUCCESS;
650678
}
651679

652680

653681
static PHP_MSHUTDOWN_FUNCTION(libxml)
654682
{
683+
if (!_php_libxml_per_request_initialization) {
684+
xmlSetGenericErrorFunc(NULL, NULL);
685+
xmlSetStructuredErrorFunc(NULL, NULL);
686+
687+
xmlParserInputBufferCreateFilenameDefault(NULL);
688+
xmlOutputBufferCreateFilenameDefault(NULL);
689+
}
655690
php_libxml_shutdown();
656691

657692
return SUCCESS;
@@ -661,11 +696,13 @@ static PHP_MSHUTDOWN_FUNCTION(libxml)
661696
static PHP_RSHUTDOWN_FUNCTION(libxml)
662697
{
663698
/* reset libxml generic error handling */
664-
xmlSetGenericErrorFunc(NULL, NULL);
665-
xmlSetStructuredErrorFunc(NULL, NULL);
699+
if (_php_libxml_per_request_initialization) {
700+
xmlSetGenericErrorFunc(NULL, NULL);
701+
xmlSetStructuredErrorFunc(NULL, NULL);
666702

667-
xmlParserInputBufferCreateFilenameDefault(NULL);
668-
xmlOutputBufferCreateFilenameDefault(NULL);
703+
xmlParserInputBufferCreateFilenameDefault(NULL);
704+
xmlOutputBufferCreateFilenameDefault(NULL);
705+
}
669706

670707
if (LIBXML(stream_context)) {
671708
zval_ptr_dtor(&LIBXML(stream_context));

0 commit comments

Comments
 (0)