/src/php-src/ext/uri/uri_parser_whatwg.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 | | #include "php.h" |
16 | | #include "uri_parser_whatwg.h" |
17 | | #include "php_uri_common.h" |
18 | | #include "Zend/zend_enum.h" |
19 | | #include "Zend/zend_smart_str.h" |
20 | | #include "Zend/zend_exceptions.h" |
21 | | #ifdef HAVE_ARPA_INET_H |
22 | | #include <arpa/inet.h> |
23 | | #endif |
24 | | |
25 | | ZEND_TLS lexbor_mraw_t lexbor_mraw = {0}; |
26 | | ZEND_TLS lxb_url_parser_t lexbor_parser = {0}; |
27 | | ZEND_TLS lxb_unicode_idna_t lexbor_idna = {0}; |
28 | | |
29 | | static const size_t lexbor_mraw_byte_size = 8192; |
30 | | |
31 | | static zend_always_inline void zval_string_or_null_to_lexbor_str(zval *value, lexbor_str_t *lexbor_str) |
32 | 0 | { |
33 | 0 | if (Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) > 0) { |
34 | 0 | lexbor_str->data = (lxb_char_t *) Z_STRVAL_P(value); |
35 | 0 | lexbor_str->length = Z_STRLEN_P(value); |
36 | 0 | } else { |
37 | 0 | ZEND_ASSERT(Z_ISNULL_P(value) || (Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) == 0)); |
38 | 0 | lexbor_str->data = (lxb_char_t *) ""; |
39 | 0 | lexbor_str->length = 0; |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexbor_str_t *lexbor_str) |
44 | 0 | { |
45 | 0 | if (Z_TYPE_P(value) == IS_LONG) { |
46 | 0 | ZVAL_STR(value, zend_long_to_str(Z_LVAL_P(value))); |
47 | 0 | lexbor_str_init_append(lexbor_str, lexbor_parser.mraw, (const lxb_char_t *) Z_STRVAL_P(value), Z_STRLEN_P(value)); |
48 | 0 | zval_ptr_dtor_str(value); |
49 | 0 | } else { |
50 | 0 | ZEND_ASSERT(Z_ISNULL_P(value)); |
51 | 0 | lexbor_str->data = (lxb_char_t *) ""; |
52 | 0 | lexbor_str->length = 0; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | /** |
57 | | * Creates a Uri\WhatWg\UrlValidationError class by mapping error codes listed in |
58 | | * https://url.spec.whatwg.org/#writing to a Uri\WhatWg\UrlValidationErrorType enum. |
59 | | * The result is passed by reference to the errors parameter. |
60 | | */ |
61 | | static const char *fill_errors(zval *errors) |
62 | 0 | { |
63 | 0 | size_t log_len; |
64 | 0 | if (lexbor_parser.log == NULL || (log_len = lexbor_plog_length(lexbor_parser.log)) == 0) { |
65 | 0 | ZVAL_EMPTY_ARRAY(errors); |
66 | 0 | return NULL; |
67 | 0 | } |
68 | | |
69 | 0 | array_init_size(errors, log_len); |
70 | 0 | const char *result = NULL; |
71 | |
|
72 | 0 | lexbor_plog_entry_t *lxb_error; |
73 | 0 | while ((lxb_error = lexbor_array_obj_pop(&lexbor_parser.log->list)) != NULL) { |
74 | 0 | zval error; |
75 | 0 | object_init_ex(&error, php_uri_ce_whatwg_url_validation_error); |
76 | 0 | zend_update_property_string(php_uri_ce_whatwg_url_validation_error, Z_OBJ(error), ZEND_STRL("context"), (const char *) lxb_error->data); |
77 | |
|
78 | 0 | const char *error_str; |
79 | 0 | zval failure; |
80 | 0 | switch (lxb_error->id) { |
81 | 0 | case LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII: |
82 | 0 | error_str = "DomainToAscii"; |
83 | 0 | ZVAL_TRUE(&failure); |
84 | 0 | break; |
85 | 0 | case LXB_URL_ERROR_TYPE_DOMAIN_TO_UNICODE: |
86 | 0 | error_str = "DomainToUnicode"; |
87 | 0 | ZVAL_FALSE(&failure); |
88 | 0 | break; |
89 | 0 | case LXB_URL_ERROR_TYPE_DOMAIN_INVALID_CODE_POINT: |
90 | 0 | error_str = "DomainInvalidCodePoint"; |
91 | 0 | ZVAL_TRUE(&failure); |
92 | 0 | break; |
93 | 0 | case LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT: |
94 | 0 | error_str = "HostInvalidCodePoint"; |
95 | 0 | ZVAL_TRUE(&failure); |
96 | 0 | break; |
97 | 0 | case LXB_URL_ERROR_TYPE_IPV4_EMPTY_PART: |
98 | 0 | error_str = "Ipv4EmptyPart"; |
99 | 0 | ZVAL_FALSE(&failure); |
100 | 0 | break; |
101 | 0 | case LXB_URL_ERROR_TYPE_IPV4_TOO_MANY_PARTS: |
102 | 0 | error_str = "Ipv4TooManyParts"; |
103 | 0 | ZVAL_TRUE(&failure); |
104 | 0 | break; |
105 | 0 | case LXB_URL_ERROR_TYPE_IPV4_NON_NUMERIC_PART: |
106 | 0 | error_str = "Ipv4NonNumericPart"; |
107 | 0 | ZVAL_TRUE(&failure); |
108 | 0 | break; |
109 | 0 | case LXB_URL_ERROR_TYPE_IPV4_NON_DECIMAL_PART: |
110 | 0 | error_str = "Ipv4NonDecimalPart"; |
111 | 0 | ZVAL_FALSE(&failure); |
112 | 0 | break; |
113 | 0 | case LXB_URL_ERROR_TYPE_IPV4_OUT_OF_RANGE_PART: |
114 | 0 | error_str = "Ipv4OutOfRangePart"; |
115 | 0 | ZVAL_TRUE(&failure); |
116 | 0 | break; |
117 | 0 | case LXB_URL_ERROR_TYPE_IPV6_UNCLOSED: |
118 | 0 | error_str = "Ipv6Unclosed"; |
119 | 0 | ZVAL_TRUE(&failure); |
120 | 0 | break; |
121 | 0 | case LXB_URL_ERROR_TYPE_IPV6_INVALID_COMPRESSION: |
122 | 0 | error_str = "Ipv6InvalidCompression"; |
123 | 0 | ZVAL_TRUE(&failure); |
124 | 0 | break; |
125 | 0 | case LXB_URL_ERROR_TYPE_IPV6_TOO_MANY_PIECES: |
126 | 0 | error_str = "Ipv6TooManyPieces"; |
127 | 0 | ZVAL_TRUE(&failure); |
128 | 0 | break; |
129 | 0 | case LXB_URL_ERROR_TYPE_IPV6_MULTIPLE_COMPRESSION: |
130 | 0 | error_str = "Ipv6MultipleCompression"; |
131 | 0 | ZVAL_TRUE(&failure); |
132 | 0 | break; |
133 | 0 | case LXB_URL_ERROR_TYPE_IPV6_INVALID_CODE_POINT: |
134 | 0 | error_str = "Ipv6InvalidCodePoint"; |
135 | 0 | ZVAL_TRUE(&failure); |
136 | 0 | break; |
137 | 0 | case LXB_URL_ERROR_TYPE_IPV6_TOO_FEW_PIECES: |
138 | 0 | error_str = "Ipv6TooFewPieces"; |
139 | 0 | ZVAL_TRUE(&failure); |
140 | 0 | break; |
141 | 0 | case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_MANY_PIECES: |
142 | 0 | error_str = "Ipv4InIpv6TooManyPieces"; |
143 | 0 | ZVAL_TRUE(&failure); |
144 | 0 | break; |
145 | 0 | case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_INVALID_CODE_POINT: |
146 | 0 | error_str = "Ipv4InIpv6InvalidCodePoint"; |
147 | 0 | ZVAL_TRUE(&failure); |
148 | 0 | break; |
149 | 0 | case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_OUT_OF_RANGE_PART: |
150 | 0 | error_str = "Ipv4InIpv6OutOfRangePart"; |
151 | 0 | ZVAL_TRUE(&failure); |
152 | 0 | break; |
153 | 0 | case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_FEW_PARTS: |
154 | 0 | error_str = "Ipv4InIpv6TooFewParts"; |
155 | 0 | ZVAL_TRUE(&failure); |
156 | 0 | break; |
157 | 0 | case LXB_URL_ERROR_TYPE_INVALID_URL_UNIT: |
158 | 0 | error_str = "InvalidUrlUnit"; |
159 | 0 | ZVAL_FALSE(&failure); |
160 | 0 | break; |
161 | 0 | case LXB_URL_ERROR_TYPE_SPECIAL_SCHEME_MISSING_FOLLOWING_SOLIDUS: |
162 | 0 | error_str = "SpecialSchemeMissingFollowingSolidus"; |
163 | 0 | ZVAL_FALSE(&failure); |
164 | 0 | break; |
165 | 0 | case LXB_URL_ERROR_TYPE_MISSING_SCHEME_NON_RELATIVE_URL: |
166 | 0 | error_str = "MissingSchemeNonRelativeUrl"; |
167 | 0 | ZVAL_TRUE(&failure); |
168 | 0 | break; |
169 | 0 | case LXB_URL_ERROR_TYPE_INVALID_REVERSE_SOLIDUS: |
170 | 0 | error_str = "InvalidReverseSoldius"; |
171 | 0 | ZVAL_FALSE(&failure); |
172 | 0 | break; |
173 | 0 | case LXB_URL_ERROR_TYPE_INVALID_CREDENTIALS: |
174 | 0 | error_str = "InvalidCredentials"; |
175 | 0 | ZVAL_FALSE(&failure); |
176 | 0 | break; |
177 | 0 | case LXB_URL_ERROR_TYPE_HOST_MISSING: |
178 | 0 | error_str = "HostMissing"; |
179 | 0 | ZVAL_TRUE(&failure); |
180 | 0 | break; |
181 | 0 | case LXB_URL_ERROR_TYPE_PORT_OUT_OF_RANGE: |
182 | 0 | error_str = "PortOutOfRange"; |
183 | 0 | ZVAL_TRUE(&failure); |
184 | 0 | break; |
185 | 0 | case LXB_URL_ERROR_TYPE_PORT_INVALID: |
186 | 0 | error_str = "PortInvalid"; |
187 | 0 | ZVAL_TRUE(&failure); |
188 | 0 | break; |
189 | 0 | case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER: |
190 | 0 | error_str = "FileInvalidWindowsDriveLetter"; |
191 | 0 | ZVAL_FALSE(&failure); |
192 | 0 | break; |
193 | 0 | case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER_HOST: |
194 | 0 | error_str = "FileInvalidWindowsDriveLetterHost"; |
195 | 0 | ZVAL_FALSE(&failure); |
196 | 0 | break; |
197 | 0 | default: ZEND_UNREACHABLE(); |
198 | 0 | } |
199 | | |
200 | 0 | zval error_type; |
201 | 0 | ZVAL_OBJ(&error_type, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_validation_error_type, error_str)); |
202 | 0 | zend_update_property_ex(php_uri_ce_whatwg_url_validation_error, Z_OBJ(error), ZSTR_KNOWN(ZEND_STR_TYPE), &error_type); |
203 | |
|
204 | 0 | zend_update_property(php_uri_ce_whatwg_url_validation_error, Z_OBJ(error), ZEND_STRL("failure"), &failure); |
205 | |
|
206 | 0 | if (Z_TYPE(failure) == IS_TRUE) { |
207 | 0 | result = error_str; |
208 | 0 | } |
209 | |
|
210 | 0 | add_next_index_zval(errors, &error); |
211 | 0 | } |
212 | | |
213 | 0 | return result; |
214 | 0 | } |
215 | | |
216 | | static void throw_invalid_url_exception_during_write(zval *errors, const char *component) |
217 | 0 | { |
218 | 0 | zval err; |
219 | 0 | const char *reason = fill_errors(&err); |
220 | 0 | zend_object *exception = zend_throw_exception_ex( |
221 | 0 | php_uri_ce_whatwg_invalid_url_exception, |
222 | 0 | 0, |
223 | 0 | "The specified %s is malformed%s%s%s", |
224 | 0 | component, |
225 | 0 | reason ? " (" : "", |
226 | 0 | reason ? reason : "", |
227 | 0 | reason ? ")" : "" |
228 | 0 | ); |
229 | 0 | zend_update_property(exception->ce, exception, ZEND_STRL("errors"), &err); |
230 | 0 | if (errors) { |
231 | 0 | zval_ptr_dtor(errors); |
232 | 0 | ZVAL_COPY_VALUE(errors, &err); |
233 | 0 | } else { |
234 | 0 | zval_ptr_dtor(&err); |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | static lxb_status_t serialize_to_smart_str_callback(const lxb_char_t *data, size_t length, void *ctx) |
239 | 0 | { |
240 | 0 | smart_str *uri_str = ctx; |
241 | |
|
242 | 0 | if (data != NULL && length > 0) { |
243 | 0 | smart_str_appendl(uri_str, (const char *) data, length); |
244 | 0 | } |
245 | |
|
246 | 0 | return LXB_STATUS_OK; |
247 | 0 | } |
248 | | |
249 | | static zend_result php_uri_parser_whatwg_scheme_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
250 | 0 | { |
251 | 0 | const lxb_url_t *lexbor_uri = uri; |
252 | |
|
253 | 0 | ZEND_ASSERT(lexbor_uri->scheme.type != LXB_URL_SCHEMEL_TYPE__UNDEF); |
254 | |
|
255 | 0 | ZVAL_STRINGL(retval, (const char *) lexbor_uri->scheme.name.data, lexbor_uri->scheme.name.length); |
256 | |
|
257 | 0 | return SUCCESS; |
258 | 0 | } |
259 | | |
260 | | static zend_result php_uri_parser_whatwg_scheme_write(void *uri, zval *value, zval *errors) |
261 | 0 | { |
262 | 0 | lxb_url_t *lexbor_uri = uri; |
263 | 0 | lexbor_str_t str = {0}; |
264 | |
|
265 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
266 | |
|
267 | 0 | if (lxb_url_api_protocol_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
268 | 0 | throw_invalid_url_exception_during_write(errors, "scheme"); |
269 | |
|
270 | 0 | return FAILURE; |
271 | 0 | } |
272 | | |
273 | 0 | return SUCCESS; |
274 | 0 | } |
275 | | |
276 | | ZEND_ATTRIBUTE_NONNULL bool php_uri_parser_whatwg_is_special(const lxb_url_t *lexbor_uri) |
277 | 0 | { |
278 | 0 | return lxb_url_is_special(lexbor_uri); |
279 | 0 | } |
280 | | |
281 | | /* 4.2. URL miscellaneous: A URL includes credentials if its username or password is not the empty string. */ |
282 | | static bool includes_credentials(const lxb_url_t *lexbor_uri) |
283 | 0 | { |
284 | 0 | return lexbor_uri->username.length > 0 || lexbor_uri->password.length > 0; |
285 | 0 | } |
286 | | |
287 | | static zend_result php_uri_parser_whatwg_username_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
288 | 0 | { |
289 | 0 | const lxb_url_t *lexbor_uri = uri; |
290 | |
|
291 | 0 | if (includes_credentials(lexbor_uri)) { |
292 | 0 | ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->username.data, lexbor_uri->username.length); |
293 | 0 | } else { |
294 | 0 | ZVAL_NULL(retval); |
295 | 0 | } |
296 | |
|
297 | 0 | return SUCCESS; |
298 | 0 | } |
299 | | |
300 | | static zend_result php_uri_parser_whatwg_username_write(void *uri, zval *value, zval *errors) |
301 | 0 | { |
302 | 0 | lxb_url_t *lexbor_uri = uri; |
303 | 0 | lexbor_str_t str = {0}; |
304 | |
|
305 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
306 | |
|
307 | 0 | if (lxb_url_api_username_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) { |
308 | 0 | throw_invalid_url_exception_during_write(errors, "username"); |
309 | |
|
310 | 0 | return FAILURE; |
311 | 0 | } |
312 | | |
313 | 0 | return SUCCESS; |
314 | 0 | } |
315 | | |
316 | | static zend_result php_uri_parser_whatwg_password_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
317 | 0 | { |
318 | 0 | const lxb_url_t *lexbor_uri = uri; |
319 | |
|
320 | 0 | if (includes_credentials(lexbor_uri)) { |
321 | 0 | ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->password.data, lexbor_uri->password.length); |
322 | 0 | } else { |
323 | 0 | ZVAL_NULL(retval); |
324 | 0 | } |
325 | |
|
326 | 0 | return SUCCESS; |
327 | 0 | } |
328 | | |
329 | | static zend_result php_uri_parser_whatwg_password_write(void *uri, zval *value, zval *errors) |
330 | 0 | { |
331 | 0 | lxb_url_t *lexbor_uri = uri; |
332 | 0 | lexbor_str_t str = {0}; |
333 | |
|
334 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
335 | |
|
336 | 0 | if (lxb_url_api_password_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) { |
337 | 0 | throw_invalid_url_exception_during_write(errors, "password"); |
338 | |
|
339 | 0 | return FAILURE; |
340 | 0 | } |
341 | | |
342 | 0 | return SUCCESS; |
343 | 0 | } |
344 | | |
345 | | static zend_result php_uri_parser_whatwg_host_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
346 | 0 | { |
347 | 0 | const lxb_url_t *lexbor_uri = uri; |
348 | |
|
349 | 0 | if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV4) { |
350 | 0 | smart_str host_str = {0}; |
351 | |
|
352 | 0 | lxb_url_serialize_host_ipv4(lexbor_uri->host.u.ipv4, serialize_to_smart_str_callback, &host_str); |
353 | |
|
354 | 0 | ZVAL_NEW_STR(retval, smart_str_extract(&host_str)); |
355 | 0 | } else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV6) { |
356 | 0 | smart_str host_str = {0}; |
357 | |
|
358 | 0 | smart_str_appendc(&host_str, '['); |
359 | 0 | lxb_url_serialize_host_ipv6(lexbor_uri->host.u.ipv6, serialize_to_smart_str_callback, &host_str); |
360 | 0 | smart_str_appendc(&host_str, ']'); |
361 | |
|
362 | 0 | ZVAL_NEW_STR(retval, smart_str_extract(&host_str)); |
363 | 0 | } else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_EMPTY) { |
364 | 0 | ZVAL_EMPTY_STRING(retval); |
365 | 0 | } else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) { |
366 | 0 | switch (read_mode) { |
367 | 0 | case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE: { |
368 | 0 | smart_str host_str = {0}; |
369 | 0 | lxb_url_serialize_host_unicode(&lexbor_idna, &lexbor_uri->host, serialize_to_smart_str_callback, &host_str); |
370 | 0 | lxb_unicode_idna_clean(&lexbor_idna); |
371 | |
|
372 | 0 | ZVAL_STR(retval, smart_str_extract(&host_str)); |
373 | 0 | break; |
374 | 0 | } |
375 | 0 | case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII: |
376 | 0 | ZEND_FALLTHROUGH; |
377 | 0 | case PHP_URI_COMPONENT_READ_MODE_RAW: |
378 | 0 | ZVAL_STRINGL(retval, (const char *) lexbor_uri->host.u.domain.data, lexbor_uri->host.u.domain.length); |
379 | 0 | break; |
380 | 0 | default: ZEND_UNREACHABLE(); |
381 | 0 | } |
382 | 0 | } else { |
383 | 0 | ZVAL_NULL(retval); |
384 | 0 | } |
385 | | |
386 | 0 | return SUCCESS; |
387 | 0 | } |
388 | | |
389 | | ZEND_ATTRIBUTE_NONNULL void php_uri_parser_whatwg_host_type_read(const lxb_url_t *lexbor_uri, zval *retval) |
390 | 0 | { |
391 | 0 | switch (lexbor_uri->host.type) { |
392 | 0 | case LXB_URL_HOST_TYPE_IPV4: |
393 | 0 | ZVAL_OBJ_COPY(retval, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_host_type, "IPv4")); |
394 | 0 | return; |
395 | 0 | case LXB_URL_HOST_TYPE_IPV6: |
396 | 0 | ZVAL_OBJ_COPY(retval, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_host_type, "IPv6")); |
397 | 0 | return; |
398 | 0 | case LXB_URL_HOST_TYPE_DOMAIN: |
399 | 0 | ZVAL_OBJ_COPY(retval, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_host_type, "Domain")); |
400 | 0 | return; |
401 | 0 | case LXB_URL_HOST_TYPE_EMPTY: |
402 | 0 | ZVAL_OBJ_COPY(retval, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_host_type, "Empty")); |
403 | 0 | return; |
404 | 0 | case LXB_URL_HOST_TYPE_OPAQUE: |
405 | 0 | ZVAL_OBJ_COPY(retval, zend_enum_get_case_cstr(php_uri_ce_whatwg_url_host_type, "Opaque")); |
406 | 0 | return; |
407 | 0 | case LXB_URL_HOST_TYPE__UNDEF: |
408 | 0 | ZVAL_NULL(retval); |
409 | 0 | return; |
410 | 0 | default: ZEND_UNREACHABLE(); |
411 | 0 | } |
412 | 0 | } |
413 | | |
414 | | static zend_result php_uri_parser_whatwg_host_write(void *uri, zval *value, zval *errors) |
415 | 0 | { |
416 | 0 | lxb_url_t *lexbor_uri = uri; |
417 | 0 | lexbor_str_t str = {0}; |
418 | |
|
419 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
420 | |
|
421 | 0 | if (lxb_url_api_hostname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
422 | 0 | throw_invalid_url_exception_during_write(errors, "host"); |
423 | |
|
424 | 0 | return FAILURE; |
425 | 0 | } |
426 | | |
427 | 0 | return SUCCESS; |
428 | 0 | } |
429 | | |
430 | | static zend_result php_uri_parser_whatwg_port_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
431 | 0 | { |
432 | 0 | const lxb_url_t *lexbor_uri = uri; |
433 | |
|
434 | 0 | if (lexbor_uri->has_port) { |
435 | 0 | ZVAL_LONG(retval, lexbor_uri->port); |
436 | 0 | } else { |
437 | 0 | ZVAL_NULL(retval); |
438 | 0 | } |
439 | |
|
440 | 0 | return SUCCESS; |
441 | 0 | } |
442 | | |
443 | | static zend_result php_uri_parser_whatwg_port_write(void *uri, zval *value, zval *errors) |
444 | 0 | { |
445 | 0 | lxb_url_t *lexbor_uri = uri; |
446 | 0 | lexbor_str_t str = {0}; |
447 | |
|
448 | 0 | zval_long_or_null_to_lexbor_str(value, &str); |
449 | |
|
450 | 0 | if (lxb_url_api_port_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
451 | 0 | throw_invalid_url_exception_during_write(errors, "port"); |
452 | |
|
453 | 0 | return FAILURE; |
454 | 0 | } |
455 | | |
456 | 0 | return SUCCESS; |
457 | 0 | } |
458 | | |
459 | | static zend_result php_uri_parser_whatwg_path_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
460 | 0 | { |
461 | 0 | const lxb_url_t *lexbor_uri = uri; |
462 | |
|
463 | 0 | if (lexbor_uri->path.str.length > 0) { |
464 | 0 | ZVAL_STRINGL(retval, (const char *) lexbor_uri->path.str.data, lexbor_uri->path.str.length); |
465 | 0 | } else { |
466 | 0 | ZVAL_EMPTY_STRING(retval); |
467 | 0 | } |
468 | |
|
469 | 0 | return SUCCESS; |
470 | 0 | } |
471 | | |
472 | | static zend_result php_uri_parser_whatwg_path_write(void *uri, zval *value, zval *errors) |
473 | 0 | { |
474 | 0 | lxb_url_t *lexbor_uri = uri; |
475 | 0 | lexbor_str_t str = {0}; |
476 | |
|
477 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
478 | |
|
479 | 0 | if (lxb_url_api_pathname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
480 | 0 | throw_invalid_url_exception_during_write(errors, "path"); |
481 | |
|
482 | 0 | return FAILURE; |
483 | 0 | } |
484 | | |
485 | 0 | return SUCCESS; |
486 | 0 | } |
487 | | |
488 | | static zend_result php_uri_parser_whatwg_query_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
489 | 0 | { |
490 | 0 | const lxb_url_t *lexbor_uri = uri; |
491 | |
|
492 | 0 | if (lexbor_uri->query.data != NULL) { |
493 | 0 | ZVAL_STRINGL(retval, (const char *) lexbor_uri->query.data, lexbor_uri->query.length); |
494 | 0 | } else { |
495 | 0 | ZVAL_NULL(retval); |
496 | 0 | } |
497 | |
|
498 | 0 | return SUCCESS; |
499 | 0 | } |
500 | | |
501 | | static zend_result php_uri_parser_whatwg_query_write(void *uri, zval *value, zval *errors) |
502 | 0 | { |
503 | 0 | lxb_url_t *lexbor_uri = uri; |
504 | 0 | lexbor_str_t str = {0}; |
505 | |
|
506 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
507 | |
|
508 | 0 | if (lxb_url_api_search_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
509 | 0 | throw_invalid_url_exception_during_write(errors, "query string"); |
510 | |
|
511 | 0 | return FAILURE; |
512 | 0 | } |
513 | | |
514 | 0 | return SUCCESS; |
515 | 0 | } |
516 | | |
517 | | static zend_result php_uri_parser_whatwg_fragment_read(void *uri, php_uri_component_read_mode read_mode, zval *retval) |
518 | 0 | { |
519 | 0 | const lxb_url_t *lexbor_uri = uri; |
520 | |
|
521 | 0 | if (lexbor_uri->fragment.data != NULL) { |
522 | 0 | ZVAL_STRINGL(retval, (const char *) lexbor_uri->fragment.data, lexbor_uri->fragment.length); |
523 | 0 | } else { |
524 | 0 | ZVAL_NULL(retval); |
525 | 0 | } |
526 | |
|
527 | 0 | return SUCCESS; |
528 | 0 | } |
529 | | |
530 | | static zend_result php_uri_parser_whatwg_fragment_write(void *uri, zval *value, zval *errors) |
531 | 0 | { |
532 | 0 | lxb_url_t *lexbor_uri = uri; |
533 | 0 | lexbor_str_t str = {0}; |
534 | |
|
535 | 0 | zval_string_or_null_to_lexbor_str(value, &str); |
536 | |
|
537 | 0 | if (lxb_url_api_hash_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { |
538 | 0 | throw_invalid_url_exception_during_write(errors, "fragment"); |
539 | |
|
540 | 0 | return FAILURE; |
541 | 0 | } |
542 | | |
543 | 0 | return SUCCESS; |
544 | 0 | } |
545 | | |
546 | | PHP_RINIT_FUNCTION(uri_parser_whatwg) |
547 | 44.4k | { |
548 | 44.4k | lxb_status_t status; |
549 | | |
550 | 44.4k | status = lexbor_mraw_init(&lexbor_mraw, lexbor_mraw_byte_size); |
551 | 44.4k | if (status != LXB_STATUS_OK) { |
552 | 0 | goto fail; |
553 | 0 | } |
554 | | |
555 | 44.4k | status = lxb_url_parser_init(&lexbor_parser, &lexbor_mraw); |
556 | 44.4k | if (status != LXB_STATUS_OK) { |
557 | 0 | goto fail; |
558 | 0 | } |
559 | | |
560 | 44.4k | status = lxb_unicode_idna_init(&lexbor_idna); |
561 | 44.4k | if (status != LXB_STATUS_OK) { |
562 | 0 | goto fail; |
563 | 0 | } |
564 | | |
565 | 44.4k | return SUCCESS; |
566 | | |
567 | 0 | fail: |
568 | | |
569 | | /* Unconditionally calling the _destroy() functions is |
570 | | * safe on a zeroed structure. */ |
571 | 0 | lxb_unicode_idna_destroy(&lexbor_idna, false); |
572 | 0 | memset(&lexbor_idna, 0, sizeof(lexbor_idna)); |
573 | 0 | lxb_url_parser_destroy(&lexbor_parser, false); |
574 | 0 | memset(&lexbor_parser, 0, sizeof(lexbor_parser)); |
575 | 0 | lexbor_mraw_destroy(&lexbor_mraw, false); |
576 | 0 | memset(&lexbor_mraw, 0, sizeof(lexbor_mraw)); |
577 | |
|
578 | 0 | return FAILURE; |
579 | 44.4k | } |
580 | | |
581 | | ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri_parser_whatwg) |
582 | 44.4k | { |
583 | 44.4k | lxb_unicode_idna_destroy(&lexbor_idna, false); |
584 | 44.4k | memset(&lexbor_idna, 0, sizeof(lexbor_idna)); |
585 | 44.4k | lxb_url_parser_destroy(&lexbor_parser, false); |
586 | 44.4k | memset(&lexbor_parser, 0, sizeof(lexbor_parser)); |
587 | 44.4k | lexbor_mraw_destroy(&lexbor_mraw, false); |
588 | 44.4k | memset(&lexbor_mraw, 0, sizeof(lexbor_mraw)); |
589 | | |
590 | 44.4k | return SUCCESS; |
591 | 44.4k | } |
592 | | |
593 | | lxb_url_t *php_uri_parser_whatwg_parse_ex(const char *uri_str, size_t uri_str_len, const lxb_url_t *lexbor_base_url, zval *errors, bool silent) |
594 | 0 | { |
595 | 0 | lxb_url_parser_clean(&lexbor_parser); |
596 | |
|
597 | 0 | lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) uri_str, uri_str_len); |
598 | |
|
599 | 0 | if ((url == NULL && !silent) || errors != NULL) { |
600 | 0 | zval err; |
601 | 0 | const char *reason = fill_errors(&err); |
602 | 0 | if (url == NULL && !silent) { |
603 | 0 | zend_object *exception = zend_throw_exception_ex(php_uri_ce_whatwg_invalid_url_exception, 0, "The specified URI is malformed%s%s%s", reason ? " (" : "", reason ? reason : "", reason ? ")" : ""); |
604 | 0 | zend_update_property(exception->ce, exception, ZEND_STRL("errors"), &err); |
605 | 0 | } |
606 | 0 | if (errors != NULL) { |
607 | 0 | zval_ptr_dtor(errors); |
608 | 0 | ZVAL_COPY_VALUE(errors, &err); |
609 | 0 | } else { |
610 | 0 | zval_ptr_dtor(&err); |
611 | 0 | } |
612 | 0 | } |
613 | |
|
614 | 0 | return url; |
615 | 0 | } |
616 | | |
617 | | static void *php_uri_parser_whatwg_parse(const char *uri_str, size_t uri_str_len, const void *base_url, zval *errors, bool silent) |
618 | 0 | { |
619 | 0 | return php_uri_parser_whatwg_parse_ex(uri_str, uri_str_len, base_url, errors, silent); |
620 | 0 | } |
621 | | |
622 | | static void *php_uri_parser_whatwg_clone(void *uri) |
623 | 0 | { |
624 | 0 | const lxb_url_t *lexbor_uri = uri; |
625 | |
|
626 | 0 | return lxb_url_clone(lexbor_parser.mraw, lexbor_uri); |
627 | 0 | } |
628 | | |
629 | | static zend_string *php_uri_parser_whatwg_to_string(void *uri, php_uri_recomposition_mode recomposition_mode, bool exclude_fragment) |
630 | 0 | { |
631 | 0 | const lxb_url_t *lexbor_uri = uri; |
632 | 0 | smart_str uri_str = {0}; |
633 | |
|
634 | 0 | switch (recomposition_mode) { |
635 | 0 | case PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE: |
636 | 0 | ZEND_FALLTHROUGH; |
637 | 0 | case PHP_URI_RECOMPOSITION_MODE_NORMALIZED_UNICODE: |
638 | 0 | lxb_url_serialize_idna(&lexbor_idna, lexbor_uri, serialize_to_smart_str_callback, &uri_str, exclude_fragment); |
639 | 0 | lxb_unicode_idna_clean(&lexbor_idna); |
640 | 0 | break; |
641 | 0 | case PHP_URI_RECOMPOSITION_MODE_RAW_ASCII: |
642 | 0 | ZEND_FALLTHROUGH; |
643 | 0 | case PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII: |
644 | 0 | lxb_url_serialize(lexbor_uri, serialize_to_smart_str_callback, &uri_str, exclude_fragment); |
645 | 0 | break; |
646 | 0 | default: ZEND_UNREACHABLE(); |
647 | 0 | } |
648 | | |
649 | 0 | return smart_str_extract(&uri_str); |
650 | 0 | } |
651 | | |
652 | | static void php_uri_parser_whatwg_destroy(void *uri) |
653 | 2 | { |
654 | 2 | lxb_url_t *lexbor_uri = uri; |
655 | | |
656 | 2 | lxb_url_destroy(lexbor_uri); |
657 | 2 | } |
658 | | |
659 | | PHPAPI const php_uri_parser php_uri_parser_whatwg = { |
660 | | .name = PHP_URI_PARSER_WHATWG, |
661 | | .parse = php_uri_parser_whatwg_parse, |
662 | | .clone = php_uri_parser_whatwg_clone, |
663 | | .to_string = php_uri_parser_whatwg_to_string, |
664 | | .destroy = php_uri_parser_whatwg_destroy, |
665 | | { |
666 | | .scheme = {.read = php_uri_parser_whatwg_scheme_read, .write = php_uri_parser_whatwg_scheme_write}, |
667 | | .username = {.read = php_uri_parser_whatwg_username_read, .write = php_uri_parser_whatwg_username_write}, |
668 | | .password = {.read = php_uri_parser_whatwg_password_read, .write = php_uri_parser_whatwg_password_write}, |
669 | | .host = {.read = php_uri_parser_whatwg_host_read, .write = php_uri_parser_whatwg_host_write}, |
670 | | .port = {.read = php_uri_parser_whatwg_port_read, .write = php_uri_parser_whatwg_port_write}, |
671 | | .path = {.read = php_uri_parser_whatwg_path_read, .write = php_uri_parser_whatwg_path_write}, |
672 | | .query = {.read = php_uri_parser_whatwg_query_read, .write = php_uri_parser_whatwg_query_write}, |
673 | | .fragment = {.read = php_uri_parser_whatwg_fragment_read, .write = php_uri_parser_whatwg_fragment_write}, |
674 | | } |
675 | | }; |