Skip to content

Commit b650bd9

Browse files
committed
Factor out HTML document creation from stream to separate function
1 parent 0537968 commit b650bd9

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

ext/dom/html_document.c

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -994,34 +994,17 @@ PHP_METHOD(Dom_HTMLDocument, createFromString)
994994
RETURN_THROWS();
995995
}
996996

997-
PHP_METHOD(Dom_HTMLDocument, createFromFile)
997+
static void dom_html_document_create_from_stream(
998+
zval *return_value,
999+
php_stream *stream,
1000+
zend_long options,
1001+
const char *override_encoding,
1002+
size_t override_encoding_len,
1003+
zend_string *opened_path,
1004+
const char *filename
1005+
)
9981006
{
999-
const char *filename, *override_encoding = NULL;
10001007
php_dom_private_data *private_data = NULL;
1001-
size_t filename_len, override_encoding_len;
1002-
zend_long options = 0;
1003-
php_stream *stream = NULL;
1004-
if (zend_parse_parameters(
1005-
ZEND_NUM_ARGS(),
1006-
"p|lp!",
1007-
&filename,
1008-
&filename_len,
1009-
&options,
1010-
&override_encoding,
1011-
&override_encoding_len
1012-
) == FAILURE) {
1013-
RETURN_THROWS();
1014-
}
1015-
1016-
/* See php_libxml_streams_IO_open_wrapper(), apparently this caused issues in the past. */
1017-
if (strstr(filename, "%00")) {
1018-
zend_argument_value_error(1, "must not contain percent-encoded NUL bytes");
1019-
RETURN_THROWS();
1020-
}
1021-
1022-
if (!check_options_validity(2, options)) {
1023-
RETURN_THROWS();
1024-
}
10251008

10261009
dom_lexbor_libxml2_bridge_application_data application_data;
10271010
application_data.input_name = filename;
@@ -1058,15 +1041,6 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
10581041
dom_setup_parser_encoding_manually((const lxb_char_t *) buf, encoding_data, &decoding_encoding_ctx, &application_data);
10591042
}
10601043

1061-
zend_string *opened_path = NULL;
1062-
stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, &opened_path, php_libxml_get_stream_context());
1063-
if (!stream) {
1064-
if (!EG(exception)) {
1065-
zend_throw_exception_ex(NULL, 0, "Cannot open file '%s'", filename);
1066-
}
1067-
RETURN_THROWS();
1068-
}
1069-
10701044
/* MIME sniff */
10711045
if (should_determine_encoding_implicitly) {
10721046
zend_string *charset = php_libxml_sniff_charset_from_stream(stream);
@@ -1192,12 +1166,6 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
11921166
lxml_doc->URL = xmlStrdup((const xmlChar *) filename);
11931167
}
11941168

1195-
if (opened_path != NULL) {
1196-
zend_string_release_ex(opened_path, false);
1197-
}
1198-
php_stream_close(stream);
1199-
stream = NULL;
1200-
12011169
dom_object *intern = php_dom_instantiate_object_helper(
12021170
return_value,
12031171
dom_html_document_class_entry,
@@ -1216,10 +1184,52 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
12161184
php_dom_private_data_destroy(private_data);
12171185
}
12181186
lxb_html_document_destroy(document);
1219-
php_stream_close(stream);
1187+
}
1188+
1189+
PHP_METHOD(Dom_HTMLDocument, createFromFile)
1190+
{
1191+
const char *filename, *override_encoding = NULL;
1192+
size_t filename_len, override_encoding_len;
1193+
zend_long options = 0;
1194+
if (zend_parse_parameters(
1195+
ZEND_NUM_ARGS(),
1196+
"p|lp!",
1197+
&filename,
1198+
&filename_len,
1199+
&options,
1200+
&override_encoding,
1201+
&override_encoding_len
1202+
) == FAILURE) {
1203+
RETURN_THROWS();
1204+
}
1205+
1206+
/* See php_libxml_streams_IO_open_wrapper(), apparently this caused issues in the past. */
1207+
if (strstr(filename, "%00")) {
1208+
zend_argument_value_error(1, "must not contain percent-encoded NUL bytes");
1209+
RETURN_THROWS();
1210+
}
1211+
1212+
if (!check_options_validity(2, options)) {
1213+
RETURN_THROWS();
1214+
}
1215+
1216+
zend_string *opened_path = NULL;
1217+
php_stream *stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, &opened_path, php_libxml_get_stream_context());
1218+
if (!stream) {
1219+
if (!EG(exception)) {
1220+
zend_throw_exception_ex(NULL, 0, "Cannot open file '%s'", filename);
1221+
}
1222+
RETURN_THROWS();
1223+
}
1224+
1225+
dom_html_document_create_from_stream(
1226+
return_value, stream, options, override_encoding, override_encoding_len, opened_path, filename
1227+
);
1228+
12201229
if (opened_path != NULL) {
12211230
zend_string_release_ex(opened_path, false);
12221231
}
1232+
php_stream_close(stream);
12231233
}
12241234

12251235
static zend_result dom_write_output_smart_str(void *ctx, const char *buf, size_t size)

0 commit comments

Comments
 (0)