Coverage Report

Created: 2026-06-02 06:36

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