Coverage Report

Created: 2025-09-27 06:26

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