Coverage Report

Created: 2025-11-16 06:23

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
/* 4.2. URL miscellaneous: A URL includes credentials if its username or password is not the empty string. */
278
static bool includes_credentials(const lxb_url_t *lexbor_uri)
279
0
{
280
0
  return lexbor_uri->username.length > 0 || lexbor_uri->password.length > 0;
281
0
}
282
283
static zend_result php_uri_parser_whatwg_username_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
284
0
{
285
0
  const lxb_url_t *lexbor_uri = uri;
286
287
0
  if (includes_credentials(lexbor_uri)) {
288
0
    ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->username.data, lexbor_uri->username.length);
289
0
  } else {
290
0
    ZVAL_NULL(retval);
291
0
  }
292
293
0
  return SUCCESS;
294
0
}
295
296
static zend_result php_uri_parser_whatwg_username_write(void *uri, zval *value, zval *errors)
297
0
{
298
0
  lxb_url_t *lexbor_uri = uri;
299
0
  lexbor_str_t str = {0};
300
301
0
  zval_string_or_null_to_lexbor_str(value, &str);
302
303
0
  if (lxb_url_api_username_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) {
304
0
    throw_invalid_url_exception_during_write(errors, "username");
305
306
0
    return FAILURE;
307
0
  }
308
309
0
  return SUCCESS;
310
0
}
311
312
static zend_result php_uri_parser_whatwg_password_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
313
0
{
314
0
  const lxb_url_t *lexbor_uri = uri;
315
316
0
  if (includes_credentials(lexbor_uri)) {
317
0
    ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->password.data, lexbor_uri->password.length);
318
0
  } else {
319
0
    ZVAL_NULL(retval);
320
0
  }
321
322
0
  return SUCCESS;
323
0
}
324
325
static zend_result php_uri_parser_whatwg_password_write(void *uri, zval *value, zval *errors)
326
0
{
327
0
  lxb_url_t *lexbor_uri = uri;
328
0
  lexbor_str_t str = {0};
329
330
0
  zval_string_or_null_to_lexbor_str(value, &str);
331
332
0
  if (lxb_url_api_password_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) {
333
0
    throw_invalid_url_exception_during_write(errors, "password");
334
335
0
    return FAILURE;
336
0
  }
337
338
0
  return SUCCESS;
339
0
}
340
341
static zend_result php_uri_parser_whatwg_host_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
342
0
{
343
0
  const lxb_url_t *lexbor_uri = uri;
344
345
0
  if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV4) {
346
0
    smart_str host_str = {0};
347
348
0
    lxb_url_serialize_host_ipv4(lexbor_uri->host.u.ipv4, serialize_to_smart_str_callback, &host_str);
349
350
0
    ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
351
0
  } else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV6) {
352
0
    smart_str host_str = {0};
353
354
0
    smart_str_appendc(&host_str, '[');
355
0
    lxb_url_serialize_host_ipv6(lexbor_uri->host.u.ipv6, serialize_to_smart_str_callback, &host_str);
356
0
    smart_str_appendc(&host_str, ']');
357
358
0
    ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
359
0
  } else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_EMPTY) {
360
0
    ZVAL_EMPTY_STRING(retval);
361
0
  } else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) {
362
0
    switch (read_mode) {
363
0
      case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE: {
364
0
        smart_str host_str = {0};
365
0
        lxb_url_serialize_host_unicode(&lexbor_idna, &lexbor_uri->host, serialize_to_smart_str_callback, &host_str);
366
0
        lxb_unicode_idna_clean(&lexbor_idna);
367
368
0
        ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
369
0
        break;
370
0
      }
371
0
      case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII:
372
0
        ZEND_FALLTHROUGH;
373
0
      case PHP_URI_COMPONENT_READ_MODE_RAW:
374
0
        ZVAL_STRINGL(retval, (const char *) lexbor_uri->host.u.domain.data, lexbor_uri->host.u.domain.length);
375
0
        break;
376
0
      EMPTY_SWITCH_DEFAULT_CASE()
377
0
    }
378
0
  } else {
379
0
    ZVAL_NULL(retval);
380
0
  }
381
382
0
  return SUCCESS;
383
0
}
384
385
static zend_result php_uri_parser_whatwg_host_write(void *uri, zval *value, zval *errors)
386
0
{
387
0
  lxb_url_t *lexbor_uri = uri;
388
0
  lexbor_str_t str = {0};
389
390
0
  zval_string_or_null_to_lexbor_str(value, &str);
391
392
0
  if (lxb_url_api_hostname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
393
0
    throw_invalid_url_exception_during_write(errors, "host");
394
395
0
    return FAILURE;
396
0
  }
397
398
0
  return SUCCESS;
399
0
}
400
401
static zend_result php_uri_parser_whatwg_port_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
402
0
{
403
0
  const lxb_url_t *lexbor_uri = uri;
404
405
0
  if (lexbor_uri->has_port) {
406
0
    ZVAL_LONG(retval, lexbor_uri->port);
407
0
  } else {
408
0
    ZVAL_NULL(retval);
409
0
  }
410
411
0
  return SUCCESS;
412
0
}
413
414
static zend_result php_uri_parser_whatwg_port_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_long_or_null_to_lexbor_str(value, &str);
420
421
0
  if (lxb_url_api_port_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
422
0
    throw_invalid_url_exception_during_write(errors, "port");
423
424
0
    return FAILURE;
425
0
  }
426
427
0
  return SUCCESS;
428
0
}
429
430
static zend_result php_uri_parser_whatwg_path_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->path.str.length > 0) {
435
0
    ZVAL_STRINGL(retval, (const char *) lexbor_uri->path.str.data, lexbor_uri->path.str.length);
436
0
  } else {
437
0
    ZVAL_EMPTY_STRING(retval);
438
0
  }
439
440
0
  return SUCCESS;
441
0
}
442
443
static zend_result php_uri_parser_whatwg_path_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_string_or_null_to_lexbor_str(value, &str);
449
450
0
  if (lxb_url_api_pathname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
451
0
    throw_invalid_url_exception_during_write(errors, "path");
452
453
0
    return FAILURE;
454
0
  }
455
456
0
  return SUCCESS;
457
0
}
458
459
static zend_result php_uri_parser_whatwg_query_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->query.data != NULL) {
464
0
    ZVAL_STRINGL(retval, (const char *) lexbor_uri->query.data, lexbor_uri->query.length);
465
0
  } else {
466
0
    ZVAL_NULL(retval);
467
0
  }
468
469
0
  return SUCCESS;
470
0
}
471
472
static zend_result php_uri_parser_whatwg_query_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_search_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
480
0
    throw_invalid_url_exception_during_write(errors, "query string");
481
482
0
    return FAILURE;
483
0
  }
484
485
0
  return SUCCESS;
486
0
}
487
488
static zend_result php_uri_parser_whatwg_fragment_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->fragment.data != NULL) {
493
0
    ZVAL_STRINGL(retval, (const char *) lexbor_uri->fragment.data, lexbor_uri->fragment.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_fragment_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_hash_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
509
0
    throw_invalid_url_exception_during_write(errors, "fragment");
510
511
0
    return FAILURE;
512
0
  }
513
514
0
  return SUCCESS;
515
0
}
516
517
PHP_RINIT_FUNCTION(uri_parser_whatwg)
518
247k
{
519
247k
  lxb_status_t status;
520
  
521
247k
  status = lexbor_mraw_init(&lexbor_mraw, lexbor_mraw_byte_size);
522
247k
  if (status != LXB_STATUS_OK) {
523
0
    goto fail;
524
0
  }
525
526
247k
  status = lxb_url_parser_init(&lexbor_parser, &lexbor_mraw);
527
247k
  if (status != LXB_STATUS_OK) {
528
0
    goto fail;
529
0
  }
530
531
247k
  status = lxb_unicode_idna_init(&lexbor_idna);
532
247k
  if (status != LXB_STATUS_OK) {
533
0
    goto fail;
534
0
  }
535
536
247k
  return SUCCESS;
537
538
0
 fail:
539
540
  /* Unconditionally calling the _destroy() functions is
541
   * safe on a zeroed structure. */
542
0
  lxb_unicode_idna_destroy(&lexbor_idna, false);
543
0
  memset(&lexbor_idna, 0, sizeof(lexbor_idna));
544
0
  lxb_url_parser_destroy(&lexbor_parser, false);
545
0
  memset(&lexbor_parser, 0, sizeof(lexbor_parser));
546
0
  lexbor_mraw_destroy(&lexbor_mraw, false);
547
0
  memset(&lexbor_mraw, 0, sizeof(lexbor_mraw));
548
549
0
  return FAILURE;
550
247k
}
551
552
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri_parser_whatwg)
553
247k
{
554
247k
  lxb_unicode_idna_destroy(&lexbor_idna, false);
555
247k
  memset(&lexbor_idna, 0, sizeof(lexbor_idna));
556
247k
  lxb_url_parser_destroy(&lexbor_parser, false);
557
247k
  memset(&lexbor_parser, 0, sizeof(lexbor_parser));
558
247k
  lexbor_mraw_destroy(&lexbor_mraw, false);
559
247k
  memset(&lexbor_mraw, 0, sizeof(lexbor_mraw));
560
561
247k
  return SUCCESS;
562
247k
}
563
564
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)
565
0
{
566
0
  lxb_url_parser_clean(&lexbor_parser);
567
568
0
  lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) uri_str, uri_str_len);
569
570
0
  if ((url == NULL && !silent) || errors != NULL) {
571
0
    zval err;
572
0
    const char *reason = fill_errors(&err);
573
0
    if (url == NULL && !silent) {
574
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 ? ")" : "");
575
0
      zend_update_property(exception->ce, exception, ZEND_STRL("errors"), &err);
576
0
    }
577
0
    if (errors != NULL) {
578
0
      zval_ptr_dtor(errors);
579
0
      ZVAL_COPY_VALUE(errors, &err);
580
0
    } else {
581
0
      zval_ptr_dtor(&err);
582
0
    }
583
0
  }
584
585
0
  return url;
586
0
}
587
588
static void *php_uri_parser_whatwg_parse(const char *uri_str, size_t uri_str_len, const void *base_url, zval *errors, bool silent)
589
0
{
590
0
  return php_uri_parser_whatwg_parse_ex(uri_str, uri_str_len, base_url, errors, silent);
591
0
}
592
593
static void *php_uri_parser_whatwg_clone(void *uri)
594
0
{
595
0
  const lxb_url_t *lexbor_uri = uri;
596
597
0
  return lxb_url_clone(lexbor_parser.mraw, lexbor_uri);
598
0
}
599
600
static zend_string *php_uri_parser_whatwg_to_string(void *uri, php_uri_recomposition_mode recomposition_mode, bool exclude_fragment)
601
0
{
602
0
  const lxb_url_t *lexbor_uri = uri;
603
0
  smart_str uri_str = {0};
604
605
0
  switch (recomposition_mode) {
606
0
    case PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE:
607
0
      ZEND_FALLTHROUGH;
608
0
    case PHP_URI_RECOMPOSITION_MODE_NORMALIZED_UNICODE:
609
0
      lxb_url_serialize_idna(&lexbor_idna, lexbor_uri, serialize_to_smart_str_callback, &uri_str, exclude_fragment);
610
0
      lxb_unicode_idna_clean(&lexbor_idna);
611
0
      break;
612
0
    case PHP_URI_RECOMPOSITION_MODE_RAW_ASCII:
613
0
      ZEND_FALLTHROUGH;
614
0
    case PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII:
615
0
      lxb_url_serialize(lexbor_uri, serialize_to_smart_str_callback, &uri_str, exclude_fragment);
616
0
      break;
617
0
    EMPTY_SWITCH_DEFAULT_CASE()
618
0
  }
619
620
0
  return smart_str_extract(&uri_str);
621
0
}
622
623
static void php_uri_parser_whatwg_destroy(void *uri)
624
16
{
625
16
  lxb_url_t *lexbor_uri = uri;
626
627
16
  lxb_url_destroy(lexbor_uri);
628
16
}
629
630
PHPAPI const php_uri_parser php_uri_parser_whatwg = {
631
  .name = PHP_URI_PARSER_WHATWG,
632
  .parse = php_uri_parser_whatwg_parse,
633
  .clone = php_uri_parser_whatwg_clone,
634
  .to_string = php_uri_parser_whatwg_to_string,
635
  .destroy = php_uri_parser_whatwg_destroy,
636
  {
637
    .scheme = {.read = php_uri_parser_whatwg_scheme_read, .write = php_uri_parser_whatwg_scheme_write},
638
    .username = {.read = php_uri_parser_whatwg_username_read, .write = php_uri_parser_whatwg_username_write},
639
    .password = {.read = php_uri_parser_whatwg_password_read, .write = php_uri_parser_whatwg_password_write},
640
    .host = {.read = php_uri_parser_whatwg_host_read, .write = php_uri_parser_whatwg_host_write},
641
    .port = {.read = php_uri_parser_whatwg_port_read, .write = php_uri_parser_whatwg_port_write},
642
    .path = {.read = php_uri_parser_whatwg_path_read, .write = php_uri_parser_whatwg_path_write},
643
    .query = {.read = php_uri_parser_whatwg_query_read, .write = php_uri_parser_whatwg_query_write},
644
    .fragment = {.read = php_uri_parser_whatwg_fragment_read, .write = php_uri_parser_whatwg_fragment_write},
645
  }
646
};