/src/php-src/ext/uri/uri_parser_rfc3986.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_rfc3986.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 | | |
22 | | #include <uriparser/Uri.h> |
23 | | |
24 | | struct php_uri_parser_rfc3986_uris { |
25 | | UriUriA uri; |
26 | | UriUriA normalized_uri; |
27 | | unsigned int normalization_mask; |
28 | | bool normalized_uri_initialized; |
29 | | }; |
30 | | |
31 | | static void *php_uri_parser_rfc3986_memory_manager_malloc(UriMemoryManager *memory_manager, size_t size) |
32 | 0 | { |
33 | 0 | return emalloc(size); |
34 | 0 | } |
35 | | |
36 | | static void *php_uri_parser_rfc3986_memory_manager_calloc(UriMemoryManager *memory_manager, size_t nmemb, size_t size) |
37 | 0 | { |
38 | 0 | return ecalloc(nmemb, size); |
39 | 0 | } |
40 | | |
41 | | static void *php_uri_parser_rfc3986_memory_manager_realloc(UriMemoryManager *memory_manager, void *ptr, size_t size) |
42 | 0 | { |
43 | 0 | return erealloc(ptr, size); |
44 | 0 | } |
45 | | |
46 | | static void *php_uri_parser_rfc3986_memory_manager_reallocarray(UriMemoryManager *memory_manager, void *ptr, size_t nmemb, size_t size) |
47 | 0 | { |
48 | 0 | return safe_erealloc(ptr, nmemb, size, 0); |
49 | 0 | } |
50 | | |
51 | | static void php_uri_parser_rfc3986_memory_manager_destroy(UriMemoryManager *memory_manager, void *ptr) |
52 | 0 | { |
53 | 0 | efree(ptr); |
54 | 0 | } |
55 | | |
56 | | static const UriMemoryManager php_uri_parser_rfc3986_memory_manager = { |
57 | | .malloc = php_uri_parser_rfc3986_memory_manager_malloc, |
58 | | .calloc = php_uri_parser_rfc3986_memory_manager_calloc, |
59 | | .realloc = php_uri_parser_rfc3986_memory_manager_realloc, |
60 | | .reallocarray = php_uri_parser_rfc3986_memory_manager_reallocarray, |
61 | | .free = php_uri_parser_rfc3986_memory_manager_destroy, |
62 | | .userData = NULL, |
63 | | }; |
64 | | |
65 | | /* The library expects a pointer to a non-const UriMemoryManager, but does |
66 | | * not actually modify it (and neither does our implementation). Use a |
67 | | * const struct with a non-const pointer for convenience. */ |
68 | | static UriMemoryManager* const mm = (UriMemoryManager*)&php_uri_parser_rfc3986_memory_manager; |
69 | | |
70 | | static inline size_t get_text_range_length(const UriTextRangeA *range) |
71 | 0 | { |
72 | 0 | return range->afterLast - range->first; |
73 | 0 | } |
74 | | |
75 | | static inline bool has_text_range(const UriTextRangeA *range) |
76 | 0 | { |
77 | 0 | return range->first != NULL && range->afterLast != NULL; |
78 | 0 | } |
79 | | |
80 | | ZEND_ATTRIBUTE_NONNULL static void copy_uri(UriUriA *new_uriparser_uri, const UriUriA *uriparser_uri) |
81 | 0 | { |
82 | 0 | int result = uriCopyUriMmA(new_uriparser_uri, uriparser_uri, mm); |
83 | 0 | ZEND_ASSERT(result == URI_SUCCESS); |
84 | 0 | } |
85 | | |
86 | 0 | ZEND_ATTRIBUTE_NONNULL static UriUriA *get_normalized_uri(php_uri_parser_rfc3986_uris *uriparser_uris) { |
87 | 0 | if (!uriparser_uris->normalized_uri_initialized) { |
88 | 0 | int mask_result = uriNormalizeSyntaxMaskRequiredExA(&uriparser_uris->uri, &uriparser_uris->normalization_mask); |
89 | 0 | ZEND_ASSERT(mask_result == URI_SUCCESS); |
90 | |
|
91 | 0 | if (uriparser_uris->normalization_mask != URI_NORMALIZED) { |
92 | 0 | copy_uri(&uriparser_uris->normalized_uri, &uriparser_uris->uri); |
93 | 0 | int result = uriNormalizeSyntaxExMmA(&uriparser_uris->normalized_uri, uriparser_uris->normalization_mask, mm); |
94 | 0 | ZEND_ASSERT(result == URI_SUCCESS); |
95 | 0 | } |
96 | 0 | uriparser_uris->normalized_uri_initialized = true; |
97 | 0 | } |
98 | |
|
99 | 0 | if (uriparser_uris->normalization_mask == URI_NORMALIZED) { |
100 | 0 | return &uriparser_uris->uri; |
101 | 0 | } |
102 | | |
103 | 0 | return &uriparser_uris->normalized_uri; |
104 | 0 | } |
105 | | |
106 | | ZEND_ATTRIBUTE_NONNULL static UriUriA *get_uri_for_reading(php_uri_parser_rfc3986_uris *uriparser_uris, const php_uri_component_read_mode read_mode) |
107 | 0 | { |
108 | 0 | switch (read_mode) { |
109 | 0 | case PHP_URI_COMPONENT_READ_MODE_RAW: |
110 | 0 | return &uriparser_uris->uri; |
111 | 0 | case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII: |
112 | 0 | ZEND_FALLTHROUGH; |
113 | 0 | case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE: |
114 | 0 | return get_normalized_uri(uriparser_uris); |
115 | 0 | default: ZEND_UNREACHABLE(); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | ZEND_ATTRIBUTE_NONNULL static UriUriA *get_uri_for_writing(php_uri_parser_rfc3986_uris *uriparser_uris) |
120 | 0 | { |
121 | 0 | return &uriparser_uris->uri; |
122 | 0 | } |
123 | | |
124 | | ZEND_ATTRIBUTE_NONNULL void php_uri_parser_rfc3986_uri_type_read(php_uri_parser_rfc3986_uris *uri, zval *retval) |
125 | 0 | { |
126 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, PHP_URI_COMPONENT_READ_MODE_RAW); |
127 | |
|
128 | 0 | zend_object *value; |
129 | |
|
130 | 0 | if (has_text_range(&uriparser_uri->scheme)) { |
131 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_type, ZEND_ENUM_Uri_Rfc3986_UriType_Uri); |
132 | 0 | } else if (has_text_range(&uriparser_uri->hostText)) { |
133 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_type, ZEND_ENUM_Uri_Rfc3986_UriType_NetworkPathReference); |
134 | 0 | } else if (uriparser_uri->absolutePath) { |
135 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_type, ZEND_ENUM_Uri_Rfc3986_UriType_AbsolutePathReference); |
136 | 0 | } else { |
137 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_type, ZEND_ENUM_Uri_Rfc3986_UriType_RelativePathReference); |
138 | 0 | } |
139 | |
|
140 | 0 | ZVAL_OBJ_COPY(retval, value); |
141 | 0 | } |
142 | | |
143 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_scheme_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
144 | 0 | { |
145 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
146 | |
|
147 | 0 | if (has_text_range(&uriparser_uri->scheme)) { |
148 | 0 | ZVAL_STRINGL(retval, uriparser_uri->scheme.first, get_text_range_length(&uriparser_uri->scheme)); |
149 | 0 | } else { |
150 | 0 | ZVAL_NULL(retval); |
151 | 0 | } |
152 | |
|
153 | 0 | return SUCCESS; |
154 | 0 | } |
155 | | |
156 | | static zend_result php_uri_parser_rfc3986_scheme_write(void *uri, const zval *value, zval *errors) |
157 | 0 | { |
158 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
159 | 0 | int result; |
160 | |
|
161 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
162 | 0 | result = uriSetSchemeMmA(uriparser_uri, NULL, NULL, mm); |
163 | 0 | } else { |
164 | 0 | result = uriSetSchemeMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
165 | 0 | } |
166 | |
|
167 | 0 | switch (result) { |
168 | 0 | case URI_SUCCESS: |
169 | 0 | return SUCCESS; |
170 | 0 | case URI_ERROR_SYNTAX: |
171 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified scheme is malformed", 0); |
172 | 0 | return FAILURE; |
173 | 0 | default: |
174 | | /* This should be unreachable in practice. */ |
175 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the scheme", 0); |
176 | 0 | return FAILURE; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_userinfo_read(php_uri_parser_rfc3986_uris *uri, const php_uri_component_read_mode read_mode, zval *retval) |
181 | 0 | { |
182 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
183 | |
|
184 | 0 | if (has_text_range(&uriparser_uri->userInfo)) { |
185 | 0 | ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, get_text_range_length(&uriparser_uri->userInfo)); |
186 | 0 | } else { |
187 | 0 | ZVAL_NULL(retval); |
188 | 0 | } |
189 | |
|
190 | 0 | return SUCCESS; |
191 | 0 | } |
192 | | |
193 | | zend_result php_uri_parser_rfc3986_userinfo_write(php_uri_parser_rfc3986_uris *uri, const zval *value, zval *errors) |
194 | 0 | { |
195 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
196 | 0 | int result; |
197 | |
|
198 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
199 | 0 | result = uriSetUserInfoMmA(uriparser_uri, NULL, NULL, mm); |
200 | 0 | } else { |
201 | 0 | result = uriSetUserInfoMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
202 | 0 | } |
203 | |
|
204 | 0 | switch (result) { |
205 | 0 | case URI_SUCCESS: |
206 | 0 | return SUCCESS; |
207 | 0 | case URI_ERROR_SETUSERINFO_HOST_NOT_SET: |
208 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "Cannot set a userinfo without having a host", 0); |
209 | 0 | return FAILURE; |
210 | 0 | case URI_ERROR_SYNTAX: |
211 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified userinfo is malformed", 0); |
212 | 0 | return FAILURE; |
213 | 0 | default: |
214 | | /* This should be unreachable in practice. */ |
215 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the userinfo", 0); |
216 | 0 | return FAILURE; |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_username_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
221 | 0 | { |
222 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
223 | |
|
224 | 0 | if (has_text_range(&uriparser_uri->userInfo)) { |
225 | 0 | size_t length = get_text_range_length(&uriparser_uri->userInfo); |
226 | 0 | const char *c = memchr(uriparser_uri->userInfo.first, ':', length); |
227 | |
|
228 | 0 | if (c == NULL && length > 0) { |
229 | 0 | ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, length); |
230 | 0 | } else if (c != NULL && c - uriparser_uri->userInfo.first > 0) { |
231 | 0 | ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, c - uriparser_uri->userInfo.first); |
232 | 0 | } else { |
233 | 0 | ZVAL_EMPTY_STRING(retval); |
234 | 0 | } |
235 | 0 | } else { |
236 | 0 | ZVAL_NULL(retval); |
237 | 0 | } |
238 | |
|
239 | 0 | return SUCCESS; |
240 | 0 | } |
241 | | |
242 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_password_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
243 | 0 | { |
244 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
245 | |
|
246 | 0 | if (has_text_range(&uriparser_uri->userInfo)) { |
247 | 0 | const char *c = memchr(uriparser_uri->userInfo.first, ':', get_text_range_length(&uriparser_uri->userInfo)); |
248 | |
|
249 | 0 | if (c != NULL && uriparser_uri->userInfo.afterLast - c - 1 > 0) { |
250 | 0 | ZVAL_STRINGL(retval, c + 1, uriparser_uri->userInfo.afterLast - c - 1); |
251 | 0 | } else { |
252 | 0 | ZVAL_EMPTY_STRING(retval); |
253 | 0 | } |
254 | 0 | } else { |
255 | 0 | ZVAL_NULL(retval); |
256 | 0 | } |
257 | |
|
258 | 0 | return SUCCESS; |
259 | 0 | } |
260 | | |
261 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_host_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
262 | 0 | { |
263 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
264 | |
|
265 | 0 | if (has_text_range(&uriparser_uri->hostText)) { |
266 | 0 | if (uriparser_uri->hostData.ip6 != NULL || uriparser_uri->hostData.ipFuture.first != NULL) { |
267 | | /* the textual representation of the host is always accessible in the .hostText field no matter what the host is */ |
268 | 0 | zend_string *host_str = zend_string_concat3( |
269 | 0 | "[", 1, |
270 | 0 | uriparser_uri->hostText.first, get_text_range_length(&uriparser_uri->hostText), |
271 | 0 | "]", 1 |
272 | 0 | ); |
273 | 0 | ZVAL_NEW_STR(retval, host_str); |
274 | 0 | } else { |
275 | 0 | ZVAL_STRINGL(retval, uriparser_uri->hostText.first, get_text_range_length(&uriparser_uri->hostText)); |
276 | 0 | } |
277 | 0 | } else { |
278 | 0 | ZVAL_NULL(retval); |
279 | 0 | } |
280 | |
|
281 | 0 | return SUCCESS; |
282 | 0 | } |
283 | | |
284 | | ZEND_ATTRIBUTE_NONNULL void php_uri_parser_rfc3986_host_type_read(php_uri_parser_rfc3986_uris *uri, zval *retval) |
285 | 0 | { |
286 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, PHP_URI_COMPONENT_READ_MODE_RAW); |
287 | |
|
288 | 0 | if (!has_text_range(&uriparser_uri->hostText)) { |
289 | 0 | ZVAL_NULL(retval); |
290 | 0 | return; |
291 | 0 | } |
292 | | |
293 | 0 | zend_object *value; |
294 | |
|
295 | 0 | if (uriparser_uri->hostData.ip4 != NULL) { |
296 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_host_type, ZEND_ENUM_Uri_Rfc3986_UriHostType_IPv4); |
297 | 0 | } else if (uriparser_uri->hostData.ip6 != NULL) { |
298 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_host_type, ZEND_ENUM_Uri_Rfc3986_UriHostType_IPv6); |
299 | 0 | } else if (has_text_range(&uriparser_uri->hostData.ipFuture)) { |
300 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_host_type, ZEND_ENUM_Uri_Rfc3986_UriHostType_IPvFuture); |
301 | 0 | } else { |
302 | 0 | value = zend_enum_get_case_by_id(php_uri_ce_rfc3986_uri_host_type, ZEND_ENUM_Uri_Rfc3986_UriHostType_RegisteredName); |
303 | 0 | } |
304 | |
|
305 | 0 | ZVAL_OBJ_COPY(retval, value); |
306 | 0 | } |
307 | | |
308 | | static zend_result php_uri_parser_rfc3986_host_write(void *uri, const zval *value, zval *errors) |
309 | 0 | { |
310 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
311 | 0 | int result; |
312 | |
|
313 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
314 | 0 | result = uriSetHostAutoMmA(uriparser_uri, NULL, NULL, mm); |
315 | 0 | } else { |
316 | 0 | result = uriSetHostAutoMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
317 | 0 | } |
318 | |
|
319 | 0 | switch (result) { |
320 | 0 | case URI_SUCCESS: |
321 | 0 | return SUCCESS; |
322 | 0 | case URI_ERROR_SETHOST_PORT_SET: |
323 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "Cannot remove the host from a URI that has a port", 0); |
324 | 0 | return FAILURE; |
325 | 0 | case URI_ERROR_SETHOST_USERINFO_SET: |
326 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "Cannot remove the host from a URI that has a userinfo", 0); |
327 | 0 | return FAILURE; |
328 | 0 | case URI_ERROR_SYNTAX: |
329 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified host is malformed", 0); |
330 | 0 | return FAILURE; |
331 | 0 | default: |
332 | | /* This should be unreachable in practice. */ |
333 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the host", 0); |
334 | 0 | return FAILURE; |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | ZEND_ATTRIBUTE_NONNULL static zend_long port_str_to_zend_long_checked(const char *str, const size_t len) |
339 | 0 | { |
340 | 0 | if (len > MAX_LENGTH_OF_LONG) { |
341 | 0 | return -1; |
342 | 0 | } |
343 | | |
344 | 0 | char buf[MAX_LENGTH_OF_LONG + 1]; |
345 | 0 | *(char*)zend_mempcpy(buf, str, len) = 0; |
346 | |
|
347 | 0 | zend_ulong result = ZEND_STRTOUL(buf, NULL, 10); |
348 | |
|
349 | 0 | if (result > ZEND_LONG_MAX) { |
350 | 0 | return -1; |
351 | 0 | } |
352 | | |
353 | 0 | return (zend_long)result; |
354 | 0 | } |
355 | | |
356 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_port_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
357 | 0 | { |
358 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
359 | |
|
360 | 0 | if (has_text_range(&uriparser_uri->portText) && get_text_range_length(&uriparser_uri->portText) > 0) { |
361 | 0 | ZVAL_LONG(retval, port_str_to_zend_long_checked(uriparser_uri->portText.first, get_text_range_length(&uriparser_uri->portText))); |
362 | 0 | } else { |
363 | 0 | ZVAL_NULL(retval); |
364 | 0 | } |
365 | |
|
366 | 0 | return SUCCESS; |
367 | 0 | } |
368 | | |
369 | | static zend_result php_uri_parser_rfc3986_port_write(void *uri, const zval *value, zval *errors) |
370 | 0 | { |
371 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
372 | 0 | int result; |
373 | |
|
374 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
375 | 0 | result = uriSetPortTextMmA(uriparser_uri, NULL, NULL, mm); |
376 | 0 | } else { |
377 | 0 | zend_string *tmp = zend_long_to_str(Z_LVAL_P(value)); |
378 | 0 | result = uriSetPortTextMmA(uriparser_uri, ZSTR_VAL(tmp), ZSTR_VAL(tmp) + ZSTR_LEN(tmp), mm); |
379 | 0 | zend_string_release_ex(tmp, false); |
380 | 0 | } |
381 | |
|
382 | 0 | switch (result) { |
383 | 0 | case URI_SUCCESS: |
384 | 0 | return SUCCESS; |
385 | 0 | case URI_ERROR_SETPORT_HOST_NOT_SET: |
386 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "Cannot set a port without having a host", 0); |
387 | 0 | return FAILURE; |
388 | 0 | case URI_ERROR_SYNTAX: |
389 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified port is malformed", 0); |
390 | 0 | return FAILURE; |
391 | 0 | default: |
392 | | /* This should be unreachable in practice. */ |
393 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the port", 0); |
394 | 0 | return FAILURE; |
395 | 0 | } |
396 | 0 | } |
397 | | |
398 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_path_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
399 | 0 | { |
400 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
401 | |
|
402 | 0 | if (uriparser_uri->pathHead != NULL) { |
403 | 0 | size_t total_len = 0; |
404 | 0 | const bool need_leading_slash = uriparser_uri->absolutePath || uriHasHostA(uriparser_uri); |
405 | |
|
406 | 0 | if (need_leading_slash) { |
407 | 0 | total_len++; |
408 | 0 | } |
409 | |
|
410 | 0 | for (const UriPathSegmentA *p = uriparser_uri->pathHead; p; p = p->next) { |
411 | 0 | total_len += get_text_range_length(&p->text); |
412 | 0 | if (p->next) { |
413 | 0 | total_len++; |
414 | 0 | } |
415 | 0 | } |
416 | |
|
417 | 0 | zend_string *str = zend_string_alloc(total_len, false); |
418 | 0 | char *out = ZSTR_VAL(str); |
419 | |
|
420 | 0 | if (need_leading_slash) { |
421 | 0 | *(out++) = '/'; |
422 | 0 | } |
423 | |
|
424 | 0 | for (const UriPathSegmentA *p = uriparser_uri->pathHead; p; p = p->next) { |
425 | 0 | out = zend_mempcpy(out, p->text.first, get_text_range_length(&p->text)); |
426 | 0 | if (p->next) { |
427 | 0 | *(out++) = '/'; |
428 | 0 | } |
429 | 0 | } |
430 | |
|
431 | 0 | *out = '\0'; |
432 | 0 | ZVAL_NEW_STR(retval, str); |
433 | 0 | } else if (uriparser_uri->absolutePath) { |
434 | 0 | ZVAL_CHAR(retval, '/'); |
435 | 0 | } else { |
436 | 0 | ZVAL_EMPTY_STRING(retval); |
437 | 0 | } |
438 | |
|
439 | 0 | return SUCCESS; |
440 | 0 | } |
441 | | |
442 | | static zend_result php_uri_parser_rfc3986_path_write(void *uri, const zval *value, zval *errors) |
443 | 0 | { |
444 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
445 | 0 | int result; |
446 | |
|
447 | 0 | if (Z_STRLEN_P(value) == 0) { |
448 | 0 | result = uriSetPathMmA(uriparser_uri, NULL, NULL, mm); |
449 | 0 | } else { |
450 | 0 | result = uriSetPathMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
451 | 0 | } |
452 | |
|
453 | 0 | switch (result) { |
454 | 0 | case URI_SUCCESS: |
455 | 0 | return SUCCESS; |
456 | 0 | case URI_ERROR_SYNTAX: |
457 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified path is malformed", 0); |
458 | 0 | return FAILURE; |
459 | 0 | default: |
460 | | /* This should be unreachable in practice. */ |
461 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the path", 0); |
462 | 0 | return FAILURE; |
463 | 0 | } |
464 | 0 | } |
465 | | |
466 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_query_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
467 | 0 | { |
468 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
469 | |
|
470 | 0 | if (has_text_range(&uriparser_uri->query)) { |
471 | 0 | ZVAL_STRINGL(retval, uriparser_uri->query.first, get_text_range_length(&uriparser_uri->query)); |
472 | 0 | } else { |
473 | 0 | ZVAL_NULL(retval); |
474 | 0 | } |
475 | |
|
476 | 0 | return SUCCESS; |
477 | 0 | } |
478 | | |
479 | | static zend_result php_uri_parser_rfc3986_query_write(void *uri, const zval *value, zval *errors) |
480 | 0 | { |
481 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
482 | 0 | int result; |
483 | |
|
484 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
485 | 0 | result = uriSetQueryMmA(uriparser_uri, NULL, NULL, mm); |
486 | 0 | } else { |
487 | 0 | result = uriSetQueryMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
488 | 0 | } |
489 | |
|
490 | 0 | switch (result) { |
491 | 0 | case URI_SUCCESS: |
492 | 0 | return SUCCESS; |
493 | 0 | case URI_ERROR_SYNTAX: |
494 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified query is malformed", 0); |
495 | 0 | return FAILURE; |
496 | 0 | default: |
497 | | /* This should be unreachable in practice. */ |
498 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the query", 0); |
499 | 0 | return FAILURE; |
500 | 0 | } |
501 | 0 | } |
502 | | |
503 | | ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_fragment_read(void *uri, const php_uri_component_read_mode read_mode, zval *retval) |
504 | 0 | { |
505 | 0 | const UriUriA *uriparser_uri = get_uri_for_reading(uri, read_mode); |
506 | |
|
507 | 0 | if (has_text_range(&uriparser_uri->fragment)) { |
508 | 0 | ZVAL_STRINGL(retval, uriparser_uri->fragment.first, get_text_range_length(&uriparser_uri->fragment)); |
509 | 0 | } else { |
510 | 0 | ZVAL_NULL(retval); |
511 | 0 | } |
512 | |
|
513 | 0 | return SUCCESS; |
514 | 0 | } |
515 | | |
516 | | static zend_result php_uri_parser_rfc3986_fragment_write(void *uri, const zval *value, zval *errors) |
517 | 0 | { |
518 | 0 | UriUriA *uriparser_uri = get_uri_for_writing(uri); |
519 | 0 | int result; |
520 | |
|
521 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
522 | 0 | result = uriSetFragmentMmA(uriparser_uri, NULL, NULL, mm); |
523 | 0 | } else { |
524 | 0 | result = uriSetFragmentMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm); |
525 | 0 | } |
526 | |
|
527 | 0 | switch (result) { |
528 | 0 | case URI_SUCCESS: |
529 | 0 | return SUCCESS; |
530 | 0 | case URI_ERROR_SYNTAX: |
531 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified fragment is malformed", 0); |
532 | 0 | return FAILURE; |
533 | 0 | default: |
534 | | /* This should be unreachable in practice. */ |
535 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to update the fragment", 0); |
536 | 0 | return FAILURE; |
537 | 0 | } |
538 | 0 | } |
539 | | |
540 | | static php_uri_parser_rfc3986_uris *uriparser_create_uris(void) |
541 | 0 | { |
542 | 0 | php_uri_parser_rfc3986_uris *uriparser_uris = ecalloc(1, sizeof(*uriparser_uris)); |
543 | 0 | uriparser_uris->normalized_uri_initialized = false; |
544 | |
|
545 | 0 | return uriparser_uris; |
546 | 0 | } |
547 | | |
548 | | static zend_result php_uri_parser_rfc3986_add_base_url( |
549 | | UriUriA *tmp, const UriUriA *uri, const php_uri_parser_rfc3986_uris *uriparser_base_urls, const bool silent |
550 | 0 | ) { |
551 | 0 | const int result = uriAddBaseUriExMmA(tmp, uri, &uriparser_base_urls->uri, URI_RESOLVE_STRICTLY, mm); |
552 | 0 | if (result != URI_SUCCESS) { |
553 | 0 | if (!silent) { |
554 | 0 | switch (result) { |
555 | 0 | case URI_ERROR_ADDBASE_REL_BASE: |
556 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified base URI must be absolute", 0); |
557 | 0 | break; |
558 | 0 | default: |
559 | | /* This should be unreachable in practice. */ |
560 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to resolve the specified URI against the base URI", 0); |
561 | 0 | break; |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | 0 | return FAILURE; |
566 | 0 | } |
567 | | |
568 | 0 | return SUCCESS; |
569 | 0 | } |
570 | | |
571 | | php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_parse_ex(const char *uri_str, const size_t uri_str_len, const php_uri_parser_rfc3986_uris *uriparser_base_urls, const bool silent) |
572 | 0 | { |
573 | 0 | UriUriA uri = {0}; |
574 | | |
575 | | /* Parse the URI. */ |
576 | 0 | const int result = uriParseSingleUriExMmA(&uri, uri_str, uri_str + uri_str_len, NULL, mm); |
577 | 0 | if (result != URI_SUCCESS) { |
578 | 0 | if (!silent) { |
579 | 0 | switch (result) { |
580 | 0 | case URI_ERROR_SYNTAX: |
581 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified URI is malformed", 0); |
582 | 0 | break; |
583 | 0 | default: |
584 | | /* This should be unreachable in practice. */ |
585 | 0 | zend_throw_exception(php_uri_ce_error, "Failed to parse the specified URI", 0); |
586 | 0 | break; |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | 0 | goto fail; |
591 | 0 | } |
592 | | |
593 | 0 | if (uriparser_base_urls != NULL) { |
594 | 0 | UriUriA tmp = {0}; |
595 | | |
596 | | /* Combine the parsed URI with the base URI and store the result in 'tmp', |
597 | | * since the target and source URLs must be distinct. */ |
598 | 0 | if (php_uri_parser_rfc3986_add_base_url(&tmp, &uri, uriparser_base_urls, silent) == FAILURE) { |
599 | 0 | goto fail; |
600 | 0 | } |
601 | | |
602 | | /* Store the combined URI back into 'uri'. */ |
603 | 0 | uriFreeUriMembersMmA(&uri, mm); |
604 | 0 | uri = tmp; |
605 | 0 | } |
606 | | |
607 | | /* Make the resulting URI independent of the 'uri_str'. */ |
608 | 0 | uriMakeOwnerMmA(&uri, mm); |
609 | |
|
610 | 0 | if (has_text_range(&uri.portText) && get_text_range_length(&uri.portText) > 0) { |
611 | 0 | if (port_str_to_zend_long_checked(uri.portText.first, get_text_range_length(&uri.portText)) == -1) { |
612 | 0 | if (!silent) { |
613 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The port is out of range", 0); |
614 | 0 | } |
615 | |
|
616 | 0 | goto fail; |
617 | 0 | } |
618 | 0 | } |
619 | | |
620 | 0 | php_uri_parser_rfc3986_uris *uriparser_uris = uriparser_create_uris(); |
621 | 0 | uriparser_uris->uri = uri; |
622 | |
|
623 | 0 | return uriparser_uris; |
624 | | |
625 | 0 | fail: |
626 | |
|
627 | 0 | uriFreeUriMembersMmA(&uri, mm); |
628 | |
|
629 | 0 | return NULL; |
630 | 0 | } |
631 | | |
632 | | static void *php_uri_parser_rfc3986_parse(const char *uri_str, const size_t uri_str_len, const void *base_url, zval *errors, bool silent) |
633 | 0 | { |
634 | 0 | return php_uri_parser_rfc3986_parse_ex(uri_str, uri_str_len, base_url, silent); |
635 | 0 | } |
636 | | |
637 | | ZEND_ATTRIBUTE_NONNULL static void *php_uri_parser_rfc3986_clone(void *uri) |
638 | 0 | { |
639 | 0 | const php_uri_parser_rfc3986_uris *uriparser_uris = uri; |
640 | |
|
641 | 0 | php_uri_parser_rfc3986_uris *new_uriparser_uris = uriparser_create_uris(); |
642 | 0 | copy_uri(&new_uriparser_uris->uri, &uriparser_uris->uri); |
643 | | /* Do not copy the normalized URI: The expected action after cloning is |
644 | | * modifying the cloned URI (which will invalidate the cached normalized |
645 | | * URI). */ |
646 | |
|
647 | 0 | return new_uriparser_uris; |
648 | 0 | } |
649 | | |
650 | | ZEND_ATTRIBUTE_NONNULL static zend_string *php_uri_parser_rfc3986_to_string(void *uri, const php_uri_recomposition_mode recomposition_mode, const bool exclude_fragment) |
651 | 0 | { |
652 | 0 | php_uri_parser_rfc3986_uris *uriparser_uris = uri; |
653 | 0 | const UriUriA *uriparser_uri; |
654 | |
|
655 | 0 | if (recomposition_mode == PHP_URI_RECOMPOSITION_MODE_RAW_ASCII || recomposition_mode == PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE) { |
656 | 0 | uriparser_uri = &uriparser_uris->uri; |
657 | 0 | } else { |
658 | 0 | uriparser_uri = get_normalized_uri(uriparser_uris); |
659 | 0 | } |
660 | |
|
661 | 0 | int charsRequired = 0; |
662 | 0 | int result = uriToStringCharsRequiredA(uriparser_uri, &charsRequired); |
663 | 0 | ZEND_ASSERT(result == URI_SUCCESS); |
664 | |
|
665 | 0 | charsRequired++; |
666 | |
|
667 | 0 | zend_string *uri_string = zend_string_alloc(charsRequired - 1, false); |
668 | 0 | result = uriToStringA(ZSTR_VAL(uri_string), uriparser_uri, charsRequired, NULL); |
669 | 0 | ZEND_ASSERT(result == URI_SUCCESS); |
670 | |
|
671 | 0 | if (exclude_fragment) { |
672 | 0 | const char *pos = zend_memrchr(ZSTR_VAL(uri_string), '#', ZSTR_LEN(uri_string)); |
673 | 0 | if (pos != NULL) { |
674 | 0 | uri_string = zend_string_truncate(uri_string, (pos - ZSTR_VAL(uri_string)), false); |
675 | 0 | ZSTR_VAL(uri_string)[ZSTR_LEN(uri_string)] = '\0'; |
676 | 0 | } |
677 | 0 | } |
678 | |
|
679 | 0 | return uri_string; |
680 | 0 | } |
681 | | |
682 | | static void php_uri_parser_rfc3986_destroy(void *uri) |
683 | 16 | { |
684 | 16 | php_uri_parser_rfc3986_uris *uriparser_uris = uri; |
685 | | |
686 | 16 | if (UNEXPECTED(uriparser_uris == NULL)) { |
687 | 16 | return; |
688 | 16 | } |
689 | | |
690 | 0 | uriFreeUriMembersMmA(&uriparser_uris->uri, mm); |
691 | 0 | uriFreeUriMembersMmA(&uriparser_uris->normalized_uri, mm); |
692 | |
|
693 | 0 | efree(uriparser_uris); |
694 | 0 | } |
695 | | |
696 | | static zend_always_inline zend_result php_uri_parser_rfc3986_validate_component_result(const bool well_formed, const char *component_name) |
697 | 0 | { |
698 | 0 | if (well_formed) { |
699 | 0 | return SUCCESS; |
700 | 0 | } |
701 | | |
702 | 0 | zend_throw_exception_ex(php_uri_ce_invalid_uri_exception, 0, "The specified %s is malformed", component_name); |
703 | 0 | return FAILURE; |
704 | 0 | } |
705 | | |
706 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_scheme(const zend_string *scheme) |
707 | 0 | { |
708 | 0 | const char *p = ZSTR_VAL(scheme); |
709 | 0 | const size_t len = ZSTR_LEN(scheme); |
710 | 0 | const bool well_formed = uriIsWellFormedSchemeA(p, p + len) == URI_TRUE; |
711 | |
|
712 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "scheme"); |
713 | 0 | } |
714 | | |
715 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_userinfo(const zend_string *userinfo) |
716 | 0 | { |
717 | 0 | const char *p = ZSTR_VAL(userinfo); |
718 | 0 | const size_t len = ZSTR_LEN(userinfo); |
719 | 0 | const bool well_formed = uriIsWellFormedUserInfoA(p, p + len) == URI_TRUE; |
720 | |
|
721 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "userinfo"); |
722 | 0 | } |
723 | | |
724 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_host(const zend_string *host) |
725 | 0 | { |
726 | 0 | const char *p = ZSTR_VAL(host); |
727 | 0 | const size_t len = ZSTR_LEN(host); |
728 | |
|
729 | 0 | if (len == 0) { |
730 | 0 | return SUCCESS; |
731 | 0 | } |
732 | | |
733 | 0 | if (p[0] == '[') { |
734 | 0 | if (p[len - 1] != ']') { |
735 | 0 | return php_uri_parser_rfc3986_validate_component_result(false, "host"); |
736 | 0 | } |
737 | | |
738 | 0 | if (len >= 2 && (p[1] == 'v' || p[1] == 'V')) { |
739 | 0 | return php_uri_parser_rfc3986_validate_component_result( |
740 | 0 | uriIsWellFormedHostIpFutureMmA(p + 1, p + len - 1, mm) == URI_SUCCESS, |
741 | 0 | "host" |
742 | 0 | ); |
743 | 0 | } |
744 | | |
745 | 0 | return php_uri_parser_rfc3986_validate_component_result( |
746 | 0 | uriIsWellFormedHostIp6MmA(p + 1, p + len - 1, mm) == URI_SUCCESS, |
747 | 0 | "host" |
748 | 0 | ); |
749 | 0 | } |
750 | | |
751 | 0 | if (uriIsWellFormedHostIp4A(p, p + len) == URI_TRUE) { |
752 | 0 | return SUCCESS; |
753 | 0 | } |
754 | | |
755 | 0 | return php_uri_parser_rfc3986_validate_component_result( |
756 | 0 | uriIsWellFormedHostRegNameA(p, p + len) == URI_TRUE, |
757 | 0 | "host" |
758 | 0 | ); |
759 | 0 | } |
760 | | |
761 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_port(const zend_long port) |
762 | 0 | { |
763 | 0 | char buf[MAX_LENGTH_OF_LONG + 1]; |
764 | 0 | const char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, port); |
765 | |
|
766 | 0 | const bool well_formed = uriIsWellFormedPortA(res, res + strlen(res)); |
767 | |
|
768 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "port"); |
769 | 0 | } |
770 | | |
771 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_path(const zend_string *path) |
772 | 0 | { |
773 | 0 | const char *p = ZSTR_VAL(path); |
774 | 0 | const size_t len = ZSTR_LEN(path); |
775 | | /* The build() method checks whether the path begins with a "/" when there's a host. |
776 | | * In order to skip doing the same check, a false hasHost argument is passed to uriIsWellFormedPathA(). */ |
777 | 0 | const bool well_formed = uriIsWellFormedPathA(p, p + len, /* hasHost */ false); |
778 | |
|
779 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "path"); |
780 | 0 | } |
781 | | |
782 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_query(const zend_string *query) |
783 | 0 | { |
784 | 0 | const char *p = ZSTR_VAL(query); |
785 | 0 | const size_t len = ZSTR_LEN(query); |
786 | 0 | const bool well_formed = uriIsWellFormedQueryA(p, p + len); |
787 | |
|
788 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "query"); |
789 | 0 | } |
790 | | |
791 | | ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_validate_fragment(const zend_string *fragment) |
792 | 0 | { |
793 | 0 | const char *p = ZSTR_VAL(fragment); |
794 | 0 | const size_t len = ZSTR_LEN(fragment); |
795 | 0 | const bool well_formed = uriIsWellFormedFragmentA(p, p + len); |
796 | |
|
797 | 0 | return php_uri_parser_rfc3986_validate_component_result(well_formed, "fragment"); |
798 | 0 | } |
799 | | |
800 | | ZEND_ATTRIBUTE_NONNULL_ARGS(2,3,4,5,6,7,8) php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_build_from_zval( |
801 | | const php_uri_parser_rfc3986_uris *uriparser_base_uris, |
802 | | const zval *scheme, const zval *userinfo, const zval *host, const zval *port, |
803 | | const zval *path, const zval *query, const zval *fragment |
804 | 0 | ) { |
805 | 0 | php_uri_parser_rfc3986_uris *uriparser_uris = uriparser_create_uris(); |
806 | |
|
807 | 0 | if (Z_STRLEN_P(path) > 0) { |
808 | | /* The first segment of the path must not contain ":" if the URI does not contain a scheme */ |
809 | 0 | if (Z_TYPE_P(scheme) == IS_NULL) { |
810 | 0 | const char *p = Z_STRVAL_P(path); |
811 | 0 | while (*p != '\0' && *p != '/') { |
812 | 0 | if (*p == ':') { |
813 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The path must not begin with \":\" when the URI does not contain a scheme", 0); |
814 | 0 | goto failure; |
815 | 0 | } |
816 | | |
817 | 0 | p++; |
818 | 0 | } |
819 | 0 | } |
820 | | |
821 | | /* The path must not begin with "//" if the URI does not contain a host */ |
822 | 0 | if (Z_TYPE_P(host) == IS_NULL && zend_string_starts_with_literal(Z_STR_P(path), "//")) { |
823 | 0 | zend_throw_exception(php_uri_ce_invalid_uri_exception, "The path must not begin with \"//\" when the URI does not contain a host", 0); |
824 | 0 | goto failure; |
825 | 0 | } |
826 | 0 | } |
827 | | |
828 | 0 | zend_result result = php_uri_parser_rfc3986_scheme_write(uriparser_uris, scheme, NULL); |
829 | 0 | if (result == FAILURE) { |
830 | 0 | goto failure; |
831 | 0 | } |
832 | 0 | result = php_uri_parser_rfc3986_host_write(uriparser_uris, host, NULL); |
833 | 0 | if (result == FAILURE) { |
834 | 0 | goto failure; |
835 | 0 | } |
836 | | /* Intentionally writing userinfo after host to avoid error when the userinfo is set but the host is missing */ |
837 | 0 | result = php_uri_parser_rfc3986_userinfo_write(uriparser_uris, userinfo, NULL); |
838 | 0 | if (result == FAILURE) { |
839 | 0 | goto failure; |
840 | 0 | } |
841 | | /* Intentionally writing userinfo after host to avoid error when the port is set but the host is missing */ |
842 | 0 | result = php_uri_parser_rfc3986_port_write(uriparser_uris, port, NULL); |
843 | 0 | if (result == FAILURE) { |
844 | 0 | goto failure; |
845 | 0 | } |
846 | 0 | result = php_uri_parser_rfc3986_path_write(uriparser_uris, path, NULL); |
847 | 0 | if (result == FAILURE) { |
848 | 0 | goto failure; |
849 | 0 | } |
850 | 0 | result = php_uri_parser_rfc3986_query_write(uriparser_uris, query, NULL); |
851 | 0 | if (result == FAILURE) { |
852 | 0 | goto failure; |
853 | 0 | } |
854 | 0 | result = php_uri_parser_rfc3986_fragment_write(uriparser_uris, fragment, NULL); |
855 | 0 | if (result == FAILURE) { |
856 | 0 | goto failure; |
857 | 0 | } |
858 | | |
859 | 0 | if (uriparser_base_uris != NULL) { |
860 | 0 | UriUriA tmp = {0}; |
861 | |
|
862 | 0 | if (php_uri_parser_rfc3986_add_base_url(&tmp, &uriparser_uris->uri, uriparser_base_uris, false) == FAILURE) { |
863 | 0 | goto failure; |
864 | 0 | } |
865 | | |
866 | 0 | uriMakeOwnerMmA(&tmp, mm); |
867 | 0 | uriFreeUriMembersMmA(&uriparser_uris->uri, mm); |
868 | 0 | uriparser_uris->uri = tmp; |
869 | 0 | } |
870 | | |
871 | 0 | return uriparser_uris; |
872 | | |
873 | 0 | failure: |
874 | 0 | uriFreeUriMembersMmA(&uriparser_uris->uri, mm); |
875 | 0 | efree(uriparser_uris); |
876 | | return NULL; |
877 | 0 | } |
878 | | |
879 | | PHPAPI const php_uri_parser php_uri_parser_rfc3986 = { |
880 | | .name = PHP_URI_PARSER_RFC3986, |
881 | | .parse = php_uri_parser_rfc3986_parse, |
882 | | .clone = php_uri_parser_rfc3986_clone, |
883 | | .to_string = php_uri_parser_rfc3986_to_string, |
884 | | .destroy = php_uri_parser_rfc3986_destroy, |
885 | | { |
886 | | .scheme = {.read = php_uri_parser_rfc3986_scheme_read, .write = php_uri_parser_rfc3986_scheme_write}, |
887 | | .username = {.read = php_uri_parser_rfc3986_username_read, .write = NULL}, |
888 | | .password = {.read = php_uri_parser_rfc3986_password_read, .write = NULL}, |
889 | | .host = {.read = php_uri_parser_rfc3986_host_read, .write = php_uri_parser_rfc3986_host_write}, |
890 | | .port = {.read = php_uri_parser_rfc3986_port_read, .write = php_uri_parser_rfc3986_port_write}, |
891 | | .path = {.read = php_uri_parser_rfc3986_path_read, .write = php_uri_parser_rfc3986_path_write}, |
892 | | .query = {.read = php_uri_parser_rfc3986_query_read, .write = php_uri_parser_rfc3986_query_write}, |
893 | | .fragment = {.read = php_uri_parser_rfc3986_fragment_read, .write = php_uri_parser_rfc3986_fragment_write}, |
894 | | } |
895 | | }; |