Skip to content

Commit 3794154

Browse files
committed
Some review fixes
1 parent b34c8c6 commit 3794154

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

ext/uri/tests/026.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The specified host is malformed
111111
The specified host is malformed
112112
string(7) "foo.com"
113113
string(8) "test.com"
114-
The specified host is malformed
114+
Cannot remove the host from a URI that has a userinfo
115115
string(11) "example.com"
116116
string(8) "test.com"
117117
string(8) "test.com"

ext/uri/tests/027.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int(8080)
6565
NULL
6666
int(80)
6767
NULL
68-
The specified port is malformed
68+
Cannot set a port without having a host
6969
int(8080)
7070
int(22)
7171
NULL

ext/uri/uri_parser_rfc3986.c

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,17 @@ zend_result php_uri_parser_rfc3986_userinfo_write(struct uri_internal_t *interna
172172
result = uriSetUserInfoMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
173173
}
174174

175-
if (result != URI_SUCCESS) {
176-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified userinfo is malformed", 0);
177-
return FAILURE;
175+
switch (result) {
176+
case URI_SUCCESS:
177+
reset_normalized_uri_after_writing(internal_uri);
178+
return SUCCESS;
179+
case URI_ERROR_SETUSERINFO_HOST_NOT_SET:
180+
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot set a userinfo without having a host", 0);
181+
return FAILURE;
182+
default:
183+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified userinfo is malformed", 0);
184+
return FAILURE;
178185
}
179-
180-
reset_normalized_uri_after_writing(internal_uri);
181-
182-
return SUCCESS;
183186
}
184187

185188
ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_username_read(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
@@ -258,14 +261,20 @@ static zend_result php_uri_parser_rfc3986_host_write(struct uri_internal_t *inte
258261
result = uriSetHostAutoMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
259262
}
260263

261-
if (result != URI_SUCCESS) {
262-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified host is malformed", 0);
263-
return FAILURE;
264+
switch (result) {
265+
case URI_SUCCESS:
266+
reset_normalized_uri_after_writing(internal_uri);
267+
return SUCCESS;
268+
case URI_ERROR_SETHOST_PORT_SET:
269+
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot remove the host from a URI that has a port", 0);
270+
return FAILURE;
271+
case URI_ERROR_SETHOST_USERINFO_SET:
272+
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot remove the host from a URI that has a userinfo", 0);
273+
return FAILURE;
274+
default:
275+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified host is malformed", 0);
276+
return FAILURE;
264277
}
265-
266-
reset_normalized_uri_after_writing(internal_uri);
267-
268-
return SUCCESS;
269278
}
270279

271280
ZEND_ATTRIBUTE_NONNULL static zend_long port_str_to_zend_long_checked(const char *str, size_t len)
@@ -307,18 +316,22 @@ static zend_result php_uri_parser_rfc3986_port_write(struct uri_internal_t *inte
307316
if (Z_TYPE_P(value) == IS_NULL) {
308317
result = uriSetPortTextMmA(uriparser_uri, NULL, NULL, mm);
309318
} else {
310-
ZVAL_STR(value, zend_long_to_str(Z_LVAL_P(value)));
311-
result = uriSetPortTextMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
312-
}
313-
314-
if (result != URI_SUCCESS) {
315-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified port is malformed", 0);
316-
return FAILURE;
319+
zend_string *tmp = zend_long_to_str(Z_LVAL_P(value));
320+
result = uriSetPortTextMmA(uriparser_uri, ZSTR_VAL(tmp), ZSTR_VAL(tmp) + ZSTR_LEN(tmp), mm);
321+
zend_string_release(tmp);
322+
}
323+
324+
switch (result) {
325+
case URI_SUCCESS:
326+
reset_normalized_uri_after_writing(internal_uri);
327+
return SUCCESS;
328+
case URI_ERROR_SETPORT_HOST_NOT_SET:
329+
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot set a port without having a host", 0);
330+
return FAILURE;
331+
default:
332+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified port is malformed", 0);
333+
return FAILURE;
317334
}
318-
319-
reset_normalized_uri_after_writing(internal_uri);
320-
321-
return SUCCESS;
322335
}
323336

324337
ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_path_read(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)

0 commit comments

Comments
 (0)