Skip to content

Commit 9ea0b89

Browse files
committed
uri: Remove php_uri_parse() and php_uri_free()
This API is both less powerful as well as less efficient compared to just calling the `->parse_uri()` handler of the parser directly. With regard to efficiency it needlessly allocates 32 byte of memory to store two pointers, of which one is already known, because it's the input. While it would be possible to just return the resulting `uri_internal_t` struct (instead of a pointer to a freshly allocated one), which would be returned as a register pair, users of the API can also just create the struct themselves for even more flexibility in allocations. The API is also less powerful, because it does not support base URIs.
1 parent aca597c commit 9ea0b89

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

ext/openssl/xp_ssl.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,14 +2640,19 @@ static char *php_openssl_get_url_name(const char *resourcename,
26402640
return NULL;
26412641
}
26422642

2643-
uri_internal_t *internal_uri = php_uri_parse(uri_parser, resourcename, resourcenamelen, true);
2644-
if (internal_uri == NULL) {
2643+
void *parsed = uri_parser->parse_uri(resourcename, resourcenamelen,
2644+
/* base_url */ NULL, /* errors */ NULL, /* silent */ true);
2645+
if (parsed == NULL) {
26452646
return NULL;
26462647
}
2648+
uri_internal_t internal_uri = {
2649+
.parser = uri_parser,
2650+
.uri = parsed,
2651+
};
26472652

26482653
char * url_name = NULL;
26492654
zval host_zv;
2650-
zend_result result = php_uri_get_host(internal_uri, URI_COMPONENT_READ_RAW, &host_zv);
2655+
zend_result result = php_uri_get_host(&internal_uri, URI_COMPONENT_READ_RAW, &host_zv);
26512656
if (result == SUCCESS && Z_TYPE(host_zv) == IS_STRING) {
26522657
const char * host = Z_STRVAL(host_zv);
26532658
size_t len = Z_STRLEN(host_zv);
@@ -2662,7 +2667,7 @@ static char *php_openssl_get_url_name(const char *resourcename,
26622667
}
26632668
}
26642669

2665-
php_uri_free(internal_uri);
2670+
uri_parser->free_uri(parsed);
26662671
zval_ptr_dtor(&host_zv);
26672672

26682673
return url_name;

ext/uri/php_uri.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,6 @@ PHPAPI uri_parser_t *php_uri_get_parser(const zend_string *uri_parser_name)
116116
return uri_parser_by_name(ZSTR_VAL(uri_parser_name), ZSTR_LEN(uri_parser_name));
117117
}
118118

119-
ZEND_ATTRIBUTE_NONNULL PHPAPI uri_internal_t *php_uri_parse(const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, bool silent)
120-
{
121-
void *parsed = uri_parser->parse_uri(uri_str, uri_str_len, NULL, NULL, silent);
122-
123-
if (parsed == NULL) {
124-
return NULL;
125-
}
126-
127-
uri_internal_t *internal_uri = emalloc(sizeof(*internal_uri));
128-
internal_uri->parser = uri_parser;
129-
internal_uri->uri = parsed;
130-
131-
return internal_uri;
132-
}
133-
134119
ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_get_property(const uri_internal_t *internal_uri, uri_property_name_t property_name, uri_component_read_mode_t read_mode, zval *zv)
135120
{
136121
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name);
@@ -181,96 +166,95 @@ ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_fragment(const uri_interna
181166
return php_uri_get_property(internal_uri, URI_PROPERTY_NAME_FRAGMENT, read_mode, zv);
182167
}
183168

184-
ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_free(uri_internal_t *internal_uri)
185-
{
186-
internal_uri->parser->free_uri(internal_uri->uri);
187-
internal_uri->uri = NULL;
188-
internal_uri->parser = NULL;
189-
efree(internal_uri);
190-
}
191-
192169
ZEND_ATTRIBUTE_NONNULL PHPAPI php_uri *php_uri_parse_to_struct(
193170
const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, uri_component_read_mode_t read_mode, bool silent
194171
) {
195-
uri_internal_t *uri_internal = php_uri_parse(uri_parser, uri_str, uri_str_len, silent);
196-
if (uri_internal == NULL) {
172+
void *parsed = uri_parser->parse_uri(uri_str, uri_str_len,
173+
/* base_url */ NULL, /* errors */ NULL, silent);
174+
if (parsed == NULL) {
197175
return NULL;
198176
}
199177

178+
uri_internal_t uri_internal = {
179+
.parser = uri_parser,
180+
.uri = parsed,
181+
};
182+
200183
php_uri *uri = ecalloc(1, sizeof(*uri));
201184
zval tmp;
202185
zend_result result;
203186

204-
result = php_uri_get_scheme(uri_internal, read_mode, &tmp);
187+
result = php_uri_get_scheme(&uri_internal, read_mode, &tmp);
205188
if (result == FAILURE) {
206189
goto error;
207190
}
208191
if (Z_TYPE(tmp) == IS_STRING) {
209192
uri->scheme = Z_STR(tmp);
210193
}
211194

212-
result = php_uri_get_username(uri_internal, read_mode, &tmp);
195+
result = php_uri_get_username(&uri_internal, read_mode, &tmp);
213196
if (result == FAILURE) {
214197
goto error;
215198
}
216199
if (Z_TYPE(tmp) == IS_STRING) {
217200
uri->user = Z_STR(tmp);
218201
}
219202

220-
result = php_uri_get_password(uri_internal, read_mode, &tmp);
203+
result = php_uri_get_password(&uri_internal, read_mode, &tmp);
221204
if (result == FAILURE) {
222205
goto error;
223206
}
224207
if (Z_TYPE(tmp) == IS_STRING) {
225208
uri->password = Z_STR(tmp);
226209
}
227210

228-
result = php_uri_get_host(uri_internal, read_mode, &tmp);
211+
result = php_uri_get_host(&uri_internal, read_mode, &tmp);
229212
if (result == FAILURE) {
230213
goto error;
231214
}
232215
if (Z_TYPE(tmp) == IS_STRING) {
233216
uri->host = Z_STR(tmp);
234217
}
235218

236-
result = php_uri_get_port(uri_internal, read_mode, &tmp);
219+
result = php_uri_get_port(&uri_internal, read_mode, &tmp);
237220
if (result == FAILURE) {
238221
goto error;
239222
}
240223
if (Z_TYPE(tmp) == IS_LONG) {
241224
uri->port = Z_LVAL(tmp);
242225
}
243226

244-
result = php_uri_get_path(uri_internal, read_mode, &tmp);
227+
result = php_uri_get_path(&uri_internal, read_mode, &tmp);
245228
if (result == FAILURE) {
246229
goto error;
247230
}
248231
if (Z_TYPE(tmp) == IS_STRING) {
249232
uri->path = Z_STR(tmp);
250233
}
251234

252-
result = php_uri_get_query(uri_internal, read_mode, &tmp);
235+
result = php_uri_get_query(&uri_internal, read_mode, &tmp);
253236
if (result == FAILURE) {
254237
goto error;
255238
}
256239
if (Z_TYPE(tmp) == IS_STRING) {
257240
uri->query = Z_STR(tmp);
258241
}
259242

260-
result = php_uri_get_fragment(uri_internal, read_mode, &tmp);
243+
result = php_uri_get_fragment(&uri_internal, read_mode, &tmp);
261244
if (result == FAILURE) {
262245
goto error;
263246
}
264247
if (Z_TYPE(tmp) == IS_STRING) {
265248
uri->fragment = Z_STR(tmp);
266249
}
267250

268-
php_uri_free(uri_internal);
251+
uri_parser->free_uri(parsed);
269252

270253
return uri;
271254

272255
error:
273-
php_uri_free(uri_internal);
256+
257+
uri_parser->free_uri(parsed);
274258
php_uri_struct_free(uri);
275259

276260
return NULL;
@@ -347,7 +331,8 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
347331
base_url = internal_base_url->uri;
348332
}
349333

350-
void *uri = uri_parser->parse_uri(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw);
334+
void *uri = uri_parser->parse_uri(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str),
335+
base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw);
351336
if (UNEXPECTED(uri == NULL)) {
352337
if (should_throw) {
353338
zval_ptr_dtor(&errors);

ext/uri/php_uri.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ PHPAPI zend_result php_uri_parser_register(const uri_parser_t *uri_parser);
4949
*/
5050
PHPAPI uri_parser_t *php_uri_get_parser(const zend_string *uri_parser_name);
5151

52-
ZEND_ATTRIBUTE_NONNULL PHPAPI uri_internal_t *php_uri_parse(const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, bool silent);
53-
5452
/**
5553
* Retrieves the scheme component based on the read_mode and passes it to the zv ZVAL in case of success.
5654
*
@@ -171,13 +169,6 @@ ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_query(const uri_internal_t
171169
*/
172170
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_fragment(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *zv);
173171

174-
/**
175-
* Frees the uri member within the provided internal URI.
176-
*
177-
* @param internal_uri The internal URI
178-
*/
179-
ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_free(uri_internal_t *internal_uri);
180-
181172
/**
182173
* Creates a new php_uri struct containing all the URI components. The components are retrieved based on the read_mode parameter.
183174
*

0 commit comments

Comments
 (0)