Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/uri/php_uri_common.h
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
#ifndef PHP_URI_COMMON_H
16
#define PHP_URI_COMMON_H
17
18
#include "php_uri_decl.h"
19
20
extern zend_class_entry *php_uri_ce_rfc3986_uri_builder;
21
extern zend_class_entry *php_uri_ce_rfc3986_uri;
22
extern zend_class_entry *php_uri_ce_rfc3986_uri_type;
23
extern zend_class_entry *php_uri_ce_rfc3986_uri_host_type;
24
extern zend_class_entry *php_uri_ce_whatwg_url_builder;
25
extern zend_class_entry *php_uri_ce_whatwg_url;
26
extern zend_class_entry *php_uri_ce_whatwg_url_percent_encoding_mode;
27
extern zend_class_entry *php_uri_ce_comparison_mode;
28
extern zend_class_entry *php_uri_ce_exception;
29
extern zend_class_entry *php_uri_ce_error;
30
extern zend_class_entry *php_uri_ce_invalid_uri_exception;
31
extern zend_class_entry *php_uri_ce_whatwg_invalid_url_exception;
32
extern zend_class_entry *php_uri_ce_whatwg_url_validation_error_type;
33
extern zend_class_entry *php_uri_ce_whatwg_url_validation_error;
34
extern zend_class_entry *php_uri_ce_whatwg_url_host_type;
35
36
typedef enum php_uri_recomposition_mode {
37
  PHP_URI_RECOMPOSITION_MODE_RAW_ASCII,
38
  PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE,
39
  PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII,
40
  PHP_URI_RECOMPOSITION_MODE_NORMALIZED_UNICODE,
41
} php_uri_recomposition_mode;
42
43
typedef enum php_uri_component_read_mode {
44
  PHP_URI_COMPONENT_READ_MODE_RAW,
45
  PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII,
46
  PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE,
47
} php_uri_component_read_mode;
48
49
typedef zend_result (*php_uri_property_handler_read)(void *uri, php_uri_component_read_mode read_mode, zval *retval);
50
51
typedef zend_result (*php_uri_property_handler_write)(void *uri, const zval *value, zval *errors);
52
53
typedef enum php_uri_property_name {
54
  PHP_URI_PROPERTY_NAME_SCHEME,
55
  PHP_URI_PROPERTY_NAME_USERNAME,
56
  PHP_URI_PROPERTY_NAME_PASSWORD,
57
  PHP_URI_PROPERTY_NAME_HOST,
58
  PHP_URI_PROPERTY_NAME_PORT,
59
  PHP_URI_PROPERTY_NAME_PATH,
60
  PHP_URI_PROPERTY_NAME_QUERY,
61
  PHP_URI_PROPERTY_NAME_FRAGMENT,
62
} php_uri_property_name;
63
64
typedef struct php_uri_property_handler {
65
  php_uri_property_handler_read read;
66
  php_uri_property_handler_write write;
67
} php_uri_property_handler;
68
69
typedef struct php_uri_parser {
70
  /**
71
   * Name (the FQCN) of the URI parser. The "" name is reserved for the handler of the legacy parse_url().
72
   */
73
  const char *name;
74
75
  /**
76
   * Parses a URI string into a URI.
77
   *
78
   * If the URI string is valid, a URI is returned. In case of failure, NULL is
79
   * returned.
80
   *
81
   * The errors by-ref parameter can contain errors that occurred during parsing.
82
   * If the input value is NULL, or there were no errors, the errors parameter should
83
   * not be modified.
84
   *
85
   * If the URI string is valid and the base_url URI is not NULL, the URI object
86
   * is resolved against the base_url.
87
   *
88
   * If the silent parameter is true, a Uri\InvalidUriException instance must be thrown.
89
   * If the parameter is false, the possible errors should be handled by the caller.
90
   *
91
   * @param uri_str The input string that is going to be parsed
92
   * @param uri_str_len Length of the input string
93
   * @param base_url The base URI if reference resolution should be performed, otherwise NULL
94
   * @param errors An out parameter that stores additional error information
95
   * @param silent Whether to throw a Uri\InvalidUriException in case of failure
96
   */
97
  void *(*parse)(const char *uri_str, size_t uri_str_len, const void *base_url, zval *errors, bool silent);
98
99
  /**
100
   * Clones a URI to a new URI.
101
   *
102
   * A deep-clone must be performed that copies all pointer members to a new memory address.
103
   * @param uri The input URI
104
   * @return The cloned URI
105
   */
106
  void *(*clone)(void *uri);
107
108
  /**
109
   * Recomposes a URI as a string according to the recomposition_mode and exclude_fragment parameters.
110
   * The returned zend_string must not be persistent.
111
   *
112
   * Recomposition_mode can be one of the following:
113
   * - PHP_URI_RECOMPOSITION_MODE_RAW_ASCII: Recomposes the raw, non-normalized variant of the URI as a string that must only contain ASCII characters
114
   * - PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE: Recomposes the raw, non-normalized variant of the URI as a string that may contain Unicode codepoints
115
   * - PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII: Recomposes the normalized variant of the URI as a string that must only contain ASCII characters
116
   * - PHP_URI_RECOMPOSITION_MODE_NORMALIZED_UNICODE: Recomposes the normalized variant of the URI as a string that may contain Unicode codepoints
117
   *
118
   * @param uri The input URI
119
   * @param recomposition_mode The type of recomposition
120
   * @param exclude_fragment Whether the fragment component should be part of the recomposed URI
121
   * @return The recomposed URI as a non-persistent zend_string
122
   */
123
  zend_string *(*to_string)(void *uri, php_uri_recomposition_mode recomposition_mode, bool exclude_fragment);
124
125
  /**
126
   * Destroy (free) the provided URI.
127
   *
128
   * @param uri The URI to free. Must do nothing if NULL.
129
   */
130
  void (*destroy)(void *uri);
131
132
  struct {
133
    php_uri_property_handler scheme;
134
    php_uri_property_handler username;
135
    php_uri_property_handler password;
136
    php_uri_property_handler host;
137
    php_uri_property_handler port;
138
    php_uri_property_handler path;
139
    php_uri_property_handler query;
140
    php_uri_property_handler fragment;
141
  } property_handler;
142
} php_uri_parser;
143
144
typedef struct php_uri_internal {
145
  const php_uri_parser *parser;
146
  void *uri;
147
} php_uri_internal;
148
149
typedef struct php_uri_object {
150
  const php_uri_parser *parser;
151
  void *uri;
152
  zend_object std;
153
} php_uri_object;
154
155
91
#define php_uri_object_from_obj(object) ZEND_CONTAINER_OF(object, php_uri_object, std)
156
157
0
#define Z_URI_OBJECT_P(zv) php_uri_object_from_obj(Z_OBJ_P((zv)))
158
159
PHPAPI php_uri_object *php_uri_object_create(zend_class_entry *class_type, const php_uri_parser *parser);
160
PHPAPI void php_uri_object_handler_free(zend_object *object);
161
PHPAPI zend_object *php_uri_object_handler_clone(zend_object *object);
162
163
#define PHP_URI_PARSER_RFC3986 "Uri\\Rfc3986\\Uri"
164
#define PHP_URI_PARSER_WHATWG "Uri\\WhatWg\\Url"
165
0
#define PHP_URI_PARSER_PHP_PARSE_URL "parse_url"
166
0
#define PHP_URI_SERIALIZE_URI_FIELD_NAME "uri"
167
168
static inline const php_uri_property_handler *php_uri_parser_property_handler_by_name(const php_uri_parser *parser, php_uri_property_name property_name)
169
0
{
170
0
  switch (property_name) {
171
0
    case PHP_URI_PROPERTY_NAME_SCHEME:
172
0
      return &parser->property_handler.scheme;
173
0
    case PHP_URI_PROPERTY_NAME_USERNAME:
174
0
      return &parser->property_handler.username;
175
0
    case PHP_URI_PROPERTY_NAME_PASSWORD:
176
0
      return &parser->property_handler.password;
177
0
    case PHP_URI_PROPERTY_NAME_HOST:
178
0
      return &parser->property_handler.host;
179
0
    case PHP_URI_PROPERTY_NAME_PORT:
180
0
      return &parser->property_handler.port;
181
0
    case PHP_URI_PROPERTY_NAME_PATH:
182
0
      return &parser->property_handler.path;
183
0
    case PHP_URI_PROPERTY_NAME_QUERY:
184
0
      return &parser->property_handler.query;
185
0
    case PHP_URI_PROPERTY_NAME_FRAGMENT:
186
0
      return &parser->property_handler.fragment;
187
0
    default: ZEND_UNREACHABLE();
188
0
  }
189
0
}
Unexecuted instantiation: ftp_fopen_wrapper.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: http_fopen_wrapper.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: php_uri.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: php_uri_common.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: uri_parser_rfc3986.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: uri_parser_whatwg.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: uri_parser_php_parse_url.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: streams.c:php_uri_parser_property_handler_by_name
Unexecuted instantiation: internal_functions_cli.c:php_uri_parser_property_handler_by_name
190
191
zend_result php_uri_pass_errors_by_ref_and_free(zval *errors_zv, zval *errors);
192
void php_uri_property_read_helper(INTERNAL_FUNCTION_PARAMETERS, php_uri_property_name property_name, php_uri_component_read_mode component_read_mode);
193
void php_uri_property_write_str_helper(INTERNAL_FUNCTION_PARAMETERS, php_uri_property_name property_name);
194
void php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAMETERS, php_uri_property_name property_name);
195
void php_uri_property_write_long_or_null_helper(INTERNAL_FUNCTION_PARAMETERS, php_uri_property_name property_name);
196
197
#endif