Coverage Report

Created: 2026-07-25 06:39

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