forked from laruence/yaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.c
215 lines (182 loc) · 5.75 KB
/
static.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: static.c 327627 2012-09-13 06:12:38Z laruence $ */
zend_class_entry * yaf_route_static_ce;
/** {{{ ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(yaf_route_static_match_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, uri)
ZEND_END_ARG_INFO()
/* }}} */
static int yaf_route_pathinfo_route(yaf_request_t *request, char *req_uri, int req_uri_len TSRMLS_DC) /* {{{ */ {
zval *params;
char *module = NULL, *controller = NULL, *action = NULL, *rest = NULL;
do {
#define strip_slashs(p) while (*p == ' ' || *p == '/') { ++p; }
char *s, *p;
char *uri;
if (req_uri_len == 0
|| (req_uri_len == 1 && *req_uri == '/')) {
break;
}
uri = req_uri;
s = p = uri;
if (req_uri_len) {
char *q = req_uri + req_uri_len - 1;
while (q > req_uri && (*q == ' ' || *q == '/')) {
*q-- = '\0';
}
}
strip_slashs(p);
if ((s = strstr(p, "/")) != NULL) {
if (yaf_application_is_module_name(p, s-p TSRMLS_CC)) {
module = estrndup(p, s - p);
p = s + 1;
strip_slashs(p);
if ((s = strstr(p, "/")) != NULL) {
controller = estrndup(p, s - p);
p = s + 1;
}
} else {
controller = estrndup(p, s - p);
p = s + 1;
}
}
strip_slashs(p);
if ((s = strstr(p, "/")) != NULL) {
action = estrndup(p, s - p);
p = s + 1;
}
strip_slashs(p);
if (*p != '\0') {
do {
if (!module && !controller && !action) {
if (yaf_application_is_module_name(p, strlen(p) TSRMLS_CC)) {
module = estrdup(p);
break;
}
}
if (!controller) {
controller = estrdup(p);
break;
}
if (!action) {
action = estrdup(p);
break;
}
rest = estrdup(p);
} while (0);
}
if (module && controller == NULL) {
controller = module;
module = NULL;
} else if (module && action == NULL) {
action = controller;
controller = module;
module = NULL;
} else if (controller && action == NULL ) {
/* /controller */
if (YAF_G(action_prefer)) {
action = controller;
controller = NULL;
}
}
} while (0);
if (module != NULL) {
zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), module TSRMLS_CC);
efree(module);
}
if (controller != NULL) {
zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), controller TSRMLS_CC);
efree(controller);
}
if (action != NULL) {
zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), action TSRMLS_CC);
efree(action);
}
if (rest) {
params = yaf_router_parse_parameters(rest TSRMLS_CC);
(void)yaf_request_set_params_multi(request, params TSRMLS_CC);
zval_ptr_dtor(¶ms);
efree(rest);
}
return 1;
}
/* }}} */
/** {{{ int yaf_route_static_route(yaf_route_t *route, yaf_request_t *request TSRMLS_DC)
*/
int yaf_route_static_route(yaf_route_t *route, yaf_request_t *request TSRMLS_DC) {
zval *zuri, *base_uri;
char *req_uri;
int req_uri_len;
zuri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1 TSRMLS_CC);
base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1 TSRMLS_CC);
if (base_uri && IS_STRING == Z_TYPE_P(base_uri)
&& !strncasecmp(Z_STRVAL_P(zuri), Z_STRVAL_P(base_uri), Z_STRLEN_P(base_uri))) {
req_uri = estrdup(Z_STRVAL_P(zuri) + Z_STRLEN_P(base_uri));
req_uri_len = Z_STRLEN_P(zuri) - Z_STRLEN_P(base_uri);
} else {
req_uri = estrdup(Z_STRVAL_P(zuri));
req_uri_len = Z_STRLEN_P(zuri);
}
yaf_route_pathinfo_route(request, req_uri, req_uri_len TSRMLS_CC);
efree(req_uri);
return 1;
}
/* }}} */
/** {{{ proto public Yaf_Router_Classical::route(Yaf_Request $req)
*/
PHP_METHOD(yaf_route_static, route) {
yaf_request_t *request;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, yaf_request_ce) == FAILURE) {
return;
} else {
RETURN_BOOL(yaf_route_static_route(getThis(), request TSRMLS_CC));
}
}
/* }}} */
/** {{{ proto public Yaf_Router_Classical::match(string $uri)
*/
PHP_METHOD(yaf_route_static, match) {
RETURN_TRUE;
}
/* }}} */
/** {{{ yaf_route_static_methods
*/
zend_function_entry yaf_route_static_methods[] = {
PHP_ME(yaf_route_static, match, yaf_route_static_match_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_route_static, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
YAF_STARTUP_FUNCTION(route_static) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Route_Static", "Yaf\\Route_Static", yaf_route_static_methods);
yaf_route_static_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
zend_class_implements(yaf_route_static_ce TSRMLS_CC, 1, yaf_route_ce);
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/