/src/php-src/ext/uri/php_uri.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Máté Kocsis <kocsismate@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #ifdef HAVE_CONFIG_H |
16 | | # include <config.h> |
17 | | #endif |
18 | | |
19 | | #include "php.h" |
20 | | #include "Zend/zend_interfaces.h" |
21 | | #include "Zend/zend_exceptions.h" |
22 | | #include "Zend/zend_attributes.h" |
23 | | #include "Zend/zend_enum.h" |
24 | | #include "ext/standard/info.h" |
25 | | |
26 | | #include "php_uri.h" |
27 | | #include "uri_parser_whatwg.h" |
28 | | #include "uri_parser_rfc3986.h" |
29 | | #include "uri_parser_php_parse_url.h" |
30 | | #include "php_uri_arginfo.h" |
31 | | #include "uriparser/Uri.h" |
32 | | |
33 | | zend_class_entry *php_uri_ce_rfc3986_uri; |
34 | | zend_class_entry *php_uri_ce_rfc3986_uri_type; |
35 | | zend_class_entry *php_uri_ce_rfc3986_uri_host_type; |
36 | | zend_class_entry *php_uri_ce_whatwg_url; |
37 | | zend_class_entry *php_uri_ce_comparison_mode; |
38 | | zend_class_entry *php_uri_ce_exception; |
39 | | zend_class_entry *php_uri_ce_error; |
40 | | zend_class_entry *php_uri_ce_invalid_uri_exception; |
41 | | zend_class_entry *php_uri_ce_whatwg_url_host_type; |
42 | | zend_class_entry *php_uri_ce_whatwg_invalid_url_exception; |
43 | | zend_class_entry *php_uri_ce_whatwg_url_validation_error_type; |
44 | | zend_class_entry *php_uri_ce_whatwg_url_validation_error; |
45 | | |
46 | | static zend_object_handlers object_handlers_rfc3986_uri; |
47 | | static zend_object_handlers object_handlers_whatwg_uri; |
48 | | |
49 | | static const zend_module_dep uri_deps[] = { |
50 | | ZEND_MOD_REQUIRED("lexbor") |
51 | | ZEND_MOD_END |
52 | | }; |
53 | | |
54 | | static zend_array uri_parsers; |
55 | | |
56 | | static HashTable *uri_get_debug_properties(php_uri_object *object) |
57 | 0 | { |
58 | 0 | const HashTable *std_properties = zend_std_get_properties(&object->std); |
59 | 0 | HashTable *result = zend_array_dup(std_properties); |
60 | |
|
61 | 0 | const php_uri_parser * const parser = object->parser; |
62 | 0 | void * const uri = object->uri; |
63 | |
|
64 | 0 | if (UNEXPECTED(uri == NULL)) { |
65 | 0 | return result; |
66 | 0 | } |
67 | | |
68 | 0 | zval tmp; |
69 | 0 | if (parser->property_handler.scheme.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
70 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_SCHEME), &tmp); |
71 | 0 | } |
72 | |
|
73 | 0 | if (parser->property_handler.username.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
74 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_USERNAME), &tmp); |
75 | 0 | } |
76 | |
|
77 | 0 | if (parser->property_handler.password.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
78 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PASSWORD), &tmp); |
79 | 0 | } |
80 | |
|
81 | 0 | if (parser->property_handler.host.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
82 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_HOST), &tmp); |
83 | 0 | } |
84 | |
|
85 | 0 | if (parser->property_handler.port.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
86 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PORT), &tmp); |
87 | 0 | } |
88 | |
|
89 | 0 | if (parser->property_handler.path.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
90 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PATH), &tmp); |
91 | 0 | } |
92 | |
|
93 | 0 | if (parser->property_handler.query.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
94 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_QUERY), &tmp); |
95 | 0 | } |
96 | |
|
97 | 0 | if (parser->property_handler.fragment.read(uri, PHP_URI_COMPONENT_READ_MODE_RAW, &tmp) == SUCCESS) { |
98 | 0 | zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_FRAGMENT), &tmp); |
99 | 0 | } |
100 | |
|
101 | 0 | return result; |
102 | 0 | } |
103 | | |
104 | | PHPAPI const php_uri_parser *php_uri_get_parser(zend_string *uri_parser_name) |
105 | 0 | { |
106 | 0 | if (uri_parser_name == NULL) { |
107 | 0 | return zend_hash_str_find_ptr(&uri_parsers, PHP_URI_PARSER_PHP_PARSE_URL, sizeof(PHP_URI_PARSER_PHP_PARSE_URL) - 1); |
108 | 0 | } |
109 | | |
110 | 0 | return zend_hash_find_ptr(&uri_parsers, uri_parser_name); |
111 | 0 | } |
112 | | |
113 | | ZEND_ATTRIBUTE_NONNULL PHPAPI php_uri_internal *php_uri_parse(const php_uri_parser *uri_parser, const char *uri_str, size_t uri_str_len, bool silent) |
114 | 0 | { |
115 | 0 | void *uri = uri_parser->parse(uri_str, uri_str_len, NULL, NULL, silent); |
116 | |
|
117 | 0 | if (uri == NULL) { |
118 | 0 | return NULL; |
119 | 0 | } |
120 | | |
121 | 0 | php_uri_internal *internal_uri = emalloc(sizeof(*internal_uri)); |
122 | 0 | internal_uri->parser = uri_parser; |
123 | 0 | internal_uri->uri = uri; |
124 | |
|
125 | 0 | return internal_uri; |
126 | 0 | } |
127 | | |
128 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_scheme(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
129 | 0 | { |
130 | 0 | return internal_uri->parser->property_handler.scheme.read(internal_uri->uri, read_mode, zv); |
131 | 0 | } |
132 | | |
133 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_username(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
134 | 0 | { |
135 | 0 | return internal_uri->parser->property_handler.username.read(internal_uri->uri, read_mode, zv); |
136 | 0 | } |
137 | | |
138 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_password(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
139 | 0 | { |
140 | 0 | return internal_uri->parser->property_handler.password.read(internal_uri->uri, read_mode, zv); |
141 | 0 | } |
142 | | |
143 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_host(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
144 | 0 | { |
145 | 0 | return internal_uri->parser->property_handler.host.read(internal_uri->uri, read_mode, zv); |
146 | 0 | } |
147 | | |
148 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_port(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
149 | 0 | { |
150 | 0 | return internal_uri->parser->property_handler.port.read(internal_uri->uri, read_mode, zv); |
151 | 0 | } |
152 | | |
153 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_path(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
154 | 0 | { |
155 | 0 | return internal_uri->parser->property_handler.path.read(internal_uri->uri, read_mode, zv); |
156 | 0 | } |
157 | | |
158 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_query(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
159 | 0 | { |
160 | 0 | return internal_uri->parser->property_handler.query.read(internal_uri->uri, read_mode, zv); |
161 | 0 | } |
162 | | |
163 | | ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_fragment(const php_uri_internal *internal_uri, php_uri_component_read_mode read_mode, zval *zv) |
164 | 0 | { |
165 | 0 | return internal_uri->parser->property_handler.fragment.read(internal_uri->uri, read_mode, zv); |
166 | 0 | } |
167 | | |
168 | | ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_free(php_uri_internal *internal_uri) |
169 | 0 | { |
170 | 0 | internal_uri->parser->destroy(internal_uri->uri); |
171 | 0 | internal_uri->uri = NULL; |
172 | 0 | internal_uri->parser = NULL; |
173 | 0 | efree(internal_uri); |
174 | 0 | } |
175 | | |
176 | | ZEND_ATTRIBUTE_NONNULL PHPAPI php_uri *php_uri_parse_to_struct( |
177 | | const php_uri_parser *uri_parser, const char *uri_str, size_t uri_str_len, php_uri_component_read_mode read_mode, bool silent |
178 | 0 | ) { |
179 | 0 | php_uri_internal *uri_internal = php_uri_parse(uri_parser, uri_str, uri_str_len, silent); |
180 | 0 | if (uri_internal == NULL) { |
181 | 0 | return NULL; |
182 | 0 | } |
183 | | |
184 | 0 | php_uri *uri = ecalloc(1, sizeof(*uri)); |
185 | 0 | zval tmp; |
186 | 0 | zend_result result; |
187 | |
|
188 | 0 | result = php_uri_get_scheme(uri_internal, read_mode, &tmp); |
189 | 0 | if (result == FAILURE) { |
190 | 0 | goto error; |
191 | 0 | } |
192 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
193 | 0 | uri->scheme = Z_STR(tmp); |
194 | 0 | } |
195 | |
|
196 | 0 | result = php_uri_get_username(uri_internal, read_mode, &tmp); |
197 | 0 | if (result == FAILURE) { |
198 | 0 | goto error; |
199 | 0 | } |
200 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
201 | 0 | uri->user = Z_STR(tmp); |
202 | 0 | } |
203 | |
|
204 | 0 | result = php_uri_get_password(uri_internal, read_mode, &tmp); |
205 | 0 | if (result == FAILURE) { |
206 | 0 | goto error; |
207 | 0 | } |
208 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
209 | 0 | uri->password = Z_STR(tmp); |
210 | 0 | } |
211 | |
|
212 | 0 | result = php_uri_get_host(uri_internal, read_mode, &tmp); |
213 | 0 | if (result == FAILURE) { |
214 | 0 | goto error; |
215 | 0 | } |
216 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
217 | 0 | uri->host = Z_STR(tmp); |
218 | 0 | } |
219 | |
|
220 | 0 | result = php_uri_get_port(uri_internal, read_mode, &tmp); |
221 | 0 | if (result == FAILURE) { |
222 | 0 | goto error; |
223 | 0 | } |
224 | 0 | if (Z_TYPE(tmp) == IS_LONG) { |
225 | 0 | uri->port = Z_LVAL(tmp); |
226 | 0 | } |
227 | |
|
228 | 0 | result = php_uri_get_path(uri_internal, read_mode, &tmp); |
229 | 0 | if (result == FAILURE) { |
230 | 0 | goto error; |
231 | 0 | } |
232 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
233 | 0 | uri->path = Z_STR(tmp); |
234 | 0 | } |
235 | |
|
236 | 0 | result = php_uri_get_query(uri_internal, read_mode, &tmp); |
237 | 0 | if (result == FAILURE) { |
238 | 0 | goto error; |
239 | 0 | } |
240 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
241 | 0 | uri->query = Z_STR(tmp); |
242 | 0 | } |
243 | |
|
244 | 0 | result = php_uri_get_fragment(uri_internal, read_mode, &tmp); |
245 | 0 | if (result == FAILURE) { |
246 | 0 | goto error; |
247 | 0 | } |
248 | 0 | if (Z_TYPE(tmp) == IS_STRING) { |
249 | 0 | uri->fragment = Z_STR(tmp); |
250 | 0 | } |
251 | |
|
252 | 0 | php_uri_free(uri_internal); |
253 | |
|
254 | 0 | return uri; |
255 | | |
256 | 0 | error: |
257 | 0 | php_uri_free(uri_internal); |
258 | 0 | php_uri_struct_free(uri); |
259 | |
|
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | | ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_struct_free(php_uri *uri) |
264 | 0 | { |
265 | 0 | if (uri->scheme) { |
266 | 0 | zend_string_release(uri->scheme); |
267 | 0 | } |
268 | 0 | if (uri->user) { |
269 | 0 | zend_string_release(uri->user); |
270 | 0 | } |
271 | 0 | if (uri->password) { |
272 | 0 | zend_string_release(uri->password); |
273 | 0 | } |
274 | 0 | if (uri->host) { |
275 | 0 | zend_string_release(uri->host); |
276 | 0 | } |
277 | 0 | if (uri->path) { |
278 | 0 | zend_string_release(uri->path); |
279 | 0 | } |
280 | 0 | if (uri->query) { |
281 | 0 | zend_string_release(uri->query); |
282 | 0 | } |
283 | 0 | if (uri->fragment) { |
284 | 0 | zend_string_release(uri->fragment); |
285 | 0 | } |
286 | |
|
287 | 0 | efree(uri); |
288 | 0 | } |
289 | | |
290 | | /** |
291 | | * Pass the errors parameter by ref to errors_zv for userland, and frees it if |
292 | | * it is not not needed anymore. |
293 | | */ |
294 | | static zend_result pass_errors_by_ref_and_free(zval *errors_zv, zval *errors) |
295 | 0 | { |
296 | 0 | ZEND_ASSERT(Z_TYPE_P(errors) == IS_UNDEF || Z_TYPE_P(errors) == IS_ARRAY); |
297 | | |
298 | | /* There was no error during parsing */ |
299 | 0 | if (Z_ISUNDEF_P(errors)) { |
300 | 0 | return SUCCESS; |
301 | 0 | } |
302 | | |
303 | | /* The errors parameter is an array, but the pass-by ref argument stored by |
304 | | * errors_zv was not passed - the URI implementation either doesn't support |
305 | | * returning additional error information, or the caller is not interested in it */ |
306 | 0 | if (errors_zv == NULL) { |
307 | 0 | zval_ptr_dtor(errors); |
308 | 0 | return SUCCESS; |
309 | 0 | } |
310 | | |
311 | 0 | ZEND_TRY_ASSIGN_REF_TMP(errors_zv, errors); |
312 | 0 | if (EG(exception)) { |
313 | 0 | return FAILURE; |
314 | 0 | } |
315 | | |
316 | 0 | return SUCCESS; |
317 | 0 | } |
318 | | |
319 | | ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri( |
320 | | INTERNAL_FUNCTION_PARAMETERS, const zend_string *uri_str, const php_uri_object *base_url_object, |
321 | | bool should_throw, bool should_update_this_object, zval *errors_zv |
322 | 0 | ) { |
323 | |
|
324 | 0 | php_uri_object *uri_object; |
325 | 0 | if (should_update_this_object) { |
326 | 0 | uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
327 | 0 | if (uri_object->uri != NULL) { |
328 | 0 | zend_throw_error(NULL, "Cannot modify readonly object of class %s", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name)); |
329 | 0 | RETURN_THROWS(); |
330 | 0 | } |
331 | 0 | } else { |
332 | 0 | if (EX(func)->common.fn_flags & ZEND_ACC_STATIC) { |
333 | 0 | object_init_ex(return_value, Z_CE_P(ZEND_THIS)); |
334 | 0 | } else { |
335 | 0 | object_init_ex(return_value, Z_OBJCE_P(ZEND_THIS)); |
336 | 0 | } |
337 | 0 | uri_object = Z_URI_OBJECT_P(return_value); |
338 | 0 | } |
339 | | |
340 | 0 | const php_uri_parser *uri_parser = uri_object->parser; |
341 | |
|
342 | 0 | zval errors; |
343 | 0 | ZVAL_UNDEF(&errors); |
344 | |
|
345 | 0 | void *base_url = NULL; |
346 | 0 | if (base_url_object != NULL) { |
347 | 0 | ZEND_ASSERT(base_url_object->std.ce == uri_object->std.ce); |
348 | 0 | ZEND_ASSERT(base_url_object->uri != NULL); |
349 | 0 | ZEND_ASSERT(base_url_object->parser == uri_parser); |
350 | 0 | base_url = base_url_object->uri; |
351 | 0 | } |
352 | |
|
353 | 0 | void *uri = uri_parser->parse(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), base_url, errors_zv != NULL ? &errors : NULL, !should_throw); |
354 | 0 | if (UNEXPECTED(uri == NULL)) { |
355 | 0 | if (should_throw) { |
356 | 0 | zval_ptr_dtor(&errors); |
357 | 0 | RETURN_THROWS(); |
358 | 0 | } else { |
359 | 0 | if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) { |
360 | 0 | RETURN_THROWS(); |
361 | 0 | } |
362 | 0 | zval_ptr_dtor(return_value); |
363 | 0 | RETURN_NULL(); |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | 0 | if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) { |
368 | 0 | uri_parser->destroy(uri); |
369 | 0 | RETURN_THROWS(); |
370 | 0 | } |
371 | | |
372 | 0 | uri_object->uri = uri; |
373 | 0 | } |
374 | | |
375 | | static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor) |
376 | 2 | { |
377 | 2 | zend_string *uri_str; |
378 | 2 | zend_object *base_url_object = NULL; |
379 | | |
380 | 4 | ZEND_PARSE_PARAMETERS_START(1, 2) |
381 | 4 | Z_PARAM_STR(uri_str) |
382 | 0 | Z_PARAM_OPTIONAL |
383 | 0 | Z_PARAM_OBJ_OF_CLASS_OR_NULL(base_url_object, php_uri_ce_rfc3986_uri) |
384 | 2 | ZEND_PARSE_PARAMETERS_END(); |
385 | | |
386 | 0 | php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, |
387 | 0 | uri_str, base_url_object ? php_uri_object_from_obj(base_url_object) : NULL, is_constructor, is_constructor, NULL); |
388 | 0 | } |
389 | | |
390 | | static bool is_list_of_whatwg_validation_errors(const HashTable *array) |
391 | 0 | { |
392 | 0 | if (!zend_array_is_list(array)) { |
393 | 0 | return false; |
394 | 0 | } |
395 | | |
396 | 0 | ZEND_HASH_FOREACH_VAL(array, zval *val) { |
397 | | /* Do not allow references as they may change types after checking. */ |
398 | |
|
399 | 0 | if (Z_TYPE_P(val) != IS_OBJECT) { |
400 | 0 | return false; |
401 | 0 | } |
402 | | |
403 | 0 | if (!instanceof_function(Z_OBJCE_P(val), php_uri_ce_whatwg_url_validation_error)) { |
404 | 0 | return false; |
405 | 0 | } |
406 | 0 | } ZEND_HASH_FOREACH_END(); |
407 | | |
408 | 0 | return true; |
409 | 0 | } |
410 | | |
411 | | PHP_METHOD(Uri_Rfc3986_Uri, parse) |
412 | 0 | { |
413 | 0 | create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false); |
414 | 0 | } |
415 | | |
416 | | PHP_METHOD(Uri_Rfc3986_Uri, __construct) |
417 | 2 | { |
418 | 2 | create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, true); |
419 | 2 | } |
420 | | |
421 | | PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct) |
422 | 2 | { |
423 | 2 | zend_string *message = NULL; |
424 | 2 | zval *errors = NULL; |
425 | 2 | zend_long code = 0; |
426 | 2 | zval *previous = NULL; |
427 | | |
428 | 6 | ZEND_PARSE_PARAMETERS_START(0, 4) |
429 | 6 | Z_PARAM_OPTIONAL |
430 | 6 | Z_PARAM_STR(message) |
431 | 0 | Z_PARAM_ARRAY(errors) |
432 | 0 | Z_PARAM_LONG(code) |
433 | 0 | Z_PARAM_OBJECT_OF_CLASS_OR_NULL(previous, zend_ce_throwable) |
434 | 2 | ZEND_PARSE_PARAMETERS_END(); |
435 | | |
436 | 2 | if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) { |
437 | 0 | RETURN_THROWS(); |
438 | 0 | } |
439 | | |
440 | 2 | if (errors == NULL) { |
441 | 2 | zval tmp; |
442 | 2 | ZVAL_EMPTY_ARRAY(&tmp); |
443 | 2 | zend_update_property(php_uri_ce_whatwg_invalid_url_exception, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp); |
444 | 2 | } else { |
445 | 0 | if (!is_list_of_whatwg_validation_errors(Z_ARR_P(errors))) { |
446 | 0 | zend_argument_value_error(2, "must be a list of %s", ZSTR_VAL(php_uri_ce_whatwg_url_validation_error->name)); |
447 | 0 | RETURN_THROWS(); |
448 | 0 | } |
449 | | |
450 | 0 | zend_update_property(php_uri_ce_whatwg_invalid_url_exception, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), errors); |
451 | 0 | } |
452 | 2 | if (EG(exception)) { |
453 | 0 | RETURN_THROWS(); |
454 | 0 | } |
455 | 2 | } |
456 | | |
457 | | PHP_METHOD(Uri_WhatWg_UrlValidationError, __construct) |
458 | 2 | { |
459 | 2 | zend_string *context; |
460 | 2 | zval *type; |
461 | 2 | bool failure; |
462 | | |
463 | 4 | ZEND_PARSE_PARAMETERS_START(3, 3) |
464 | 4 | Z_PARAM_STR(context) |
465 | 0 | Z_PARAM_OBJECT_OF_CLASS(type, php_uri_ce_whatwg_url_validation_error_type) |
466 | 0 | Z_PARAM_BOOL(failure) |
467 | 2 | ZEND_PARSE_PARAMETERS_END(); |
468 | | |
469 | 0 | zend_update_property_str(php_uri_ce_whatwg_url_validation_error, Z_OBJ_P(ZEND_THIS), ZEND_STRL("context"), context); |
470 | 0 | if (EG(exception)) { |
471 | 0 | RETURN_THROWS(); |
472 | 0 | } |
473 | | |
474 | 0 | zend_update_property_ex(php_uri_ce_whatwg_url_validation_error, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_TYPE), type); |
475 | 0 | if (EG(exception)) { |
476 | 0 | RETURN_THROWS(); |
477 | 0 | } |
478 | | |
479 | 0 | zval failure_zv; |
480 | 0 | ZVAL_BOOL(&failure_zv, failure); |
481 | 0 | zend_update_property(php_uri_ce_whatwg_url_validation_error, Z_OBJ_P(ZEND_THIS), ZEND_STRL("failure"), &failure_zv); |
482 | 0 | if (EG(exception)) { |
483 | 0 | RETURN_THROWS(); |
484 | 0 | } |
485 | 0 | } |
486 | | |
487 | | static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor) |
488 | 2 | { |
489 | 2 | zend_string *uri_str; |
490 | 2 | zend_object *base_url_object = NULL; |
491 | 2 | zval *errors = NULL; |
492 | | |
493 | 4 | ZEND_PARSE_PARAMETERS_START(1, 3) |
494 | 4 | Z_PARAM_STR(uri_str) |
495 | 0 | Z_PARAM_OPTIONAL |
496 | 0 | Z_PARAM_OBJ_OF_CLASS_OR_NULL(base_url_object, php_uri_ce_whatwg_url) |
497 | 0 | Z_PARAM_ZVAL(errors) |
498 | 2 | ZEND_PARSE_PARAMETERS_END(); |
499 | | |
500 | 0 | php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, |
501 | 0 | uri_str, base_url_object ? php_uri_object_from_obj(base_url_object) : NULL, is_constructor, is_constructor, errors); |
502 | 0 | } |
503 | | |
504 | | PHP_METHOD(Uri_WhatWg_Url, parse) |
505 | 0 | { |
506 | 0 | create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false); |
507 | 0 | } |
508 | | |
509 | | PHP_METHOD(Uri_WhatWg_Url, __construct) |
510 | 2 | { |
511 | 2 | create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, true); |
512 | 2 | } |
513 | | |
514 | | PHP_METHOD(Uri_Rfc3986_Uri, getUriType) |
515 | 0 | { |
516 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
517 | | |
518 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
519 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
520 | |
|
521 | 0 | php_uri_parser_rfc3986_uri_type_read(uri_object->uri, return_value); |
522 | 0 | } |
523 | | |
524 | | PHP_METHOD(Uri_Rfc3986_Uri, getScheme) |
525 | 0 | { |
526 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
527 | 0 | } |
528 | | |
529 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawScheme) |
530 | 0 | { |
531 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME, PHP_URI_COMPONENT_READ_MODE_RAW); |
532 | 0 | } |
533 | | |
534 | | PHP_METHOD(Uri_Rfc3986_Uri, withScheme) |
535 | 0 | { |
536 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME); |
537 | 0 | } |
538 | | |
539 | | static void rfc3986_userinfo_read(INTERNAL_FUNCTION_PARAMETERS, php_uri_component_read_mode read_mode) |
540 | 0 | { |
541 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
542 | | |
543 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
544 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
545 | |
|
546 | 0 | if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_read(uri_object->uri, read_mode, return_value) == FAILURE)) { |
547 | 0 | zend_throw_error(NULL, "The userinfo component cannot be retrieved"); |
548 | 0 | RETURN_THROWS(); |
549 | 0 | } |
550 | 0 | } |
551 | | |
552 | | PHP_METHOD(Uri_Rfc3986_Uri, getUserInfo) |
553 | 0 | { |
554 | 0 | rfc3986_userinfo_read(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
555 | 0 | } |
556 | | |
557 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawUserInfo) |
558 | 0 | { |
559 | 0 | rfc3986_userinfo_read(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_COMPONENT_READ_MODE_RAW); |
560 | 0 | } |
561 | | |
562 | | PHP_METHOD(Uri_Rfc3986_Uri, withUserInfo) |
563 | 0 | { |
564 | 0 | zend_string *value; |
565 | |
|
566 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
567 | 0 | Z_PARAM_STR_OR_NULL(value) |
568 | 0 | ZEND_PARSE_PARAMETERS_END(); |
569 | | |
570 | 0 | zval zv; |
571 | 0 | if (value == NULL) { |
572 | 0 | ZVAL_NULL(&zv); |
573 | 0 | } else { |
574 | 0 | ZVAL_STR(&zv, value); |
575 | 0 | } |
576 | |
|
577 | 0 | php_uri_object *old_uri_object = php_uri_object_from_obj(Z_OBJ_P(ZEND_THIS)); |
578 | 0 | ZEND_ASSERT(old_uri_object->uri != NULL); |
579 | |
|
580 | 0 | zend_object *new_object = old_uri_object->std.handlers->clone_obj(&old_uri_object->std); |
581 | 0 | if (new_object == NULL) { |
582 | 0 | RETURN_THROWS(); |
583 | 0 | } |
584 | | |
585 | | /* Assign the object early. The engine will take care of destruction in |
586 | | * case of an exception being thrown. */ |
587 | 0 | RETVAL_OBJ(new_object); |
588 | |
|
589 | 0 | php_uri_object *new_uri_object = php_uri_object_from_obj(new_object); |
590 | 0 | ZEND_ASSERT(new_uri_object->uri != NULL); |
591 | |
|
592 | 0 | if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_write(new_uri_object->uri, &zv, NULL) == FAILURE)) { |
593 | 0 | RETURN_THROWS(); |
594 | 0 | } |
595 | 0 | } |
596 | | |
597 | | PHP_METHOD(Uri_Rfc3986_Uri, getUsername) |
598 | 0 | { |
599 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_USERNAME, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
600 | 0 | } |
601 | | |
602 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawUsername) |
603 | 0 | { |
604 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_USERNAME, PHP_URI_COMPONENT_READ_MODE_RAW); |
605 | 0 | } |
606 | | |
607 | | PHP_METHOD(Uri_Rfc3986_Uri, getPassword) |
608 | 0 | { |
609 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PASSWORD, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
610 | 0 | } |
611 | | |
612 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawPassword) |
613 | 0 | { |
614 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PASSWORD, PHP_URI_COMPONENT_READ_MODE_RAW); |
615 | 0 | } |
616 | | |
617 | | PHP_METHOD(Uri_Rfc3986_Uri, getHost) |
618 | 0 | { |
619 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
620 | 0 | } |
621 | | |
622 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawHost) |
623 | 0 | { |
624 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_RAW); |
625 | 0 | } |
626 | | |
627 | | PHP_METHOD(Uri_Rfc3986_Uri, getHostType) |
628 | 0 | { |
629 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
630 | | |
631 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
632 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
633 | |
|
634 | 0 | php_uri_parser_rfc3986_host_type_read(uri_object->uri, return_value); |
635 | 0 | } |
636 | | |
637 | | PHP_METHOD(Uri_Rfc3986_Uri, withHost) |
638 | 0 | { |
639 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST); |
640 | 0 | } |
641 | | |
642 | | PHP_METHOD(Uri_Rfc3986_Uri, getPort) |
643 | 0 | { |
644 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PORT, PHP_URI_COMPONENT_READ_MODE_RAW); |
645 | 0 | } |
646 | | |
647 | | PHP_METHOD(Uri_Rfc3986_Uri, withPort) |
648 | 0 | { |
649 | 0 | php_uri_property_write_long_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PORT); |
650 | 0 | } |
651 | | |
652 | | PHP_METHOD(Uri_Rfc3986_Uri, getPath) |
653 | 0 | { |
654 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PATH, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
655 | 0 | } |
656 | | |
657 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawPath) |
658 | 0 | { |
659 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PATH, PHP_URI_COMPONENT_READ_MODE_RAW); |
660 | 0 | } |
661 | | |
662 | | PHP_METHOD(Uri_Rfc3986_Uri, withPath) |
663 | 0 | { |
664 | 0 | php_uri_property_write_str_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PATH); |
665 | 0 | } |
666 | | |
667 | | PHP_METHOD(Uri_Rfc3986_Uri, getQuery) |
668 | 0 | { |
669 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
670 | 0 | } |
671 | | |
672 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawQuery) |
673 | 0 | { |
674 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, PHP_URI_COMPONENT_READ_MODE_RAW); |
675 | 0 | } |
676 | | |
677 | | PHP_METHOD(Uri_Rfc3986_Uri, withQuery) |
678 | 0 | { |
679 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY); |
680 | 0 | } |
681 | | |
682 | | PHP_METHOD(Uri_Rfc3986_Uri, getFragment) |
683 | 0 | { |
684 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
685 | 0 | } |
686 | | |
687 | | PHP_METHOD(Uri_Rfc3986_Uri, getRawFragment) |
688 | 0 | { |
689 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT, PHP_URI_COMPONENT_READ_MODE_RAW); |
690 | 0 | } |
691 | | |
692 | | PHP_METHOD(Uri_Rfc3986_Uri, withFragment) |
693 | 0 | { |
694 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT); |
695 | 0 | } |
696 | | |
697 | | static void throw_cannot_recompose_uri_to_string(php_uri_object *object) |
698 | 0 | { |
699 | 0 | zend_throw_exception_ex(php_uri_ce_error, 0, "Cannot recompose %s to a string", ZSTR_VAL(object->std.ce->name)); |
700 | 0 | } |
701 | | |
702 | | static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, php_uri_object *that_object, zend_enum_Uri_UriComparisonMode comparison_mode) |
703 | 0 | { |
704 | 0 | php_uri_object *this_object = Z_URI_OBJECT_P(ZEND_THIS); |
705 | 0 | ZEND_ASSERT(this_object->uri != NULL); |
706 | 0 | ZEND_ASSERT(that_object->uri != NULL); |
707 | |
|
708 | 0 | if (this_object->std.ce != that_object->std.ce && |
709 | 0 | !instanceof_function(this_object->std.ce, that_object->std.ce) && |
710 | 0 | !instanceof_function(that_object->std.ce, this_object->std.ce) |
711 | 0 | ) { |
712 | 0 | RETURN_FALSE; |
713 | 0 | } |
714 | | |
715 | 0 | bool exclude_fragment = comparison_mode == ZEND_ENUM_Uri_UriComparisonMode_ExcludeFragment; |
716 | |
|
717 | 0 | zend_string *this_str = this_object->parser->to_string( |
718 | 0 | this_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment); |
719 | 0 | if (this_str == NULL) { |
720 | 0 | throw_cannot_recompose_uri_to_string(this_object); |
721 | 0 | RETURN_THROWS(); |
722 | 0 | } |
723 | | |
724 | 0 | zend_string *that_str = that_object->parser->to_string( |
725 | 0 | that_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment); |
726 | 0 | if (that_str == NULL) { |
727 | 0 | zend_string_release(this_str); |
728 | 0 | throw_cannot_recompose_uri_to_string(that_object); |
729 | 0 | RETURN_THROWS(); |
730 | 0 | } |
731 | | |
732 | 0 | RETVAL_BOOL(zend_string_equals(this_str, that_str)); |
733 | |
|
734 | 0 | zend_string_release(this_str); |
735 | 0 | zend_string_release(that_str); |
736 | 0 | } |
737 | | |
738 | | PHP_METHOD(Uri_Rfc3986_Uri, equals) |
739 | 0 | { |
740 | 0 | zend_object *that_object; |
741 | 0 | zend_enum_Uri_UriComparisonMode comparison_mode = ZEND_ENUM_Uri_UriComparisonMode_ExcludeFragment; |
742 | |
|
743 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
744 | 0 | Z_PARAM_OBJ_OF_CLASS(that_object, php_uri_ce_rfc3986_uri) |
745 | 0 | Z_PARAM_OPTIONAL |
746 | 0 | Z_PARAM_ENUM(comparison_mode, php_uri_ce_comparison_mode) |
747 | 0 | ZEND_PARSE_PARAMETERS_END(); |
748 | | |
749 | 0 | uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_uri_object_from_obj(that_object), comparison_mode); |
750 | 0 | } |
751 | | |
752 | | PHP_METHOD(Uri_Rfc3986_Uri, toRawString) |
753 | 0 | { |
754 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
755 | | |
756 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
757 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
758 | |
|
759 | 0 | zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false); |
760 | 0 | if (uri_str == NULL) { |
761 | 0 | throw_cannot_recompose_uri_to_string(uri_object); |
762 | 0 | RETURN_THROWS(); |
763 | 0 | } |
764 | | |
765 | 0 | RETURN_STR(uri_str); |
766 | 0 | } |
767 | | |
768 | | PHP_METHOD(Uri_Rfc3986_Uri, toString) |
769 | 0 | { |
770 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
771 | | |
772 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
773 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
774 | |
|
775 | 0 | zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, false); |
776 | 0 | if (uri_str == NULL) { |
777 | 0 | throw_cannot_recompose_uri_to_string(uri_object); |
778 | 0 | RETURN_THROWS(); |
779 | 0 | } |
780 | | |
781 | 0 | RETURN_STR(uri_str); |
782 | 0 | } |
783 | | |
784 | | PHP_METHOD(Uri_Rfc3986_Uri, resolve) |
785 | 0 | { |
786 | 0 | zend_string *uri_str; |
787 | |
|
788 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
789 | 0 | Z_PARAM_STR(uri_str) |
790 | 0 | ZEND_PARSE_PARAMETERS_END(); |
791 | | |
792 | 0 | php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, |
793 | 0 | uri_str, Z_URI_OBJECT_P(ZEND_THIS), true, false, NULL); |
794 | 0 | } |
795 | | |
796 | | PHP_METHOD(Uri_Rfc3986_Uri, __serialize) |
797 | 0 | { |
798 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
799 | | |
800 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
801 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
802 | | |
803 | | /* Serialize state: "uri" key in the first array */ |
804 | 0 | zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false); |
805 | 0 | if (uri_str == NULL) { |
806 | 0 | throw_cannot_recompose_uri_to_string(uri_object); |
807 | 0 | RETURN_THROWS(); |
808 | 0 | } |
809 | 0 | zval tmp; |
810 | 0 | ZVAL_STR(&tmp, uri_str); |
811 | |
|
812 | 0 | array_init(return_value); |
813 | |
|
814 | 0 | zval arr; |
815 | 0 | array_init(&arr); |
816 | 0 | zend_hash_str_add_new(Z_ARRVAL(arr), PHP_URI_SERIALIZE_URI_FIELD_NAME, sizeof(PHP_URI_SERIALIZE_URI_FIELD_NAME) - 1, &tmp); |
817 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); |
818 | | |
819 | | /* Serialize regular properties: second array */ |
820 | 0 | ZVAL_EMPTY_ARRAY(&arr); |
821 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); |
822 | 0 | } |
823 | | |
824 | | static void uri_unserialize(INTERNAL_FUNCTION_PARAMETERS) |
825 | 0 | { |
826 | 0 | HashTable *data; |
827 | |
|
828 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
829 | 0 | Z_PARAM_ARRAY_HT(data) |
830 | 0 | ZEND_PARSE_PARAMETERS_END(); |
831 | | |
832 | 0 | php_uri_object *uri_object = php_uri_object_from_obj(Z_OBJ_P(ZEND_THIS)); |
833 | 0 | if (uri_object->uri != NULL) { |
834 | | /* Intentionally throw two exceptions for proper chaining. */ |
835 | 0 | zend_throw_error(NULL, "Cannot modify readonly object of class %s", ZSTR_VAL(uri_object->std.ce->name)); |
836 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
837 | 0 | RETURN_THROWS(); |
838 | 0 | } |
839 | | |
840 | | /* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */ |
841 | 0 | if (zend_hash_num_elements(data) != 2) { |
842 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
843 | 0 | RETURN_THROWS(); |
844 | 0 | } |
845 | | |
846 | | /* Unserialize state: "uri" key in the first array */ |
847 | 0 | zval *arr = zend_hash_index_find(data, 0); |
848 | 0 | if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) { |
849 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
850 | 0 | RETURN_THROWS(); |
851 | 0 | } |
852 | | |
853 | | /* Verify the expected number of elements inside the first array, this implicitly ensures that no additional elements are present. */ |
854 | 0 | if (zend_hash_num_elements(Z_ARRVAL_P(arr)) != 1) { |
855 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
856 | 0 | RETURN_THROWS(); |
857 | 0 | } |
858 | | |
859 | 0 | zval *uri_zv = zend_hash_str_find(Z_ARRVAL_P(arr), ZEND_STRL(PHP_URI_SERIALIZE_URI_FIELD_NAME)); |
860 | 0 | if (uri_zv == NULL || Z_TYPE_P(uri_zv) != IS_STRING) { |
861 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
862 | 0 | RETURN_THROWS(); |
863 | 0 | } |
864 | | |
865 | 0 | uri_object->uri = uri_object->parser->parse(Z_STRVAL_P(uri_zv), Z_STRLEN_P(uri_zv), NULL, NULL, true); |
866 | 0 | if (uri_object->uri == NULL) { |
867 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
868 | 0 | RETURN_THROWS(); |
869 | 0 | } |
870 | | |
871 | | /* Unserialize regular properties: second array */ |
872 | 0 | arr = zend_hash_index_find(data, 1); |
873 | 0 | if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) { |
874 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
875 | 0 | RETURN_THROWS(); |
876 | 0 | } |
877 | | |
878 | | /* Verify that there is no regular property in the second array, because the URI classes have no properties and they are final. */ |
879 | 0 | if (zend_hash_num_elements(Z_ARRVAL_P(arr)) > 0) { |
880 | 0 | zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name)); |
881 | 0 | RETURN_THROWS(); |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | | PHP_METHOD(Uri_Rfc3986_Uri, __unserialize) |
886 | 0 | { |
887 | 0 | uri_unserialize(INTERNAL_FUNCTION_PARAM_PASSTHRU); |
888 | 0 | } |
889 | | |
890 | | PHP_METHOD(Uri_Rfc3986_Uri, __debugInfo) |
891 | 0 | { |
892 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
893 | | |
894 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
895 | |
|
896 | 0 | RETURN_ARR(uri_get_debug_properties(uri_object)); |
897 | 0 | } |
898 | | |
899 | | PHP_METHOD(Uri_WhatWg_Url, getScheme) |
900 | 0 | { |
901 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
902 | 0 | } |
903 | | |
904 | | PHP_METHOD(Uri_WhatWg_Url, withScheme) |
905 | 0 | { |
906 | 0 | php_uri_property_write_str_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME); |
907 | 0 | } |
908 | | |
909 | | PHP_METHOD(Uri_WhatWg_Url, isSpecialScheme) |
910 | 0 | { |
911 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
912 | | |
913 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
914 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
915 | |
|
916 | 0 | RETVAL_BOOL(php_uri_parser_whatwg_is_special(uri_object->uri)); |
917 | 0 | } |
918 | | |
919 | | PHP_METHOD(Uri_WhatWg_Url, withUsername) |
920 | 0 | { |
921 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_USERNAME); |
922 | 0 | } |
923 | | |
924 | | PHP_METHOD(Uri_WhatWg_Url, withPassword) |
925 | 0 | { |
926 | 0 | php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_PASSWORD); |
927 | 0 | } |
928 | | |
929 | | PHP_METHOD(Uri_WhatWg_Url, getAsciiHost) |
930 | 0 | { |
931 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII); |
932 | 0 | } |
933 | | |
934 | | PHP_METHOD(Uri_WhatWg_Url, getUnicodeHost) |
935 | 0 | { |
936 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE); |
937 | 0 | } |
938 | | |
939 | | PHP_METHOD(Uri_WhatWg_Url, getHostType) |
940 | 0 | { |
941 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
942 | | |
943 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
944 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
945 | |
|
946 | 0 | php_uri_parser_whatwg_host_type_read(uri_object->uri, return_value); |
947 | 0 | } |
948 | | |
949 | | PHP_METHOD(Uri_WhatWg_Url, getFragment) |
950 | 0 | { |
951 | 0 | php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE); |
952 | 0 | } |
953 | | |
954 | | PHP_METHOD(Uri_WhatWg_Url, equals) |
955 | 0 | { |
956 | 0 | zend_object *that_object; |
957 | 0 | zend_enum_Uri_UriComparisonMode comparison_mode = ZEND_ENUM_Uri_UriComparisonMode_ExcludeFragment; |
958 | |
|
959 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
960 | 0 | Z_PARAM_OBJ_OF_CLASS(that_object, php_uri_ce_whatwg_url) |
961 | 0 | Z_PARAM_OPTIONAL |
962 | 0 | Z_PARAM_ENUM(comparison_mode, php_uri_ce_comparison_mode) |
963 | 0 | ZEND_PARSE_PARAMETERS_END(); |
964 | | |
965 | 0 | uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_uri_object_from_obj(that_object), comparison_mode); |
966 | 0 | } |
967 | | |
968 | | PHP_METHOD(Uri_WhatWg_Url, toUnicodeString) |
969 | 0 | { |
970 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
971 | | |
972 | 0 | zend_object *this_object = Z_OBJ_P(ZEND_THIS); |
973 | 0 | php_uri_object *uri_object = php_uri_object_from_obj(this_object); |
974 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
975 | |
|
976 | 0 | RETURN_STR(uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE, false)); |
977 | 0 | } |
978 | | |
979 | | PHP_METHOD(Uri_WhatWg_Url, toAsciiString) |
980 | 0 | { |
981 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
982 | | |
983 | 0 | zend_object *this_object = Z_OBJ_P(ZEND_THIS); |
984 | 0 | php_uri_object *uri_object = php_uri_object_from_obj(this_object); |
985 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
986 | |
|
987 | 0 | RETURN_STR(uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false)); |
988 | 0 | } |
989 | | |
990 | | PHP_METHOD(Uri_WhatWg_Url, resolve) |
991 | 0 | { |
992 | 0 | zend_string *uri_str; |
993 | 0 | zval *errors = NULL; |
994 | |
|
995 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
996 | 0 | Z_PARAM_STR(uri_str) |
997 | 0 | Z_PARAM_OPTIONAL |
998 | 0 | Z_PARAM_ZVAL(errors) |
999 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1000 | | |
1001 | 0 | php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, |
1002 | 0 | uri_str, Z_URI_OBJECT_P(ZEND_THIS), true, false, errors); |
1003 | 0 | } |
1004 | | |
1005 | | PHP_METHOD(Uri_WhatWg_Url, __serialize) |
1006 | 0 | { |
1007 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1008 | | |
1009 | 0 | php_uri_object *this_object = Z_URI_OBJECT_P(ZEND_THIS); |
1010 | 0 | ZEND_ASSERT(this_object->uri != NULL); |
1011 | | |
1012 | | /* Serialize state: "uri" key in the first array */ |
1013 | 0 | zend_string *uri_str = this_object->parser->to_string(this_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false); |
1014 | 0 | if (uri_str == NULL) { |
1015 | 0 | throw_cannot_recompose_uri_to_string(this_object); |
1016 | 0 | RETURN_THROWS(); |
1017 | 0 | } |
1018 | 0 | zval tmp; |
1019 | 0 | ZVAL_STR(&tmp, uri_str); |
1020 | |
|
1021 | 0 | array_init(return_value); |
1022 | |
|
1023 | 0 | zval arr; |
1024 | 0 | array_init(&arr); |
1025 | 0 | zend_hash_str_add_new(Z_ARRVAL(arr), ZEND_STRL(PHP_URI_SERIALIZE_URI_FIELD_NAME), &tmp); |
1026 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); |
1027 | | |
1028 | | /* Serialize regular properties: second array */ |
1029 | 0 | ZVAL_EMPTY_ARRAY(&arr); |
1030 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); |
1031 | 0 | } |
1032 | | |
1033 | | PHP_METHOD(Uri_WhatWg_Url, __unserialize) |
1034 | 0 | { |
1035 | 0 | uri_unserialize(INTERNAL_FUNCTION_PARAM_PASSTHRU); |
1036 | 0 | } |
1037 | | |
1038 | | PHP_METHOD(Uri_WhatWg_Url, __debugInfo) |
1039 | 0 | { |
1040 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1041 | | |
1042 | 0 | php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS); |
1043 | |
|
1044 | 0 | RETURN_ARR(uri_get_debug_properties(uri_object)); |
1045 | 0 | } |
1046 | | |
1047 | | PHPAPI php_uri_object *php_uri_object_create(zend_class_entry *class_type, const php_uri_parser *parser) |
1048 | 4 | { |
1049 | 4 | php_uri_object *uri_object = zend_object_alloc(sizeof(*uri_object), class_type); |
1050 | | |
1051 | 4 | zend_object_std_init(&uri_object->std, class_type); |
1052 | 4 | object_properties_init(&uri_object->std, class_type); |
1053 | | |
1054 | 4 | uri_object->parser = parser; |
1055 | 4 | uri_object->uri = NULL; |
1056 | | |
1057 | 4 | return uri_object; |
1058 | 4 | } |
1059 | | |
1060 | | static zend_object *php_uri_object_create_rfc3986(zend_class_entry *ce) |
1061 | 2 | { |
1062 | 2 | return &php_uri_object_create(ce, &php_uri_parser_rfc3986)->std; |
1063 | 2 | } |
1064 | | |
1065 | | static zend_object *php_uri_object_create_whatwg(zend_class_entry *ce) |
1066 | 2 | { |
1067 | 2 | return &php_uri_object_create(ce, &php_uri_parser_whatwg)->std; |
1068 | 2 | } |
1069 | | |
1070 | | PHPAPI void php_uri_object_handler_free(zend_object *object) |
1071 | 4 | { |
1072 | 4 | php_uri_object *uri_object = php_uri_object_from_obj(object); |
1073 | | |
1074 | 4 | uri_object->parser->destroy(uri_object->uri); |
1075 | 4 | zend_object_std_dtor(&uri_object->std); |
1076 | 4 | } |
1077 | | |
1078 | | PHPAPI zend_object *php_uri_object_handler_clone(zend_object *object) |
1079 | 0 | { |
1080 | 0 | const php_uri_object *uri_object = php_uri_object_from_obj(object); |
1081 | |
|
1082 | 0 | ZEND_ASSERT(uri_object->uri != NULL); |
1083 | |
|
1084 | 0 | php_uri_object *new_uri_object = php_uri_object_from_obj(object->ce->create_object(object->ce)); |
1085 | 0 | ZEND_ASSERT(new_uri_object->parser == uri_object->parser); |
1086 | |
|
1087 | 0 | void *uri = uri_object->parser->clone(uri_object->uri); |
1088 | 0 | ZEND_ASSERT(uri != NULL); |
1089 | |
|
1090 | 0 | new_uri_object->uri = uri; |
1091 | |
|
1092 | 0 | zend_objects_clone_members(&new_uri_object->std, &uri_object->std); |
1093 | |
|
1094 | 0 | return &new_uri_object->std; |
1095 | 0 | } |
1096 | | |
1097 | | PHPAPI zend_result php_uri_parser_register(const php_uri_parser *uri_parser) |
1098 | 6 | { |
1099 | 6 | zend_string *key = zend_string_init_interned(uri_parser->name, strlen(uri_parser->name), true); |
1100 | | |
1101 | 6 | ZEND_ASSERT(uri_parser->name != NULL); |
1102 | 6 | ZEND_ASSERT(uri_parser->parse != NULL); |
1103 | 6 | ZEND_ASSERT(uri_parser->clone != NULL || strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0); |
1104 | 6 | ZEND_ASSERT(uri_parser->to_string != NULL || strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0); |
1105 | 6 | ZEND_ASSERT(uri_parser->destroy != NULL); |
1106 | | |
1107 | 6 | zend_result result = zend_hash_add_ptr(&uri_parsers, key, (void *) uri_parser) != NULL ? SUCCESS : FAILURE; |
1108 | | |
1109 | 6 | zend_string_release_ex(key, true); |
1110 | | |
1111 | 6 | return result; |
1112 | 6 | } |
1113 | | |
1114 | | static PHP_MINIT_FUNCTION(uri) |
1115 | 2 | { |
1116 | 2 | php_uri_ce_rfc3986_uri = register_class_Uri_Rfc3986_Uri(); |
1117 | 2 | php_uri_ce_rfc3986_uri->create_object = php_uri_object_create_rfc3986; |
1118 | 2 | php_uri_ce_rfc3986_uri->default_object_handlers = &object_handlers_rfc3986_uri; |
1119 | 2 | memcpy(&object_handlers_rfc3986_uri, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
1120 | 2 | object_handlers_rfc3986_uri.offset = offsetof(php_uri_object, std); |
1121 | 2 | object_handlers_rfc3986_uri.free_obj = php_uri_object_handler_free; |
1122 | 2 | object_handlers_rfc3986_uri.clone_obj = php_uri_object_handler_clone; |
1123 | | |
1124 | 2 | php_uri_ce_rfc3986_uri_type = register_class_Uri_Rfc3986_UriType(); |
1125 | 2 | php_uri_ce_rfc3986_uri_host_type = register_class_Uri_Rfc3986_UriHostType(); |
1126 | | |
1127 | 2 | php_uri_ce_whatwg_url = register_class_Uri_WhatWg_Url(); |
1128 | 2 | php_uri_ce_whatwg_url->create_object = php_uri_object_create_whatwg; |
1129 | 2 | php_uri_ce_whatwg_url->default_object_handlers = &object_handlers_whatwg_uri; |
1130 | 2 | memcpy(&object_handlers_whatwg_uri, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
1131 | 2 | object_handlers_whatwg_uri.offset = offsetof(php_uri_object, std); |
1132 | 2 | object_handlers_whatwg_uri.free_obj = php_uri_object_handler_free; |
1133 | 2 | object_handlers_whatwg_uri.clone_obj = php_uri_object_handler_clone; |
1134 | | |
1135 | 2 | php_uri_ce_comparison_mode = register_class_Uri_UriComparisonMode(); |
1136 | 2 | php_uri_ce_exception = register_class_Uri_UriException(zend_ce_exception); |
1137 | 2 | php_uri_ce_error = register_class_Uri_UriError(zend_ce_error); |
1138 | 2 | php_uri_ce_invalid_uri_exception = register_class_Uri_InvalidUriException(php_uri_ce_exception); |
1139 | 2 | php_uri_ce_whatwg_invalid_url_exception = register_class_Uri_WhatWg_InvalidUrlException(php_uri_ce_invalid_uri_exception); |
1140 | 2 | php_uri_ce_whatwg_url_host_type = register_class_Uri_WhatWg_UrlHostType(); |
1141 | 2 | php_uri_ce_whatwg_url_validation_error = register_class_Uri_WhatWg_UrlValidationError(); |
1142 | 2 | php_uri_ce_whatwg_url_validation_error_type = register_class_Uri_WhatWg_UrlValidationErrorType(); |
1143 | | |
1144 | 2 | zend_hash_init(&uri_parsers, 4, NULL, NULL, true); |
1145 | | |
1146 | 2 | if (php_uri_parser_register(&php_uri_parser_rfc3986) == FAILURE) { |
1147 | 0 | return FAILURE; |
1148 | 0 | } |
1149 | | |
1150 | 2 | if (php_uri_parser_register(&php_uri_parser_whatwg) == FAILURE) { |
1151 | 0 | return FAILURE; |
1152 | 0 | } |
1153 | | |
1154 | 2 | if (php_uri_parser_register(&php_uri_parser_php_parse_url) == FAILURE) { |
1155 | 0 | return FAILURE; |
1156 | 0 | } |
1157 | | |
1158 | 2 | return SUCCESS; |
1159 | 2 | } |
1160 | | |
1161 | | static PHP_MINFO_FUNCTION(uri) |
1162 | 1 | { |
1163 | 1 | php_info_print_table_start(); |
1164 | 1 | php_info_print_table_row(2, "URI support", "active"); |
1165 | 1 | #ifdef URI_STATIC_BUILD |
1166 | 1 | php_info_print_table_row(2, "uriparser bundled version", URI_VER_ANSI); |
1167 | | #else |
1168 | | php_info_print_table_row(2, "uriparser compiled version", URI_VER_ANSI); |
1169 | | php_info_print_table_row(2, "uriparser loaded version", uriBaseRuntimeVersionA()); |
1170 | | #endif |
1171 | 1 | php_info_print_table_end(); |
1172 | 1 | } |
1173 | | |
1174 | | static PHP_MSHUTDOWN_FUNCTION(uri) |
1175 | 0 | { |
1176 | 0 | zend_hash_destroy(&uri_parsers); |
1177 | |
|
1178 | 0 | return SUCCESS; |
1179 | 0 | } |
1180 | | |
1181 | | PHP_RINIT_FUNCTION(uri) |
1182 | 33.5k | { |
1183 | 33.5k | if (PHP_RINIT(uri_parser_whatwg)(INIT_FUNC_ARGS_PASSTHRU) == FAILURE) { |
1184 | 0 | return FAILURE; |
1185 | 0 | } |
1186 | | |
1187 | 33.5k | return SUCCESS; |
1188 | 33.5k | } |
1189 | | |
1190 | | ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri) |
1191 | 33.5k | { |
1192 | 33.5k | if (ZEND_MODULE_POST_ZEND_DEACTIVATE_N(uri_parser_whatwg)() == FAILURE) { |
1193 | 0 | return FAILURE; |
1194 | 0 | } |
1195 | | |
1196 | 33.5k | return SUCCESS; |
1197 | 33.5k | } |
1198 | | |
1199 | | zend_module_entry uri_module_entry = { |
1200 | | STANDARD_MODULE_HEADER_EX, NULL, |
1201 | | uri_deps, |
1202 | | "uri", /* Extension name */ |
1203 | | NULL, /* zend_function_entry */ |
1204 | | PHP_MINIT(uri), /* PHP_MINIT - Module initialization */ |
1205 | | PHP_MSHUTDOWN(uri), /* PHP_MSHUTDOWN - Module shutdown */ |
1206 | | PHP_RINIT(uri), /* PHP_RINIT - Request initialization */ |
1207 | | NULL, /* PHP_RSHUTDOWN - Request shutdown */ |
1208 | | PHP_MINFO(uri), /* PHP_MINFO - Module info */ |
1209 | | PHP_VERSION, /* Version */ |
1210 | | NO_MODULE_GLOBALS, |
1211 | | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(uri), |
1212 | | STANDARD_MODULE_PROPERTIES_EX |
1213 | | }; |