Skip to content

Commit

Permalink
Refactor Yaf_Route_*s
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Apr 6, 2020
1 parent 59350d9 commit 96fe6d7
Show file tree
Hide file tree
Showing 35 changed files with 1,056 additions and 729 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ before_script:
- ./travis/compile.sh

# Run PHPs run-tests.php
#script: TEST_PHP_ARGS="--show-diff" make test
script: TEST_PHP_ARGS="--show-diff" make test
13 changes: 7 additions & 6 deletions configs/yaf_config_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,16 @@ void yaf_config_ini_init(yaf_config_object *conf, zval *filename, zend_string *s
return;
}

if (section_name && ZSTR_LEN(section_name)) {
zval *section;
if (section_name && ZSTR_LEN(section_name)) {
zval *section, garbage;
if ((section = zend_symtable_find(Z_ARRVAL(configs), section_name)) == NULL) {
zval_ptr_dtor(&configs);
yaf_trigger_error(E_ERROR, "There is no section '%s' in '%s'", ZSTR_VAL(section_name), ini_file);
return;
}
Z_ADDREF_P(section);
zval_ptr_dtor(&configs);
ZVAL_COPY_VALUE(&configs, section);
ZVAL_COPY_VALUE(&garbage, &configs);
ZVAL_COPY(&configs, section);
zval_ptr_dtor(&garbage);
}

conf->config = Z_ARRVAL(configs);
Expand Down Expand Up @@ -467,7 +467,7 @@ PHP_METHOD(yaf_config_ini, get) {
RETURN_NULL();
}
if (Z_TYPE_P(val) == IS_ARRAY) {
yaf_config_instance(return_value, val, NULL);
RETURN_OBJ(yaf_config_format_child(Z_OBJCE_P(getThis()), val, conf->readonly));
} else {
RETURN_ZVAL(val, 1, 0);
}
Expand Down Expand Up @@ -506,6 +506,7 @@ zend_function_entry yaf_config_ini_methods[] = {
PHP_ME(yaf_config_ini, get, yaf_config_ini_get_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_config_ini, set, yaf_config_ini_set_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_config_ini, readonly, yaf_config_ini_void_arginfo, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_config_ini, offsetGet, get, yaf_config_ini_get_arginfo, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_config_ini, offsetSet, set, yaf_config_ini_set_arginfo, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_config_ini, __set, set, yaf_config_ini_set_arginfo, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
<file name="093.phpt" role="test" />
<file name="094.phpt" role="test" />
<file name="095.phpt" role="test" />
<file name="096.phpt" role="test" />
<file name="build.inc" role="test" />
<file name="bug61493.phpt" role="test" />
<file name="bug63381.phpt" role="test" />
Expand Down
39 changes: 21 additions & 18 deletions responses/yaf_response_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int yaf_response_alter_header(yaf_response_object *response, zend_string *name,
}
/* }}} */

int yaf_response_set_redirect(yaf_response_object *response, zend_string *url) {
int yaf_response_set_redirect(yaf_response_object *response, zend_string *url) /* {{{ */ {
sapi_header_line ctr = {0};

if (strcmp("cli", sapi_module.name) == 0 || strcmp("phpdbg", sapi_module.name) == 0) {
Expand All @@ -112,6 +112,7 @@ int yaf_response_set_redirect(yaf_response_object *response, zend_string *url) {
ctr.line_len = spprintf(&(ctr.line), 0, "%s %s", "Location:", ZSTR_VAL(url));
ctr.response_code = 0;
if (sapi_header_op(SAPI_HEADER_REPLACE, &ctr) == SUCCESS) {
response->header_sent = 1;
efree(ctr.line);
return 1;
}
Expand All @@ -126,24 +127,26 @@ int yaf_response_http_send(yaf_response_object *response) /* {{{ */ {
zend_string *header_name;
sapi_header_line ctr = {0};

if (response->code) {
SG(sapi_headers).http_response_code = response->code;
}

ZEND_HASH_FOREACH_KEY_VAL(&response->header, num_key, header_name, entry) {
if (header_name) {
ctr.line_len = spprintf(&(ctr.line), 0, "%s: %s", ZSTR_VAL(header_name), Z_STRVAL_P(entry));
} else {
ctr.line_len = spprintf(&(ctr.line), 0, ""ZEND_ULONG_FMT": %s", num_key, Z_STRVAL_P(entry));
}

ctr.response_code = 0;
if (sapi_header_op(SAPI_HEADER_REPLACE, &ctr) != SUCCESS) {
efree(ctr.line);
return 0;
if (response->header_sent == 0) {
if (response->code) {
SG(sapi_headers).http_response_code = response->code;
}
} ZEND_HASH_FOREACH_END();
efree(ctr.line);
ZEND_HASH_FOREACH_KEY_VAL(&response->header, num_key, header_name, entry) {
if (header_name) {
ctr.line_len = spprintf(&(ctr.line), 0, "%s: %s", ZSTR_VAL(header_name), Z_STRVAL_P(entry));
} else {
ctr.line_len = spprintf(&(ctr.line), 0, ""ZEND_ULONG_FMT": %s", num_key, Z_STRVAL_P(entry));
}

ctr.response_code = 0;
if (sapi_header_op(SAPI_HEADER_REPLACE, &ctr) != SUCCESS) {
efree(ctr.line);
return 0;
}
} ZEND_HASH_FOREACH_END();
efree(ctr.line);
response->header_sent = 1;
}

ZEND_HASH_FOREACH_VAL(&response->body, entry) {
zend_string *str = zval_get_string(entry);
Expand Down
94 changes: 43 additions & 51 deletions routes/yaf_route_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,99 +37,91 @@

zend_class_entry *yaf_route_ce;

yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, HashTable *config) /* {{{ */ {
zval *match, *def, *map, *verify, *reverse, *pzval;
yaf_route_t *instance = NULL;
int yaf_route_instance(yaf_route_t *route, HashTable *config) /* {{{ */ {
zval *pzval;

if (!config) {
return NULL;
if (UNEXPECTED(config == NULL)) {
return 0;
}

if ((pzval = zend_hash_str_find(config, ZEND_STRL("type"))) == NULL ||
IS_STRING != Z_TYPE_P(pzval)) {
return NULL;
if (UNEXPECTED((pzval = zend_hash_str_find(config, ZEND_STRL("type"))) == NULL || IS_STRING != Z_TYPE_P(pzval))) {
return 0;
}

if (zend_string_equals_literal_ci(Z_STR_P(pzval), "rewrite")) {
if ((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL ||
Z_TYPE_P(match) != IS_STRING) {
return NULL;
zval *match, *router, *verify;
if (UNEXPECTED((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL || Z_TYPE_P(match) != IS_STRING)) {
return 0;
}
if ((def = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL ||
Z_TYPE_P(def) != IS_ARRAY) {
return NULL;

if (UNEXPECTED((router = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL || Z_TYPE_P(router) != IS_ARRAY)) {
return 0;
}

if ((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL ||
Z_TYPE_P(verify) != IS_ARRAY) {
if (((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL || Z_TYPE_P(verify) != IS_ARRAY)) {
verify = NULL;
}

instance = yaf_route_rewrite_instance(this_ptr, match, def, verify);
yaf_route_rewrite_instance(route, Z_STR_P(match), router, verify);
} else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "regex")) {
if ((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL ||
Z_TYPE_P(match) != IS_STRING) {
return NULL;
zval *match, *router, *verify, *map, *reverse;
if (UNEXPECTED((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL || Z_TYPE_P(match) != IS_STRING)) {
return 0;
}
if ((def = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL ||
Z_TYPE_P(def) != IS_ARRAY) {
return NULL;

if (UNEXPECTED((router = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL || Z_TYPE_P(router) != IS_ARRAY)) {
return 0;
}
if ((map = zend_hash_str_find(config, ZEND_STRL("map"))) == NULL ||
Z_TYPE_P(map) != IS_ARRAY) {

if (((map = zend_hash_str_find(config, ZEND_STRL("map"))) == NULL || Z_TYPE_P(map) != IS_ARRAY)) {
map = NULL;
}

if ((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL ||
Z_TYPE_P(verify) != IS_ARRAY) {
if ((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL || Z_TYPE_P(verify) != IS_ARRAY) {
verify = NULL;
}

if ((reverse = zend_hash_str_find(config, ZEND_STRL("reverse"))) == NULL ||
Z_TYPE_P(reverse) != IS_STRING) {
if ((reverse = zend_hash_str_find(config, ZEND_STRL("reverse"))) == NULL || Z_TYPE_P(reverse) != IS_STRING) {
reverse = NULL;
}

instance = yaf_route_regex_instance(this_ptr, match, def, map, verify, reverse);
yaf_route_regex_instance(route, Z_STR_P(match), router, map, verify, reverse? Z_STR_P(reverse) : NULL);
} else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "map")) {
zend_string *delimiter = NULL;
zend_bool controller_prefer = 0;
zend_bool ctl_prefer = 0;

if ((pzval = zend_hash_str_find(config, ZEND_STRL("controllerPrefer"))) != NULL) {
controller_prefer = zend_is_true(pzval);
ctl_prefer = zend_is_true(pzval);
}

if ((pzval = zend_hash_str_find(config, ZEND_STRL("delimiter"))) != NULL
&& Z_TYPE_P(pzval) == IS_STRING) {
if ((pzval = zend_hash_str_find(config, ZEND_STRL("delimiter"))) != NULL && Z_TYPE_P(pzval) == IS_STRING) {
delimiter = Z_STR_P(pzval);
}

instance = yaf_route_map_instance(this_ptr, controller_prefer, delimiter);
yaf_route_map_instance(route, ctl_prefer, delimiter);
} else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "simple")) {
if ((match = zend_hash_str_find(config, ZEND_STRL("module"))) == NULL ||
Z_TYPE_P(match) != IS_STRING) {
return NULL;
zval *m, *c, *a;

if (UNEXPECTED((m = zend_hash_str_find(config, ZEND_STRL("module"))) == NULL || Z_TYPE_P(m) != IS_STRING)) {
return 0;
}
if ((def = zend_hash_str_find(config, ZEND_STRL("controller"))) == NULL ||
Z_TYPE_P(def) != IS_STRING) {
return NULL;
if (UNEXPECTED((c = zend_hash_str_find(config, ZEND_STRL("controller"))) == NULL || Z_TYPE_P(c) != IS_STRING)) {
return 0;
}
if ((map = zend_hash_str_find(config, ZEND_STRL("action"))) == NULL ||
Z_TYPE_P(map) != IS_STRING) {
return NULL;
if (UNEXPECTED((a = zend_hash_str_find(config, ZEND_STRL("action"))) == NULL || Z_TYPE_P(a) != IS_STRING)) {
return 0;
}

instance = yaf_route_simple_instance(this_ptr, match, def, map);
yaf_route_simple_instance(route, Z_STR_P(a), Z_STR_P(c), Z_STR_P(a));
} else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "supervar")) {
if ((match = zend_hash_str_find(config, ZEND_STRL("varname"))) == NULL ||
Z_TYPE_P(match) != IS_STRING) {
return NULL;
zval *varname;
if (UNEXPECTED((varname = zend_hash_str_find(config, ZEND_STRL("varname"))) == NULL || Z_TYPE_P(varname) != IS_STRING)) {
return 0;
}

instance = yaf_route_supervar_instance(this_ptr, match);
yaf_route_supervar_instance(route, Z_STR_P(varname));
}

return instance;
return 1;
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ YAF_END_ARG_INFO()

extern zend_class_entry *yaf_route_ce;

yaf_route_t *yaf_route_instance(yaf_route_t *this_ptr, HashTable *config);
int yaf_route_instance(yaf_route_t *route, HashTable *config);

YAF_STARTUP_FUNCTION(route);

Expand Down
Loading

0 comments on commit 96fe6d7

Please sign in to comment.