Skip to content

Commit d162401

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

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
@@ -964,34 +964,17 @@ PHP_METHOD(Dom_HTMLDocument, createFromString)
964964
RETURN_THROWS();
965965
}
966966

967-
PHP_METHOD(Dom_HTMLDocument, createFromFile)
967+
static void dom_html_document_create_from_stream(
968+
zval *return_value,
969+
php_stream *stream,
970+
zend_long options,
971+
const char *override_encoding,
972+
size_t override_encoding_len,
973+
zend_string *opened_path,
974+
const char *filename
975+
)
968976
{
969-
const char *filename, *override_encoding = NULL;
970977
php_dom_private_data *private_data = NULL;
971-
size_t filename_len, override_encoding_len;
972-
zend_long options = 0;
973-
php_stream *stream = NULL;
974-
if (zend_parse_parameters(
975-
ZEND_NUM_ARGS(),
976-
"p|lp!",
977-
&filename,
978-
&filename_len,
979-
&options,
980-
&override_encoding,
981-
&override_encoding_len
982-
) == FAILURE) {
983-
RETURN_THROWS();
984-
}
985-
986-
/* See php_libxml_streams_IO_open_wrapper(), apparently this caused issues in the past. */
987-
if (strstr(filename, "%00")) {
988-
zend_argument_value_error(1, "must not contain percent-encoded NUL bytes");
989-
RETURN_THROWS();
990-
}
991-
992-
if (!check_options_validity(2, options)) {
993-
RETURN_THROWS();
994-
}
995978

996979
dom_lexbor_libxml2_bridge_application_data application_data;
997980
application_data.input_name = filename;
@@ -1028,15 +1011,6 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
10281011
dom_setup_parser_encoding_manually((const lxb_char_t *) buf, encoding_data, &decoding_encoding_ctx, &application_data);
10291012
}
10301013

1031-
zend_string *opened_path = NULL;
1032-
stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, &opened_path, php_libxml_get_stream_context());
1033-
if (!stream) {
1034-
if (!EG(exception)) {
1035-
zend_throw_exception_ex(NULL, 0, "Cannot open file '%s'", filename);
1036-
}
1037-
RETURN_THROWS();
1038-
}
1039-
10401014
/* MIME sniff */
10411015
if (should_determine_encoding_implicitly) {
10421016
zend_string *charset = php_libxml_sniff_charset_from_stream(stream);
@@ -1162,12 +1136,6 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
11621136
lxml_doc->URL = xmlStrdup((const xmlChar *) filename);
11631137
}
11641138

1165-
if (opened_path != NULL) {
1166-
zend_string_release_ex(opened_path, false);
1167-
}
1168-
php_stream_close(stream);
1169-
stream = NULL;
1170-
11711139
dom_object *intern = php_dom_instantiate_object_helper(
11721140
return_value,
11731141
dom_html_document_class_entry,
@@ -1186,10 +1154,52 @@ PHP_METHOD(Dom_HTMLDocument, createFromFile)
11861154
php_dom_private_data_destroy(private_data);
11871155
}
11881156
lxb_html_document_destroy(document);
1189-
php_stream_close(stream);
1157+
}
1158+
1159+
PHP_METHOD(Dom_HTMLDocument, createFromFile)
1160+
{
1161+
const char *filename, *override_encoding = NULL;
1162+
size_t filename_len, override_encoding_len;
1163+
zend_long options = 0;
1164+
if (zend_parse_parameters(
1165+
ZEND_NUM_ARGS(),
1166+
"p|lp!",
1167+
&filename,
1168+
&filename_len,
1169+
&options,
1170+
&override_encoding,
1171+
&override_encoding_len
1172+
) == FAILURE) {
1173+
RETURN_THROWS();
1174+
}
1175+
1176+
/* See php_libxml_streams_IO_open_wrapper(), apparently this caused issues in the past. */
1177+
if (strstr(filename, "%00")) {
1178+
zend_argument_value_error(1, "must not contain percent-encoded NUL bytes");
1179+
RETURN_THROWS();
1180+
}
1181+
1182+
if (!check_options_validity(2, options)) {
1183+
RETURN_THROWS();
1184+
}
1185+
1186+
zend_string *opened_path = NULL;
1187+
php_stream *stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, &opened_path, php_libxml_get_stream_context());
1188+
if (!stream) {
1189+
if (!EG(exception)) {
1190+
zend_throw_exception_ex(NULL, 0, "Cannot open file '%s'", filename);
1191+
}
1192+
RETURN_THROWS();
1193+
}
1194+
1195+
dom_html_document_create_from_stream(
1196+
return_value, stream, options, override_encoding, override_encoding_len, opened_path, filename
1197+
);
1198+
11901199
if (opened_path != NULL) {
11911200
zend_string_release_ex(opened_path, false);
11921201
}
1202+
php_stream_close(stream);
11931203
}
11941204

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

0 commit comments

Comments
 (0)