Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Mar 15, 2020
1 parent 603031a commit f70ab3d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion requests/yaf_request_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ PHP_METHOD(yaf_request_http, __construct) {
return;
}

(void)yaf_request_http_instance(self, request_uri, base_uri);
yaf_request_http_instance(self, request_uri, base_uri);
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int yaf_route_map_route(yaf_route_t *route, yaf_request_t *request) {
uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1, NULL);
base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1, NULL);

if (base_uri && IS_STRING == Z_TYPE_P(base_uri) && Z_STRLEN_P(base_uri)) {
if (Z_STRLEN_P(base_uri)) {
req_uri = yaf_request_strip_base_uri(Z_STR_P(uri), Z_STR_P(base_uri), &req_uri_len);
} else {
req_uri = Z_STRVAL_P(uri);
Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ int yaf_route_regex_route(yaf_route_t *router, yaf_request_t *request) {
uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1, NULL);
base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1, NULL);

if (base_uri && IS_STRING == Z_TYPE_P(base_uri) && Z_STRLEN_P(base_uri)) {
if (Z_STRLEN_P(base_uri)) {
req_uri = yaf_request_strip_base_uri(Z_STR_P(uri), Z_STR_P(base_uri), &req_uri_len);
} else {
req_uri = Z_STRVAL_P(uri);
Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ int yaf_route_rewrite_route(yaf_route_t *router, yaf_request_t *request) {
uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1, NULL);
base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1, NULL);

if (base_uri && IS_STRING == Z_TYPE_P(base_uri) && Z_STRLEN_P(base_uri)) {
if (Z_STRLEN_P(base_uri)) {
req_uri = yaf_request_strip_base_uri(Z_STR_P(uri), Z_STR_P(base_uri), &req_uri_len);
} else {
req_uri = Z_STRVAL_P(uri);
Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ int yaf_route_static_route(yaf_route_t *route, yaf_request_t *request) /* {{{ */
uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1, NULL);
base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1, NULL);

if (base_uri && IS_STRING == Z_TYPE_P(base_uri) && Z_STRLEN_P(base_uri)) {
if (Z_STRLEN_P(base_uri)) {
req_uri = yaf_request_strip_base_uri(Z_STR_P(uri), Z_STR_P(base_uri), &req_uri_len);
} else {
req_uri = Z_STRVAL_P(uri);
Expand Down
2 changes: 1 addition & 1 deletion tests/006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Yaf_Request_Http Object

[language:protected] =>
[_exception:protected] =>
[_base_uri:protected] => /prefix/
[_base_uri:protected] => /prefix
[uri:protected] => /prefix/controller/action/name/laruence/age/28
[dispatched:protected] =>
[routed:protected] =>
Expand Down
2 changes: 1 addition & 1 deletion tests/bug62702.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $request = new Yaf_Request_Http("/sample/A/B/C", "/sample");
$router->route($request);
var_dump($request->getControllerName());

$request = new Yaf_Request_Http("/sample", "/sAmplE");
$request = new Yaf_Request_Http("/sample", "/sAmplE/");
$router->route($request);
var_dump($request->getControllerName());

Expand Down
60 changes: 36 additions & 24 deletions yaf_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int yaf_request_set_base_uri(yaf_request_t *request, zend_string *base_uri, zend
if (script_name && EXPECTED(IS_STRING == Z_TYPE_P(script_name))) {
zend_string *script = php_basename(Z_STRVAL_P(script_name), Z_STRLEN_P(script_name), NULL, 0);

if (strncmp(ZSTR_VAL(file_name), ZSTR_VAL(script), ZSTR_LEN(file_name)) == 0) {
if (memcpy(ZSTR_VAL(file_name), ZSTR_VAL(script), ZSTR_LEN(file_name)) == 0) {
basename = zend_string_copy(Z_STR_P(script_name));
zend_string_release(file_name);
zend_string_release(script);
Expand All @@ -135,7 +135,7 @@ int yaf_request_set_base_uri(yaf_request_t *request, zend_string *base_uri, zend
phpself_name = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "PHP_SELF", sizeof("PHP_SELF") - 1);
if (phpself_name && EXPECTED(IS_STRING == Z_TYPE_P(phpself_name))) {
zend_string *phpself = php_basename(Z_STRVAL_P(phpself_name), Z_STRLEN_P(phpself_name), NULL, 0);
if (strncmp(ZSTR_VAL(file_name), ZSTR_VAL(phpself), ZSTR_LEN(file_name)) == 0) {
if (memcpy(ZSTR_VAL(file_name), ZSTR_VAL(phpself), ZSTR_LEN(file_name)) == 0) {
basename = zend_string_copy(Z_STR_P(phpself_name));
zend_string_release(file_name);
zend_string_release(phpself);
Expand All @@ -147,7 +147,7 @@ int yaf_request_set_base_uri(yaf_request_t *request, zend_string *base_uri, zend
orig_name = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "ORIG_SCRIPT_NAME", sizeof("ORIG_SCRIPT_NAME") - 1);
if (orig_name && IS_STRING == Z_TYPE_P(orig_name)) {
zend_string *orig = php_basename(Z_STRVAL_P(orig_name), Z_STRLEN_P(orig_name), NULL, 0);
if (strncmp(ZSTR_VAL(file_name), ZSTR_VAL(orig), ZSTR_LEN(file_name)) == 0) {
if (memcpy(ZSTR_VAL(file_name), ZSTR_VAL(orig), ZSTR_LEN(file_name)) == 0) {
basename = zend_string_copy(Z_STR_P(orig_name));
zend_string_release(file_name);
zend_string_release(orig);
Expand All @@ -159,33 +159,45 @@ int yaf_request_set_base_uri(yaf_request_t *request, zend_string *base_uri, zend
}
} while (0);

if (basename && strncmp(ZSTR_VAL(request_uri), ZSTR_VAL(basename), ZSTR_LEN(basename)) == 0) {
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), basename);
zend_string_release(basename);
return 1;
} else if (basename) {
zend_string *dir = zend_string_init(ZSTR_VAL(basename), ZSTR_LEN(basename), 0); /* php_dirname might alter the string */

ZSTR_LEN(dir) = php_dirname(ZSTR_VAL(dir), ZSTR_LEN(dir));
if (*(ZSTR_VAL(dir) + ZSTR_LEN(dir) - 1) == '/') {
--ZSTR_LEN(dir);
}

if (ZSTR_LEN(dir)) {
if (strncmp(ZSTR_VAL(request_uri), ZSTR_VAL(dir), ZSTR_LEN(dir)) == 0) {
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), dir);
zend_string_release(dir);
if (basename) {
if (memcpy(ZSTR_VAL(request_uri), ZSTR_VAL(basename), ZSTR_LEN(basename)) == 0) {
if (ZSTR_VAL(basename)[ZSTR_LEN(basename) - 1] == '/') {
zend_string *sanitized_uri = zend_string_init(ZSTR_VAL(basename), ZSTR_LEN(basename) - 1, 0);
zend_string_release(basename);
return 1;
basename = sanitized_uri;
}
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), basename);
return 1;
} else {
zend_string *dir = zend_string_init(ZSTR_VAL(basename), ZSTR_LEN(basename), 0); /* php_dirname might alter the string */

zend_string_release(basename);
ZSTR_LEN(dir) = php_dirname(ZSTR_VAL(dir), ZSTR_LEN(dir));
if (*(ZSTR_VAL(dir) + ZSTR_LEN(dir) - 1) == '/') {
ZSTR_VAL(dir)[ZSTR_LEN(dir) - 1] = '\0';
ZSTR_LEN(dir)--;
}
if (ZSTR_LEN(dir)) {
if (memcpy(ZSTR_VAL(request_uri), ZSTR_VAL(dir), ZSTR_LEN(dir)) == 0) {
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), dir);
zend_string_release(dir);
return 1;
}
}
zend_string_release(dir);
}
zend_string_release(dir);
zend_string_release(basename);
}

zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), "");
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), ZSTR_EMPTY_ALLOC());
} else {
zend_string *sanitized_uri = NULL;
if (UNEXPECTED(ZSTR_VAL(base_uri)[ZSTR_LEN(base_uri) - 1] == '/')) {
sanitized_uri = zend_string_init(ZSTR_VAL(base_uri), ZSTR_LEN(base_uri) - 1, 0);
base_uri = sanitized_uri;
}
zend_update_property_str(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), base_uri);
if (UNEXPECTED(sanitized_uri)) {
zend_string_release(sanitized_uri);
}
}
return 1;
}
Expand Down

0 comments on commit f70ab3d

Please sign in to comment.